$(document).ready(function(){
	// Homepage Flyouts
	$("#flyouts li").hover(
		function() { $("div", this).slideDown(300); }, 
		function() { $("div", this).hide(1); }
	);
	if (document.all) {
		$("#flyouts li").hoverClass ("hover");
	}
	
	// Homepage Feature BOx
	$("#home .features li").click(
		function() {
			window.location = $("a", this).attr("href");
		}
	);
	
	// Feature boxs on RHS
	$("#expandingBoxes li a.arrow").hover(
		//function() { return false; }
		function() { 
			if(this.parentNode.getElementsByTagName("p").length>0) {
				$(this.parentNode.getElementsByTagName("p")[0]).toggle();
			}
		},
		function() { 
			$(this.parentNode.getElementsByTagName("p")[0]).toggle(); 
		}
	);
	
	// Dynamic featue content
	$("ul.dynamic-featues li a").click(
		function() { 
			$("#featureFocusArea").empty();
			
			$(this).next().clone().prependTo("#featureFocusArea");
			$("#featureFocusArea div.extended-info").removeClass("hide");
			
			// to cater for video within content
			if($(this).text() == "Do the Sealy Comfort Test")
			{
				var so = new SWFObject("flash/mini-flv-player.swf", "mediaboxINS", "230", "191", "8", "#ffffff");
				//var so = new SWFObject("flash/mini-flv-player.swf", "mediaboxINS", "230", "191", "8", "#ffffff");
				so.addParam("wmode", "transparent");
				so.addParam("allowScriptAccess", "sameDomain");
				so.addVariable("flvPath", "videos/dual-support-system.flv");
				//so.addVariable("flvPath", "/media/9749/sealy-roll-test.flv");
				so.addVariable("bufferingTime", "5");
				
				so.write("featureFocusArea");
				$("#featureFocusArea *").addClass('right');
				$("#featureFocusArea").append("<p>We invite you to take the Sealy Comfort Test at your local retailer to discover the uniform comfort and support only the new Sealy Posturepedic provides. </p><p>The best test for proper support is to lie down on the mattress and experience its support by lying the way you do while asleep. If you feel your body sagging, the mattress is not giving you proper support. </p><p>Also, if you feel excessive pressure, the mattress is not giving you proper support.</p>");
			}
			
			return false;
		}
	);
	
	// Product Tabs
	$("#tabSet ul.tabs li").click(
		function() {
			// reset elements
			$("#tabSet ul.tabs li").each(function(i){ 
				$(this).removeClass("selected");
				var menuText = $(this).text();
				$(this).empty();
				$(this).append('<a href="'+window.location+'#'+urlCleanFormat(menuText)+'Info">'+menuText+'</a>');
			});
			$(this).addClass("selected");
			
			/* Remove link for the selected node
			var menuText = $(this).text();
			$(this).empty();
			$(this).append(menuText);
			*/
			// Show the related node that contains the content
			$("#tabSet div.roundbox").addClass("hide");
			$(".featureFocusArea").removeAttr('id');
			$("#"+urlCleanFormat($(this).text())+"Info .featureFocusArea").attr('id', "featureFocusArea");			
			$(document.getElementById(urlCleanFormat($(this).text()) + "Info")).toggleClass("hide");
			
			//change the div that has the mediaplayer id with the tvc tabset
			if($(this).parents("#tabSet").attr("class").match("tvc-set"))
			{
				$("div.media-player").each(
					function(i)
					{
						$(this).empty();
						$(this).removeClass("has-flash");
						$(this).attr("id", "");
					}
				);
				//apply the id to the media-player div that is is being switched to
				$("#"+ urlCleanFormat($(this).text()) +"Info .media-player").attr("id", "mediaPlayer");
				
				//reset all tvc li divs to unselcted
				$(".jcarousel-list li div").each(
					function(i)
					{
						$(this).removeClass("selected");
					}
				);
				
			}
			
			return false;
		}
	);
	
	// Mediabox 
	$("#mediabox a").click(
		function() { 
			if(this.className.match(/loadvideo/))
			{
				var so = new SWFObject("flash/mini-flv-player.swf", "mediaboxINS", "230", "191", "8", "#ffffff");
				//var so = new SWFObject("flash/mini-flv-player.swf", "mediaboxINS", "230", "191", "8", "#ffffff");
				so.addParam("wmode", "transparent");
				so.addParam("allowScriptAccess", "sameDomain");
				so.addVariable("flvPath", "/videos/"+$(this).attr('name'));
				//so.addVariable("flvPath", "/media/"+$(this).attr('id').replace("mid", "")+"/"+$(this).attr('name'));
				so.addVariable("bufferingTime", "5");
				
				$("#mediabox .loadvideo").addClass("hide");
				$('<div id="flashVideoPlayer" class="fele"></div>').appendTo("#mediabox");
				$('<a href="#" class="close-box">close video</a>').appendTo("#mediabox");
				
				$("#mediabox a.close-box").click(
					function() { 
						$("#mediabox .loadvideo").removeClass("hide");
						$("#flashVideoPlayer").remove();
						$("#mediabox .close-box").remove();
						$("#mediabox").removeClass("hasFlash");
						
						return false;
					}
				);
				
				so.write("flashVideoPlayer");
				
				$("#mediabox").addClass("hasFlash");
			} 
			else
			{
				$("#mediabox .loadimage").addClass("hide");
				$('<div class="fele"><img src="/media/'+this.id.replace("mid", "")+'/'+this.name+'" /></div>').appendTo("#mediabox");
				$('<a href="#" class="close-box">close</a>').appendTo("#mediabox");
				$("#mediabox").addClass("hasImage");
				
				$("#mediabox a.close-box").click(
					function() {
						$("#mediabox .loadimage").removeClass("hide");
						$("#mediabox div.fele").remove();
						$("#mediabox .close-box").remove();
						$("#mediabox").removeClass("hasImage");
						
						return false;
					}
				);
			}
			
			return false;
		}
	);
	
	// TVCs
	$(".tvc-list li div").click(
		function() {
			var areaToLoad = this.parentNode.id;
			
			$(".tvc-list li div").each(
				function(i)
				{
					$(this).removeClass("selected");
				}
			);
			$("#tvc-list li div").removeClass("selected");
			
			
			//var movieToLoad = this.className.replace(" selected", "").replace("first", "").replace(" ", "")+".flv";
			var movieToLoad;
			if($(this).attr("class").match("image"))
			{
				$(this).parent().parent().addClass("selected");
				movieToLoad = $(this).parent().parent().attr("id");
			}
			else
			{
				$(this).addClass("selected");
				movieToLoad = $(this).attr("id");
			}
			var so = new SWFObject("flash/flv-player.swf", "mediaPlayerINS", "350", "290", "8", "#ffffff");
			//var so = new SWFObject("flash/flv-player.swf", "mediaPlayerINS", "350", "290", "8", "#ffffff");
			so.addParam("wmode", "transparent");
			so.addParam("allowScriptAccess", "sameDomain");
			so.addVariable("flvPath", movieToLoad);
			//so.addVariable("flvPath", "/media/"+$(this).attr('id').replace("tvcid", "")+"/"+$(this).attr('class').replace(" selected", "")+".flv");
			so.addVariable("bufferingTime", "5");
			
			so.write("mediaPlayer");
			$("#mediaPlayer").addClass("has-flash");
			return false;
		}
	);
	
	var check = true;
	
	$(".tvc-list li div a").hover(
		function()
		{
			check = true;
			$(this).parents("#tvcCarousel").siblings("#createOnHover").html($(this).siblings(".info").html());
			//set the top and left attributes
			vars = $(this).offset({scroll: false});
			vars.left = vars.left + $(this).width();
			$(this).parents("#tvcCarousel").siblings("#createOnHover").css("display", "block");
			$(this).parents("#tvcCarousel").siblings("#createOnHover").css("top", vars.top+"px");
			$(this).parents("#tvcCarousel").siblings("#createOnHover").css("left", vars.left+"px");
			//animate the box
			$(this).parents("#tvcCarousel").siblings("#createOnHover").slideDown("normal", function(){
				if(check == false)
				{
					$("#createOnHover").css("display", "none");
				}
			});
		},
		function()
		{
			check = false;
			$(this).parents("#tvcCarousel").siblings("#createOnHover").empty();
			//set the top and left attributes
			$(this).parents("#tvcCarousel").siblings("#createOnHover").css("display", "none");
		}
	);
	
	if(document.getElementById("default") && document.getElementById("tabSet") && $("#tabSet").attr("class").match("tvc-set"))
	{
		//jcarousel
		jQuery(".jcarousel-scope").jcarousel({
			orientation: "vertical",
			itemVisible: 2,
			itemScroll: 1,
			scrollAnimation: "slow"
		});
	}
	//if the jcarousel on the screens are the tvc ones then hide all but the one that should be showing
	if(document.getElementById("tabSet") && $("#tabSet").attr("class").match("tvc-set"))
	{
		//get the href of the a within the li that is selected within the tabs ul
		var el;
		$("ul.tabs li").each(
			function(i)
			{
				if($(this).attr("class").match("selected"))
				{
					el = urlCleanFormat($(this).text());
				}
			}
		);
		$(".dynamicFeatureArea div.roundbox").each(
			function(i)
			{
				el = el + "info";
				var tester = urlCleanFormat($(this).attr("id"));
				
				if(el == tester)
				{
					$(this).removeClass("hide");
					$(this).children(".media-player").attr("id", "mediaPlayer");
				}
				else
				{
					$(this).addClass("hide");
				}
			}
		);
		$(".tvc-list li div.info").each(
			function(i)
			{
				$(this).css("display", "none");
			}
		);
	}
	
	/*box equalising*/
	//set the height of the box's on the page with the min height being 165
	if(getElementsByClassName(document, "div", "box").length > 0)
	{
		//get the max height
		maxHeight = 165;
		$("div.box").each(
			function(i)
			{
				height = this.offsetHeight - $(this).children(".head").height() - parseInt($(this).children(".body").css("padding-top")) - parseInt($(this).children(".body").css("padding-bottom"));
				if(height > maxHeight){maxHeight = height;}
			}
		);
		//set the elements with the max height
		$("div.box .body").each(
			function(i){
				$(this).css("height", maxHeight+"px");
			}
		);
	}
	
	// Ultra Premium Menu
	$("#collectionMenu li").hover(
		function() { $("div", this).slideDown("fast"); },
		function() { $("div", this).slideUp(80); }
	);
	$("#collectionMenu li a.inactive").click(
		function() { return false; }
	);
	
	//Bed-Selector
	if (document.getElementById("bed-selector"))
	{
		var version = deconcept.SWFObjectUtil.getPlayerVersion();
		
		if (version['major'] >= 7) {
			var so = new SWFObject("flash/bed-selector.swf", "Bed Selector", "961", "668", "7", "#EBFFFF");
			so.addParam("wmode", "transparent");
			so.write("bed-selector");
		}else{
			var so = new SWFObject("flash/bed-selector-updater.swf", "Bed Selector", "961", "668", "5", "#EBFFFF");
			so.addParam("wmode", "transparent");
			so.addVariable("versionNumber", version['major']);
			so.write("bed-selector");
		}
	}
	
	$("fieldset br").each(
		function(i)
		{
			$(this).remove();
		}
	);
	$("fieldset span").each(
		function(i)
		{
			var el = $(this).html().toLowerCase();
			counter = 0;
			var html = "";
			
			if($(this).attr("class") == "required")
			{}
			else
			{
				$(this).children("input").each(
					function(i){
						counter++;
					}
				);
				first = 0;
				second = 0;
				
				for(i=0; i<counter; i++)
				{
					if(i == 0)
					{
						first = el.indexOf("<input", second);
					}
					else
					{
						first = el.indexOf("<input", second+1);
					}
					html = html + "<div>";
								
					second = first;
					first = el.indexOf("</label>", second+1);
					html = html + el.substring(second, first+8) + "</div>";
				}
				//alert("el: " + html);
				$(this).empty();
				//alert("emptied");
				$(this).html(html);
				//$(this).append(html);
				//alert("added");
			}
		}
	); 
	var counter = 0;
	$("fieldset span").each(
		function(i)
		{
			$(this).children("div").each(
				function(i)
				{
					//alert(i + "\n" + $(this).prev().html());
					var test = $(this).prev().html();
					//alert(test);
					if($(this).prev().html())
					{
					}
					else
					{
						$(this).addClass("first");
					}
				}
			)
			//$(this).children().addClass("first");
		}
	)
	$("fieldset span div").each(
		function(i)
		{
			if($(this).attr("class").match("first"))
			{
				counter = 0;
			}
			if(counter == 3)
			{
				$(this).before("<div class='clear'></div>")
				//$(this).addClass("clear");
				counter = 0;
			}
			counter++; 
		}
	);
});

$.fn.hoverClass = function(c) {
	return this.each(function(){
		$(this).hover( 
			function() { $(this).addClass(c);  },
			function() { $(this).removeClass(c); }
		);
	});
};

function urlCleanFormat(s) {
	s = s.toLowerCase();
	s = s.replace(/\s/g, "").toLowerCase();
	s = s.replace(/\//g, ""); 
	s = s.replace(/'/g, ""); 
	return s;
};
