$(function(){
	//** INJECT THE DATE INTO THE FOOTER **/
	var d=new Date();
    $("#currentYear").html( d.getFullYear() );
    $(".ssbutton").addClass('ui-button ui-state-default ui-corner-all');
    //NYROMODAL LIVE UPDATING FOR FUTURE DOM OBJECTS
    $('.nyroModal').live('mouseover',function(){ $(this).nyroModal({minHeight:'100'}); });
    //USE "sstip" CLASS TO DISPLAY TOOL TIP OFF TITLE ATTR
    $("a.sstip").live('mouseover',function(){ $(".aToolTip").remove(); $(this).aToolTip().mouseover(); });
    
    /** INJECT THE QTY INTO THE AJAX ADDTOCART CALL **/
    //ALLOW ONLY NUMBERS IN QTY
    $("input.qty").keyup(function(e){
    	var qty = $(this).val();
    	qty = parseInt( qty );
    	if(qty) {
	    	$(this).val(qty);
    	}
    	else{
	    	$(this).val('');
    	}
    });
    //ADJUST ADDTOCART CALL BASED ON QTY
    $(".add2cart").click(function(e){
    	//e.preventDefault();
    	var thisid = $(this).attr('id');
    	var qtyid = thisid.replace('add2cart-','qty-');
    	var qty = $("#"+qtyid).val();
    	var rel = $(this).attr('rel');
    	var data = rel.split('|');
    	qty = parseInt( qty );
    	if( qty ) {
    		var openAJAXcall = "openAJAX('pagename=ajax_ecommercemodal&fname=addtocart|&str1="+data[0]+"&str2="+data[1]+"&str3="+qty+"&str4=add')";
    		eval(openAJAXcall);
    	}
    	else{
    		var openAJAXcall = "openAJAX('pagename=ajax_ecommercemodal&fname=addtocart|&str1="+data[0]+"&str2="+data[1]+"&str3=1&str4=add')";
    		eval(openAJAXcall);
    	}
    });
    
    /*** BUY NOW HOVERING ***/
    $(".buynowhoveritem").hover(function(){
    	//hoverIn
    	$("#buynowDropDown:hidden").fadeIn();
    },function(){
    	//hoverOut
    	$("#buynowDropDown:visible").hide();
    });
    $(".buyitnowmenu").click(function(){
    	if( $("#buynowDropDown").is(":hidden") ){
    		$("#buynowDropDown").show();
    	}else{
    		$("#buynowDropDown:visible").hide();
    	}
    });
    
	/*** PRODUCT TABS ***/
    //TOGGLE PRODUCT TABS
    $("#productTabs a").click(function(e){
    	var tabName = this.id;
    	e.preventDefault();
    	if( !$(this).hasClass("on") ){
    		var pagename = $("#email_friend_link").attr("rel");
    		$("#email_friend_link").attr("onclick","openAJAX('pagename=ajax_email_friend&fname=start|&url="+pagename+"?productTab="+tabName+"');"); //CHANGE EMAIL FRIEND ATTR
    		$("#productTabs a,#productTestimonialsLink,#productBenefitsLink").removeClass("on"); //turn off all "secondary" tabs
    		$(".productCategoryTab").hide(); //hide all productCategory content divs
    		$(this).addClass("on"); //turn on clicked main tab
    		$("#productDetailsLink").addClass("on"); //activate details sub-tab
    		$("#category-"+tabName).show(); //show clicked productCategory content div
    		$(".productBenefits,.productTestimonials,.testimonialsTitle").hide(); //hide all secondary content divs
    		var $visibleTab = $(".productCategoryTab:visible"); //grab visible productCategory content div
			$visibleTab.find("img.productPhoto").show(); //show product photo
			$visibleTab.find("div.productDescription").show(); //show product description
			$visibleTab.find("div.productTitle").show(); //show product title
			setProductBoxHeight($visibleTab); //adjust product box height
    	}
    });
    //TOGGLE DETAILS/BENEFITS
	$("#productBoxTop a").click(function(e){
		e.preventDefault();
		var $visibleTab = $(".productCategoryTab:visible");
		var $visibleBlurbDiv = $visibleTab.find("div.productBlurb");
		if( !$(this).hasClass("on") ){
			setProductBoxHeight($visibleTab); //adjust product box height
			$("#productBoxTop a").removeClass("on");
			$(this).addClass("on");
			if( this.id == 'productDetailsLink' ){
				$(".testimonialsTitle").hide();
				$visibleTab.find("div.productTitle").show();
				$visibleTab.find("img.productPhoto").show();
				$visibleBlurbDiv.children("div").hide();
				$visibleBlurbDiv.find("div.productDescription").show();
			}else if(this.id == 'productBenefitsLink'){
				$(".testimonialsTitle").hide();
				$visibleTab.find("div.productTitle").show();
				$visibleTab.find("img.productPhoto").hide();
				$visibleBlurbDiv.children("div").hide();
				$visibleBlurbDiv.find("div.productBenefits").show();
			}else if(this.id == 'productTestimonialsLink'){
				$(".testimonialsTitle,.productTitle").hide();
				$visibleTab.find("div.testimonialsTitle").show();
				$visibleTab.find("img.productPhoto").hide();
				$visibleBlurbDiv.children("div").hide();
				var $visibleTestimonial = $visibleBlurbDiv.find("div.productTestimonials");
				$visibleTestimonial.show();
				$visibleTestimonial.children("div.testimonialQuote").hide();
				$visibleTestimonial.children("div.testimonialQuote").eq(0).show();
			}
		}
	});
	
	//PRODUCT INFO MODAL
	$("a.productInfo").click(function(e){
		e.preventDefault();
		var href = $(this).attr("href");
		$("#"+href).dialog({
			title:'Product Information',
			beforeclose:function(event, ui){$(this).dialog( 'destroy' );}
		});
	});
	
	/*** PRODUCT CATEGORY IMAGE ROTATION ***/
    if( $('#categoryRotation').is("div") ){
		//$('.CategoryImageArea:eq(0)').show();
		//setTimeout ( "showNext()", rotationTime );
    	$('#categoryRotation').cycle();
    }
	
	/*** PRODUCT HIGHLIGHT SLIDER AT BOTTOM OF PAGE ***/
	var eq0 = 0;
	$("#internalproductspotlight #prev a").addClass("hideArrow");
	if( $(".productHighlight").size() <= 3 ){
		$("#internalproductspotlight #next a").addClass("hideArrow");
	}
	$(".productHighlight:gt(2)").hide(); //only show first 3
	$(".productHighlight:eq(0)").css({'left':'0px'});
	$(".productHighlight:eq(1)").css({'left':'283px'});
    $(".productHighlight:eq(2)").css({'left':'566px'});
    $("#internalproductspotlight #prev, #internalproductspotlight #next").click(function(e){
    	e.preventDefault();
    	var id = $(this).attr('id');
    	var eq1 = eq0+1;
    	var eq2 = eq0+2;
    	var eqNx = eq0+3;
    	var eqPv = eq0 - 1;
    	if(id=='next'){
    		if( $(".productHighlight:eq("+eqNx+")").is(".productHighlight") ){
    			$(".productHighlight").stop(); //stop previous animations
	    		$(".productHighlight:eq("+eq0+")").fadeOut("normal",function(){
	    			$(".productHighlight:eq("+eq1+")").animate({left: "0px"}, { queue:false, duration:400 });
	    			$(".productHighlight:eq("+eq2+")").animate({left: "283px"}, { queue:false, duration:401, complete:function(){
		    			$(".productHighlight:eq("+eqNx+")").css({'left':'566px'}).fadeIn();
		    			eq0 = eq1;
		    			$("#internalproductspotlight #prev a").removeClass("hideArrow");
		    			var last = $(".productHighlight").size() - 1;
		    			if( (eq0+2)==last ){
		    				$("#internalproductspotlight #next a").addClass("hideArrow");
		    			}
	    			} });
	    		});
    		}
    	}
    	else{
    		if( eqPv>=0 && $(".productHighlight:eq("+eqPv+")").is(".productHighlight") ){
    			$(".productHighlight").stop(); //stop previous animations
	    		$(".productHighlight:eq("+eq2+")").fadeOut("normal",function(){
	    			$(".productHighlight:eq("+eq1+")").animate({left: "566px"}, { queue:false, duration:400 });
	    			$(".productHighlight:eq("+eq0+")").animate({left: "283px"}, { queue:false, duration:401, complete:function(){
		    			$(".productHighlight:eq("+eqPv+")").css({'left':'0px'}).fadeIn();
		    			eq0 = eqPv;
		    			$("#internalproductspotlight #next a").removeClass("hideArrow");
		    			if(eq0==0){
		    				$("#internalproductspotlight #prev a").addClass("hideArrow");
		    			}
	    			} });
	    		});
    		}
    	}
    });
    //PRODUCT SPOTLIGHT LINKS
    $("#internalproductspotlight #products a").click(function(e){
    	e.preventDefault();
    	var href = $(this).attr("href");
    	if( $("#productTabs").is("div") ){
    		$("#"+href).click();
    	}else{
    		window.location='/everyday?productTab='+href;
    	}
    });
    //BUYNOW DROP DOWN
    $("#buynowDropDown a").click(function(e){
    	e.preventDefault();
    	var rel = $(this).attr('rel');
    	var data = rel.split('|');
    	var openAJAXcall = "openAJAX('pagename=ajax_ecommercemodal&fname=addtocart|&str1="+data[0]+"&str2="+data[1]+"&str3=1&str4=add')";
    	eval(openAJAXcall);
    });
    //EMAIL FRIEND LINK
    $("#email_friend_link").click(function(e){
    	e.preventDefault();
    	eval( $(this).attr('onclick') );
    });
    //CHECK QUERY STRING FOR PARAMS
    var QSproductTab = getParameterByName('productTab')
    if(QSproductTab!=''){
    	//IF 'productTab' is avail click it to activate that tab
    	$("#"+QSproductTab).click();
    }
    
    $(".prevTestimonial,.nextTestimonial").click(function(e){
    	e.preventDefault();
    	var direction = $(this).attr('href');
    	cycleTestimonial(direction);
    });
   
    /*** ACCOUNT PAGE HIGHLIGHTING ***/
    //need to use 'live' because accountcontrol items are created on the fly
    $(".accountcontrol").live('click',function(){
    	$(".accountcontrol").css('color','#727272').removeClass("accountHighlight");
    	$(this).addClass("accountHighlight");
    	$(".accountHighlight").css('color','#ed1c24');
    });
    
    /** IE6 HACK **/
    if ($.browser.msie && $.browser.version < 7){
    	$("#productDesc").css("margin","15px 0 0 10px");
    }
    
    /*SWF OBJECT PLUGIN FOR JQUERY*/
    if( $('#HomeRotation').is("div") ){
    	$('#HomeRotation').cycle({
    		timeout: 8500,
    		pager:  '#HomeRotationNav',
    		slideExpr: 'img'
    	});
    }
    $("#HomeRotationNav a").click(function(){
    	$('#HomeRotation').cycle('pause');
    });
    
    $('#everydayVideo').flash({
    	swf: '/syncshow/style/images/videoProducts/everydayVideo.swf',   
    	height: 394,   
    	width: 700,   
    	params: {
    		play:true,
    		allowScriptAccess: 'sameDomain',   
    		quality: 'high', 
    		bgcolor:'#FFFFFF', 
    		wmode:'transparent',
    		menu:'false',
    		flashvars: {
    			var1: true,
    			link: '/syncshow/style/images/videoProducts/'
    		}   
    	}  
    });
    $('#indVideo').flash({
    	swf: '/syncshow/style/images/videoProducts/industrialVideo.swf',   
    	height: 394,   
    	width: 700,   
    	params: {
    		play:true,
    		allowScriptAccess: 'sameDomain',   
    		quality: 'high', 
    		bgcolor:'#FFFFFF', 
    		wmode:'transparent',
    		menu:'false',
    		flashvars: {
    			var1: true,
    			link: '/syncshow/style/images/videoProducts/'
    		}   
    	}  
    });
    $('#playVideo').flash({
    	swf: '/syncshow/style/images/videoProducts/playVideo.swf',   
    	height: 394,   
    	width: 700,   
    	params: {
    		play:true,
    		allowScriptAccess: 'sameDomain',   
    		quality: 'high', 
    		bgcolor:'#FFFFFF', 
    		wmode:'transparent',
    		menu:'false',
    		flashvars: {
    			var1: true,
    			link: '/syncshow/style/images/videoProducts/'
    		}   
    	}  
    });
    $('#firstResponderVideo').flash({
    	swf: '/syncshow/style/images/videoProducts/firstResponderVideo.swf',   
    	height: 394,   
    	width: 700,   
    	params: {
	    	play:true,
	    	allowScriptAccess: 'sameDomain',   
	    	quality: 'high', 
	    	bgcolor:'#FFFFFF', 
	    	wmode:'transparent',
	    	menu:'false',
	    	flashvars: {
		    	var1: true,
		    	link: '/syncshow/style/images/videoProducts/'
		    }   
    	}  
    });
    $('#dryAfogClothsVideo').flash({
    	swf: '/syncshow/style/images/videoProducts/dryAfogCloths.swf',   
    	height: 394,   
    	width: 700,   
    	params: {
	    	play:true,
	    	allowScriptAccess: 'sameDomain',   
	    	quality: 'high', 
	    	bgcolor:'#FFFFFF', 
	    	wmode:'transparent',
	    	menu:'false',
	    	flashvars: {
		    	var1: true,
		    	link: '/syncshow/style/images/videoProducts/'
		    }
	    }
    });
    $('#antifogConcentrateVideo').flash({
    	swf: '/syncshow/style/images/videoProducts/antifogConcentrate.swf',   
    	height: 394,   
    	width: 700,   
    	params: {
	    	play:true,
	    	allowScriptAccess: 'sameDomain',   
	    	quality: 'high', 
	    	bgcolor:'#FFFFFF', 
	    	wmode:'transparent',
	    	menu:'false',
	    	flashvars: {
		    	var1: true,
		    	link: '/syncshow/style/images/videoProducts/'
	    	}
    	}
    });
    $(".onLoadVideoLink").click(function(){
    	//clearTimeout(to);
    	$('#categoryRotation').cycle('pause');
    });
});


