﻿// JScript 文件

$(function(){
    createRootChannel();
    $("body").click(function(){
        emptyChildChannelAll();
    });
})
function createRootChannel(){
   
   var linkTable = $('#NavLinkTable');
   
   var linkIndex = 0;
    $('#Nav1_ChannelLink a').each(function(i){
        var id = $(this).attr('id').split('#')[0];
        var parentID =$(this).attr('id').split('#')[1];
        if(parentID == '0'){
            $(this).clone().appendTo(linkTable.find("td:eq("+linkIndex+")")).hover( 
                function () {
                    var child = $(".ChannelLinkList1");
                        //debugger;
                    if(child.length > 0 && child.data("targetid") == this.id){
                        
                    }
                    else{
                        showChildChannel(id,this,1);
                    }
                    
                },
                function () {
                    
                    var y=event.clientY+ $(window).scrollTop();
                    if(y < parseInt($(this).offset().top) + 10){
                        emptyChildChannel(1);
                    }
                    
                }
            ); 
            //0 2 4 6 8 是栏目列， 奇数为分隔符列
            linkIndex = linkIndex + 2;
        }
    });

    
}

function showChildChannel(_parentID,target, classIndex){
    var l = $("#Nav1_ChannelLink a[id*='"+_parentID+"']").length;
    $("#test").html(l);
    if(l == 1){
        return;
    }
    
    var tempClass = "ChannelLinkList" + classIndex;
    var nextClass = "ChannelLinkList" + (classIndex+1);
    emptyChildChannel(classIndex);
    
    var linkList = $("<div class='"+tempClass+"'></div>").data("target",target).appendTo('body');
//    if(classIndex > 1){
//        linkList.append($("<img style='position:absolute;top:3px; left:0px;' src='Images/next.gif'></img>"));
//    }
    
    var counter = 0;
    
    $('#Nav1_ChannelLink a').each(function(i){
        var id = $(this).attr('id').split('#')[0];
        var parentID =$(this).attr('id').split('#')[1];
        if(parentID == _parentID){
            var span = $("<div></div>").hover(
                function(){
                    emptyChildChannel(classIndex+1);
                    var child = $("."+nextClass);
                    if(child.length > 0 && child.data("target") == this){
                        
                    }
                    else{
                        showChildChannel(id,this,classIndex+1);
                    }
                    
                }
            );
            span.append($(this).clone());
            linkList.append(span);
            counter ++;
        }
    });
    if(counter >0){
        linkList.show();
    }
    else{
        return;
    }
    
    var targetParent = $(target).parent();
    var position = targetParent.offset();
    if(classIndex == 1){
        if(targetParent.attr('class') != 'firsttdlink'){
            linkList.css({'left':(position.left - 20) +'px','top':(position.top+44)+'px'});
        }
        else{
            linkList.css({'left':position.left+'px','top':(position.top+44)+'px'});
        }
        
    }
    if(classIndex > 1){
        linkList.css({'left':(position.left+139)+'px','top':(position.top+5)+'px'});
    }
    
}


function emptyChildChannel(classIndex){
    
    /*if($('.'+tempClass).length > 0){
        $('.'+tempClass)[0].removeNode(true); 
    }*/
    for(var i = classIndex; i < classIndex+5; i++){
        var tempClass = "ChannelLinkList" + i;
        $('.'+tempClass).remove(); 
    }
    
}

function emptyChildChannelAll(){
    $('div:[class*="ChannelLinkList"]').remove(); 
}

function jugeXYInLinkList(Event){
    var x=Event.clientX+ $(window).scrollLeft();
    var y = Event.clientY+ $(window).scrollTop();
    var flag = false;
    for(var i = 1; i < 10; i++){
        var tempClass = "ChannelLinkList" + i;
        var linkList = $('.'+tempClass);
        if(linkList.length == 0){
            break;
        }
        var postion = linkList.offset();
        var h = linkList.height();
        var w = linkList.width();
        var extent = {minx: postion.left, miny: postion.top, maxx: postion.left+w, maxy: postion.top+h  };
        if(x > extent.minx && x < extent.maxx && y > extent.miny && y < extent.maxy){
            flag = true;
            break;
        } 
    }
    return flag;
}

/*
function addhoverToLinkDiv(){
    $('div:[class*="ChannelLinkList"]').find('div').each(function(i){

    $(this).hover(
        function(){
            $(this).addClass('HoverLink');
        },
        function(){
            $(this).removeClass('HoverLink');
        }
    );

    });

}
*/



