google.load("swfobject", "2.2");
(function($) {
	if (!$) return;
	window.showHomepageVideo = function() {
		swfobject.embedSWF("http://www.youtube.com/v/dvkAiWDT1WA&hl=en&fs=1&autoplay=1&rel=0", "homepageVideo", 301, 243, "8", null, null, { "wmode": "transparent" }, null, function() {
			$("#flyin .close").click();
			// track video play in Google Analytics
			if (window.pageTracker) {
				pageTracker._trackPageview("/homepage/videoPlayback");
			}
		});
		return false;
	};
	$(function() {
		var flashFile = "http://www.youtube.com/v/dvkAiWDT1WA&hl=en&fs=1&autoplay=1",
		    dimensions = {
			"width": "320",
			"height": "240"
		    };
		// code to handle Play Video buttons
		$(".demoVideoButton").click(function() {
			if ($.browser.msie) $("#flyin input").css("visibility", "hidden");
			var container = $(document.createElement("div")).attr("id", "demoVideoContainer");
			container.append($(document.createElement("div")).attr("id", "demoVideo"));
			synOverlay.setContent(container);
			swfobject.embedSWF(flashFile, "demoVideo", dimensions.width, dimensions.height, "8", null, null, null, null, function() {
				synOverlay.resizeContent();
				synOverlay.show();
				// track video play in Google Analytics
				if (window.pageTracker) {
					pageTracker._trackPageview("/flyin/videoPlayback");
				}
				// add a handler to stop the video playback in IE
				var overlayCloseElement = $("#synOverlayElement"),
				    removeVideo = function() {
					$("#demoVideoContainer").empty();
					overlayCloseElement.unbind("click", removeVideo);
					if ($.browser.msie) $("#flyin input").css("visibility", "visible");
				    };
				overlayCloseElement.click(removeVideo);
			});
			return false;
		});
		// cookie code source: http://www.quirksmode.org/js/cookies.html
		function createCookie(name,value,days) {
			if (days) {
				var date = new Date();
				date.setTime(date.getTime()+(days*24*60*60*1000));
				var expires = "; expires="+date.toGMTString();
			}
			else var expires = "";
			document.cookie = name+"="+value+expires+"; path=/";
		}
		function readCookie(name) {
			var nameEQ = name + "=";
			var ca = document.cookie.split(';');
			for(var i=0;i < ca.length;i++) {
				var c = ca[i];
				while (c.charAt(0)==' ') c = c.substring(1,c.length);
				if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
			}
			return null;
		}
		function eraseCookie(name) {
			createCookie(name,"",-1);
		}
		// code to handle fly-in
		var flyinCouponCookie = readCookie("flyinCoupon"),
		    farLeft = 10 - $("#flyin").width(),
		    showOnce = function() {
			$("#flyin").animate({ "left": farLeft }, "normal", function() {
				$(this).css("z-index", 3).animate({ "left": 10 });
			});
		    },
		    startShowHover = function() {
			var timer = setTimeout(function() {
				showOnce();
				timer = null;
			    }, 100);
			$(this).one("mouseout", function() {
				if (timer) {
					clearTimeout(timer);
					$(this).one("mouseover", startShowHover);
				}
			});
		    };
		if (/* flyinCouponCookie !== "sent" && */ flyinCouponCookie !== "hidden") {
			$("#flyin").css({ "left": "-100px", "opacity": "0" }).show().animate({ "left": "100px", "opacity": "1"}, 1200);
			createCookie("flyinCoupon", "hidden", 180);
		}
		if (flyinCouponCookie === "hidden") {
			$("#flyin").css({ "left": -20, "z-index": 1 }).show().one("mouseover", startShowHover);
		}
		// if (flyinCouponCookie !== "sent") {
			$("#flyin .close").click(function() {
				createCookie("flyinCoupon", "hidden", 180);
				$("#flyin").animate({ "left": farLeft }, "normal", function() {
					$(this).css("z-index", 1).animate({ "left": -20 }).one("mouseover", startShowHover);
				});
				return false;
			});
			$("#flyin form").submit(function() {
				var obj = $(this),
				    windowName = "flyinWindow",
				    emailInput = obj.find("input[name='email']"),
				    email = emailInput.val();
				if (email && /[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+(?:[A-Z]{2}|com|org|net|gov|mil|biz|info|mobi|name|aero|jobs|museum)\b/.test(email)) {
					createCookie("flyinCoupon", "sent", 180);
					obj.attr("target", windowName);
					window.open("", windowName, "width=400,height=300,scrollbars,resizable");
					$("#flyin .close").click();
					return true;
				}
				emailInput.focus();
				$("#flyin .checkEmail").fadeIn().one("mouseover", function() { $(this).fadeOut(); });
				return false;
			});
		// }
	});
})(window.jQuery);

