var CurrentTR = undefined;
var common = {
    //返回当前页面高度
    PageHeight: function () {
        if ($.browser.msie) {
            return document.compatMode == "CSS1Compat" ? document.documentElement.clientHeight : document.body.clientHeight;
        } else {
            return self.innerHeight;
        }
    },
    //返回当前页面宽度
    PageWidth: function () {
        if ($.browser.msie) {
            return document.compatMode == "CSS1Compat" ? document.documentElement.clientWidth : document.body.clientWidth;
        } else {
            return self.innerWidth;
        }
    },

    GridViewClick: function (GridViewID, OutFirst, clickFunction) {
        $("#" + GridViewID + " tr").mousedown(function (event) {
            TR = this;
            if (OutFirst == undefined) OutFirst = true;
            if (TR == undefined || (OutFirst && TR.rowIndex == 0)) return;
            CurrentTR = TR;

            if (event.which == "3") {
                var arr = null;
                var chkValue = $("#HdnSelectedValues").val();
                //此处判断有没有选择
                arr = chkValue.split('`');
                if (arr.length == 1) {
                    //还没有选择，要单选作为当前行
                    var check = $(CurrentTR).find("#chkSelect");
                    $(CurrentTR).addClass("click");
                    check[0].checked = true;
                    AddRemoveValues(check[0]);

                    //启用原来禁用的菜单
                    var item = $('#mm').menu('findItem', '关联文件');
                    $('#mm').menu('enableItem', item.target);
                    var item1 = $('#mm').menu('findItem', '复制文件地址');
                    $('#mm').menu('enableItem', item1.target);
                    var item2 = $('#mm').menu('findItem', '下载文件');
                    $('#mm').menu('enableItem', item2.target);
                }
                else if (arr.length == 2) {
                    //已单选
                    var checkTr = $(CurrentTR).parent().children(".click");

                    var check = $(checkTr).find("#chkSelect");
                    $(checkTr).removeClass("click");
                    check[0].checked = false;
                    AddRemoveValues(check[0]);

                    check = $(CurrentTR).find("#chkSelect");
                    $(CurrentTR).addClass("click");
                    check[0].checked = true;
                    AddRemoveValues(check[0]);

                    //启用原来禁用的菜单
                    var item = $('#mm').menu('findItem', '关联文件');
                    $('#mm').menu('enableItem', item.target);
                    var item1 = $('#mm').menu('findItem', '复制文件地址');
                    $('#mm').menu('enableItem', item1.target);
                    var item2 = $('#mm').menu('findItem', '下载文件');
                    $('#mm').menu('enableItem', item2.target);
                }
                else if (arr.length > 2) {
                    //已多选，只针对多选的操作,禁用只针对单条记录的菜单
                    var item = $('#mm').menu('findItem', '关联文件');
                    $('#mm').menu('disableItem', item.target);
                    var item1 = $('#mm').menu('findItem', '复制文件地址');
                    $('#mm').menu('disableItem', item1.target);
                    var item2 = $('#mm').menu('findItem', '下载文件');
                    $('#mm').menu('disableItem', item2.target);
                }
            }
            else {
                //高亮与反高亮样式
                var check = $(CurrentTR).find("#chkSelect");
                var flag = $(CurrentTR).hasClass("click");
                $(CurrentTR)[flag ? "removeClass" : "addClass"]("click");
                if (typeof event.srcElement == "undefined" || event.srcElement.type != "checkbox") {
                    check[0].checked = flag ? false : true;
                }
                AddRemoveValues(check[0]);
                //全选与反选
                var allcheck = $(CurrentTR).parent().find("tr.click").length == $(CurrentTR).siblings().length;
                $(CurrentTR).parent().find(":first-child").find("#chkSelect")[0].checked = (!flag && allcheck) ? true : false;

            }
        });
    }

}

function showContents(path) {
    $("#wdcontent", window.parent.document).attr("src", path);
}
function over(obj) { 
    var tag = $(obj);
    tag[tag.hasClass("GridMove") ? "removeClass" : "addClass"]("GridMove");
}

function CopyCode(copyText) {
    if (window.clipboardData) {
        window.clipboardData.setData("Text", copyText)
    }
    else {
        var flashcopier = 'flashcopier';
        if (!document.getElementById(flashcopier)) {
            var divholder = document.createElement('div');
            divholder.id = flashcopier;
            document.body.appendChild(divholder);
        }
        document.getElementById(flashcopier).innerHTML = '';
        alert(encodeURIComponent(copyText));
        var divinfo = '<embed src="js/_clipboard.swf"  FlashVars="clipboard=' + encodeURIComponent(copyText) + '" width="0" height="0" type="application/x-shockwave-flash"></embed>';
        document.getElementById(flashcopier).innerHTML = divinfo;
    }
    $.messager.show({
        title: '提示消息',
        msg: '复制成功，已将地址复制到粘贴版!',
        timeout: 2000,
        showType: 'show',
        style: {
            right: '',
            top: window.parent.document.body.scrollTop + window.parent.document.documentElement.scrollTop+100,
            bottom: ''
        }
    });
}

function std_playUrl(txt) {
    if (txt.charAt(txt.length - 1) == "#") {
        document.all.url.src = 'shorturl.aspx?str=' + txt;
        return false;
    }
    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('链接已复制到剪贴板！');
    $.messager.show({
        title: '提示消息',
        msg: '复制成功，已将地址复制到粘贴版!',
        timeout: 2000,
        showType: 'show',
        style: {
            right: '',
            top: window.parent.document.body.scrollTop + window.parent.document.documentElement.scrollTop + 100,
            bottom: ''
        }
    });
    return true;
}



function checkAll() {
    var a = $("input[name='checkbox']");
    var check = document.getElementById("chkSelect").checked;

    for (var i = 0; i < a.length; i++) {
        if (a[i].checked != check)
            $(a[i]).trigger("mousedown");
    }
}

function AddRemoveValues(oChk) {
    var reg = new RegExp("`" + oChk.value);
    var form = $("#Form1")[0];
    if (oChk.checked && !reg.test(form.HdnSelectedValues.value))
        form.HdnSelectedValues.value += "`" + oChk.value;
    else if (!oChk.checked)
        form.HdnSelectedValues.value = form.HdnSelectedValues.value.replace("`" + oChk.value, "");

}