$(document).ready(function() {		
	//Set up global namespace vars
	AmanaNav.navTimer; //Timer
	AmanaNav.navElements = $('#gnav').children('h2'); //Main nav elements
	AmanaNav.navList = '.drop-down-box'; //Subnav ul lists
	//Iterate through the main nav elements and assign AmanaNav.hover to them
	$.each(AmanaNav.navElements, function() {
		AmanaNav.hover(this, AmanaNav.navList);
	});
	
	// if user is coming from the mobile site to the desktop site, the following functions are necessary to keep them on the dekstop site.
	var url = $(location).attr('href');	
	var nomobile = "?nomobile=1"	
	var nomobile2 = "&nomobile=1"	
	var cookie = $.cookie('desktop_version');	

	// first time to the site (cookie has not been set yet, but URL does contain nomobile)
	if (cookie != "yes" && url.indexOf("nomobile")!=-1) {	
		add_nomobile()
		$.cookie('desktop_version', 'yes', { expires: 1, path: '/' });	
	}	
	// all subsequent visits
	if (cookie == "yes") {	
		add_nomobile()
	}	
	
	// adds the link which allows the user to return to the mobile site
	// adds the nomobile URL segment to all A tags
	function add_nomobile() {	
		$("#tnav").prepend("<li class='return_to_mobile_site'>Return to Mobile Site</li><li> | </li>")
		$("a").each(function() {
			var path = $(this).attr("href")
			// path already contains a question mark
			if (path.indexOf("?")==-1) {	
				$(this).attr("href", $(this).attr("href")+nomobile)
			// path does NOT already contain a question mark
			} else {
				$(this).attr("href", $(this).attr("href")+nomobile2)
			}
	  })				
	}
	
	// allows the user to return to the mobile site
	$("#tnav li.return_to_mobile_site").click(function() {
		$.cookie('desktop_version', 'no', { expires: 1, path: '/' });	
		window.location.href = "http://m.amana.com"
	})
	
	// adds the "#1 Rated and Best Buy" ribbons
	$("form[name='productForm'] #cat-pro-feat-logos img").each(function() {
		var src = $(this).attr("src")
		if (src.indexOf("callout_BB.gif")!=-1) {
			$(this).hide()
			$("#cat-pro-top-wrapper").append("<div id='ribbon'></div>")
			$("#ribbon").css("background", "url('/assets/amana/images/global/ribbon_best_buy.png') no-repeat")
			// marks the following section with "ribbon" so that when the user clicks a thumbnail, the jquery can select a thumbnail that's actually associated with a ribbon
			$("#page-content div.scroll.fl").addClass("ribbon")
		} 
		else if (src.indexOf("callout_1andbb.gif")!=-1) {
			$(this).hide()
			$("#cat-pro-top-wrapper").append("<div id='ribbon'></div>")
			$("#ribbon").css("background", "url('/assets/amana/images/global/ribbon_number_1.png') no-repeat")
			// marks the following section with "ribbon" so that when the user clicks a thumbnail, the jquery can select a thumbnail that's actually associated with a ribbon
			$("#page-content div.scroll.fl").addClass("ribbon")	
		}
	})	

	// when user clicks thumbnail, on a product page, after the ribbon has been added to the page
	$("#page-content div.scroll.fl.ribbon span").click(function() {
		element_selected = $(this)
		if ($(element_selected).is(":first-child")) {
			$("#ribbon").show()			
		}
		else {
			$("#ribbon").hide()
		}
	})

	// Display the product demo if a particular product page loads
	var url = (window).location.toString()
	var selected_product = "amana__174__21_cu__ft__top_freezer_refrigerator_a1rxngfys" 
		// amana__174__21_cu__ft__top_freezer_refrigerator_a1rxngfys
		// product_page
	var product_demo_src = "http://d1kuntqguvpk86.cloudfront.net/pool/amana/product_demo/iframe.html"
		// http://d1kuntqguvpk86.cloudfront.net/pool/amana/product_demo/iframe.html
		// http://datastore0.arsecom.com/pool/amana/product_demo/iframe.html
		// /assets/iframe/iframe.html
	if (url.indexOf(selected_product)!=-1 && url.indexOf("close_demo")==-1) {
		overlay_on()
		insert_iframe()
	}
	if (url.indexOf(selected_product)!=-1 && url.indexOf("close_demo")!=-1) {
		overlay_on()
	}

	function overlay_on() {
		$("body")
			.append("<div id='overlay'></div>")
			.append('<div id="close_demo"></div>')
		$("#close_demo").hide()
		$("#overlay")
			.css("height", $(document).height() + "px")
			.css("width", $(document).width() + "px")
		$("#cat-pro-top-wrapper").append('<img id="open_demo" src="/assets/amana/images/global/btn_view_demo.jpg" />')
	}
	function insert_iframe() {
		$("#overlay, #close_demo").show();
		$("body")
			.append('<iframe id="product_demo" src="' + product_demo_src + '" frameborder="0" scrolling="no"></iframe>')
		$("#product_demo").show()
		set_left_position()
	}
	function set_left_position() {
		width_of_demo = 969
		left_edge = ($(window).width() - width_of_demo) / 2
		$("#product_demo").css("left", left_edge+"px")
		$("#close_demo").css("right", left_edge+6+"px")
	}
	$("#close_demo").click(function() {
		$("#product_demo").fadeTo(800, 0, function() {
			$(this).remove()
			// must be removed completely because of iE - otherwise video will continue to play - or in iE8 the special font won't load when you reopen the demo
		});
    	$("#overlay, #close_demo").fadeTo(800, 0, function() {
			$(this).hide()
		});
	})
	$("#open_demo").click(function() {
		$("#overlay")
			.removeAttr("style")
			// because of iE, must remove the style completely because you can't set opacity in jQuery for iE
			.css("height", $(document).height() + "px")
			.css("width", $(document).width() + "px")
			.fadeTo(400, .7, function() {
				$("body").append('<iframe id="product_demo" src="' + product_demo_src + '" frameborder="0" scrolling="no"></iframe>')
				$("#product_demo").show()
				set_left_position()
			})
			$("#close_demo").removeAttr("style").show()
	})
	
	$(window).resize(function() {
		set_left_position()
		$("#overlay")
			.css("height", $(document).height() + "px")
			.css("width", $(window).width() + "px")
	})
	
	// when user clicks outside demo, close demo
	$("#overlay").click(function() {
		$("#close_demo").trigger("click")
	})
	
	// when user preses escape key, close demo - this only works if the user has not moved the focus to the iframe
	document.onkeydown = function(evt) {
		evt = evt || window.event;
		if (evt.keyCode == 27 && url.indexOf(selected_product)!=-1) {
			$("#close_demo").trigger("click")
		}
	};

	// on page load, insert styles (i.e. when a particular product page loads, inject some CSS to expand the content area in the upper right section) 
	var url = (window).location.toString()
	var selected_product = "tall_tub_dishwasher_adb1400aws" 
	var style = "min-height:472px"
	if (url.indexOf(selected_product)!=-1) {
		$("div#cat-pro-top-wrapper, div#cat-pro-top-wrapper div#cat-pro-product-text").attr("style", style)
	}

})

