
var Tabs = function(options)
{
	var controllerWrapper = options.controllerWrapper || "";
	var contentWrapper = options.contentWrapper || "";
	var carrot = options.carrot || "";
	var currentTab = options.currentTab || 0;

	this.getControllerWrapper = function(){
		return controllerWrapper;
	}
	
	this.imagePanInit = 0;
	
	if(controllerWrapper[0] && contentWrapper[0])
	{
		controllerWrapper.delegate("li", "mouseover", function(){
			$("a", this).addClass("hover");
		});
		controllerWrapper.delegate("li", "mouseout", function(){
			$("a", this).removeClass("hover");
		});
		controllerWrapper.delegate("li", "click", function(e){
//			e.preventDefault();
			var num = $(this).index();
			
			if(currentTab == num) return;
			$("a", this).removeClass("hover");
			
			var currTab = $(contentchildren[currentTab]);
			var newTab = $(contentchildren[num]);
			
			$("#subContent").css({height:Math.max(newTab.height(), currTab.height())});
			newTab.css({position:"absolute", top:0});
			currTab.css({position:"absolute", top:0});
			if($.browser.WebkitMobile || $.browser.bbos5)
			{
				currTab.removeClass("active").addClass('inactive');
				newTab.addClass("active").removeClass("inactive").css({"position":"static"});
				 $("#subContent").css({height:"auto"});
				
			}else
			{
				currTab.fadeOut("fast", function(){ $(this).removeClass("active").addClass("inactive");});
				newTab.fadeIn("fast", function(){ $(this).addClass("active").removeClass("inactive").css({"position":"static"}); $("#subContent").css({height:"auto"});});
			}
			$(".selected", this.parentNode).removeClass("selected");
			$("a", this).addClass("selected");
			
			leftCarrotPos(num);
			
			currentTab = num;
		});
		var tabchildren = controllerWrapper.children();
		var contentchildren = contentWrapper.children();

		var leftCarrotPos = function(num)
		{	
			if(carrot[0])
			{
				var offset = $(tabchildren[num]).offset();
				var width = $(tabchildren[num]).width()/2;	
				var carwidth = carrot.width()/2;
				carrot.css({"left":(offset.left+width-carwidth-carrot.parent().offset().left), "zIndex":999});
			}
		}

		$(contentchildren[currentTab]).addClass("active");

		if(contentchildren.length)
		{
			var maxheight = 0;
			for(var i=0; i<contentchildren.length; i++)
			{
				maxheight = Math.max(maxheight, $(contentchildren[i]).height());
			}
		}
		if(!$.browser.msie)
		{
			contentWrapper[0].style.height = "auto !important";
		}
		contentWrapper.css({"min-height":maxheight,  height:"auto !important", "height":maxheight});
		leftCarrotPos(currentTab);

	}
}

Tabs.prototype.destroy = function()
{
	this.getControllerWrapper().undelegate();
}


