var curSelFrame,gFlag,dlgAry=[],zIndex=100;
fAddEvent(window,"load",function(){autoFit();});
fAddEvent(window,"resize",function(){autoFit();});
//获取页面对象
function $(objectId){
    if(document.getElementById&&document.getElementById(objectId)){
        return document.getElementById(objectId);
    }else if(document.all&&document.all(objectId)){
        return document.all(objectId);
    }else if(document.layers&&document.layers[objectId]){
        return document.layers[objectId];
    }else{
        return false;
    } 
} 
function autoFit() { 
    var cWidth = document.body.clientWidth;
    var cHeight = document.body.clientHeight;
    if(cWidth<300) cWidth=300;
    if(cHeight<300) cHeight=300;
//    document.getElementById("content").style.width = cWidth+"px";
//    document.getElementById("sub_content").style.height = cHeight-123+"px";
//    document.getElementById("left_main_nav").style.height = cHeight-123+"px";
//    document.getElementById("right_main_nav").style.height = cHeight-123+"px";
}
//mouse over
function over(Id){
    SetBorderColor(Id, "#CCCCCC", "#000000");
}
//mouse out
function out(Id){
    SetBorderColor(Id, "#FFFFFF", "#FFFFFF");
}
//设置边框颜色
function SetBorderColor(Id, borderColorLight, borderColorDark){
    $(Id).style.borderLeftColor = borderColorLight;
    $(Id).style.borderTopColor = borderColorLight;
    $(Id).style.borderRightColor = borderColorDark;
    $(Id).style.borderBottomColor = borderColorDark;
}
function fAddEvent(oTarget, sType, fHandler, bRemove) {
    if (!oTarget){return false;}
    if (oTarget.addEventListener) {
        bRemove ? oTarget.removeEventListener(sType, fHandler, false) : oTarget.addEventListener(sType, fHandler, false);
    }else if (oTarget.attachEvent) {
        bRemove ? oTarget.detachEvent("on" + sType, fHandler) : oTarget.attachEvent("on" + sType, fHandler);
    }else {
        oTarget["on" +sType] = bRemove ? null : fHandler;
    }
}
//插入代码
function InsertHTML(el, where, html) {
	if (!el) return;
	if (el.insertAdjacentHTML){
            el.insertAdjacentHTML(where, html);
	}else{
            var range = el.ownerDocument.createRange();
            var isBefore = where.indexOf("before") == 0;
            var isBegin = where.indexOf("Begin") != -1;
            if (isBefore == isBegin){
                range[isBefore ? "setStartBefore" : "setStartAfter"](el);
                el.parentNode.insertBefore(range.createContextualFragment(html), isBegin ? el : el.nextSibling);
            }else{
                var obj = el[isBefore ? "lastChild" : "firstChild"];
                if (obj){
                    range[isBefore ? "setStartAfter" : "setStartBefore"](obj);
                    el[isBefore ? "appendChild" : "insertBefore"](range.createContextualFragment(html), obj);
                }else{
                    el.innerHTML = html;
                }
            }
	}
}
//显示、隐藏菜单

function switchBar(flag){
    if (flag){
        document.getElementById("left").style.display="none";
        document.getElementById("switch").innerHTML = "<img src=\"../images/switch_right.gif\" onClick=\"switchBar(false)\" style=\"cursor:pointer;\"  title=\"显示\">";
    }else{
        document.getElementById("left").style.display="";
        document.getElementById("switch").innerHTML = "<img src=\"../images/switch_left.gif\" onClick=\"switchBar(true)\" style=\"cursor:pointer;\" title=\"隐藏\">";
    }
}
//显示对话框

function showDialog(title,url,width,height){
    var i = dlgAry.length;
    dlgAry.push(i);
    var maskid= "dlg_mask"+i;
    //var dlgid = "dlg_page"+i;
    createMask(maskid);
    createDialog(i,title,url,width,height);
}
//关闭对话框


function closeDialog(){
    var i = dlgAry.length-1;
    var maskid= "dlg_mask"+i;
    var dlgid = "dlg_page"+i;
    $(maskid).parentNode.removeChild($(maskid));
    $(dlgid).parentNode.removeChild($(dlgid));
    dlgAry.baoremove(i);
    zIndex = zIndex-2;
}
//创建遮罩层

