/*
*  Public JavaScript File for Geek Studio
*  Last Update: 2011/12/09 
*  by Edi Wang
*/

// Add to Favorite
function AddFavorite(sURL, sTitle) {
    try {
        window.external.addFavorite(sURL, sTitle);
    }
    catch (e) {
        try {
            window.sidebar.addPanel(sTitle, sURL, "");
        }
        catch (e) {
            alert("加入收藏失败，请使用Ctrl+D进行添加");
        }
    }
}

// Set HomePage
function SetHome(obj, vrl) {
    try {
        obj.style.behavior = 'url(#default#homepage)'; obj.setHomePage(vrl);
    }
    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', vrl);
        }
    }
}

// 防止被别人frame
if (top.location != self.location) top.location = self.location;

// PopUp Window
function PopUpPhoto(URL) {
    var widthCenter = (screen.width - 955) / 2;
    var heightCenter = (screen.height - 700) / 2;
    eval("window.open(URL,'popwindow','toolbar=0,scrollbars=1,status=0,location=0,menubar=0,resizable=1,width=955,height=700,top=" + heightCenter + ",left=" + widthCenter + "')");
}
function popUp(URL) {
    var widthCenter = (screen.width - 720) / 2;
    var heightCenter = (screen.height - 500) / 2;
    eval("window.open(URL,'popwindow','toolbar=0,scrollbars=1,status=0,location=0,menubar=0,resizable=1,width=720,height=500,top=" + heightCenter + ",left=" + widthCenter + "')");
}

function popUpHackerinn(URL) {
    var widthCenter = (screen.width - 860) / 2;
    var heightCenter = (screen.height - 740) / 2;
    eval("window.open(URL,'help','toolbar=0,scrollbars=1,status=0,location=0,menubar=0,resizable=1,width=860,height=740,top=" + heightCenter + ",left=" + widthCenter + "')");
}
function popUpGeekStudio(URL) {
    var widthCenter = (screen.width - 1074) / 2;
    var heightCenter = (screen.height - 1382) / 2;
    eval("window.open(URL,'help','toolbar=0,scrollbars=1,status=0,location=0,menubar=0,resizable=1,width=860,height=740,top=" + heightCenter + ",left=" + widthCenter + "')");
}


//隐藏显示DIV
function discontrol(targetid) {
    if (document.getElementById) {
        target = document.getElementById(targetid);
        if (target.style.display == "block") {
            target.style.display = "none";
            var aaa = document.all("aa");
            var s = '';
            for (var i = 0; i < aaa.length; i++) { if (aaa[i].checked == true) { s += aaa[i].value; } }
        }
        else { target.style.display = "block"; }
    }
}

//隐藏显示DIV
function closediv(targetid) {
    if (document.getElementById) {
        target = document.getElementById(targetid);
        target.style.display = "none";
    }
}


//平滑滚动显示DIV
function Scroll(obj, h, s) {
    var h = h || 200;
    var s = s || 1.2;
    var obj = typeof (obj) == "string" ? document.getElementById(obj) : obj;
    if (obj == undefined) { return false; }
    var status = obj.getAttribute("status") == null;
    var oh = parseInt(obj.offsetHeight);
    obj.style.height = oh;
    obj.style.display = "block";
    obj.style.overflow = "hidden";
    if (obj.getAttribute("oldHeight") == null) {
        obj.setAttribute("oldHeight", oh);
    } else {
        var oldH = Math.ceil(obj.getAttribute("oldHeight"));
    }
    var reSet = function () {
        if (status) {
            if (oh < h) {
                oh = Math.ceil(h - (h - oh) / s);
                obj.style.height = oh + "px";
            } else {
                obj.setAttribute("status", false);
                window.clearInterval(IntervalId);
            }
        } else {
            obj.style.height = oldH + "px";
            obj.removeAttribute("status");
            window.clearInterval(IntervalId);
        }
    }
    var IntervalId = window.setInterval(reSet, 10);
    return status;
}

// Copy Content (IE Only)
function copyContent() {
    var str = document.getElementById("LtrContent").innerText;
    str = str + "<p>本文转自汪宇杰个人网站http://www.wyjexplorer.cn/</p>" + document.getElementById("contentarea").innerHTML;
    var orgurl = "原文地址:";
    orgurl += this.location.href;
    str = str + orgurl
    window.clipboardData.setData("text", str);
    alert("复制成功");
}

//复制地址(兼容IE和Fx)
function copyToClipBoard() {
    var txt = window.location.toString();
    //txt += document.title;
    if (window.clipboardData) {
        window.clipboardData.clearData();
        window.clipboardData.setData("Text", txt);
    } else if (navigator.userAgent.indexOf("Opera") != -1) {
        window.location = txt;
    } else if (window.netscape) {
        try {
            netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
        } catch (e) {
            alert("被浏览器拒绝！\n请在浏览器地址栏输入'about:config'并回车\n然后将'signed.applets.codebase_principal_support'设置为'true'");
        }
        var clip = Components.classes['@mozilla.org/widget/clipboard;1'].createInstance(Components.interfaces.nsIClipboard);
        if (!clip)
            return;
        var trans = Components.classes['@mozilla.org/widget/transferable;1'].createInstance(Components.interfaces.nsITransferable);
        if (!trans)
            return;
        trans.addDataFlavor('text/unicode');
        var str = new Object();
        var len = new Object();
        var str = Components.classes["@mozilla.org/supports-string;1"].createInstance(Components.interfaces.nsISupportsString);
        var copytext = txt;
        str.data = copytext;
        trans.setTransferData("text/unicode", str, copytext.length * 2);
        var clipid = Components.interfaces.nsIClipboard;
        if (!clip)
            return false;
        clip.setData(trans, null, clipid.kGlobalClipboard);
    }
    alert("本文地址已复制到您的剪切板！");
}
