
//专题添加
var PageSize = 30;//每页计数
var CurrentPage = 1; //当前页
var keyword = '';
var catID = '0';
var bid = '0';
var status = 2;

//列表查询条件
var search_urldata = 'type=ajax&Action=GetList&keyword=' + escape(keyword) + '&CatID=' + catID + '&Status=' + status + '&PageSize='+PageSize+'&Page='+CurrentPage;
$(document).ready(function(){
    //loading
    $('#loadingContainer').ajaxStart(function(){
	    $(this).show();
	    $('#loadingbackground').show().css({"opacity": "0.5"});
	}).ajaxStop(function(){
	    $(this).hide();
	});
	
    setInterval(function(){
        $('div.sub_container_content').css('height',$(window.parent.frames.right).height()-$('div.sub_container_title').height()-$('div.sub_container_operation').height()-$('div.sub_container_listhead').height()); //设置内容区域的高度
        $('div.sub_container_page').css('top',$(window.parent.frames.right).height()-$('div.sub_container_page').height()-2); //设置底部分页区域的位置
    },1000);
    
    $('input[@name=chkAll]').click(function(){$('input[@name=chkItem]').attr('checked',$(this).attr('checked'));});//全选
    
    BindList('ZhuanMai.aspx',search_urldata);
    
    $('#btnSearch').click(function(){
        if(($('#ddlBrand').val() != bid) || ($('#sel_status').val() != status)){CurrentPage = 1;};//如果类别或者状态发生变化,要将当前页码设置为1
        keyword = $('#txtKeyword').val();
        bid = $('#ddlBrand').val();
        status = $('#sel_status').val();
        search_urldata = 'type=ajax&Action=GetList&keyword=' + escape(keyword) + '&CatID=' + catID + '&bid=' + bid + '&Status=' + status + '&PageSize='+PageSize+'&Page='+CurrentPage
        BindList('ZhuanMai.aspx',search_urldata);
    });
    
    $('#btnDelete').click(function(){
        var IDs = '';
        $('input[@name=chkItem][@checked]').each(function(){
            IDs += $(this).val() + ',';
        });
        if(IDs != ''){
            IDs = IDs.substring(0,IDs.length-1);
            if(confirm('确定要删除选中的项吗?')){
                Delete(IDs);
            }
        }else{
            alert('请选择要删除的项');
        }
    });
    
});// end ready

//绑定数据
function BindList(weburl,urldata){
    $('#template').hide();//将第一行模板隐藏
    $.ajax({
        type:"get",
        dataType:"json",
        url:weburl+'?NoCopyRight=1',
        data:urldata,
        cache:false,
        success:function(msg){
            $('input[@name=chkAll]').attr('checked','');
            $('tr:gt(0)','#datas').remove();
            var data = msg.Activity;
            $.each(data,function(i,n){
                var row = $('#template').clone();
                row.show().mouseover(function(){$(this).addClass('on')}).mouseout(function(){$(this).removeClass('on')}); //将复制的行显示出来并添加样式
                row.find('#checkbox').html('<input type="checkbox" name="chkItem" value="' + n.ActivityID + '" />');//checkbox
                row.find('#Title').html(n.Title); //标题
                row.find('#ProductNo').text(n.ProductNo);   //编码
                var brandName = (n.BrandID != '1' ? $('#ddlBrand option[@value=' + n.BrandID + ']').text() : "");
                row.find('#BrandName').text(brandName);//商品类别
                row.find('#Url').html('<a href="'+baseUrl+n.ActivityID+'" target="_blank" >'+baseUrl+n.ActivityID+'</a>　<span style="cursor:pointer;">复制</span>')
                .find('span').click(function(){
                    window.clipboardData.clearData();
                    window.clipboardData.setData("Text", $(this).prev().text());
                });//链接地址
                row.find('#Status').html('<a href="ZhuanMai.aspx?NoCopyRight=1&type=ajax&Action=ChangeStatus&ID='+n.ActivityID + '&Status=' + n.Status + '"><img src="images/publish_' + n.Status + '.png" border="0"></a>')
                .find('a').click(function(){
                    var currObj = $(this);
                    $.ajax({
                        url:currObj.attr('href'),
                        cache:false,
                        success:function(data){
                            if(!checkLoginStatus(data)){return false;};
                            if(data > 0){
                                var oldlink = currObj.attr('href');
                                if(oldlink.indexOf('Status=0') != -1){
                                    currObj.attr('href',oldlink.replace('Status=0','Status=1'));
                                    currObj.find('img').attr('src','images/publish_1.png');
                                }else{
                                    currObj.attr('href',oldlink.replace('Status=1','Status=0'));
                                    currObj.find('img').attr('src','images/publish_0.png');
                                };
                            };
                        },
                        error:function(){alert('error');}
                    });
                    return false;
                });//状态
                row.find('#Modify').html('<a href="ZhuanMaiAdd.aspx?ID='+ n.ActivityID +'">修改</a>');//修改
                row.find('#Delete').html('<span style="cursor:pointer">删除</span>').find('span').click(function(){
                    if(confirm('确定要删除吗?')){
                        Delete(n.ActivityID);
                    };
                });//删除
				row.attr("id","ready_" + n.ActivityID);//改变绑定好数据的行的id
				row.appendTo("#datas");//添加到模板的容器中
            });
           
            //分页
            $('.sub_container_page').show();
            var pagerStyle = "flickr";//jPagerBar样式
            ShowPageBar("pageDataList1_Pager",//[containerId]
               "Activity.aspx",//[url]
               {style:pagerStyle,mark:"pageDataList1Mark",
               totalCount:msg.totalCount,showPageNumber:3,pageCount:PageSize,currentPageIndex:CurrentPage,noRecordTip:" 没有记录",preWord:"上一页",nextWord:"下一页",
               onclick:"CurrentPage={pageindex};BindList('ZhuanMai.aspx','type=ajax&Action=GetList&keyword=" + escape(keyword) + "&CatID=" + catID + '&bid=' + bid + "&Status=" + status + "&PageSize=" + PageSize + "&Page={pageindex}');return false;"}//[attr]
            );
            
        },//end success
        error:function(XMLHttpRequest,textStatus){if(textStatus=='parsererror'){alert('身份已过期,请重新登陆!')}else{alert(textStatus)};}
    });//end ajax
}//end funciton

function Delete(ids){
    $.ajax({
        url:'ZhuanMai.aspx?NoCopyRight=1&type=ajax&Action=Delete&IDs='+ids,
        cache:false,
        success:function(data){
            if(!checkLoginStatus(data)){return false;};
            if(data > 0){BindList('ZhuanMai.aspx',search_urldata);};
        },
        error:function(){alert('error');}
    });
};

function checkLoginStatus(data){
    var result = true;
    if(data.indexOf('<!DOCTYPE') != -1){
        result = false;
        alert('身份已过期,请重新登陆!');
    }
    return result;
};
