﻿/// <reference path="mootools-1.2.4-core-yc.js" />
/// 基于 mootools 1.2.4 的方法扩展包

// 把 QueryString 字符串转换成为hash对象
String.prototype.toHash = function () {
    var hash = new Hash();
    var str = this.substr(this.indexOf("?") + 1);
    str.split('&').each(function (item, index) {
        hash.include(item.split('=')[0], unescape(item.split('=')[1]));
    });
    return hash;
}

String.prototype.getHash = function (key) {
    var hash = this.toHash();
    if (hash.has(key)) return hash.get(key);
    return "";
}

// 获取html代码里面body的部分
String.prototype.getBody = function () {
    if (html.indexOf("<body>") == -1 || html.indexOf("</body>") == -1) return null;    
    return html.substring(html.indexOf("<body>"), html.indexOf("</body>"));
}

// 绑定事件的原生对象
function Bind() { };

// 绑定全选
Bind.prototype.SelectAll = function (obj, list) {
    obj.addEvent("click", function () {
        var checked = obj.get("checked");
        list.each(function (item) {
            item.set("checked", checked);
        });
    });
};

// 绑定排序操作
Bind.prototype.Sort = function (obj, valueObj, btnUp, btnDown) {
    function sort() {
        var btn = this.get("id").toLowerCase();
        var ac = "down";
        if (btn.indexOf("up") != -1) ac = "up";
        var index = obj.selectedIndex;
        if (index == -1) { alert("请选中要移动的项目！"); obj.focus(); return; }
        var count = obj.options.length;
        switch (ac) {
            case "up":
                if (index == 0) return;
                var op1 = $(obj.options[index]);
                var op2 = op1.getPrevious();
                op1.inject(op2, "before");
                break;
            case "down":
                if (index == count - 1) return;
                var op1 = $(obj.options[index]);
                var op2 = op1.getNext();
                op1.inject(op2, "after");
                break;
        }
        var value = "";
        for (var i = 0; i < obj.options.length; i++) {
            var id = obj.options[i].value;
            value += id + (i == obj.options.length - 1 ? "" : ",");
        }
        valueObj.set("value", value);
    }
    btnUp.addEvent("click", sort);
    btnDown.addEvent("click", sort);
};

// 页面元素操作
var UI = {};

// 获取屏幕宽高
UI.getSize = function () {
    var height = Math.max(document.documentElement.scrollHeight, document.documentElement.clientHeight);
    var width = Math.max(document.documentElement.scrollWidth, document.documentElement.clientWidth);
    return { x: width, y: height,
        height: $(document.body).getSize().y,
        top: Math.max(document.body.scrollTop, document.documentElement.scrollTop)
    };
};

// 显示一个遮罩
UI.mask = function (ac) {
    if (ac != undefined) {
        var div = $("ui_mask");
        if (div != null) div.dispose();
        return;
    }
    if ($("ui_mask") != null) return;
    var div = new Element("div", {
        "id": "ui_mask",
        "style": "filter:alpha(opacity=80);opacity:0.8;background:#000;width:100%;position:absolute;left:0;top:0;z-index:1;"
    });
    div.setStyle("height", Math.max(document.documentElement.scrollHeight, document.documentElement.clientHeight));
    div.inject($(document.body));
};

UI.diagObj = null;

UI.diag = function (title, html, width, height) {
    if (arguments.length == 1 && arguments[0] == 'close') {
        var div = $("ui_diag");
        if (div != null) div.dispose();
        UI.mask('close');
        return;
    }
    if ($("ui_diag") != null) return;
    UI.mask();
    var div = new Element("div", {
        "id": "ui_diag",
        "class": "diag",
        "style": "border:solid 1px #CCC; background:#FFF; position:absolute;z-index:2;",
        "html": "<div class='title'><span class='tit'>" + title + "</span><a href='javascript:UI.diag(\"close\")'></a></div><div class='body'>" + html + "</div>"
    });
    div.setStyles({
        "width": width,
        "height": height,
        "top": (UI.getSize().height - height) / 2 + UI.getSize().top,
        "left": (UI.getSize().x - width) / 2
    });
    div.inject($(document.body));
    UI.diagObj = div;
    return div;
};

// 设置对象居中
UI.center = function (obj) {
    var width = obj.getSize().x;
    var height = obj.getSize().y;
    obj.setStyles({
        "top": (UI.getSize().height - height) / 2 + UI.getSize().top,
        "left": (UI.getSize().x - width) / 2
    });
}

//  设置切换脚本
// 格式为 UI.tab(tagObj1,containerObj1,tagObj2,containerObj2,tagObj3,containerObj3 ...)
UI.tab = function () {
    var arg = new Array();
    for (var i = 0; i < arguments.length; i++) {
        arg.push($(arguments[i]));
    }
    if (arg.length % 2 != 0) {
        alert("标签和内容必须一一对应");
        return;
    }
    arg.each(function (item, index) {
        if (index % 2 == 0) {
            obj = arg[index + 1];
            if (obj == null || item == null) return;
            item.store("obj", obj);
            item.addEvent("mouseover", function () {
                arg.each(function (item, index) {
                    if (item != null) {
                        if (index % 2 == 0) {
                            item.removeClass("on");
                        } else {
                            item.setStyle("display", "none");
                        }
                    }
                });
                this.addClass("on");
                this.retrieve("obj").setStyle("display", "block");
            });
        }
    });
}


