var __nonMSDOMBrowser = (window.navigator.appName.toLowerCase().indexOf('explorer') == -1);

function BackgrounScreen(Container, Options) {
    if (!Container)
        Container = $(document.body);
    this.Container = Container;
    if (!Options)
        Options = { "styles": null, "events": { "hide": null} };
    if (!Options.events) {
        Options.events = { "hide": null };
    }
    this.Options = Options;
    this.Obj = null;

    if (!this.Options.styles) {
        this.Options.styles = {
            "opacity": "0.7",
            "z-index": "1001",
            "position": "absolute",
            "top": "0px",
            "left": "0px",
            "width": "100%",
            "height": "100%",
            "display": "none"
        };
    }

    if (!this.Options.bgcolor) {
        this.Options.bgcolor = "#ffffff";
    }

    if (!this.Options.clickHide) {
        this.Options.clickHide = false;
    }

    this.Init = function() {
        this.Create();
    };

    this.Create = function() {
        this.Obj = new Element('div');

        if (this.Options.className) {
            this.Obj.addClass(this.Options.className);
        }
        else {
            this.Obj.setStyle("background", this.Options.bgcolor);
        }

        if (this.Options.clickHide == true) {
            this.Obj.addEvent("click", function(a) {
                return function() {
                    a.Hide();
                }
            } (this));
        }

        this.Obj.inject(this.Container, "top");
        this.Obj.setStyles(this.Options.styles);
    };

    this.Show = function() {
        this.Obj.setStyle("display", "block");
        var bscrollsize = this.Container.getScrollSize();
        this.Obj.setStyle("height", bscrollsize.y);
        this.Obj.setStyle("width", bscrollsize.x);
    };

    this.Hide = function() {
        if (this.IsShow()) {
            this.Obj.setStyle("display", "none");
            if (this.Options.events.hide) {
                var temp = this.Options.events.hide;
                temp();
            }
        }
    };

    this.IsShow = function() {
        if (this.Obj.getStyle("display") == "block")
            return true;
        else
            return false;
    };

    this.SetZIndex = function(zi) {
        this.Obj.setStyle("z-index", zi);
    };

    this.Delete = function() {
        this.Obj.dispose();
        this.Obj.destroy();
    };

    this.Init();
};

function CenteringElement(el, Options) {
    if (!el)
        return false;
    this.el = el;
    if (!Options)
        Options = null;
    this.Options = Options;

    this.b = $(document.body);

    this.Refresh = function() {
        this.boduScroll = this.b.getScroll();
        this.bodySize = this.b.getSize();
        this.elSize = this.el.getSize();
        this.el.setStyle("left", this.bodySize.x / 2 - this.elSize.x / 2 + this.boduScroll.x);
        this.el.setStyle("top", this.bodySize.y / 2 - this.elSize.y / 2 + this.boduScroll.y);
    };

    this.Refresh();
};

