/**
 * jiangsj
 */
$(document).ready(function(){
    bind();
});
function bind(){
    /*鼠标右键*/
    $('.menuItem').contextMenu('myMenu', {
        onShowMenu: function(e, menu){
            var module = $(e.target).attr("module");
            if (module == undefined || module == '') {
                $('#myMenu_add', menu).remove();
            }
            else 
                if (module > 3) {
                    $('#myMenu_add', menu).remove();
                }
            return menu;
        },
        bindings: {
            'myMenu_add': function(t){
                var channel_id = $(t).attr("value");
                var module_id = $(t).attr("module");
                if (module_id != "" && channel_id != "") {
                    switch (module_id) {
                        case "1":
                            window.parent.location.href = './index.php?r=Article.add&channel_id=' + channel_id;
                            break;
                        case "2":
                            window.parent.location.href = './index.php?r=Software.add&channel_id=' + channel_id;
                            break;
                        case "3":
                            window.parent.location.href = './index.php?r=Game.addgameinfo&channel_id=' + channel_id;
                            break;
                    }
                }
                
            },
            'myMenu_clear': function(t){
                if (confirm("你确定要清除【" + $(t).attr("text") + "】频道吗？这样将会清除该频道下的所有频道。")) {
                    var channel_id = $(t).attr("value");
                    if (channel_id != "") {
                        $.get("index.php?r=layout.clearChannel", {
                            channel_id: channel_id
                        }, function(data){
                            if (data.status == 1) {
                                //window.location.reload();
                                window.parent.frames['cpanelLayout'].location.reload();
                            }
                            else {
                                alert("不能清除,还有子频道");
                            }
                        }, "json");
                    }
                }
            },
            'myMenu_delete': function(t){
                if (confirm("你确定要删除【" + $(t).attr("text") + "】频道吗？这样将会删除该频道下的所有频道。")) {
                    var channel_id = $(t).attr("value");
                    if (channel_id != "" && channel_id != 1) {
                        $.get("index.php?r=layout.deleteChannel", {
                            channel_id: channel_id
                        }, function(data){
                            if (data.status == 1) {
                                window.location.reload();
                                window.parent.frames['cpanelLayout'].location.href = "./index.php?r=Layout.cpanel";
                            }
                            else {
                                alert("不能删除,还有子频道");
                            }
                        }, "json");
                    }
                    else 
                        if (channel_id == 1) {
                            alert("首页不能删除");
                        }
                }
            },
            'myMenu_copy': function(t){
                var channel_id = $(t).attr("value");
                if (channel_id != "" && channel_id != 1) {
                    $.get("index.php?r=layout.getChannelUrl", {
                        channel_id: channel_id
                    }, function(data){
                        if (data.status == 1) {
                        	copyToClipboard(data.url);
                        }
                        else {
                            alert("不能复制");
                        }
                    }, "json");
                }
                else 
                    if (channel_id == 1) {
                        alert("不能复制");
                    }
                
            }
        }
    });
}

/** 
 * 复制代码，支持IE/Firefox/NS
 */
function copyToClipboard(txt){
    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("你使用的FireFox浏览器,复制功能被浏览器拒绝！\n请在浏览器地址栏输入“about:config”并回车。\n然后将“signed.applets.codebase_principal_support”双击，设置为“true”");
                    return;
                }
                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);
            }
}
