$(document).ready(function(){
    if(!isSearchGiftsLoaded){
        BindGift('ProductAdd_Step2.aspx','keyword='+escape($('#txtGiftKeyword').val()));
    }
    $('#ShowGift').popup({width:'80%',height:'500',popupHead:'GiftHead',btnClose:'CloseGift'});
    isSearchGiftsLoaded = true;
    
    $('#btnAddPresent').click(function(){
        if($('[@name=rdPresentType][@checked]').val() == '2'){
            BindGift('ProductAdd_Step2.aspx','keyword='+escape($('#txtGiftKeyword').val()));
            $('#ShowGift').popup({width:'80%',height:'500',popupHead:'GiftHead',btnClose:'CloseGift'});
        };
    });
    
    $('#chkAll_gift').click(function(){$('[@name=chkGiftItem]').attr('checked',$(this).attr('checked'));});
	setInterval(function(){$('#search_result_content_gift').css('height','370px');},1000);
	
	$('#btnAddGifts').click(function(){
        var productIDs = '';
        var selectedPIDs = '';
        var objSelectedPIDs = $('#PreGID_curr');//记录当前页面中已选择的商品的控件
        var objResultContainer = $('#presentcontent_gifts');//显示选择结果的控件
        var objHideName = 'pregid';
        if(objSelectedPIDs.val() != ''){
            selectedPIDs = ','+objSelectedPIDs.val()+',';
        };
        $('input[@name=chkGiftItem][@checked]').each(function(){
            var currobj = $(this);
            if(selectedPIDs.indexOf(',' + currobj.val() + ',') == -1){
                var productname = currobj.parent().siblings('#giftName').text();
                var row = $('<tr><td class="delete"><img src="../images/x-gray.gif" alt="删除" /><input type="hidden" name="' + objHideName + '" value=' + currobj.val() + '></td><td>' + productname + '</td><td><input type="text" name="preNum_gift" value="1" /></td></tr>');
                row.find('.delete').css('cursor','pointer').click(function(){
                    row.remove();
                    var ids = '';
                    $('input[@name=' + objHideName + ']').each(function(){
                        ids += $(this).val() + ',';
                    });
                    if(ids != ''){ids = ids.substring(0,ids.length-1);};
                    objSelectedPIDs.val(ids);
                }).find('img').mouseover(function(){
                    $(this).attr('src','../images/x-red.gif');
                }).mouseout(function(){
                    $(this).attr('src','../images/x-gray.gif');
                });
                row.find('[@name=preNum_gift]').blur(function(){
                    CheckNum($(this));
                });
                objResultContainer.append(row);
                productIDs += currobj.val() + ',';
            };
        });//end each
        if(productIDs != ""){
            productIDs = productIDs.substring(0,productIDs.length-1);
            objSelectedPIDs.val(objSelectedPIDs.val() + (objSelectedPIDs.val() != '' ? ',' : '') + productIDs);//隐藏控件记录已选择的商品ID(逗号分隔1,2,3)
        };
        $('#CloseGift').click();
    });//end click
    
	
})

function BindGift(weburl,urldata){
    $('#template').hide();//将第一行模板隐藏
    $.ajax({
        type:"get",
        dataType:"json",
        url:weburl+'?NoCopyRight=1&type=ajax&Action=SearchGiftAjax',
        data:urldata,
        cache:false,
        success:function(msg){
            $('input[@name=chkAll_gift]').attr('checked','');
            $('tr:gt(0)','#datas_gift').remove();
            var data = msg.Gift;
            $.each(data,function(i,n){
                var row = $('#template_gift').clone();
                row.show().mouseover(function(){$(this).addClass('on')}).mouseout(function(){$(this).removeClass('on')}); //将复制的行显示出来并添加样式
                row.find('#checkbox_gift').html('<input type="checkbox" name="chkGiftItem" value="' + n.GiftID + '" />');//checkbox
                row.find('#giftName').html(n.GiftName); //商品名称
			    row.attr("id","giftready_" + n.GiftID);//改变绑定好数据的行的id
			    row.appendTo("#datas_gift");//添加到模板的容器中
            });
        },
        error:function(XMLHttpRequest,textStatus){if(textStatus=='parsererror'){alert('身份已过期,请重新登陆!')}else{alert('error')};}
    });
}