function Ajax(Method, Url, eventCallback, Data) {
    this.Http = null;
    this.Working = false;
    this.Context = null;

    if (Method == undefined || Method == null)
        this.Method = 'GET';
    else
        this.Method = Method;
    if (Url == undefined || Url == null)
        this.Url = null;
    else
        this.Url = Url;
    if (eventCallback == undefined || eventCallback == null)
        this.eventCallback = null;
    else
        this.eventCallback = eventCallback;
    if (Data == undefined || Data == null)
        this.Data = null;
    else
        this.Data = Data;

    this.Dom = null;

    this.Start = function() {
        if (!this.Http) {
            this.Http = this.GetHttp();
            this.Working = false;
        }
        if (!this.Working && this.Http) {
            if (this.eventCallback != null) {
                this.Http.onreadystatechange = function(a) {
                    return function() {
                        if (a.Http.readyState == 4) {
                            var temp = String(a.Http.responseText);
                            var vs = temp.substr(0, temp.indexOf("endviewstate9843tdjf3r9df"));
                            temp = temp.substr(temp.indexOf("endviewstate9843tdjf3r9df") + 25);
                            document.getElementById("__VIEWSTATE").value = vs;
                            a.eventCallback(temp, a.Http.responseXML, a.Context);
                        }
                    }
                } (this);
            }
            this.Http.open(this.Method, this.Url, true);
            //this.Http.setRequestHeader("Accept-Language", "ru, en");
            //this.Http.setRequestHeader("Accept-Charset", "windows-1251");
            this.Http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");
            this.Working = true;
            this.Http.send(this.Data);
        }
        if (!this.Http) {
            alert('Ошибка при создании XMLHTTP объекта!')
        }
    };

    this.GetHttp = function() {
        var xmlhttp;
        if (window.XMLHttpRequest) {
            xmlhttp = new XMLHttpRequest();
        }
        else if (window.ActiveXObject) {
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }

        return xmlhttp;
    };

    this.GetDomXML = function() {
        var oXml;
        try {
            var test = this.Http.responseXML.firstChild;
            oXml = this.Http.responseXML;
        }
        catch (e) {
            try {
                oXml = (new DOMParser()).parseFromString(this.Http.responseText, 'text/xml');
            }
            catch (e) { }
        }

        if (!oXml || !oXml.firstChild || oXml.firstChild.nodeName == 'parsererror') {
            alert('The server didn\'t send back a proper XML response. Please contact your system administrator.\n\n' +
            'XML request error: ' + this.Http.statusText + ' (' + this.Http.status + ')\n\n' +
            'Response text:\n' + this.Http.responseText);
            return;
        }

        this.Dom = oXml;
    };

    this.SelectNodes = function(xpath) {
        if (navigator.userAgent.indexOf('MSIE') >= 0)		// IE
            return this.Dom.selectNodes(xpath);
        else					// Gecko
        {
            var aNodeArray = new Array();

            var xPathResult = dom.evaluate(xpath, this.Dom,
				this.Dom.createNSResolver(this.Dom.documentElement), XPathResult.ORDERED_NODE_ITERATOR_TYPE, null);
            if (xPathResult) {
                var oNode = xPathResult.iterateNext();
                while (oNode) {
                    aNodeArray[aNodeArray.length] = oNode;
                    oNode = xPathResult.iterateNext();
                }
            }
            return aNodeArray;
        }
    };

    this.SelectSingleNode = function(xpath) {
        if (navigator.userAgent.indexOf('MSIE') >= 0)		// IE
            return this.Dom.selectSingleNode(xpath);
        else					// Gecko
        {
            var xPathResult = this.Dom.evaluate(xpath, this.Dom, this.Dom.createNSResolver(this.Dom.documentElement), 9, null);

            if (xPathResult && xPathResult.singleNodeValue)
                return xPathResult.singleNodeValue;
            else
                return null;
        }
    };
};

function __doCallBack(eventTarget, eventArgument, eventCallback, fieldsRequest, context) {
    if (typeof (eventCallback) != 'function') {
        eventCallback = function(responseText, responseXML) {
        };
    }
    //    alert(typeof fieldsRequest);
    var postData = '';
    if (typeof fieldsRequest == 'object' && fieldsRequest !== null) {
        var i = 0;
        var postData = '';
        for (i = 0; i < fieldsRequest.length; i++) {
            postData = postData + '&' + fieldsRequest[i].name + '=' + __EncodeCallback(fieldsRequest[i].value);
        }
    }
    if (String(location.href).indexOf("#") >= 0)
        var url = String(location.href).substr(0, String(location.href).indexOf("#"));
    else
        var url = location.href;

    var form_data = "";
    if ($("form1")) {
        form_data = String("&" + $("form1").toQueryString());
        form_data = form_data.replace("&__POST=1", "");
        form_data = form_data.replace("&__EVENTTARGET=", "");
        form_data = form_data.replace("&__EVENTARGUMENT=", "");
    }

    //alert(form_data);
    //document.write('__AJAX=1&__POST=1&__EVENTTARGET=' + eventTarget + '&__EVENTARGUMENT=' + eventArgument + postData + form_data);

    var aj = new Ajax("post", url, eventCallback);
    aj.Context = context;
    //alert('__AJAX=1&__POST=1&__EVENTTARGET=' + eventTarget + '&__EVENTARGUMENT=' + eventArgument + postData + form_data);
    aj.Data = '__AJAX=1&__POST=1&__EVENTTARGET=' + eventTarget + '&__EVENTARGUMENT=' + eventArgument + postData + form_data;
    aj.Start();
};

function __EncodeCallback(parameter) {
    if (encodeURIComponent) {
        return encodeURIComponent(parameter);
    }
    else {
        return escape(parameter);
    }
};

function Stripes(Container, Selector, StripeClass) {
    if (Container && Selector != "") {
        els = Container.getElements(Selector);
        for (var i = 0; i < els.length; i++) {
            els[i].addClass(StripeClass);
            i++;
        }
        for (var i = 1; i < els.length; i++) {
            els[i].removeClass(StripeClass);
            i++;
        }
    }
};