/*** FUNCTIONS ***/
var to;
var rotationTime = 5000;
function showNext(){
	var count = $('.CategoryImageArea').length;
	var index = $('.CategoryImageArea:visible').index('.CategoryImageArea');
	var nextIndex = index+1;
	if(nextIndex == count) nextIndex = 0;
	$('.CategoryImageArea:visible').fadeOut(400,function(){
		$('.CategoryImageArea:eq('+nextIndex+')').fadeIn();
		to = setTimeout("showNext()", rotationTime);
	});
}
function cycleTestimonial(direction){
	//$(".productCategoryTab:visible > div.productBlurb > div.productTestimonials > div.testimonialQuote:eq(0)").show();
	var $visibleTestimonial = $("div.productTestimonials:visible");
	var count = $visibleTestimonial.children("div.testimonialQuote").length;
	var index = $visibleTestimonial.children("div.testimonialQuote:visible").index('div.productTestimonials:visible > div.testimonialQuote');
	if(direction=='next'){
		var nextIndex = index+1;
		if(nextIndex == count) nextIndex = 0;
		$visibleTestimonial.children('div.testimonialQuote:visible').fadeOut(400,function(){
			$visibleTestimonial.children('div.testimonialQuote:eq('+nextIndex+')').fadeIn();
		});
	}else
	if(direction=='prev'){
		var prevIndex = index-1;
		if(prevIndex < 0) prevIndex = (count - 1);
		$visibleTestimonial.children('div.testimonialQuote:visible').fadeOut(400,function(){
			$visibleTestimonial.children('div.testimonialQuote:eq('+prevIndex+')').fadeIn();
		});		
	}
}
var g_ProductBoxHeight = 0; //global product box height
function setProductBoxHeight($visibleTab){
	//LOGIC BELOW ADJUSTS THE HEIGHT IF NEED BE
	var boxHeight = $visibleTab.find("div.productBoxHeight").outerHeight(true);
	g_ProductBoxHeight = (boxHeight > g_ProductBoxHeight) ? boxHeight : g_ProductBoxHeight;
	$visibleTab.find("div.productBoxHeight").height(g_ProductBoxHeight);
	//$visibleTab.find("div.productBoxHeight").animate({height:g_ProductBoxHeight}, { queue:false, duration:400 });
}
function getParameterByName( name )
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}
function addbookmark()
{
	if ( navigator.appName != 'Microsoft Internet Explorer')
		window.sidebar.addPanel(document.title,document.URL,"");
	else
		window.external.AddFavorite(document.URL,document.title);
}
