/*
'=====================================================================
' Module Name:
' File name: gift.js
' Author: william.lu
' Project : saywo_gift
' Copyright (c) all rights reserved by juuyou.com Co., LTD.
' Create on 2009/12/17
' Version: 1.0
'
'=====================================================================
'*********************************************************************
' Usage: Gift页JS函数集
'
'*********************************************************************
*/

/**  定义juuyou.gift命名空间  **/
juuyou.gift = {};

/**  礼品列表排序  **/
juuyou.gift.changeGiftListOrder = function(cid, p){
	var strOrderVal = juuyou.common.getSelectVal("j_sel_order_by");
	window.location = "http://gift.saywo.net/gift/giftList.php?cid="+cid+"&p="+p+"&o="+strOrderVal;
}

/**  礼品搜索值验证  **/
juuyou.gift.checkSearchVal = function(){
	if($.trim($("#txt_search_val").val()) == ""){
		$("#txt_search_val").focus();
		$("#txt_search_val").val("");
		return false;
	}
	$("#frm_search").submit();
}


//初始化喜欢动作
$(document).ready(function(){
	$(".j_like_li").live("click", function(){	
		$.ajax({
	 		type:"POST",
			url:"../json/gift/giftActiveLike.php",
			async:true,
			data: {
				aid: strActiveId,
				act: "like"
			},
			success:function(data){
				if($.trim(data) == juuyou.common.json.failure){
				}else{
					$(".j_like_li").hide();
					$(".j_likes").replaceWith(data);
				}
			}
		});
		return false;
	});
});

//初始化gift saying删除
$(document).ready(function(){
	$(".j_gift_saying_del").live("click", function(){	
		var objDl = $(this).parents("dl");
		var strSID = objDl.attr("sid");
		
		$.ajax({
	 		type:"POST",
			url:"../json/gift/giftSayingDelete.php",
			async:true,
			data: {
				sid: strSID,
				act: "delete"
			},
			success:function(data){
				if($.trim(data) == juuyou.common.json.failure){
				}else{
					objDl.hide();
					intSayingCount = intSayingCount - 1;
					$("#j_comment_count_span").html(intSayingCount);
				}
			}
		});
		return false;
	});
});

// gift saying提交
juuyou.gift.checkGiftComment = function(){
	var strComment = $.trim($("#frm_gift_comment #txt_gift_comment").val());
	if(strComment == ""){
		$("#frm_gift_comment #txt_gift_comment").focus();
		return false;
	}
	
	if(strComment.length > 300){
		alert("字符数最大不得超过300");
		return false;
	}

	$.ajax({
	 	type:"POST",
		url:"../json/gift/giftSayingAdd.php",
		async:false,
		data: {
			aid: strActiveId,
			val: strComment
		},
		success:function(data){
			if($.trim(data) == juuyou.common.json.failure){
			}else{
				$("#frm_gift_comment #txt_gift_comment").val("");
				$("#j_comment_list").prepend(data);
				intSayingCount = 1 + intSayingCount;
				$("#j_comment_count_span").html(intSayingCount);
			}
		}
	});
}


//初始化礼品图切换显示
$(document).ready(function(){
	$(".j_gift_s_img_li").live("mouseover", function(){	
		var strMUrl = $(this).attr("m");
		var strLUrl = $(this).attr("l");
		
		$("#j_gift_m_img").attr("src", strMUrl);
		$("#j_gift_l_img").attr("src", strLUrl);
		
	});
	
	$("#j_gift_m_img_div").live("click", function(){	
		$("#j_gift_l_img_div").fadeIn(300);
	});
	
	$("#j_gift_l_img_div").live("click", function(){	
		$("#j_gift_l_img_div").fadeOut(300);
	});
});

//关闭弹出层
juuyou.gift.followDivHide = function(){
	$("#j_drop_div").hide();
	$("#j_follow_drift_div").hide();
	$("#j_set_div").html("");
}
juuyou.gift.followDivHide1 = function(){
	$("#j_drop_div").hide();
	$("#j_follow_drift_div_1").hide();
}

//申请购买礼品
juuyou.gift.applyGiftConfirm = function(){
	$("#j_set_div").html($("#j_apply_ini_div").html());
	$("#j_drop_div").show();
	$("#j_follow_drift_div").show();
}
juuyou.gift.applyGift = function(){
	var intBuyCnt = $.trim($("#txt_buy_cnt").val());
	
	if(!juuyou.common.IsNumeric(intBuyCnt) || intBuyCnt <= 0 || intBuyCnt > intGiftCnt){
		$("#txt_buy_cnt").val("1");
		alert("请设定正确的购买件数");
		juuyou.gift.followDivHide();
		return;
	}
	
	$.ajax({
	 	type:"POST",
		url:"../json/gift/giftApplyAdd.php",
		async:false,
		data: {
			gid: strGiftId,
			bcnt: intBuyCnt
		},
		success:function(data){
			if($.trim(data) == juuyou.common.json.failure){
			}else{
				$("#j_check_apply_btn").hide();
			}
		}
	});
	juuyou.gift.followDivHide();
}

