﻿/// <reference path="jquery-1.5.min.js" />
/// <reference path="cufon-yui.js" />


/***
	INIT JAVASCRIPT

    The purpose of this script is to perform any tasks that are required on all pages in the site.

    @Authors
    Hugo & Cat
    Graham Licence

***/


/* ---------------------------------------------------------------- */
/* init.js															*/
/* ---------------------------------------------------------------- */

(function () {

	$(function () {
		MandG.Base.init();
	});

	/* ---------------------------------- */

	if (typeof MandG === "undefined") {
		MandG = {};
	}

	if (typeof console === "undefined") {
		console = { log: function () { } };
	}

	MandG.Base = {
		externalLinkTitle: " (opens in a new window)",
		externalLinkText: "External link",
		ga: "UA-21886781-1",

		init: function () {
			$base = this;
			this.ie = this.detectIE();
			this.externalLinks($base);

			if (this.ie <= 6) {
				//this.pngFix("img, div, a, span");
				this.pngFix("#navigation ul li .shadow-left, #navigation ul li .shadow-right, .feature a span, .planting-guide p span, .garden-view .highlight, .garden-view .video-intro a.video span, .garden-view .details a.video span, .garden-view .details a.cta span");
			}
			// Define label fields for label in input
			//this.dynamicLabel([".search label"]);

			Cufon.replace(".branding h1", { fontFamily: "FS Albert ExtraBold" });
			$(".branding h1, .branding h3").show();

			this.evenHeights("#footer .links");

			this.twitter.init();

			// As Mac's render bold fonts differently to PC a reduced margin is needed
			if (navigator.platform === "MacIntel") {
				$("#navigation ul li a.last").show().parent().css("margin-left", "-25px");
			} else {
				$("#navigation ul li a.last").show();
			}
		},

		twitter: {
			tot: "",
			pos: -1,
			cur: 1,
			animating: false,
			init: function () {
				this.tot = $(".tweets ul li").length;
				var $tweets = this;
				$(".tweets ul").width(this.tot * 261);
				$(".tweets-wrapper .left").click(function () {
					if (!$(this).hasClass("inactive") || $tweets.animating === true) {
						$tweets.pos = $tweets.pos + 261;
						$tweets.animate($tweets);
						$tweets.cur--;
					}
					return false;
				});
				$(".tweets-wrapper .right").click(function () {
					if (!$(this).hasClass("inactive") || $tweets.animating === true) {
						$tweets.pos = $tweets.pos - 261;
						$tweets.animate($tweets);
						$tweets.cur++;
					}
					return false;
				});
			},
			animate: function ($tweets) {
				$tweets.animating = true;
				$(".tweets ul").delay(300).animate({
					left: $tweets.pos
				}, 300, function () {
					$tweets.animating = false;
					$tweets.checkButtons($tweets);
				});
			},
			checkButtons: function ($tweets) {
				$(".tweets-wrapper span").removeClass("inactive");
				if ($tweets.cur === ($tweets.tot - 2)) {
					$(".tweets-wrapper .right").addClass("inactive");
				}
				if ($tweets.cur === 1) {
					$(".tweets-wrapper .left").addClass("inactive");
				}
			}

		},

		dynamicLabel: function (labelChange) {
			var i;
			for (i = 0; i < labelChange.length; i++) {
				var inactive = "inactive";
				var active = "active";
				var focused = "focused";

				// function
				$(labelChange[i]).each(function () {
					obj = document.getElementById($(this).attr("for"));
					if (($(obj).attr("type") === "text") || (obj.tagName.toLowerCase() === "textarea")) {
						$(obj).addClass(inactive);
						var text = $(this).text();
						$(this).css("display", "none");
						$(obj).val(text);
						$(obj).focus(function () {
							$(this).addClass(focused);
							$(this).removeClass(inactive);
							$(this).removeClass(active);
							if ($(this).val() === text) { $(this).val(""); }
						});
						$(obj).blur(function () {
							$(this).removeClass(focused);
							if ($(this).val() === "") {
								$(this).val(text);
								$(this).addClass(inactive);
							} else {
								$(this).addClass(active);
							}
						});
					}
				});
				//
			}
		},

		evenHeights: function (target) {
			/// <summary>Preloads images</summary>
			/// <param name="target" type="String">Elements to give equal height</param>
			var $base = this;
			var highest = 0;

			$(target).each(function () {
				if ($(this).height() > highest) {
					highest = $(this).height();
				}
			});
			$(target).each(function () {
				if ($base.ie === 6) {
					$(this).height(highest);
				} else {
					$(this).css("min-height", highest + "px");
				}
			});
		},

		evenWidths: function (target) {
			var $base = this;
			var widest = 0;

			$(target).each(function () {
				if ($(this).width() > widest) {
					widest = $(this).width();
				}
			});
			$(target).each(function () {
				if ($base.ie === 6) {
					$(this).width(widest);
				} else {
					$(this).css("min-width", widest + "px");
				}
			});
		},

		externalLinks: function ($base) {
			var message = $base.externalLinkTitle;
			var title = null;
			$("a[href^='http']:not([href^='http://" + location.hostname + "'])").each(function () {
				if ($(this).attr("rel") !== "internal") {
					title = $(this).attr("title");

					if (title === "") {
						title = $base.externalLinkText;
					}
					$(this).attr("title", title + message);

					$(this).click(function (e) {
						window.open($(this).attr("href"));
						_gaq.push(['_setAccount', $base.ga]);
						_gaq.push(['_trackEvent', 'External link', $(this).text(), document.title]);
						e.preventDefault();
					});
				}
			});
		},

		detectIE: function () {
			if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)) {
				return new Number(RegExp.$1);
			} else {
				return false;
			}
		},

		pngFix: function (target) {
			if (target) {
				try {
					DD_belatedPNG.fix(target);
				} catch (error) {
					// ohai
				}
			}
		},

		hasFlash: function () {
			/// <summary>Check if flash player installed</summary>
			/// <returns type"boolean"></returns>
			if (swfobject.hasFlashPlayerVersion("1")) {
				return true;
			} else {
				return false;
			}
		},

		preload: function (arrayOfImages) {
			/// <summary>Preloads images</summary>
			/// <param name="arrayOfImages" type="Array">Array of images to preload</param>
			$(arrayOfImages).each(function () {
				(new Image()).src = this;
			});
		}
	}

})();