$(document).ready(function(){
	
	(function(){
		var simmons = window.simmons || {};
		simmons = {
			init: function(){
				simmons.tabwrapper.init();	
				
				var currentIndex = 0;
				if(window.techPage==1)
				{
						function supports_video() {
						  return !!document.createElement('video').canPlayType;
						}

						$(function(){

							var videoLinks = ['Tech-Page-Pocketed-Coil', 'Tech-Page-Motion-Separation', 'Tech-Page-Conformability'];
						
							$("#technologyController").delegate("li", "click", function(){

								$("#learnMoreWrapper").css("display","none"); 
								$("#videoInfo").css("display","block");
								var index = $(this).index();
								if(currentIndex==-1){
									if(document.getElementById("video0")){
										if(supports_video())
										{
											document.getElementById("video0").pause();
										}
										var parent = $("#technologyVideos").children(":nth-child(1)");
										$(".videoWrapper", parent).html("");
	//									$("#video0").remove();
									}
									 currentIndex =index;
								}else{						
									if(supports_video())
									{
										var el = document.getElementById("video"+currentIndex);
										el.pause();
										
										if($.browser.WebkitMobile){
											$(el).remove();
										}
									}
									else if($.browser.msie){
										var el = document.getElementById("EmbedObj"+currentIndex);
										if(el){
												
											try{												
													el.Stop();
													el.SetControllerVisible(false);
													//var stat = el.GetPluginStatus();	
											}catch(e){
												alert("no plugin"+e);
											}
										}
									}
									currentIndex = index;
								}
								
								if(document.getElementById("video"+index) && !$.browser.bbos5)
								{
									if($.browser.msie){
										var el = document.getElementById("EmbedObj"+index);
										if(el){
												
											try{				
													el.SetControllerVisible(true);
											}catch(e){	}
										}
									}
									document.getElementById("video"+index).style.display = 'block';
									
									return;
								}
								var parent = $("#technologyVideos").children(":nth-child("+(index+1)+")");

								
									$(".videoWrapper", parent).get(0).innerHTML =  '<video id="video'+index+'" controls preload="auto" class="theVideo" width="555" height="315" poster="videos/poster/'+videoLinks[index]+'.jpg" >'+
																			($.browser.WebkitMobile ? '<source class="video" src="videos/'+videoLinks[index]+'.iphone.mp4"/>' : '<source class="video" src="videos/'+videoLinks[index]+'.mp4" />')+
																						'<source class="video" src="videos/'+videoLinks[index]+'.webm"/>\
																						<source class="video" src="videos/'+videoLinks[index]+'.theora.ogv" />'+
																				QT_GenerateOBJECTText("videos/"+videoLinks[index]+".mp4", "560", "320", "", "obj#ID", "EmbedObj"+index, "obj#NAME", "EmbedObj"+index, "emb#ID", "EmbedVideo"+index, "emb#NAME", "EmbedObj"+index, "autoplay", "true", "enablejavascript", "true", "controller", "true", "scale", "tofit")+'\
																				</video>';
								

									if(supports_video()){
											var el = document.getElementById('video'+index);
										if(el){	

											if(!$.browser.WebkitMobile)
												el.removeAttribute("controls");

											el.load();
											el.play();	
										}
									}
									else if($.browser.msie){
										var el = document.getElementById("EmbedObj"+index);
										if(el){
												
											try{				
													el.SetControllerVisible(true);												
											}catch(e){
												
											}
										}
									}
							});


							$(".theVideo").live("mouseenter", function(){
								if(!this.hasAttribute("controls"))
								     this.setAttribute("controls","controls");

							}).live("mouseleave", function(){
								this.removeAttribute("controls");
							});

							
							if(!location.hash){
								if(supports_video()){
									currentIndex = -1;
									$("#nf_pocketed-coil").trigger("click");
								}
							}


							$("#learnMore").bind('click', function(){ 
								$("#videoInfo").css("display","none"); 
								$("#learnMoreWrapper").css("display","block"); 
							});
							$("#pocketedCoil").bind('click', function(){ 
								$("#learnMoreWrapper").css("display","none"); 
								$("#videoInfo").css("display","block"); 


							});

						});
				}	
				
				if(location.hash){
					currentIndex = -1;
					if(document.getElementById(location.hash.substr(1)))
						$(location.hash).trigger("click");
					else
					$("#nf_"+location.hash.substr(1)).trigger("click");
				}
					
				$("#zipcode").bind({click : function(){
					if(this.value == 'Enter Zip')					
						this.value = '';
					},
					blur : function(){
						if(this.value == "")
							$(this).val("Enter Zip");
					}
					
				});
			}
		}
		simmons.tabwrapper = {
			tabs : {},
			
		
			init : function ()
			{
				this.bindTriggers();
				
				var level = 0;
				var currentTab = 0;
				var productInfo = $("#productInfo").children();
				$("#productInfo").children(":nth-child("+(currentTab+1)+")").removeClass('inactive').addClass("active");
			
				var tabs = this.tabs;
				var parent = productInfo[currentTab];
				var tabController = $(".tabControllers:first", parent);
				if(!tabController)
				{
					var parent = document;
				}
				var tabContent = $(".tabContent:first", parent);
				
				currentTab = $('.active', tabContent).index();
				if(currentTab<0) currentTab = 0;
				var carrot = $(".contentCarrot:first", parent);
				carrot.css("left", 0);
				tabs['"'+currentTab+'"'] = new Tabs({"controllerWrapper": tabController, "contentWrapper":tabContent, "carrot":carrot, "currentTab":currentTab});

				var self = this;
				$("#productModels").delegate("li", "mouseover", function(){
					$("a", this).addClass("hover");
				});
				$("#productModels").delegate("li", "mouseout", function(){
					$("a", this).removeClass("hover");
				});
				$("#productModels").delegate("li", "click", function(e){
					
//					e.preventDefault();
					var index = $(this).index();
					
					if(currentTab == index) return;
					
					$("#productModels .selected").removeClass("selected");
					$("a", this).addClass("selected");
					
					var currTab = $("#productInfo").children(":nth-child("+(currentTab+1)+")");
					var newTab = $("#productInfo").children(":nth-child("+(index+1)+")");
					
					$("#subContent").css({height:Math.max(newTab.height(), currTab.height())});
					newTab.css({position:"absolute", top:0});
					currTab.css({position:"absolute", top:0});
					if($.browser.WebkitMobile || $.browser.bbos5)
					{
						currTab.removeClass("active").addClass('inactive');
						newTab.addClass("active").removeClass("inactive").css({"position":"static"});
						 $("#subContent").css({height:"auto"});
					}else
					{
						currTab.fadeOut("fast", function(){ $(this).removeClass("active").addClass("inactive");});
						newTab.fadeIn("fast", function(){ $(this).addClass("active").removeClass("inactive").css({"position":"static"}); $("#subContent").css({height:"auto"});});
					}
					self.createTab(index, productInfo[index]);
					if(imgList[currentTab])
						$(".imageWrapper:first", currTab).crossSlideStop();
						
					if(imgList[index])
					{
						self.triggerImagePan(index, newTab);
					}
					var height= newTab.height();
					
					
					
					$("#productInfo").css({"height":height}); 
					currentTab = index;
					
					$("a", this).removeClass("hover");
				});
			
				$(window).unload(this.destroy);
			},
		
			createTab: function(index, product)
			{
				if(!this.tabs['"'+index+'"'])
				{
					var tabs = $('.tabContent:first li', product);
					if(tabs.length)
					{
						var maxheight = 0;
						for(var i=0; i<tabs.length; i++)
						{
							maxheight = Math.max(maxheight, $(tabs[i]).height());
						}
						var tabController = $(".tabControllers:first", product);
						var tabContent = $(".tabContent:first", product);
					
						if(!$.browser.msie)
						{
							tabContent[0].style.height = "auto !important";
						}
						tabContent.css({"min-height":maxheight,  "height":maxheight});
						var carrot = $(".contentCarrot", product).first();
						this.tabs['"'+index+'"'] = new Tabs({"controllerWrapper": tabController, "contentWrapper":tabContent, "carrot":carrot});
					}
				}
			},
			
			bindTriggers: function()
			{
				var triggers = $(".triggers");
				for(var i=0; i<triggers.length; i++)
				{
					$(triggers[i]).bind("click", (function(){var num = i; return function(e){
						var href = triggers[num].getAttribute("href");
						var trig = href.substr(href.indexOf("#"));

						if(document.getElementById(trig.substr(1))){
							$(trig).trigger("click");
						}else{
							$("#nf_"+trig.substr(1)).trigger("click");
						}
					}})());
				}
			},
			
			triggerImagePan: function(index, obj)
			{
				if(this.tabs['"'+index+'"']["imagePanInit"]!=1)
				{
					$(".imageWrapper:first", obj).crossSlide({
												fade:1
												}, 
												imgList[index]					
					);
					this.tabs['"'+index+'"']["imagePanInit"] = 1;
				}
				else
				{
					$(".imageWrapper:first", obj).crossSlideRestart();
				}
						
			},
			
			destroy :function()
			{
				for(var i in this.tabs)
				{
					this.tabs[i].destroy();
					delete this.tabs[i];
				
				}
				this.tabs = null;	
			}
				
		}
		simmons.init();
		window.simmons = simmons;
	})();
});
