/**
 * Alltosun - goods.js 商品公用JS函数库
 * Copyright (c) 2009-2011 Alltosun.INC - http://www.alltosun.com
 * Date: 2011/05/11
 * @author ninghx@alltosun.com
 * @requires jQuery v1.4.4+
 * @requires jQuery-ui v1.8.7+
 */

// 收藏商品功能
$(function(){
	// 分享
	$(".shareBtn").live('click', function(){
		var shareObj = $(this);
		var resId = $(this).next().val();
		var resName = $(this).next().next().val();
		if (!resId) {
		  alert("请正确选择要操作的记录！");
		  return false;
		}
		if (!resName) {
		  alert("请正确选择要操作的资源类型！");
		  return false;
		}
		var html = '';
		html += '<div class="pbtm8" style="color:#cdcdcd;">你可以填写分享的理由</div>';
		html += '<textarea id="share_reason" style="width:320px; height:100px; position:relation;" /></textarea>';
		art.dialog({
		  title:'分享',
		  content:html,
		  yesFn:function(){
		    var shareReason = $("#share_reason").val();
		    
		    $.post(siteUrl+"/share/ajax", { 'res_name':resName, 'res_id':resId, 'reason':shareReason }, function(json){
		      if (json.info == 'ok') {
		        $("#share_reason").val('');
		        shareObj.find('.totalShare').text(parseInt(shareObj.find('.totalShare').text()) + 1);
		        art.dialog('恭喜您，分享成功！', function(){});
		      } else {
		        art.dialog(json.info, function(){});
		      }
		    }, 'json');
	      },
	      noFn:function(){}
		});
	});
	
	// 收藏
	$(".fav").click(function(){
		var favObj = $(this);
		var followObj = $(this).parent();
		var res_id = $(this).next().val();
		var res_name = $(this).next().next().val();
		if (!res_id) {
		  alert("请正确选择要操作的记录。");
		}
		if (!res_name) {
			res_name = "goods";
		}
		
		if (favObj.IsBubblePopupOpen() == true) {
			return false;
		}
		favObj.CreateBubblePopup({
			innerHtml: '加载中...',
			innerHtmlStyle: {color:'#333333', 'font-size':'14px', 'text-align':'center'},
			divStyle: {'margin-top':'-10px'},
			themeName: 	'orange',
			themePath: 	'static/js/development/bubblePopup/jquerybubblepopup-theme'
	    });
		
		favObj.ShowBubblePopup();
		favObj.FreezeBubblePopup();
		
		$.post("fav/ajax", {"res_id":res_id, "res_name":res_name}, function(json){
			if (json.info == "ok") {
				var innerHtml = "收藏成功";
				// 收藏总数加一
				var totalFavObj = $(".totalFav", favObj.parent());
				if ($(".totalFav", favObj.parent()).length) {
				  totalFavObj.html(parseInt(totalFavObj.html())+1);
				}
			} else {
				var innerHtml = json.info;
			}
			favObj.SetBubblePopupInnerHtml(innerHtml, false);
			favObj.FreezeBubblePopup();
			
			var timer = setTimeout(function(){
				favObj.RemoveBubblePopup();
			},5000);
			
			$("#"+favObj.GetBubblePopupID()).hover(function(){
				clearTimeout(timer);
			},function(){
				timer = setTimeout(function(){
					favObj.RemoveBubblePopup();
				},1000);
			});
		}, "json");
	});

	$(".addcart, .cart").click(function(){
		var cartObj = $(this);
		var res_id = cartObj.next().val();
		var res_name = cartObj.next().next().val();
		if (!res_id) {
		  alert("请正确选择商品。");
		}
		var buyNum = $("#buyNum").val();
		if (!buyNum) {
			buyNum = 1;
		}
		if (parseInt(buyNum) <= 0) {
		  alert("请正确填写商品数量。");
		  return false;
		}
		
		if (cartObj.IsBubblePopupOpen() == true) {
			return false;
		}
		
		cartObj.CreateBubblePopup({
			innerHtml: '加载中...',
			innerHtmlStyle: {color:'#333333', 'font-size':'14px', 'text-align':'center'},
			divStyle: {'margin-top':'-10px'},
			themeName: 	'orange',
			themePath: 	'static/js/development/bubblePopup/jquerybubblepopup-theme'
	    });
		
		cartObj.ShowBubblePopup();
		cartObj.FreezeBubblePopup();
		
		$.post("goods/cart/add", {"res_name":res_name, "res_id":res_id, 'num':buyNum}, function(json){
			if (json.info == "ok") {
				var innerHtml = "商品已成功加入购物车";
				// 更改头部购物车的数量
				if (json.is_new) {
					$("#headerCartTotalCount").html(parseInt($("#headerCartTotalCount").html())+1);
				}
			} else {
				var innerHtml = json.info;
			}
			cartObj.SetBubblePopupInnerHtml(innerHtml, false);
			cartObj.FreezeBubblePopup();
			
			var timer = setTimeout(function(){
				cartObj.RemoveBubblePopup();
			},5000);
			
			$("#"+cartObj.GetBubblePopupID()).hover(function(){
				clearTimeout(timer);
			},function(){
				timer = setTimeout(function(){
					cartObj.RemoveBubblePopup();
				},1000);
			});
		}, "json");
	});
	
	// 购物车的展示
	/*$(".site-orderservice").hover(function(){
	  // if (timeIntval > 3) {
  		if ($("#site-submenu").css("display") == "none") {
  			var obj = $(this);
  			if (!obj.data("cart_get_list")) {
  				$.post("goods/cart/get_list", function(json){
  					var submenuContent = '';
  					if (json.total_count) {
  						submenuContent += '<ol>';
  						$.each(json.list, function(k, v){
  							submenuContent += '<li class="clearfix">\
  					              <a href="'+v.res_name+'/'+v.res_id+'" class="left pd-avatar"><img width="50" height="50" src="'+v.cover+'" alt="'+v.title+'" /></a>\
  					              <div class="left submenu-bd">\
  					                <h6><a href="'+v.res_name+'/'+v.res_id+'">'+v.title+'</a></h6>\
  					                <div class="price-account"><b>￥'+v.price+'×'+v.num+'</b><span id="'+v.res_name+'_'+v.res_id+'_'+(accurateMultiply(parseFloat(v.price), parseInt(v.num)))+'" class="cursor sub-del">删除</span></div>\
  					              </div>\
  					            </li>';
  						})
  					    submenuContent += '</ol>';
  						submenuContent += '<div class="total-money">共<b id="cartTotalCount">'+json.total_count+'</b>项服务 金额总计：<b>￥<span id="cartTotalPrice">'+json.total_price+'</span></b></div>'; 
  						submenuContent += '<div class="site-goto"><a href="goods/cart.html">前往购物车结算</a></div>';
  					} else {
  						submenuContent += '<div class="txtcenter">您的购物车中暂无商品。</div>';
  					}
  					
  					$("#site-submenu").fadeIn(function(){
  						$("#site-submenu").html(submenuContent);
  					});
  					// 设置缓存
  					obj.data("cart_get_list", submenuContent);
  				}, 'json');
  			} else {
  				$("#site-submenu").fadeIn(function(){
  					// $("#site-submenu").html(obj.data("cart_get_list"));
  				});
  			}
  		} else {
  			$("#site-submenu").fadeOut(function(){
  				// $("#site-submenu").html('');
  			});
  		}
	  //}
	});*/
	
	// 购物车中物品的删除
	$("#site-submenu .sub-del").live('click', function(e){
		var obj = $(this);
		var resArr = obj.attr('id').split('_');
		$.post("goods/cart/delete", {'res_id':resArr[1], 'res_name':resArr[0]}, function(json){
			if (json.info == 'ok') {
				obj.closest("li").fadeOut(function(){
					if ($("#site-submenu li:visible").length == 0) {
						$("#site-submenu").html('<div class="txtcenter">您的购物车中暂无商品。</div>').show();
					} else {
						$("#cartTotalCount").html(parseInt($("#cartTotalCount").html())-1);
						$("#cartTotalPrice").html(accurateMinus(parseFloat($("#cartTotalPrice").html()), parseFloat(resArr[2])));
						$("#headerCartTotalCount").html(parseInt($("#headerCartTotalCount").html())-1);
					}
				});
				return false;
			} else {
				alert(json.info);
				return false;
			}
		}, 'json');
	});
});
