var tip = '<div class="Tip_Title">' +
		'<em><img src="'+context+'/images/tip_close.jpg" class="Tip_Close"></em>' +
		'${existFavorite ? "该商品已收藏！" : "商品收藏成功！"}</div>' +
		'<div class="Tip_Content">${existFavorite ? "该商品已收藏！": "商品收藏成功！" }' +
		'<a href="'+viewGoodsFavorite+'">查看收藏夹</a><br/><strong id="timer_5">2</strong>秒后自动关闭</div>';
		
var cartTip = '<div class="big_18"><div class="big_19">' +
		'<img src="'+context+'/images/{if success.indexOf(\'exist\') > -1}big_06_b.gif{else}big_06.gif{/if}" width="16" height="16" /></div>' +
		'<div class="big_20">{if success.indexOf(\'exist\') > -1}添加商品失败{else}商品已成功添加到购物车{/if}</div><div class="big_21">' +
		'<img src="'+context+'/images/big_07.gif" width="15" height="14" /></div></div>' +
		'<div class="big_22">{if success.indexOf(\'1_\') > -1}购物车内共 ${count} 款商品&nbsp;&nbsp;&nbsp;&nbsp;合计：<span class="huang_a">${total}</span>元{else}购物车内已经有这个商品了，请进入购物车调整该商品的购买数量。{/if}</div>' +
		'<div class="big_23"><div class="big_24"><input type="button" class="big_four" onmouseover="this.className= \'big_four_b\'" onmouseout="this.className= \'big_four\'"  name="Submit2" value="查看购物车" onclick="window.location.href=\''+context+'/good/viewCart.html\'"/></div>' +
		'<div class="big_24"><input id="goon" type="button" class="big_four" onmouseover="this.className= \'big_four_b\'" onmouseout="this.className= \'big_four\'"  name="Submit2" value="继续选商品" /></div></div>';
$(function() {
	$("img[id^=coll], :image[id^=coll], :input[id^=coll]").bind("click", function() {
		var current = $(this);

		$.login({
					complete : function(result) {
						if (result != null && result.IsAuthenticated != null && result.IsAuthenticated) {
							var productId = parseInt($(current).attr("id")
									.replace("coll", ""));
							if (productId > 0) {
								$.getJSON(goodsFavorite, {
											'gid' : productId,
											"t":new Date()
										}, function(result) {
											if (result.existFavorite != null) {
												$("#Collect_Tip")
														.html(tip.process(result));
												showTip2("#Collect_Tip");
											}
										});
							}
						}
					}
				});
	});
	$("img[id^=cart],:input[id^=cart],a[id^=cart]").bind("click", function() {
		var current = $(this);
		var productId = $(current).attr("id").replace("cart", "");
	    var count = $('#count').val();
	    if(count) {
	    	if($('#qtip').is(':visible')){
	    		return;
	    	}
	    }
		var goodsType = $(current).attr("gtype");
		var ojbClass = $(current).attr("class");
		var quantity = $('#quantity').html();
		if(goodsType == 4 || ojbClass == 'butt_four_b') {
			count = 1;
		}
		if($(current).attr("quantity")) {
			quantity = $(current).attr("quantity");
			count = $(current).attr("count");;
		}
		quantity = parseInt(quantity);
        if(count > quantity) {
        	alert('该商品库存不足，请稍后再尝试购买');
        	return;
        }
		$.get(add_cart,{'t':new Date(),'gid':productId,'count':count,'goodsType':goodsType},function(res){
			if(res.indexOf('limit') > -1){
				alert("本商品为限量购买商品，您的购买数量已经超过限量("+res.split('_')[1]+")");
			} else if(res.indexOf('base') > -1){
				alert("本商品起购数量为("+res.split('_')[1]+")");
			} else {
				var value = {'success':res,'count':res.split('_')[1],'total':cc(res.split('_')[2])};
				$("#Collect_Tip2").html(cartTip.process(value));
				showTip3("#Collect_Tip2");
			}
		});
	});
});
