
var PageSize = 50;//每页计数
var CurrentPage = 1; //当前页
var searchType = '0';
var status = 0;

//列表查询条件
var search_urldata = 'type=ajax&Action=GetMsgList&SearchType=' + searchType + '&PageSize='+PageSize+'&Page='+CurrentPage;

function setList(){
	var pageWidth=$('#popupContact').width()-20;
	$(".sub_container_content").css("width",pageWidth);
	$(".sub_container_listhead").css("width",pageWidth);
	$(".llist").css("width",pageWidth);
	var c1_width = pageWidth*0.05+"px";
	var c2_width = pageWidth*0.25+"px";
	var c3_width = pageWidth*0.25+"px";
	var c4_width = pageWidth*0.15+"px";
	var c5_width = pageWidth*0.15+"px";
	var c6_width = pageWidth*0.15+"px";
	$('.c1').css("width",c1_width);
	$('.c2').css("width",c2_width);
	$('.c3').css("width",c3_width);
	$('.c4').css("width",c4_width);
	$('.c5').css("width",c5_width);	
	$('.c6').css("width",c6_width);	
}

$(document).ready(function(){
    //loading
    $('#loadingContainer').ajaxStart(function(){$(this).show();}).ajaxStop(function(){$(this).hide();});	
		
	if($.browser.mozilla){setInterval(setList,100);}
	setInterval(function(){
        $('div.sub_container_page').css('top',$(window.parent.frames.right).height()-$('div.sub_container_page').height()-2); //设置底部分页区域的位置
        $('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').height()-10); //设置内容区域的高度
    },100);
    
    $('input[@name=chkAll]').click(function(){$('input[@name=chkItem]').attr('checked',$(this).attr('checked'));});//全选
    
    $('#btnDelete').click(function(){
        var ids = '';
        $('input[@name=chkItem][@checked]').each(function(){ids += $(this).val() + ',';});
        if(ids != ''){ids = ids.substring(0,ids.length-1);Delete(searchType,ids);};
    });    
    
    searchType = getQueryStringRegExp('ListType');
    status = getQueryStringRegExp('Status');
    search_urldata = 'type=ajax&Action=GetMsgList&SearchType=' + searchType + '&Status=' + status + '&PageSize='+PageSize+'&Page='+CurrentPage;
    BindList('MsgList.aspx',search_urldata);
	if(searchType == '1'){$('.c4').hide();$('.c3').show();}else{$('.c4').show();$('.c3').hide();}
});    

//绑定数据
function BindList(weburl,urldata){
    $('#template').hide();//将第一行模板隐藏
    $.ajax({
        type:"get",
        dataType:"json",
        url:weburl+'?NoCopyRight=1',
        data:urldata,
        cache:false,
        success:function(msg){
//            document.write(msg);
            $('input[@name=chkAll]').attr('checked','');
            $('tr:gt(0)','#datas').remove();
            var data = msg.Message;
            $.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.MessageID + '" />');//checkbox
                row.find('#Title').html(n.Title)
                    .addClass('hand')
                    .attr('title','点击查看内容')
                    .click(function(){
                        $('#frmdetail').attr('src','MsgDetail.aspx?MID='+n.MessageID);
                        $('#popupContact').popup({width:'80%',height:'500',popupHead:'popupHead',btnClose:'popupClose'});
                    });
                row.find('#Sender').html(n.FromMember);//发送者
                row.find('#Receiver').html(n.SendType == 0 ? '所有会员' : n.ToMember);
                row.find('#SendTime').html(n.AddTime);
                row.find('#Delete').html('<span class="hand">删除</span>').find('span').click(function(){Delete(searchType,n.MessageID);});

				row.attr("id","ready_" + n.ProductID);//改变绑定好数据的行的id
				row.appendTo("#datas");//添加到模板的容器中
            });
           
            //分页
            $('.sub_container_page').show();
            var pagerStyle = "flickr";//jPagerBar样式
            ShowPageBar("pageDataList1_Pager",//[containerId]
               "MsgList.aspx",//[url]
               {style:pagerStyle,mark:"pageDataList1Mark",
               totalCount:msg.totalCount,showPageNumber:3,pageCount:PageSize,currentPageIndex:CurrentPage,noRecordTip:" 没有记录",preWord:"上一页",nextWord:"下一页",
               onclick:"CurrentPage={pageindex};BindList('MsgList.aspx','type=ajax&Action=GetMsgList&SearchType=" + searchType + '&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(type,mid){
    $.ajax({
        url:'MsgList.aspx?type=ajax&Action=DeleteMessage&SearchType=' + type + '&IDs=' + mid,
        cache:false,
        success:function(data){
            if(!checkLoginStatus(data)){return false;}
            BindList('MsgList.aspx',search_urldata);
        },
        error:function(){alert('error');}
    });//end ajax
};

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

//获取URL中参数
function getQueryStringRegExp(name){    
	var reg = new RegExp("(^|\\?|&)"+ name +"=([^&]*)(\\s|&|$)", "i");
	if (reg.test(location.href))
		return unescape(RegExp.$2.replace(/\+/g, " "));
		return "";
};
