//admin ajax tools
void function(){
    var b = new Hash();
    try {
        b.empty.periodical(60 * 1000, b)
    } 
    catch (c) {
    }
    var ajaxHelper = new Class({
        Extends: Ajax,
        initialize: function(url, opt){
            b.set(url, "init");
            this.addEvent("onCancel", function(){
                b.set(url, "cancel")
            });
            this.parent(url, opt)
        },
        send: function(toUrl, param){
            b.set(this.url, "request");
            if (!this.check(arguments.callee, param)) {
                return this
            }
            this.running = true;
            var h = $type(param);
            if (h == "string" || h == "element") {
                param = {
                    data: param
                }
            }
            var opt = this.options;
            param = $extend({
                data: opt.data,
                url: toUrl,
                method: opt.method
            }, param);
            var q = param.data, toUrl = param.url, md = param.method;
           
            switch ($type(q)) {
                case "element":
                    q = $(q).toQueryString();
                    break;
                case "object":
                case "hash":
                    q = Hash.toQueryString(q)
            }
            if (this.options.urlEncoded && "post" == md.toLowerCase()) {
                var i = (this.options.encoding) ? "; charset=" + this.options.encoding : "";
                this.headers.set("Content-type", "application/x-www-form-urlencoded" + i)
            }
            if (q && "get" == md.toLowerCase()) {
                toUrl = toUrl + (toUrl.contains("?") ? "&" : "?") + q;
                q = null
            }
            this.xhr.open(md.toUpperCase(), toUrl, this.options.async);
            this.xhr.onreadystatechange = this.onStateChange.bind(this);
            this.headers.each(function(v, i){
                if (!$try(function(){
                    this.xhr.setRequestHeader(i, v);
                    return true
                }
				.bind(this))) {
                    this.fireEvent("exception", [i, v])
                }
            }, this);
            this.loadMask("onrequest", $(this.options.update));
            if (window.ie && this.options.update && $(this.options.update)) {
                try {
                    $ES("iframe", this.options.update).each(function(m){
                        m.remove()
                    })
                } 
                catch (k) {
                }
            }
            this.fireEvent("request");
            this.xhr.send(q);
            if (!this.options.async) {
                this.onStateChange()
            }
            return this
        },
        onFailure: function(){
            if ($chk($("loadMask"))) {
                this.loadMask("onfailure")
            }
            if (this.options.elMap) {
                switch (this.transport.status) {
                    case 404:
                        MessageBox.error("页面未找到.");
                        break;
                    case 401:
                        new Dialog("401.html", {
                            ajaksable: false,
                            modal: true,
                            width: 300,
                            height: 150,
                            resizeable: false
                        });
                        break;
                    case 403:
                        MessageBox.error("您无此操作的权限.");
                        break
                }
            }
            b.set(this.url, "failure");
            this.fireEvent("onFailure", this.transport);
        },
        onException: function(e, d){
            if ($chk($("loadMask"))) {
                this.loadMask("onException")
            }
            MessageBox.error("出现错误:" + e + ":" + d)
        },
        success: function(text, xml){
            try {
                var opt = this.options;
                var rsp = this.response;
                var o = $(opt.update);
                rsp.html = text.stripScripts(function(e){
                    rsp.javascript = e
                });
                rsp.html = this.processHTML(rsp.html);
                if (!o) {
                    MessageBox.show(rsp.html);
                    this.loadMask("oncomplete");
                    return this.onSuccess(rsp.html)
                }
                if (opt.elMap) {
                    $each(opt.elMap, function(v, i){
                        if (i == ".sideContent" || !v) {
                            return
                        }
                        $(v).empty()
                    });
                    rsp.html = rsp.html.replace(/<\!-{5}(.*?)-{5}([\s\S]*?)-{5}(.*?)-{5}>/g, function(){
                        if (opt.elMap[arguments[1]]) {
                            if (arguments[1] == ".sideContent" && arguments[2]) {
                                opt.elMap[arguments[1]].adopt(new Element("div").setHTML(arguments[2]).getFirst().setStyle("display", "none"))
                            }
                            else {
                                opt.elMap[arguments[1]].set("html", arguments[2])
                            }
                        }
                        return ""
                    });
					//$empty
                    //updateSize()
                }
                o.empty().setHTML(rsp.html);
                if (opt.evalScripts) {
                    $exec(rsp.javascript)
                }
                makeAjaksLink(o);
                this.onSuccess(rsp.html, null, opt, null, rsp.javascript);
                this.loadMask("oncomplete")
            } 
            catch (i) {
                MessageBox.error("目标页面有错误!" + i)
            }
        },
        loadMask: function(type, target){
            target = $(target);
            var e = target || window;
            switch (type) {
                case "onrequest":
                    return $("loadMask").amongTo(window).show();
                default:
                    $("loadMask").hide();
                    if ($(this.options.update)) {
                        b.set(this.url, "success")
                    }
            }
        }
    });
    v5shop_AdminXHR = new Class({
        options: {
            evalScripts: true,
            autoCancel: true,
            method: "get",
            elMap: false,
            data: null,
            link: "cancel"
        },
        page: function(){
            var arg = Array.flatten(arguments).link({
                url: String.type,
                options: Object.type,
                sponsor: Element.type
            });

            var param = $merge(this.options, arg.options);
            var url = arg.url;
            var el = arg.sponsor;
            if (param.autoCancel && this.curAjax) {
                this.curAjax.cancel()
            }
            //对绑定的元素进行验证，未通过则隐藏提示层
            /*
            if ($type(param.data) == "element") {
                if (!$(param.data).bindValidator("x-input")) {
                    return $("loadMask").hide()
                }
            }
            */
            param.update = param.update || $($chk(el) ? $(el).getContainer() : false) || "main";
            if (param.update == "main" || ($type(param.update) == "element" && param.update.id == "main")) {
                if (!param.elMap) {
                    param.elMap = {
                        ".mainHead": $("headBar"),
                        ".mainFoot": $("footBar")
                    }
                }
            }
            $extend(param.elMap, {
                ".sideContent": $("sidecontent")
            });
            this.curAjax = new ajaxHelper(url, param);
            this.curAjax.request()
        }
    });
    makeAjaksLink = function(el){
        $(el|| document).getElements("form").addEvent("submit", function(e){
            var f = e ? (e.stop ? e.stop : $empty) : $empty;
            document.body.fireEvent("submit", $extend((e ||{}), {target: this,stop: f}));
        })
		return arguments.callee;
    }();    
}();

//init ajax class
var W = function(){
    return (W && $type(W) == "object") ? W : new v5shop_AdminXHR()
}();