function createMask(id){
    InsertHTML(document.body, "beforeEnd",
      ['<div id="'+id+'" style="position:absolute;left:0px;top:0px;width:100%;height:120%;filter:Alpha(Opacity=30);opacity:0.3;background-color:#EFEFEF;z-index:'+(zIndex++)+';">',
       '</div>'].join(""));	
}
//创建对话框

function createDialog(id,t,u,w,h){
    var bW = document.body.clientWidth; 
    var bH = document.body.clientHeight;
    var sW = w?w:bW-20;
    var sH = h?h:bH-50;
    var clsDrak = "../images/dlg_close_drak.gif";
    var clsBlue = "../images/dlg_close_blue.gif";
    InsertHTML(document.body, "beforeEnd", 
       ['<div id="dlg_page'+id+'" style="position:absolute;z-index:'+(zIndex++)+';top:'+(bH-sH)/4+'px;left:'+(bW-sW)/2+'px;width:'+sW+'px; height:'+sH+'px;">',
        '<table id="dlgTitle" cellspacing="0" cellpadding="0">',
        '<tr>',
        '<td class="dlg_left">&nbsp;</td>',
        '<td class="dlg_middle">',
      
 
        '<td class="dlg_right">&nbsp;</td>',
        '</tr>',
        '</table>',
        '<table id="dlgContent" cellspacing="0" cellpadding="0">',
        '<tr>',
        '<td class="dlg_border_left"></td>',
        '<td class="dlg_content">',
        '<div style="overflow:auto; width:'+(sW-4)+'px;height:'+sH+'px;">',
        '<iframe id="dlg_frame'+id+'" width="100%" height="100%" frameBorder=0 src="'+(u?u:'about:blank')+'"></iframe>',
        '</div></td>',
        '<td class="dlg_border_right"></td>',
        '</tr>', 
        '<tr>',
        '<td class="dlg_corner_left"></td>',
        '<td class="dlg_border_bottom"></td>',
        '<td class="dlg_corner_right"></td>',
        '</tr>',
        '</table>',
        '</div>'].join(""));
}
function MenuBar(instanceName,datas){
    this.instanceName = instanceName;
    this.topId = "nav";
    this.leftId = "left_main_nav";
    this.rightId = "right_main_nav";
    this.frameId = "mainFrame";
    this.selTopId = "";
    this.selLeftId = "";
    this.cols = datas;
    MenuBar.prototype.init = function(){
        this.showNav();
    };
    MenuBar.prototype.showNav = function(){
        var strNav = new Array;
        strNav.push("<ul>");
        for(var i=0;i<this.cols.length;i++){
            if(i==0){
                strNav.push("<li id=\"top_nav_"+i+"\" onclick=\""+this.instanceName+".clickNav("+i+",id)\" class=\"bg_image_onclick\">"+this.cols[i]["title"]+"</li>");
                this.selTopId = "top_nav_"+i;
                if(this.frameId!="" && this.cols[i]["url"]!="") $(this.frameId).src=this.cols[i]["url"];
                else $(this.frameId).src="";
            }else{
                strNav.push("<li id=\"top_nav_"+i+"\" onclick=\""+this.instanceName+".clickNav("+i+",id)\" class=\"bg_image\">"+this.cols[i]["title"]+"</li>");
            }
        }
        strNav.push("</ul>");
        $(this.topId).innerHTML = strNav.join("");
        this.showMenu(0);
    };
    MenuBar.prototype.clickNav = function(i,Id){
        if(Id == this.selTopId) return;
        if(this.selTopId != ""){
            $(this.selTopId).className="bg_image";
        }
        if($(Id).className == "bg_image"){
            $(Id).className="bg_image_onclick";
            this.selTopId = Id;
            if(this.frameId!="" && this.cols[i]["url"]!="") $(this.frameId).src=this.cols[i]["url"];
            else $(this.frameId).src="";
            this.showMenu(i);
        }
    };
    MenuBar.prototype.clickMenu = function(i,j,Id){
        if(Id == this.selLeftId) return;
        if(this.selLeftId!=""){
            $(this.selLeftId).className="left_back";
            $("right_nav_"+this.selLeftId.substring(9)).style.display="none";  //hidden right
        } 
        if($(Id).className=="left_back"){
            $(Id).className="left_back_onclick";	
            this.selLeftId=Id;	
            $("right_nav_"+Id.substring(9)).style.display="";    //show right
            switch (this.cols[i]["modules"][j]["type"]){
                case "menu":
                    this.showList(i,j);
                    break;
                case "tree":
                    this.showTree(i,j);
                    break;
                default: 
                    $(this.rightId).innerHTML = "";
                    break;
            }			
        }
    };
    MenuBar.prototype.showMenu = function(i){
        var strMenu = new Array;
        var strDiv = new Array; 
        var objMenu = this.cols[i]["modules"];
        strMenu.push("<ul>");
        for(var j=0;j<objMenu.length;j++){
            if(j==0){
                strMenu.push("<li id=\"left_nav_"+j+"\" onclick=\""+this.instanceName+".clickMenu("+i+","+j+",id)\" class=\"left_back_onclick\">"+objMenu[j]["title"]+"</li>");
                this.selLeftId = "left_nav_"+j ;
            }else{
                strMenu.push("<li id=\"left_nav_"+j+"\" onclick=\""+this.instanceName+".clickMenu("+i+","+j+",id)\" class=\"left_back\">"+objMenu[j]["title"]+"</li>");
            }
            strDiv.push("<div style=\"width:100%;height:100%;display:none;\" id=\"right_nav_"+j+"\"></div>");
        }
        strMenu.push("</ul>");
        $(this.leftId).innerHTML = strMenu.join("");
        $(this.rightId).innerHTML = strDiv.join("");
        switch (objMenu[0]["type"]){
            case "menu":
                this.showList(i,0);
                break;
            case "tree":
                this.showTree(i,0);
                break;
        }
    };
    MenuBar.prototype.showList = function(i,j){
        curSelFrame = "";
        if($("right_nav_"+j).innerHTML!='') return;
        $("right_nav_"+j).style.display='';
        var strList = new Array;
        var objList = this.cols[i]["modules"][j]["menus"];
        strList.push("<div class=\"menuItems\">");
        for(var k=0;k<objList.length;k++){
                strList.push("<div class=\"menu\">");
                strList.push("<div id=\"list_nav_"+j+"_"+k+"\" class=\"menuIcon\" onClick=\""+this.instanceName+".clickList('"+objList[k]["url"]+"')\" onMouseOver=\"over(id)\" onMouseOut=\"out(id)\"><img class=\"menuImg\" src=\"../images/icon/"+objList[k]["icon"]+"\" /></div>");
                strList.push("<div class=\"menuText\"><a href=\"javascript:;\" onClick=\""+this.instanceName+".clickList('"+objList[k]["url"]+"')\">"+objList[k]["title"]+"</a></div>");
                strList.push("</div>");	
        }		
        strList.push("</div>");
        $("right_nav_"+j).innerHTML = strList.join("");
    };
    MenuBar.prototype.showTree = function(i,j){
        curSelFrame = "treeframe_"+i+"_"+j;
        if($("right_nav_"+j).innerHTML!='') return;
        $("right_nav_"+j).style.display='';
        var strTree = new Array;
        var sUrl = this.cols[i]["modules"][j]["url"];
        if(sUrl!=""){
            strTree.push("<iframe id=\"treeframe_"+i+"_"+j+"\" name=\"treeframe_"+i+"_"+j+"\" width=\"100%\" height=\"100%\" frameBorder=0 src=\""+sUrl+"\"></iframe>");
            $("right_nav_"+j).innerHTML = strTree.join("");
        }
    };
    MenuBar.prototype.clickList = function(sUrl){
        if(sUrl=="") return;
        $(this.frameId).src=sUrl;
    };        
}
Array.prototype.baoremove = function(dx){
    if(isNaN(dx)||dx>this.length){return false;}
    this.splice(dx,1);
}