/**
 * 
 */
var _editVal = 0;
var _shelfHeight = $('.shelf .cart_item').length * 101 + 9 +'px';
$().ready(function(){
	
	//新增購物商品到購物車
	$('#addcart').click(function(){
		//alert($("#size option:selected").attr('disabled'));
		if ($('#size').val() != "xx"){
			$.ajax({
				url: add_cart_url,
				type: "post",
				data: {"itemId":$('#size').val()},
				dataType: "json",
				success: function(data){
					alert(data['message']);
					if(data['retCode'] == "1"){
						$('.overview').empty();
						$('.overview').append(data['cart_html']);
						$('#totalPrice').text(data['totalPrice']);
						$('#totalItems').text(data['totalItems']);	
						cartHeight();			
						$('#cart').tinyscrollbar();	
						scrollShowOrHide();			
						$('.overview .closeBtn').click( deleteEvent );
						
					}else{
						
					}
				},
				error: function(e){
					alert("error");
				}
			});
		}else {
			alert(cannot_add_to_cart);
		}
	});
	
	//購物車沒商品時點擊checkout
	$('#checkoutBlock').click(function(){
		if($('.shelf .cart_item').length == 0){
			if($('input[name = "languages"]').val() == 0){
				alert('There are no products in cart');
			}else 
			if($('input[name = "languages"]').val() == 1){
				alert('购物车内没有任何商品');
			}else
			if($('input[name = "languages"]').val() == 2){
				alert('購物車內沒有任何商品');
			}
			return false;
		}
	})		
	//刪除購物商品
	$('.overview .closeBtn').click( deleteEvent );
	
	/*
	//刪除購物商品
	$('.overview .closeBtn').click(function(){
		deleteEvent($('.overview .closeBtn'));
	});
	*/
});

function deleteEvent(){
	
	$.ajax({
		url: delete_cart_url,
		type: "post",
		data: {"itemId":$(this).attr('rel')},
		dataType: "json",
		success: function(data){
			//updateOverView(data);
			//alert(data);
			//alert("成功加入購物車");
			if(data['retCode'] == "1"){
				$('.overview').empty();
				$('.overview').append(data['cart_html']);
				$('#totalPrice').text(data['totalPrice']);
				$('#totalItems').text(data['totalItems']);
				
				cartHeight();				
				$('#cart').tinyscrollbar();
				scrollShowOrHide();	
				$('.overview .closeBtn').click( deleteEvent );
				
			}else{
				
			}
			
		},
		error: function(e){
			alert("error");
		}
	});
}
function cartHeight(){
	_shelfHeight = $('.shelf .cart_item').length * 101 + 9 +'px';
	if ($('.shelf .cart_item').length <= 5) {
		$('.shelf').height(_shelfHeight);
	}else {
		$('.shelf').height('505px');
	}
}
function scrollShowOrHide(){
	if($('.shelf .cart_item').length > 5 && _editVal == 1){
		$('.scroller').show();
		$('.overview').css('position','absolute');
	}else{
		$('.scroller').hide();
		$('.overview').css('position','static');		
	}	
}