//礼品许愿
$(document).ready(function(){
	$("#j_hope_li").live("click", function(){	
		$.ajax({
		 	type:"POST",
			url:"../json/gift/giftHopeAdd.php",
			async:false,
			data: {
				gid: strGiftId
			},
			success:function(data){
				if($.trim(data) == juuyou.common.json.failure){
				}else{
					$("#j_set_div").html($("#j_hope_ini_div").html());
					$("#j_drop_div").show();
					$("#j_follow_drift_div").show();
					$("#j_hope_li").hide();
				}
			}
		});
	});
});


//礼品购买
juuyou.gift.buyGiftOwn = function(){
	var intBuyCnt = $("#txt_buy_cnt").val();
	if(!juuyou.common.IsNumeric(intBuyCnt)){
		$("#txt_buy_cnt").val("1");
		$("#txt_buy_cnt").focus();
		return;
	}
	if(intBuyCnt <= 0 || intBuyCnt > intGiftCnt){
		$("#j_set_div").html($("#j_buy_count_error_ini_div").html());
		$("#j_drop_div").show();
		$("#j_follow_drift_div").show();
		return;
	}
	
	$.ajax({
	 	type:"GET",
		url:"../json/gift/getDeliveryAddress.php",
		async:false,
		data: {},
		success:function(data){
			if($.trim(data) == juuyou.common.json.failure){
			}else{
				$("#j_set_div").html($.trim(data));
				$("#j_drop_div").show();
				$("#j_follow_drift_div").show();
			}
		}
	});
}


//验证送货地址添加
juuyou.gift.checkDeliveryAddressAdd = function(){
	var strAddressId = juuyou.common.getRadioBtnValue("rd_address");
	var intBuyCnt = $("#txt_buy_cnt").val();
	var strName = $("#frm_delivery_address #txt_name").val();
	var strAddress = $("#frm_delivery_address #txt_address").val();
	if(!strAddressId){
		alert("请选择送货地址");
		return;
	}
	
	if(strAddressId == "add" && (strName == "" || strAddress == "")){
		alert("请填写完整的收货信息");
		return;
	}
	
	
	$("#frm_delivery_address").find("#hid_gift_id").val(strGiftId);
	$("#frm_delivery_address").find("#hid_buy_cnt").val(intBuyCnt);
		
	$("#frm_delivery_address").submit();
}

//删除送货地址

$(document).ready(function(){
	$(".j_del_delivery_address_a").live("click", function(){	
		var strDeliveryAddressId = $(this).attr("did");
		var objP = $(this).parent("p");
		
		if(!strDeliveryAddressId){
			alert("非法提交");
			return;
		}
		
		$.ajax({
		 	type:"GET",
			url:"../json/gift/delDeliveryAddress.php",
			async:false,
			data: {
				id: strDeliveryAddressId
			},
			success:function(data){
				if($.trim(data) == juuyou.common.json.failure){
					alert("操作失败");
				}else{
					objP.hide();
				}
			}
		});
	});
});


//赠送给好友
juuyou.gift.giveFriendGift = function(){
	var intBuyCnt = $("#txt_buy_cnt").val();
	if(!juuyou.common.IsNumeric(intBuyCnt)){
		$("#txt_buy_cnt").val("");
		$("#txt_buy_cnt").focus();
		return;
	}
	if(intBuyCnt <= 0 || intBuyCnt > intGiftCnt){
		$("#j_set_div").html($("#j_buy_count_error_ini_div").html());
		$("#j_drop_div").show();
		$("#j_follow_drift_div").show();
		return;
	}
	
	$("#j_drop_div").show();
	$("#j_follow_drift_div_1").show();
	
}
juuyou.gift.checkGiveGift = function(){
	$obj('txt_receiver_say').value = $.trim($obj('txt_receiver_say').value);
	var strVal = $obj('txt_receiver_say').value;
	
	$("#j_follow_drift_div_1 #txt_friend").val($.trim($("#j_follow_drift_div_1 #txt_friend").val()));
	var strFriend = $.trim($("#j_follow_drift_div_1 #txt_friend").val());
	intCnt = strVal.length;

	if(intCnt > 280){
		alert("字符数最大不得超过280");
		$obj('txt_receiver_say').focus();
		return;
	}

	if(!strFriend){
		alert("请输入好友昵称");
		$("#j_follow_drift_div_1 #txt_friend").focus();
		return;
	}
	
	
	var intBuyCnt = $("#txt_buy_cnt").val();
	$("#frm_give_gift").find("#hid_give_gift_id").val(strGiftId);
	$("#frm_give_gift").find("#hid_give_buy_cnt").val(intBuyCnt);

	$("#frm_give_gift").submit();

}














