﻿var TSDD_$ = function (id) {return "string" == typeof id ? document.getElementById(id) : id;};
var ddmenu_maxwidth = 125;
var ddmenu_x = 0;
var ddmenu_y = 0;
function DDMenu(obj,target,options)
{
    target = TSDD_$(target);
    if(!target)
        return;
    if(!options)
        options = [];
    options.w = options.w || ddmenu_maxwidth;
    options.x = options.x || ddmenu_x;
    options.y = options.y || ddmenu_y;
    var pos = DDObjPosition(obj);
    
    //target.style.left = pos.x + options.x + "px";
    //target.style.top = pos.y + obj.offsetHeight + options.y + "px";
    var menuul = target.getElementsByTagName("ul");
    var menuli = menuul[0].getElementsByTagName("li");
    //obj.onmouseover = show;
    obj.onmouseout = unshow;
    menuul[0].onmouseover = show;
    menuul[0].onmouseout = unshow;    
    
    function show(){
        //menuul[0].style.width = options.w + "px";
        menuul[0].className = "ddmenuclearfix typeul block"
        //document.title = menuul[0].offsetWidth;
        //document.title = menuul[0].offsetWidth + "+" +obj.offsetLeft;
        if(parseInt(menuul[0].offsetWidth) + parseInt(obj.offsetLeft) > 980){
            menuul[0].style.left = 980 - parseInt(menuul[0].offsetWidth) + "px";
        }else{
            menuul[0].style.left = obj.offsetLeft + "px";
        }
    }
    function unshow(){
        menuul[0].className = "typeul"
    }
    for(var i = 0; i < menuli.length; i++){
        menuli[i].num = i;
        var liul = menuli[i].getElementsByTagName("ul")[0];
        if(liul){
            liul.style.left = options.w + "px";
            //liul.style.width = options.w + "px";
            menuli[i].getElementsByTagName("a")[0].className = "haschild";
            typeshow();            
        }
    }
    function typeshow(){
        menuli[i].onmouseover = showul;
        menuli[i].onmouseout = unshowul;
    }
    function showul(){
        menuli[this.num].getElementsByTagName("ul")[0].className = "block";
    }
    function unshowul(){
        menuli[this.num].getElementsByTagName("ul")[0].className = "";
    }
    show();
}
function DDObjPosition(obj){
    var target = obj;
    var pos = {x:target.offsetLeft,y:target.offsetTop};
    var target = target.offsetParent; 
    while (target) { 
        pos.x += target.offsetLeft; 
        pos.y += target.offsetTop; 
        target = target.offsetParent;
    }
    return pos;
}

//加入收藏
TS_AddFavorite = function(url, title){ 
    if(!url){url = location.href;}
    if(!title){title = document.title;}
    try{window.external.addFavorite(url, title);}catch(e){try{window.sidebar.addPanel(title, url, "");}catch (e){alert("加入收藏失败，请使用Ctrl+D进行添加");}} 
} 
//设为首页
TS_SetHome = function(obj,url){
    if(!url){url = location.href;}
    try{obj.style.behavior='url(#default#homepage)';obj.setHomePage(url);}
    catch(e){
        if(window.netscape) {
            try {netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect"); }
            catch (e){alert("此操作被浏览器拒绝！\n请在浏览器地址栏输入“about:config”并回车\n然后将 [signed.applets.codebase_principal_support]的值设置为'true',双击即可。"); }
            var prefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components.interfaces.nsIPrefBranch);
            prefs.setCharPref('browser.startup.homepage',url); 
        }
    }
}
