﻿

$(document).ready(function(){
    //载入根目录
    downNode($("#treeview")[0]);
    //初始化树
    settree();
    $("a").attr("hot","0");
    $("a").live("click",function(){
        $("a[hot='1']").attr("hot", "0").removeClass("a3 a2").addClass("a1");
        $(this).attr("hot","1").removeClass("a1 a2").addClass("a3");
        if(this.parentNode.className=="cl"){
            $(this.parentNode.firstChild.firstChild).attr("src","http://www.hphu.com/tree/skins/ofolder.gif");
            var tNode=this.parentNode.lastChild;
            if(tNode.nodeName=="UL"){			
                $(tNode).show();
            }else{
                downNode(tNode.parentNode);
            }		
        }
    });
    $(".icon img").live("click", function(){
        if(/skins\/folder\.gif/.test($(this).attr("src"))){
            $(this).attr("src","http://www.hphu.com/tree/skins/ofolder.gif");
            var tNode=this.parentNode.parentNode.lastChild;
            if(tNode.nodeName=="UL"){			
                $(tNode).show();
            }else{
                downNode(tNode.parentNode);
            }
        }else{
            var pClassName=this.parentNode.parentNode.className;
            if(pClassName=="cl"){
                var tNode=this.parentNode.parentNode.lastChild;
                $(tNode).hide();
                $(this).attr("src","http://www.hphu.com/tree/skins/folder.gif");
            }else{
                if(pClassName=="al"){
                    var tNode=this.parentNode.parentNode.lastChild;
                    $(tNode).click();		
                }			
            }
        }
    });
    setTimeout("sync()",400);
}); 



function sync(){
    if ($.cookie('sync')!=null){
        var ncode=strtoa($.cookie('syncid'));
	for(var i=0;i<ncode.length;i++){
		var sNode=$("#c"+ncode[i]);
                if(sNode[0].lastChild.nodeName=="UL"){
			$(sNode[0].lastChild).show();
		}else{
			downNode(sNode);
		}
		$(sNode[0].firstChild.firstChild).attr("src","http://www.hphu.com/tree/skins/ofolder.gif");
	}
	var acode=$.cookie('synaid');
	$("a[hot='1']").attr("a", "0").removeClass("a3").addClass("a1"); 
	if(acode==null){
		$(sNode[0].childNodes[1]).attr("hot","1").removeClass("a1 a2").addClass("a3");
	}else{
		$($("#"+acode)[0].childNodes[1]).attr("hot","1").removeClass("a1 a2").addClass("a3");
	}	
        $.cookie('sync',null, {path: "/"});
        $.cookie('syncid',null, {path: "/"});
        $.cookie('synaid',null, {path: "/"});
    }
    setTimeout("sync()",1000);
}

function settree(){
	$("a").addClass("a1").attr("target","rightwin").attr("title",function(){return this.innerHTML});
	$("a").hover(
		function(){
			this.className="a2";
		},
		function(){
			if($(this).attr("hot")=="1"){
				this.className="a3";
			}else{
				this.className="a1";
			}
		}
	); 
	$(".cl").each(function(i){
		if(this.firstChild.nodeName=="A")$(this).prepend("<span class=\"icon\"><img src=\"http://www.hphu.com/tree/skins/folder.gif\" \/><\/span>"); 
 	}); 
	$(".clo").each(function(i){
		if(this.firstChild.nodeName=="A")$(this).prepend("<span class=\"icon\"><img src=\"http://www.hphu.com/tree/skins/ofolder.gif\" \/><\/span>"); 
 	}); 
	$(".al").each(function(i){
		if(this.firstChild.nodeName=="A")$(this).prepend("<span class=\"icon\"><img src=\"http://www.hphu.com/tree/skins/article.gif\" \/><\/span>"); 
 	}); 
}

function downNode(pNode){
	var tmpNode=$(pNode);
	tmpNode.append("<ul><li class=\"el\"><span class=\"icon\"><img src=\"http://www.hphu.com/tree/skins/Unavailable.gif\" \/><\/span><span class=\"a0\">载入中…<\/span><\/li><\/ul>");
	$.ajax({
		url: "article/data.asp?id=" + tmpNode.attr("id"),cache: false,async: false,success: function(html){
			$(tmpNode[0].lastChild).replaceWith(html);
			settree();
  		},error: function(oajax,errinfo){
			$(tmpNode[0].lastChild).replaceWith("<ul id=\"ajaxload\"><li class=\"el\"><span class=\"icon\"><img src=\"http://www.hphu.com/tree/skins/Unavailable.gif\" \/><\/span><span class=\"a0\">出错"+oajax.status+"<\/span><\/li><\/ul>");
  		}
	}); 
}

function strtoa(code){var codelen=code.length/4;var codearray=[];for(var i=0;i<codelen;i++){codearray[i]=code.substring(0,(i+1)*4);}return codearray;}
