/*
 * AVShop Javascript Library 
 * Copyright (c) 2008 AvShopBuilder <avshopbuilder@yahoo.com>
 * Technology support
 */
 
function showCart(x, y)
{
	return;
	$('#cart').slideDown("slow");
	var cart = $('#cart').get(0);
	cart.style.top = y - 120;
	cart.style.left = x - 130;
	$("#Layer3").get(0).scrollTop = $("#Layer3").get(0).scrollHeight ;
}

function hideCart()
{
	$('#cart').slideUp("slow");
}

$(function(){
	$(".add_cart_button").each(function(i){
	  	var item_id = this.id.replace("p", "");
		if(cart[item_id]) {
			this.src = this.src.replace(".gif", "_on.gif");
			$($("#g"+item_id).parent()).removeClass("gift");
		}
	});
	
	$(".gift img").each(function(i){
	  	var item_id = this.id.replace("g", "");
		if(gift[item_id]) {
			$(this.parentNode).removeClass("gift");
			$(this.parentNode).show();
			this.src = this.src.replace(".gif", "_on.gif");
		}
	});	
	
	buildCartList(null);
	// $('#cart').jqDrag(".cart_hand");
	$(".add_cart_button").click( 
	function(e){
		var xy = getMousePosition(e);
		if(this.src.indexOf("_on") > 0) {	
			// alert("此片已經訂購，請勿重複訂購!");
			if(confirm("此片已經訂購了，是否要進入結帳櫃台下單?")) {
				window.location="/cart/view";
			}else {
				showCart(xy.x, xy.y);
			}	
		}else {
			orderItem(this, xy);
		}
	} 
	);
	if($.cookie('giftselect') == "1") {
		if(cart_total.gift_num > 0 && cart_total.gift_added < cart_total.gift_num) {
			$(".gift").show();
			showGiftNum();
		}else {
			$("#show_gift_num_area").hide();
		}
		$(".gift img").click( 
		function(e){
			selectGift(this);
		});	
	}else {
		$("#show_gift_num_area").hide();
	}
});

var showGiftNum = function() {
	$("#show_gift_num_area").show();
	$("#show_gift_tnum").html(cart_total.gift_num);
	$("#show_gift_snum").html(cart_total.gift_added);
	$("#show_gift_lnum").html(cart_total.gift_num - cart_total.gift_added);
	
}

var getMousePosition = function(e){
	var posx = 0;
	var posy = 0;

	if (!e) var e = window.event;

	if (e.pageX || e.pageY) {
		posx = e.pageX;
		posy = e.pageY;
	}
	else if (e.clientX || e.clientY) {
		posx = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
		posy = e.clientY + document.body.scrollTop  + document.documentElement.scrollTop;
	}
	return { 'x': posx, 'y': posy };
}

function buildCartList(xy) 
{
	if(!$('#cart_list'))return false;
	var operation = "/ajax/cartgetitems";
	$.ajax({ url: operation,
		ifModified:false,
		timeout:5000,
		type:"post",
		dataType:"json",
		success:function(json) {
			if (json == "")	{
				cart = new Object();
			}else {
				cart = json;
			}

			var cart_item_num = 0;
			var cart_total_price = 0;
			var cart_item_piece = 0;
			$('#cart_list').html("");
			
			for (var key_var in cart) {
				cart_item_num += cart[key_var].quantity;
				cart_item_piece += parseFloat(cart[key_var].piece) * cart[key_var].quantity;
				cart_total_price += (parseFloat(cart[key_var].price) * cart[key_var].quantity);
				
				var listItem = '<li id="cart_item_'+key_var+'"><em>$'+cart[key_var].price+'</em><a href="javascript:void(0)">'+cart[key_var].name+'</a></li>';
				$('#cart_list').append(listItem);
			}
			
			if(cart_item_num == 0) {
				$('#cart_list').html('<li><span>您還沒有訂購商品</span></li>');
			}else {
				
			}
			$("#cart_item_piece").html(cart_item_piece.toString());
			$("#cart_total_price").html(cart_total_price.toString());
			// 设置点击链接
			$("#cart_list li").click( 
				function(){
					MM_openBrWindow('/goods/pic/'+this.id.replace("cart_item_", ''),'','scrollbars=yes,width=920,height=600')
				} 
			);
			if(xy) {
				showCart(xy.x, xy.y);
				if(!cart_total.gift_num) {
					cart_total.gift_num = 0;
				}				
				// 判断是否有赠片
				if(cart_item_piece%_env_gift_piece==0 && cart_item_piece > 0) {		
					cart_total.gift_num+=parseFloat(_env_gift_num);
					// alert("恭喜！本站買10送2，由於您訂購了"+cart_item_piece+"片，所以可以選擇"+cart_item_piece/_env_gift_piece+"片贈片，多買多送：）\n提示：點擊商品介紹旁邊的 [加入贈片] 選擇贈片");
					// $(".gift").show();
				}				
			}

		},
		complete:function(){}
	});		
}

