﻿
//check browser type
var browser = {
    IE: !!(window.attachEvent && !window.opera),
    Opera: !!window.opera,
    WebKit: navigator.userAgent.indexOf('AppleWebKit/') > -1,
    Gecko: navigator.userAgent.indexOf('Gecko') > -1 && navigator.userAgent.indexOf('KHTML') == -1, //firefox
    MobileSafari: !!navigator.userAgent.match(/Apple.*Mobile.*Safari/)
}

//getElementById
function $(idStr) {
    if (idStr != null) {
        element = document.getElementById(idStr);
        return element;
    }
    return false;
}
//getElementsByName
function $N(nameStr) {
    if (nameStr != null) {
        var elements = new Array();
        elements = document.getElementsByName(nameStr);
        return elements;
    }
    return false;
}
//getElementsByTagName
function $T(nameStr) {
    if (nameStr != null) {
        var elements = new Array();
        elements = document.getElementsByTagName(nameStr);
        return elements;
    }
    return false;
}

//popup layer
function popupLayer(ev, id, sw, sh, cobj) {
    var e = window.event || ev;
    this.title = '';
    this.bottom = '';
    this.content = '';
    cobj = cobj || '__content__';
    var sw1 = sw == null ? 350 : sw;

    this.show = function() {
        var layer = '<div style="margin: 0px; padding: 0px; cursor: move; background: #255ba5; padding-left: 4px; font-size: 12px; color: #fff; height: 25px; line-height: 25px; border-left: 1px solid #255ba5; border-top: 1px solid #255ba5; border-right: 1px solid #255ba5;" onmousedown="oMove(parentNode,event)"><span style="float: right; padding-right: 3px;"><a href="javascript:;" onclick="closeLayer(\'' + id + '\')" style="color: #fff;">关闭</a></span><span id=\"__title__\">' + this.title + '</span></div>';
        layer += '<div style="margin: 0; background: #fff; font-size: 12px; border-left: 1px solid #255ba5; border-right: 1px solid #255ba5; clear: both; overflow: hidden; padding: 3px;" id="' + cobj + '">' + this.content + '</div>';
        var bb = '';
        if (this.bottom == '') bb = 'height: 20px; ';
        layer += '<div style="' + bb + 'background: #255ba5; text-align: center; padding: 4px; border-left: 1px solid #255ba5; border-bottom: 1px solid #255ba5; border-right: 1px solid #255ba5; margin: 0;" id="__bottom__">' + this.bottom + '</div>';

        if (!document.getElementById(id)) {
            var _div = document.createElement('div');
            _div.id = id;
            _div.style.left = (((window.innerWidth || document.body.offsetWidth) - sw1) / 2) + 'px';
            _div.style.bottom = '50%';
            _div.style.marginBottom = '-150px';
            _div.style.width = sw1 + 'px';
            if (sh != null) _div.style.height = sh + 'px';
            _div.style.background = '#fff';
            _div.style.position = 'absolute';
            _div.style.zIndex = '999';
            _div.innerHTML = layer;
            document.body.appendChild(_div);
        }
        var offTop = document.getElementById(id).offsetTop;
        if (offTop < 0) {
            if (offTop > -100) {
                offTop = -100;
            } else {
                offTop -= 100;
            }
            var marBot = -150;
            marBot += offTop;
            document.getElementById(id).style.marginBottom = marBot + 'px';
        } else {
            if (offTop > 300) document.getElementById(id).style.marginBottom = '-0px';
            if (offTop < 100) document.getElementById(id).style.marginBottom = '-220px';
        }
        document.getElementById(id).style.height = document.getElementById(id).offsetHeight + 'px';

    }
}
//remove popup layer
function closeLayer(id) {
    document.body.removeChild(document.getElementById(id));
}
//move popup layer
function oMove(obj, even) {
    var otop, oleft;
    var e = window.event || even;
    otop = e.clientY - obj.offsetTop;
    oleft = e.clientX - obj.offsetLeft;

    if (document.all) obj.setCapture();

    obj.onmousemove = function(e) {
        e = e || window.event;
        obj.style.left = (e.clientX - oleft) + 'px';
        obj.style.top = (e.clientY - otop) + 'px';
    }
    obj.onmouseup = function() {
        obj.onmousemove = null;
        if (document.all) obj.releaseCapture();
    }
}

//get upload ui
function getUploadUI(id, type, prev) {
    //id : 将上传结果地址返回给该ID
    //type : 上传文件类型
    //prev : 预览元素ID
    prev = prev || '';
    var html = '<div style="margin: 3px; font-weight: bold;">选择要上传的文件：</div>';
    html += '<div style="padding: 3px;"><iframe src="/admin/editor/filemanager/connectors/upload.html?tt=' + id + '&tp=' + type + '&prev=' + prev + '" width="330px" height="25px" style="overflow: hidden; background: transparent;" scrolling="no" frameborder="0" framewidth="0" frameheight="0"></iframe></div>';
    if (browser.IE) {
        if (prev != '' && type == 'Image') {
            html += '&nbsp;<img style="width: 150px; height: 120px; border: outset 1px #ddd; filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale);" alt="" id="prev" />';
        }
    }
    return html;
}
//check all checkbox
function checkAll(e, name) {
    var sub = $N(name);
    for (var i = 0; i < sub.length; i++) {
        if (!sub.item(i).disabled) sub.item(i).checked = e.checked;
    }
}
//get selected checkbox
function getAllSelected(name, split) {
    var sub = $N(name);
    var tmp = '';
    split = split || ',';
    for (var i = 0; i < sub.length; i++) {
        if (sub.item(i).checked) tmp += split + sub.item(i).value;
    }
    if (tmp != '') tmp = tmp.substring(split.length);
    return tmp;
}
//alt color
function setBGColor(e, color) {
    e.style.background = color;
}
//框架自适应高度
function SetWinHeight(obj) {
    var win = obj;
    if (document.getElementById) {
        if (win && !window.opera) {
            if (win.contentDocument && win.contentDocument.body.offsetHeight) {
                win.height = win.contentDocument.body.offsetHeight;
            }
            else if (win.Document && win.Document.body.scrollHeight) {
                win.height = win.Document.body.scrollHeight;
            }
        }
    }
}

//打开颜色选择器
function getColor(e, ic) {
    var m = window.showModalDialog('/js/color.html', ic, 'dialogWidth=254px; dialogHeight=223px; center=yes; help=no; status=no; scroll=no');
    if (m != null && m != undefined) {
        e.value = m;
        e.style.backgroundColor = m;
    }
}