$(function(){
	$("a.lbox, .small_images li").lightBox({
		imageLoading:			HTTP_HOST+'images/lightbox-ico-loading.gif',	
		imageBtnPrev:			HTTP_HOST+'images/lightbox-btn-prev.gif',			
		imageBtnNext:			HTTP_HOST+'images/lightbox-btn-next.gif',		
		imageBtnClose:			HTTP_HOST+'images/lightbox-btn-close.gif',	
		imageBlank:				HTTP_HOST+'images/lightbox-blank.gif'	
	});
		
	$("a.media").media();
	
	$("#carousel").jcarousel({
		vertical: true,
		scroll: 1,
		auto: 3,
		buttonNextHTML: null,
		buttonPrevHTML: null
	});
	
	$("a.cart").click(function(e) {
		addToCart($(this), e, 1);
	});
	
	$("#product_cart_add").click(function(e) {
		var quantity = parseInt($("#product_cart_quantity").val());
		addToCart($(this), e, quantity);
	});
	
	$("img.product_thumb").click(function (e) {
		onChangeThumbnail($(this), e);
	});
	
	totalDivs=calculateDivsLeftSide();
	/*intAnim = setInterval(function() {
		animateLeftSide();						   
	},animDelay*1000);*/
	$("#top_scroll").css({top:0, position:"relative"});
	setTimeout(animateLeftSide, 5000);
	
	$(".small_images li").css({cursor:"pointer"});
});


// leftbar animation script
var animateUnit = 122;
var currentDiv  = 0;
var totalDivs   = 0;
var intAnim     = 0;
var animDelay   = 5; //number of sec before animation
function calculateDivsLeftSide() {
	var i=0,n_div=0;
	while(document.getElementById('prod_'+parseInt(i++)))
		n_div++;
	
	return n_div;
}

var currentLi = 1;
function animateLeftSide() {
	if(currentLi==0)
		currentLi++;
		
	var obj = $("#top_scroll").find(["li:eq(",currentLi,")"].join(""));
	var off = $("#top_scroll").find("li:eq(0)").offset();
	var diff;
	if(obj.length)
		diff = obj.offset().top - off.top;
	else diff = 0;
	if(obj.length == 0 || obj.hasClass("clr")) {
		currentLi = -1;
	}
	
	if(currentLi == -1)
		$("#top_scroll").animate({top:0}, 1000, function() {
			setTimeout(animateLeftSide, 5000);	
		});
	else $("#top_scroll").animate({top:-parseInt(diff)}, 1000, function() {
			setTimeout(animateLeftSide, 5000);
		});
	currentLi++;
}

/*function animateLeftSide() {
	if(currentDiv==3) {
		currentDiv=0;
		$("#top_scroll").animate({top:0}, 1500, function() {
			//success																	 
		});
	} else $("#top_scroll").animate({top:-(++currentDiv*animateUnit)}, 1500, function() {
				//success																	 
		   });
}*/

function onChangeThumbnail(sender, e)
{
	bigPicture = $("#product_picture");
	bigPictureLink = $("#product_picture_link");
	
	picture = sender.attr("src");
	normalLink = picture.replace(/small_/, "normal_");
	bigLink = picture.replace(/small_/, "large_");
	
	bigPicture.attr("src", normalLink);
	bigPictureLink.attr("href", bigLink);
}

function onCartChange(sender, e)
{
	prodId = sender.attr("name");
	quantity = sender.attr("value");
	
	$.ajax({
		type: "GET",
		url: HTTP_HOST+"mods/ajax_cart.php",
		data: "prodId="+prodId+"&quantity="+quantity+"&in_cart=true",
		success: function() {
			refreshCartValue(prodId, quantity);
		}
	});
}

function refreshCartValues()
{
	totalQuantity = 0;
	$("input[id^='cart_product_quantity_']").each ( function (i) {
		totalQuantity = totalQuantity + parseInt($(this).val());
	});
	
	totalPrice = 0;
	$("span[id^='cart_product_total_']").each ( function (i) {
		totalPrice = totalPrice + parseInt($(this).text());
	});
	
	$("#cart").text(totalQuantity);
	$("#cart_quantity").text(totalQuantity);
	$("#cart_price").text(totalPrice);
}

function refreshCartValue(prodId, quantity)
{
	current_price = parseInt($("#cart_product_price_"+prodId).text());
	total_price = quantity * current_price;
	$("#cart_product_total_"+prodId).text(total_price);
	
	refreshCartValues();
}

function addToCart(sender, e, quantity)
{
	e.preventDefault();
	prodId = sender.attr("name");
	
	$.ajax({
		 type: "GET",
		 url: HTTP_HOST+"mods/ajax_cart.php",
		 data: "prodId="+prodId+"&quantity="+quantity,
		 dataType: "text",
		 success: function(data) {
			 $("#cart").html(data);
			window.top.location = HTTP_HOST + 'Page/Cart';
		 }
	});
	return false;
}

function toggleCountry() {
	var pre = '';
	if (arguments.length > 0)
		pre = arguments[0];
	
	country_val = $("#"+ pre +"country").attr('value');
	
	$("#"+ pre +"us_select").attr('disabled', 'disabled');
	$("#"+ pre +"ca_select").attr('disabled', 'disabled');
	$("#"+ pre +"ot_select").attr('disabled', 'disabled');
	$("#"+ pre +"us_tr").css("display", "none");
	$("#"+ pre +"ca_tr").css("display", "none");
	$("#"+ pre +"ot_tr").css("display", "none");

	if(country_val == 'US') {
		$("#"+ pre +"us_tr").css("display", "table-row");
		$("#"+ pre +"us_select").removeAttr('disabled');
	} else if (country_val == 'CA') {
		$("#"+ pre +"ca_tr").css("display", "table-row");
		$("#"+ pre +"ca_select").removeAttr('disabled');
	} else {
		$("#"+ pre +"ot_tr").css("display", "table-row");
		$("#"+ pre +"ot_select").removeAttr('disabled');
	}
}

// tools price calculator
var price_1_limit = 5,
	price_total = 0;
$(function() {
	$("input.quantity_tool").live("keyup", function() {
		price_total = 0;
		$("input.quantity_tool").each(function() {
			var $this = $(this);
			if($this.val()!=""&&!isNaN($this.val())&&$this.val()>0)
				if($this.val()>price_1_limit)
					price_total+=$this.val()*$this.attr("price2");
				else price_total+=$this.val()*$this.attr("price1");
		});
		if(price_total>0)
			$("input.quantity_total").val(price_total);
		else $("input.quantity_total").val("");
	});
});
