
//专题添加
var PageSize = 30;//每页计数
var CurrentPage = 1; //当前页
var keyword = '';
var catID = '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()-8); //设置内容区域的高度
    },1000);
    
    $('input[@name=chkAll]').click(function(){$('input[@name=chkItem]').attr('checked',$(this).attr('checked'));});//全选
    
    BindList();
    
    $('#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(){
    $('#template').hide();//将第一行模板隐藏
    $.ajax({
        type:"get",
        dataType:"json",
        url:'VideoCat.aspx?NoCopyRight=1&type=ajax&Action=GetList',
        cache:false,
        success:function(msg){
            $('input[@name=chkAll]').attr('checked','');
            $('tr:gt(0)','#datas').remove();
            var data = msg.VideoCat;
            $.each(data,function(i,n){
                var row = $('#template').clone();
                row.show().mouseover(function(){$(this).addClass('on')}).mouseout(function(){$(this).removeClass('on')}); //将复制的行显示出来并添加样式
                row.find('#ID').html(n.VideoCatID); //标题
                row.find('#CategoryName')
                    .html('<img src="../images/empty.gif" width="' + 10*Number(n.Depth) + '" height="1" align="absmiddle">' + (n.Child > 0 ? '<img src="../images/folder_close.gif" class="folder hand" /> ' : '<img src="../images/empty.gif" width="9" height="1">') + n.Name)
                    .find('img')
                    .click(function(){
                        var objImg = $(this);
                        if(row.attr('v') == '0'){
                            row.attr('v','1').nextAll('[@pno=' + n.CategoryNo + ']').show();
                            objImg.attr('src','../images/folder_open.gif');
                        }else{
                            row.attr('v','0').nextAll('[@catno^=' + n.CategoryNo + ']').attr('v','0').hide();
                            objImg.attr('src','../images/folder_close.gif');
                        };
                    });
                row.find('#Sort').text(n.Sort);
                row.find('#Status').html(n.Status == 1 ? '已发布' : '未发布')
                row.find('#Modify')
                    .html('<span class="hand">修改</span>')
                    .find('span')
                    .click(function(){
                        ShowModify(n.VideoCatID);
                    });//修改
                row.find('#Delete').html('<span style="cursor:pointer">删除</span>').find('span').click(function(){
                    if(confirm('确定要删除吗?')){
                        if(Delete(n.VideoCatID) == 1){
                            if($('tr[@catno=' + row.attr('pno') + ']').length != 0){
                                row.prev().find('img.folder').remove();
                            };
                            row.remove();
                        };
                    };
                });//删除
				row.attr("id","ready_" + n.VideoCatID).attr('catno',n.CategoryNo).attr('pno',n.ParentNo).attr('v','0');//改变绑定好数据的行的id
				row.appendTo("#datas");//添加到模板的容器中
				if(n.Depth > 0){row.hide()};
            });
           
        },//end success
        error:function(XMLHttpRequest,textStatus){if(textStatus=='parsererror'){alert('身份已过期,请重新登陆!')}else{alert(textStatus)};}
    });//end ajax
}//end funciton

function Delete(ids){
    var result = 0;
    $.ajax({
        url:'VideoCat.aspx?NoCopyRight=1&type=ajax&Action=Delete&IDs='+ids,
        cache:false,
        async:false,
        success:function(data){
            if(!checkLoginStatus(data)){return false;};
            switch(data){
                case '1':
                    alert('已删除');
                    break;
                case '2':
                    alert('删除失败,该分类下还有子分类');
                    break;
                case '3':
                    alert('删除失败,该分类下还有视频');
                    break;
            };
            result = data;
        },
        error:function(){alert('error:delete');}
    });
    return result;
};

//点击修改,显示修改的页面
function ShowModify(id){
    $('#frmModify').attr('src','VideoCatAdd.aspx?CatID=' + id +'&ran='+Math.random()).css('height',$(window).height()).show().siblings().hide();
}

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