// JavaScript Document

// Shadow Box Config
Shadowbox.loadSkin('classic', 'scripts/src/skin');
Shadowbox.loadLanguage('en', 'scripts/src/lang');
Shadowbox.loadPlayer(['img', 'qt'], 'scripts/src/player');
var madecasse = {};

$(document).ready(function() {
	var IMPACT_ROTATION_SPEED	= 20000;	// in milliseconds
	var NORMAL_ROTATION_SPEED	= 8000;	// in milliseconds
	
	/* *********************** *
	 * DROPDOWN MENUS
	 * *********************** */
	var timeoutId = null;
	
	$("#top-nav li a.top-nav-link").mouseenter(showDropdown);
	$("#top-nav li a.top-nav-link").mouseleave(hideDropdownAfterDelay);
	$("ul.dropdown").mouseleave(hideDropdown);
	$("ul.dropdown").mouseenter(cancelDelay);
	
	function showDropdown(e) {
		// cancel timer/delay
		cancelDelay(null);
		
		// hide all
		$("ul.dropdown").hide();
		
		// then show this one
		$(this).next().show();
	}
	function hideDropdown(e) {
		$("ul.dropdown").hide();
	}
	function hideDropdownAfterDelay(e) {
		timeoutId = setTimeout(function(e) {
			hideDropdown(null);
		}, 750);
	}
	function cancelDelay(e) {
		if(timeoutId != null) {
			clearTimeout(timeoutId);
			timeoutId = null;
		}
	}
	
	/* *********************** *
	 * PHOTO GALLERY PAGE
	 * *********************** */
	if($("body").attr("id") == "page-photo-gallery") {
		$("#page-photo-gallery #thumbs-container-video a img").each(function() {
			$overlay = $("<div class='play-symbol'></div>");
			$(this).before($overlay);
		});
	}
	
	
	/* *********************** *
	 * PRODUCT (ITEM) PAGE
	 * *********************** */
	 if($("body").attr("class") == "section-product") {
		 $("#thumb-group a").click(function() {
			 $("#thumb-group a").each(function() {$(this).removeClass("selected");});
			 
			 $("#large-group img").each(function() {$(this).fadeOut();});
			 var c = $(this).attr("class");
			 setTimeout(function (e) {
			 	$("#large-group ." + c).fadeIn();
			 }, 1000);
			 $(this).addClass("selected");
			 return false;
		 });
		 var numItems = $(".related-item").length;
		 var w = $(".related-item:first-child").width();
		 $("#container-related").width(numItems * (w + 10));
	 }
	 
	
	/* *********************** *
	 * STORE LOCATOR 
	 * *********************** */
	 if($("body").attr("class") == "section-store-locator") {
		 $('.map').maphilight({strokeColor : 'FFFFFF'});	
		 $('area').click(function(e) {
			 var state = $(this).attr("title");
			 $.ajax({
				 url: "stores.xml",
				 data: {},
				 success: function(data) {
					 // Create title with the selected state
					 $("#results").html("<h2>" + state.toLowerCase() + "</h2>\n");
					 
					 var objState;
					 
					 // first find our state in the XML data
					 $(data).find("state").each(function() {
						 if($(this).attr("name") == state) {
							 objState = $(this);
							 
							 // If there are no results for this state,
							 // display custom message... 
							 if(objState.find("store").length == 0) {
					 			$("#results").append("<p>Don&rsquo;t worry, you can always <a href='http://store.madecasse.com'>order online</a></p>");
							 } else {
							 	// ...otherwise show column headers for store results...
								 $("#results").append("<p class='header'><span class='name'>Store</span> <span class='address'>Address</span></p>\n");
								
							 	// ...and display addresses
								objState.find("store").each(function() {
									var name = $(this).find("name").text();
									var addr = $(this).find("address").text();
									var str = "<p><span class='name'>" + name + "</span><span class='address'>" + addr + "</span> <a class='map-it' target='_blank' href='http://maps.google.com?q=" + escape(addr) + "'>map-it</a></p>\n";
									$("#results").append(str);
								});
							 }
						 }
					 });
				 },
				 error: function(xhr, status, error) {
					 alert(status + " : " + error);
				 }
			 });
			 //$("#results").html(resultsHtml);
				 
		 });
	 }
	
	/* *********************** *
	 * CONTACT US 
	 * *********************** */
	 $("#contact-us form #form-item-submit").click(function() {
		 // catch the click
		 //return false;
	 });
	 
	/* *********************** *
	 * NEWSLETTER SUBMISSION 
	 * *********************** */
	madecasse.submitNewsletter = function() {
		window.open( 'http://www.verticalresponse.com', 'vr_optin_popup', 'scrollbars=yes,width=600,height=450' );
		return true;
	};
	 
	/* *********************** *
	 * ROTATOR 
	 * *********************** */
	 if($('#rotator').length > 0) {
		$('#rotator').cycle({
			fx		: 'fade', // choose your transition type, ex: fade, scrollUp, shuffle, etc...
			speed	:    1000, 
			timeout :  ($("body").attr("id") == "page-explore-impact") ? IMPACT_ROTATION_SPEED : NORMAL_ROTATION_SPEED,
			pager	: ($("body").attr("id") != "home") ? '#rotator-pages' : '#rotator-controls',
			prev	: ($("body").attr("id") != "home") ? '#rotator-prev' : null,
			next	: ($("body").attr("id") != "home") ? '#rotator-next' : null,
			before	: ($("body").attr("id") != "home") ? updateCaption : null
		});
		
		 if($("body").attr("id") == "home") {
			$('#rotator-controls').click(function() {
				window.location.href = "http://store.madecasse.com/";
			});
			$('#rotator #image-1').click(function() {
				window.location.href = "http://store.madecasse.com/products-gift-ideas.html";
			});
			$('#rotator #image-2').click(function() {
				window.location.href = "http://store.madecasse.com/arabicacoffee.html";
			});
			$('#rotator #image-3').click(function() {
				window.location.href = "http://www.madecasse.com/explore-madecasse-impact.php";
			});
			$('#rotator #image-4').click(function() {
				window.location.href = "http://store.madecasse.com/ciasa.html";
			});
		 }
	 }
	 function updateCaption(currSlideElement, nextSlideElement, options, forwardFlag) {
		 $("#content-right-bottom-right .caption").html($(nextSlideElement).attr("title"));
	 }
	
	/* *********************** *
	 * DOCK 
	 * *********************** 
	// Clear newsletter form when clicked
	$("#input-email-address").focus(function() {
		if($(this).val() == "enter email address") {
			$(this).val("");
		}
	});
	$("#input-email-address-two").focus(function() {
		if($(this).val() == "enter email address") {
			$(this).val("");
		}
	});
	//jQuery.easing.def = "easeOutCubic";
	$("#dock-hide-this").click(function() {
		//$('#dock').animate({left:'50'}, {duration: 1000, easing: method1})
		$("#dock").css({"position" : "absolute", "bottom" : "0px"});
		$(this).hide();
		return false;
	});
	*/	
	
	
	/* *********************** *
	 * SHADOWBOX (GALLERY OVERLAYS)
	 * *********************** */
	 
	// Get browser dimensions for video playback
	if (window.innerWidth) { //if browser supports window.innerWidth	
		BROWSER_WIDTH = window.innerWidth;
		BROWSER_HEIGHT = window.innerHeight;
	} else if (document.all) { //else if browser supports document.all (IE 4+)
		BROWSER_WIDTH = document.body.clientWidth;
		BROWSER_HEIGHT = document.body.clientHeight;
	}
	var video_width = 670;
	var video_height = 500;
	if(BROWSER_HEIGHT < 600) {
		var delta = 600 - BROWSER_HEIGHT;
		video_height -= delta;
		video_width = Math.floor((video_height - 35) * 4 / 3);
	}
	
	switch($("body").attr("id")) {
		case "home":
			$("#node-video a").attr("rel" ,"shadowbox;width="+video_width+";height="+video_height+";");
			break;
		case "page-photo-gallery":
			$("#thumbs-container-video a").attr("rel" ,"shadowbox;width="+video_width+";height="+video_height+";");
			break;
	}
	 
	if($("body").attr("id") == "home" || $("body").attr("id") == "page-photo-gallery") {
		Shadowbox.init();
	}
});
