$(document).ready( function() {
    var waterWheelCurrent = 3;
	if($("#waterwheelCarousel .currentImage")){
		waterWheelCurrent = $("#waterwheelCarousel .currentImage").index() + 1;
		waterWheelCurrentID = $("#waterwheelCarousel .currentImage").attr("productid");
	}
	
	$("input.clkChange, textarea.clkChange").focus( function() {
		var defaultVal = $(this).attr("defaultvalue");
		if($(this).attr("value") == defaultVal){
			$(this).attr("value", "");
		}
	});
	
	$("input.clkChange, textarea.clkChange").blur( function(){
		if($(this).attr("value") == ""){
			var defaultVal = $(this).attr("defaultvalue");
			$(this).attr("value",defaultVal);
		}
	});
	
	InitWaterWheel(waterWheelCurrent);
	
	HideWaterWheelArrows(waterWheelCurrentID);
	
	$(".leftList li").click( function(){
		var currentProduct = $(this).attr("productid");
		if(currentProduct > 0){
			$(".leftList .current").removeClass("current");
			$(this).addClass("current");
			if($("#waterwheelCarousel img[productid="+currentProduct+"]")){
				$("#waterwheelCarousel img[productid="+currentProduct+"]").trigger("click");
			}
			$("#productDetails").load("/get-product?product="+currentProduct);
			return false;
		}
	});
	
	$("#waterwheelCarousel img").click( function(){
		var product = $(this).attr("productid");
		if(product > 0){
			$(".leftList .current").removeClass("current");
			$(".leftList li[productid="+product+"]").addClass("current");
			$("#productDetails").load("/get-product?product="+product);
		}
	});
	
	$("img#left, img#right").click( function(){
		var product = $(this).attr("productid");
		if(product > 0){
			$(".leftList .current").removeClass("current");
			$(".leftList li[productid="+product+"]").addClass("current");
			$("#productDetails").load("/get-product?product="+product);
		}
	});
	
    if (navigator.userAgent.toLowerCase().match('chrome') || navigator.userAgent.toLowerCase().match('safari')){
        offset = 995 - $(window).width();
        //$("#waterwheelCarousel").css('left', '-285px');
    };
    
    $(".menuContainer .menu li .selected").each( function(){
    	$(this).parents("li").addClass("selected");
    	//$(this).removeClass("selected");
    });
    
    $(".cycleContainer").cycle({fx: "fade"});
    $(".cycleContainer").height(230);
    
    $('.contact .submit').click(function() {
  	  $(this).siblings("input:visible, textarea").each( function(){
  		  var defaultVal = $(this).attr("defaultvalue");
  		  if ( $(this).attr("value") == defaultVal ) {
  			  $(this).attr("value", "");
  		  }
  	  });
  	  return true;
    });
    
    $(".mainL a").fancybox({
		'transitionIn'	:	'elastic',
		'transitionOut'	:	'elastic',
		'speedIn'		:	600, 
		'speedOut'		:	200,
		'autoScale' : true,
		'autoDimensions' : false

	});
});

function InitWaterWheel(waterWheelCurrent){
	
	$("#waterwheelCarousel").waterwheelCarousel("horizontal",{
		  startingItem: waterWheelCurrent,
		  flankingItems: 2,
		  itemDecreaseFactor: .7,
		  opacityDecreaseFactor: 1,
		  waveSeparationFactor: 0,
		  movedToCenter: function(item){
			  $("#waterwheelCarousel .currentImage").removeClass("currentImage");
			  $(item).addClass("currentImage");
			  var currentProduct = $(item).attr("productid");
			  if($(".leftList li[productid="+ currentProduct +"]")){
				  HideWaterWheelArrows(currentProduct);
			  }
		}
		});
}
function HideWaterWheelArrows(currentProduct){
	var next = $(".leftList li[productid="+ currentProduct +"]").next().attr("productid");
	var prev = $(".leftList li[productid="+ currentProduct +"]").prev().attr("productid");
	
	 if($("#waterwheelCarousel .currentImage").index() == 0){
		  $(".rightCol img#left.left").hide();
		  $(".rightCol img#right.right").show().attr("productid", next);
	  }
	  else if($("#waterwheelCarousel .currentImage").index()+1 == $("#waterwheelCarousel img").length){
		  $(".rightCol img#right.right").hide();
		  $(".rightCol img#left.left").show().attr("productid", prev);;
	  }
	  else{
		  $(".rightCol img#left.left").show().attr("productid", prev);;
		  $(".rightCol img#right.right").show().attr("productid", next);;
	  }
}
