// when the DOM is ready...
jQuery(document).ready(function () {
	
	jQuery("#usp .SubCategories ul.SubCategories").remove();
	jQuery(".home .hider .SubCategories div.clear").remove();
	
	jQuery(".item_image").mouseover(function() {
		jQuery(this).addClass("iihover");
	  }).mouseout(function(){
		jQuery(this).removeClass("iihover");
	  });


	if( jQuery("#sliding_cart").children().size() > 2){
		jQuery("#shopping-cart").show();
	}else{
		jQuery("#shopping-cart").hide();
	}
	jQuery("#shopping-cart .emptycart a").click(function() {
		jQuery("#shopping-cart").fadeOut();
		return false;
	});
	jQuery(".wpsc_buy_button").click(function(){
		jQuery("#shopping-cart").show();
	});
	jQuery("#facebox a.close").addClass("jqmClose");
	jQuery("#clickablecleanroom a.close").addClass("jqmClose");
	
	var myOpen=function(hash){ hash.w.fadeIn('2000'); };
	var myClose=function(hash) { hash.w.fadeOut('2000',function(){ hash.o.remove(); }); };

	jQuery("#facebox").jqm({trigger:'.fbTrigger',onShow:myOpen,onHide:myClose});
	
	jQuery("#clickablecleanroom").jqm({trigger:'.ccTrigger',onShow:myOpen,onHide:myClose});
	jQuery(".ccTrigger").click(function(event) {
  		event.preventDefault();
	});
	
		
	var prodtitle = jQuery("h2.prodtitles span").text();
	jQuery("#cf2_field_1").val(prodtitle);
	
	var wait = 4000; // Milliseconds to wait for auto-switching
	var wait2 = 5000; // Milliseconds to wait for auto-switching
	
	
	if(jQuery.browser.msie){
      var pLength = jQuery("#LatestProd .product-list").children().size()-1;
   }else{
      var pLength = jQuery("#LatestProd .product-list").children().size();
   }

	//var pLength = jQuery("#LatestProd .product-list").children().size();
	var pWidth = jQuery("#LatestProd .product-list").children().width() + 4;
	var xLength = (pLength - 4) * pWidth;
	jQuery("#LatestProd .product-list").width(pWidth * pLength);
	
	var p = jQuery("#LatestProd .product-list");
	var offset = p.offset();
	
	if(offset != null){
		var farleft = offset.left - xLength;
		var faright = offset.left;
	
	setInterval(function() { 
		if(offset.left > farleft){
			jQuery("#LatestProd .product-list").animate({
				'left': '-=' + pWidth},
				1000, 
				function(){
					offset = p.offset();														
				});
		}
		else {
			jQuery("#LatestProd .product-list").animate({
				'left': 0},
				1000, 
				function(){
					offset = p.offset();														
				});
		}; }, wait);
	
	
	jQuery("#LatestProd .b-right a.next").click(function(event){
		event.preventDefault();
		if(offset.left < faright){
			jQuery("#LatestProd .product-list").animate({
				'left': '+=' + pWidth},
				1000, 
				function(){
					offset = p.offset();														
				});
		}
	});
	jQuery("#LatestProd .b-right a.prev").click(function(event){
		event.preventDefault();
		
		if(offset.left != farleft){
			jQuery("#LatestProd .product-list").animate({
				'left': '-=' + pWidth}, 
				1000, 
				function(){
					offset = p.offset();
				});
		}
		else{
			jQuery("#LatestProd .product-list").animate({
				'left': 0},
				1000, 
				function(){
					offset = p.offset();													
				});
		}
	});
	}
	if(jQuery.browser.msie){
		  var ucLength = jQuery("#usp .SubCategories").children().size() - 1;
	}else{
		 var ucLength = jQuery("#usp .SubCategories").children().size();
	}
	
	var uWidth = jQuery("#usp .SubCategories").children().width() + 4;
	var uLength = (ucLength - 4) * uWidth;
	jQuery("#usp .SubCategories").width(uWidth * ucLength);
	
	var u = jQuery("#usp .SubCategories");
	var uoffset = u.offset();
	
	if(uoffset != null){
		var ufarleft = uoffset.left - uLength;
		var ufaright = uoffset.left;
		
	setInterval(function() { 
		if(uoffset.left > ufarleft){
			jQuery("#usp .SubCategories").animate({
				'left': '-=' + uWidth},
				1000, 
				function(){
					uoffset = u.offset();
				});
		}
		else {
			jQuery("#usp .SubCategories").animate({
				'left': 0},
				1000, 
				function(){
					uoffset = u.offset();														
				});
		};}, wait2);
		
	jQuery("#usp .b-right a.next").click(function(event){
		event.preventDefault();
		
		if(uoffset.left < ufaright){
			jQuery("#usp .SubCategories").animate({
				'left': '+=' + uWidth},
				1000, 
				function(){
					uoffset = u.offset();														
				});
		}
	});
	jQuery("#usp .b-right a.prev").click(function(event){
		event.preventDefault();
		
		if(uoffset.left != ufarleft){
			jQuery("#usp .SubCategories").animate({
				'left': '-=' + uWidth}, 
				1000, 
				function(){
					uoffset = u.offset();
				});
		}
		else{
			jQuery("#usp .SubCategories").animate({
				'left': 0},
				1000, 
				function(){
					uoffset = u.offset();													
				});
		}
	});
	}

	jQuery(".page .SubCategories div:nth-child(5)").css({"clear":"both"});
	jQuery(".page .SubCategories div:nth-child(9)").css({"clear":"both"});
	jQuery(".page .SubCategories div:nth-child(13)").css({"clear":"both"});
	jQuery(".page .SubCategories div:nth-child(17)").css({"clear":"both"});
	
	if(jQuery('h1.prod-cat-title').text() == "Checkout"){
		jQuery("#shopping-cart").hide();
	}
	
	
}); 