function orderItem(item, xy, force)
{	
	if($.cookie('shop_id') == "") {
		alert("你的瀏覽器對COOKIE支持有問題，所以不能訂購，建議使用手動下單！");
		return;
	}
	
	if(item.src.indexOf("_ing") > -1) {
		return;
	}
	var item_id = item.id.replace("p", "");	
	var operation = '/ajax/cartadditem';
	var data = "item_id=" + item_id;
	if(!force) force = 0;
	var data = {item_id: item_id, force: force};
	item.src = item.src.replace(".gif", "_ing.gif");

	$.ajax({ url: operation,
		timeout:60000,
		type:"post",
		dataType:"text",
		data: data,
		success:function(data) {
			switch (data) {
				case "2":
					alert("該商品已經在贈片列表中，不需要訂購！");
					item.src = item.src.replace("_ing", "");
					break;				
				case "3":
					item.src = item.src.replace("_ing", "");
					if(confirm("該商品您曾經訂購過，還要繼續訂購嗎")) {
						orderItem(item, null, 1);
					}else {
						return;
					}
					break;				
				// 订购成功
				case "1":
					item.src = item.src.replace("ing.gif", "on.gif");
					item.alt="已訂購";
					$($("#g"+item_id).parent()).removeClass("gift").hide();
					buildCartList(xy);
					break;
				// 已经订购
				case "0":
					item.src = item.src.replace("ing.gif", "on.gif");
					showCart(xy.x, xy.y);
					break;
				default:
					alert("訂購失敗！\n" + data);
					item.src = item.src.replace("_ing", "");
			}
		},
		complete:function(){
		},
		error:function(){
			alert("訂購失敗，請重試！");
			item.src = item.src.replace("_ing", "");
		}
	});	
}

function selectGift(item)
{
	if(item.src.indexOf("_ing") > -1) {
		return;
	}
	
	if(!cart_total.gift_added) {
		cart_total.gift_added = 0;
	}
	// 已经选择
	if(item.src.indexOf("_on") > -1) {
		// alert("此片已經加入為贈片，您不需要再加入本片");
		if(confirm("此片已經為贈片了不需要在訂購是否要進入結帳櫃台下單?")) {
			window.location="/cart/view";
		}
		return;
	}
	var item_id = item.id.replace("g", "");	
	var operation = '/ajax/addgift';
	var data = "idno=" + item_id;
	
	item.src = item.src.replace(".gif", "_ing.gif");

	$.ajax({ url: operation,
		timeout:5000,
		type:"post",
		dataType:"text",
		data: data,
		success:function(data) {
			switch (data) {
				// 赠片选择完毕
				case "3":
				// 加入成功
				case "1":
					item.src = item.src.replace("ing.gif", "on.gif");
					item.alt="已加入";
					$(item.parentNode).removeClass("gift");
					cart_total.gift_added++;
					
					showGiftNum();
					
					if(cart_total.gift_added >= cart_total.gift_num) {
						$(".gift").hide();
						if(confirm("贈片選擇完畢，要去結賬嗎?")) {
							window.location = "/cart/view";
						}
					}
					/*
					if(cart_total.gift_added == cart_total.gift_num && confirm(cart_total.gift_num+"片贈片選擇完畢，要到結賬櫃檯吧？")) {
						window.location = "/cart/view";
					}*/

					break;
				case "2":
					alert("贈片已經完擇完了");
					item.src = item.src.replace("_ing", "");
					break;
				default:
					alert("加入失敗！\n" + data);
					item.src = item.src.replace("_ing", "");
			}
		},
		complete:function(){}
	});	
}