//Set up AmanaNav namespace object
var AmanaNav = {};
//AmanaNav.hover - combines params el and nav and binds them to mouseenter and mouseleave events
AmanaNav.hover = function(el, nav) {
	var elements = $(el).add($(nav));
	elements.mouseenter(function() { AmanaNav.open(this); });
	elements.mouseleave(function() { AmanaNav.timer(); });
};
//AmanaNav.target - grabs the id of the param el and splits it and reasigns it as the target element's id
AmanaNav.target = function(el) {
	var id = $(el).attr('id').split('-');
	var target = '#dd-' + id[1];
	return $(target);
};
//AmanaNav.open - clears timer, checks to see if the el has siblings, if yes show and hide the appropriate nav list
AmanaNav.open = function(el) {
	clearTimeout(AmanaNav.navTimer);
	if ($(el).siblings(AmanaNav.navList).length == 0) {
		AmanaNav.target(el).siblings(AmanaNav.navList).hide();
		AmanaNav.target(el).delay(250).show();
	}
};
//AmanaNav.timer - clears and sets a timer for mouseleave event
AmanaNav.timer = function() {
	clearTimeout(AmanaNav.navTimer);
	AmanaNav.navTimer = setTimeout(function() {
		$(AmanaNav.navList).hide();
	}, 1500);
};
