$(document).ready(function(){
	$("div.nav-top li").hover(function(){
		$(this).addClass("active");
	},function(){
		$(this).removeClass("active");
	});
});


function addToCart(id,obj) {
		if (($("#add-to-cart").css("display") == 'block')) {
			$("#add-to-cart").css("display","none");
		} else {		
			$("#add-to-cart").css("display",'none');
	
				m_top = $(obj).offset().top-102;
				m_left = $(obj).offset().left+10;
				
			
			$("#add-to-cart").css("left",m_left);
			$("#add-to-cart").css("top",m_top);	
	//		$("#tip").css("display")= 'block';
			$("#add-to-cart").show('clip',{},300,function () {
 			 setTimeout(function(){closeCart('add-to-cart');}, 3000);
  			});
		}
	
		JsHttpRequest.query(
			'/include/js_back.php', // backend
			{
				// pass a text value 
				'a': 'addtocart',
				'id':id
				},
			// Function is called when an answer arrives. 
			function(result, errors) {
				if (result["res"]==1 ){
					$("#header-cart").css("display",'block');
					$("#cart-count").html(result["cnt2"]);
				}
			},
			true  // do not disable caching
		);
}

	function closeCart(id){
		if ($("#"+id).css("display")== 'block') 	
			$("#"+id).hide('clip',{},300);	
	}

var activeItemID=null;

function delFromCartDialog(id,obj) {
		activeItemID=id;
		if (($("#del-from-cart").css("display")== 'block')) {
			$("#del-from-cart").css("display")= 'none';
		} else {		
			$("#del-from-cart").css("display",'none');
	
			m_top = $(obj).offset().top-92;
			m_left = $(obj).offset().left-175;
			$("#del-from-cart").css("left",m_left);
			$("#del-from-cart").css("top",m_top);	
	//		$("#tip").css("display")= 'block';
			$("#del-from-cart").show('clip',{},300);
		}
}

function delAllFromCartDialog(obj) {
		if (($("#clear-cart").css("display")== 'block')) {
			$("#clear-cart").css("display",'none');
		} else {		
			$("#clear-cart").css("display",'none');
	
			m_top = $(obj).offset().top-92;
			m_left = $(obj).offset().left-170;
			$("#clear-cart").css("left",m_left);
			$("#clear-cart").css("top",m_top);	
	//		$("#tip").css("display")= 'block';
			$("#clear-cart").show('clip',{},300);
		}
}

function delFromCart() {
			JsHttpRequest.query(
					'/include/js_back.php', // backend
					{
						// pass a text value 
						'a': 'changecount',
						'cnt':0,
						'id':activeItemID
						},
					// Function is called when an answer arrives. 
					function(result, errors) {
						if (result["res"]==1 ){
							$("#del-from-cart").hide('clip',{},300);
							$("#cart-sum-total").html(result["totalsum"]);
							$("#cart-count").html(result["cnt2"]);
							activeItemID = null;
							$("#item-"+result["id"]).hide('clip',{},300,function(){
								var i=1;
								$.each($("td.number:visible"),function() {  
									//this.removeClassName('error') 
									$(this).html(i+".");
									i++;
								})
 
																				});
							if (result["cnt"]==0){
								$("#cart-count").html('');
								$("#header-cart").hide();
								$("#cart").hide('drop',{},500);
								$("#no-items").show('drop',{},500);
							}else{

							}
						}
					},
					false  // do not disable caching
				);
}


function delAllFromCart() {
			$("#clear-cart").hide('clip',{},500,setTimeout(function(){clearCart();}, 300));
}

function clearCart() {
			JsHttpRequest.query(
					'/include/js_back.php', // backend
					{
						// pass a text value 
						'a': 'clearcart'
						},
					// Function is called when an answer arrives. 
					function(result, errors) {
						if (result["res"]==1 ){
							$("#cart-count").html('');
							$("#header-cart").css("display",'none');
							$("#cart").hide('drop',{},500);
							$("#no-items").show('drop',{},500);
						}
					},
					false  // do not disable caching
				);
}

	function ChangeCase() {
	document.cartgoodsform.select.value='recount';
	document.cartgoodsform.submit();
	}
	function ChangeCase2() {
	document.cartgoodsform.select.value='accept';
	document.cartgoodsform.submit();
	}
	
	function checkme(input,id)
	{
		var new_value = parseInt(input.value.replace(/(\D+)/,''));
		if(new_value >= 0 && new_value <= 100) { changeItemCount(id,new_value);return false; }
		new_value = new_value > 100? 100 : new_value;
		new_value = new_value < 0? 1 : new_value;
		input.value = new_value;
		return false;
	}

	function MakeUp(id)
	{
		var input = document.getElementById('num_'+id);
		var new_value = parseInt(input.value.replace(/(\D+)/,''))+1;
		new_value = new_value > 100? 100 : new_value;
		input.value = new_value;
		changeItemCount(id,new_value);
		return false;
	}
	
	function changeItemCount(id,cnt){

				JsHttpRequest.query(
					'/include/js_back.php', // backend
					{
						// pass a text value 
						'a': 'changecount',
						'id':id	,
						'cnt':cnt
						},
					// Function is called when an answer arrives. 
					function(result, errors) {
						if (result["res"]==1 ){
							$("#cart-sum-total").html(result["totalsum"]);
							$("#cart-count").html(result["cnt2"]);
						}
					},
					false  // do not disable caching
				);
	}

	function MakeDown(id)
	{
		var input = document.getElementById('num_'+id);
		var new_value = parseInt(input.value.replace(/(\D+)/,''));
		new_value = new_value > 100? 100 : new_value;
		--new_value;
		new_value = new_value < 1? 1 : new_value;
		input.value = new_value;
		changeItemCount(id,new_value);
		return false;
	}

function startOrder(obj){
	if ($("#is-login").val()>0){
			window.location.href='/cart/order/';
	}else{
		if (($("#do-cart-login").css("display")== 'block')) {
			$("#do-cart-login").css("display",'none');
		} else {		
			$("#do-cart-login").css("display",'none');
	
			m_top = $(obj).offset().top-92;
			m_left = $(obj).offset().left-130;
			
			$("#do-cart-login").css("left",m_left);
			$("#do-cart-login").css("top",m_top);	
	//		$("#tip").css("display")= 'block';
			$("#do-cart-login").show('clip',{},300);
		}
	}

}

function showIndexDiv(div){
	$("#li-"+div).parents("ul").children("li").removeClass("active");
	$("#li-"+div).addClass("active");
	if ($("#"+div).css("display")== 'none') {
		$(".index-tab").hide();	
		$("#"+div).show();	
	}
	
}