function CMSTips(Container, Selector, Options) {
    if (typeof (Container) != "object" || Container == null || Container == undefined) {
        if (!Container)
            Container = $(document.body);
        else if (!$(Container))
            Container = $(document.body);
        else
            Container = $(Container);
    }
    if (Selector == "")
        return null;
    var buts = Container.getElements(Selector);

    if (!Options)
        Options = {
            "className": null,
            "fixed": false,
            "showDelay": 100,
            "hideDelay": 100,
            "events": null
        };
    var Options = Options;

    if (!Options.className)
        Options.className = null;
    if (!Options.fixed)
        Options.fixed = false;
    if (!Options.showDelay)
        Options.showDelay = 100;
    if (!Options.hideDelay)
        Options.hideDelay = 100;

    buts.each(function(el) {
        if (el.title == "" || el.title == undefined)
            return;
        var temp = new Element("div", {
            "class": Options.className,
            "html": el.title
        });
        var timer = 0;
        temp.setStyle("display", "none");
        temp.setStyle("position", "absolute");
        temp.setStyle("z-index", "1000");
        temp.inject(Container, "top");
        el.title = "";
        el.addEvents({
            "mouseover": function() {
                var pos = el.getPosition();
                var size = el.getSize();
                temp.setStyle("display", "block");
                temp.setStyle("left", pos.x);
                temp.setStyle("top", pos.y + size.y + 3);
            },
            "mouseout": function() {
                timer = setTimeout(function() {
                    temp.setStyle("display", "none");
                }, 100);
            }
        });

        temp.addEvents({
            "mousemove": function() {
                clearTimeout(timer);
            },
            "mouseout": function() {
                timer = setTimeout(function() {
                    temp.setStyle("display", "none");
                }, 100);
            }
        });
    });
};

function MaxHeight() {
    var items = $(document.body).getElements(".max_height_el");
    if (items.length > 0) {
        items.each(function(el) {
            var p = el.getParent();
            var mh = 0;
            p.getChildren().each(function(el1) {
                if (!el1.hasClass("max_height_el")) {
                    mh = mh + el1.getSize().y;
                }
            });
            while (true) {
                var tag = p.get('tag');
                if (tag != "div" && tag != "table" && tag != "td" && tag != "body")
                    p = p.getParent();
                else
                    break;
            }
            var s = p.getSize();
            el.setStyle("height", s.y - mh -
                p.getStyle("margin-top").toInt() -
                p.getStyle("margin-bottom").toInt() -
                p.getStyle("padding-top").toInt() -
                p.getStyle("padding-bottom").toInt() -
                el.getStyle("margin-top").toInt() -
                el.getStyle("margin-bottom").toInt() -
                el.getStyle("padding-top").toInt() -
                el.getStyle("padding-bottom").toInt());
        });
    }
};
function __SaveScrollPositionOnSubmit() {
    if (__nonMSDOMBrowser) {
        theForm.elements['__SCROLLPOSITIONY'].value = window.pageYOffset;
        theForm.elements['__SCROLLPOSITIONX'].value = window.pageXOffset;
    }
    else {
        theForm.__SCROLLPOSITIONX.value = __GetScrollX();
        theForm.__SCROLLPOSITIONY.value = __GetScrollY();
    }
    return true;
};
function __RestoreScrollPosition() {
    if (__nonMSDOMBrowser) {
        window.scrollTo(theForm.elements['__SCROLLPOSITIONX'].value, theForm.elements['__SCROLLPOSITIONY'].value);
    }
    else {
        window.scrollTo(theForm.__SCROLLPOSITIONX.value, theForm.__SCROLLPOSITIONY.value);
    }
    return true;
};
function __GetScrollX() {
    if (__nonMSDOMBrowser) {
        return window.pageXOffset;
    }
    else {
        if (document.documentElement && document.documentElement.scrollLeft) {
            return document.documentElement.scrollLeft;
        }
        else if (document.body) {
            return document.body.scrollLeft;
        }
    }
    return 0;
};
function __GetScrollY() {
    if (__nonMSDOMBrowser) {
        return window.pageYOffset;
    }
    else {
        if (document.documentElement && document.documentElement.scrollTop) {
            return document.documentElement.scrollTop;
        }
        else if (document.body) {
            return document.body.scrollTop;
        }
    }
    return 0;
};
