$(document).ready(function() {

	$('#tabSet2').tabs();
	
	// Dynamic featue content
	$("ul.dynamic-featues li a").click(
		function() { 
			$("#featureFocusArea").empty();
			$(this).next().clone().prependTo("#featureFocusArea");
			$("#featureFocusArea div.extended-info").removeClass("hide");
			return false;
		}
	);
	
	// Ultra Premium Menu
	$("#collectionMenu li").hover(
		function() { $("div", this).slideDown("fast"); },
		function() { $("div", this).slideUp(80); }
	);
	$("#collectionMenu li a.inactive").click(
		function() { return false; }
	);

	if(document.getElementById("default") && document.getElementById("tabSet2") && $("#tabSet2").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("tabSet2") && $("#tabSet2").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");
			}
		);
	}
	
	
	// 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(); 
		}
	);
	
	// 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;
		}
	);
	
	/*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++; 
		}
	);
	
});