﻿(function($) {
	$.fn.clickDisplayMore = function() {
		$(this).click(function(e) {
			$(this).closest(".lessDyn").toggleClass("hide").next(".moreDyn").toggleClass("hide");
		});
	};
	$.fn.clickDisplayLess = function() {
		$(this).click(function(e) {
			$(this).closest(".moreDyn").toggleClass("hide").prev(".lessDyn").toggleClass("hide");
		});
	};
	$.fn.clickFetchPartial = function(url, container, settings) {
		return this.each(function() {
			var svcurl = url;
			var tcntr = container;
			var sttgs = settings;
			$(this).click(function(e) {
				e.preventDefault();
				$.ajax
				({
					url: svcurl,
					type: "GET",
					dataType: "html",
					success: function(msg) {
						$(tcntr).html(msg);
						if (sttgs && sttgs.callBack)
							sttgs.callBack();
					},
					error: function(XMLHttpRequest, textStatus, errorThrown) {
						alert('failed: ' + errorThrown.message);
					}
				});
			});
		});
	};
	$.fn.postAction = function(url, settings) {
		return this.each(function() {
			var svcurl = url;
			var sttgs = settings;
			$(this).click(function(e) {
				e.preventDefault();
				$.ajax
				({
					url: svcurl,
					type: "POST",
					dataType: "html",
					success: function(msg) {
						if (sttgs && sttgs.callBack)
							sttgs.callBack();
					},
					error: function(XMLHttpRequest, textStatus, errorThrown) {
						if (errorThrown) {
							alert('failed: ' + errorThrown.message);
						}
					}
				});
				if (sttgs && sttgs.immediateCallBack)
					sttgs.immediateCallBack();
			});
		});
	};
	$.fn.mouseoverColorSwatch = function(imgsel, settings) {
		var sel = imgsel;
		var sttgs = settings;
		return this.each(function() {
			$(this).mouseover(function(e) {
				$(sel).attr("src", $(this).attr("href"));
				if (sttgs && sttgs.textelement) {
					$(sttgs.textelement).html($(this).children("img").attr("alt"));
				}
			});
			$(this).click(function(e) {
				e.preventDefault();
			});
		});
	};
	$.fn.mouseoutColorSwatch = function(imgsel, settings) {
		var sel = imgsel;
		var sttgs = settings;
		return this.each(function() {
			$(this).mouseout(function(e) {
				$(sel).attr("src", $(this).attr("href"));
				if (sttgs && sttgs.textelement) {
					if (sttgs && sttgs.textelementsel) {
						var seltxt = $(sttgs.textelementsel).data("selhint");
						var selsrc = $(sel).data("selhint");
						if (seltxt) {
							$(sttgs.textelementsel).html(seltxt);
							$(sttgs.textelement).html(seltxt);
						}
						if (selsrc)
							$(sel).attr("src", selsrc);
					}
				}
			});
			$(this).click(function(e) {
				e.preventDefault();
				if (sttgs && sttgs.textelementsel) {
					var seltxt = $(this).children("img").attr("alt");
					$(sttgs.textelementsel).html(seltxt);
					$(sttgs.textelementsel).data("selhint", seltxt);
					$(sel).data("selhint", $(this).attr("href"));
				}
			});
		});
	};
	$.fn.tabSelect = function() {
		return $(this).each(function(i) {
			$(this).bind("click", { "index": i }, function(e) {
				var indx = e.data.index;
				$(this).siblings("a.tab").removeClass("on");
				$(this).addClass("on");
				$(this).closest(".tabs").siblings(".panels").first().children(".panel").each(function(i) {
					$(this).addClass("hide");
					if (i == indx)
						$(this).removeClass("hide");
					Vehix.Web.Ads.rotate('*');
				});
			});
		});
	};
	$.fn.twitterGet = function(account) {
		var container = $(this);
		$.getJSON('http://search.twitter.com/search.json?callback=?&rpp=3&q=from:' + account, function(data) {
			$.each(data.results, function(i, tweets) {
				container.append("<p>" + tweets.text + "<br /><span class='tweetTime'>" + relativeTime(tweets.created_at) + "</span></p>");
			});
		});
	};
	$.fn.getGoogleMap = function(lat, lon) {
		if ($(this)[0] != null) {
			var Latlong = new google.maps.LatLng(lat, lon);
			var options = { zoom: 15,
				center: Latlong,
				mapTypeId: google.maps.MapTypeId.ROADMAP,
				navigationControlOptions: {
					style: google.maps.NavigationControlStyle.SMALL
				}
			};
			var map = new google.maps.Map($(this)[0], options);
			var marker = new google.maps.Marker({
				position: Latlong,
				map: map
			});
		}
	};
})(jQuery);

function relativeTime(tweetTime) {
	var origStamp = Date.parse(tweetTime);
	var curDate = new Date();
	var currentStamp = curDate.getTime();
	var difference = parseInt((currentStamp - origStamp) / 1000);

	if (difference < 0) return false;
	if (difference <= 5) return "Just now";
	if (difference <= 20) return "Seconds ago";
	if (difference <= 60) return "A minute ago";
	if (difference < 3600) return parseInt(difference / 60) + " minutes ago";
	if (difference <= 1.5 * 3600) return "One hour ago";
	if (difference < 23.5 * 3600) return Math.round(difference / 3600) + " hours ago";
	if (difference < 1.5 * 24 * 3600) return "One day ago";
	// If the tweet is older than a day, show an absolute date/time value;

	var datesplit = tweetTime.split(' ');
	return datesplit[2] + " " + parseFloat(datesplit[1]).toFixed(0) + " " + datesplit[3];
}
