﻿function IYWindowSizeManager() { }

IYWindowSizeManager.prototype = {
    GetWindowInnerSize: function(pDoc) {
        var width, height;
        if (!pDoc) pDoc = document;

        if (self.innerHeight) {
            width = self.innerWidth;
            height = self.innerHeight;
        }
        else if (pDoc.documentElement && pDoc.documentElement.clientHeight) {
            width = pDoc.documentElement.clientWidth;
            height = pDoc.documentElement.clientHeight;
        }
        else if (pDoc.body) {
            width = pDoc.body.clientWidth;
            height = pDoc.body.clientHeight;
        }
        return { innerWidth: width, innerHeight: height };
    },

    GetWindowScrollPos: function(pDoc) {
        var left, top;
        if (!pDoc) pDoc = document;

        if (self.pageYOffset) {
            left = self.pageXOffset;
            top = self.pageYOffset;
        }
        else if (pDoc.documentElement && pDoc.documentElement.scrollTop) {
            left = document.documentElement.scrollLeft;
            top = document.documentElement.scrollTop;
        }
        else if (pDoc.body) {
            left = pDoc.body.scrollLeft;
            top = pDoc.body.scrollTop;
        }
        return { scrollLeft: left, scrollTop: top };
    },

    GetWindowScrollSize: function(pDoc) {
        var width, height;
        if (!pDoc) pDoc = document;

        if ((pDoc.compatMode && pDoc.compatMode == "CSS1Compat")) {
            width = pDoc.documentElement.scrollWidth;
            height = pDoc.documentElement.scrollHeight;
        }
        else {
            if (pDoc.body.scrollHeight > pDoc.body.offsetHeight)
                height = pDoc.body.scrollHeight;
            else
                height = pDoc.body.offsetHeight;

            if (pDoc.body.scrollWidth > pDoc.body.offsetWidth ||
            (pDoc.compatMode && pDoc.compatMode == "BackCompat") ||
            (pDoc.documentElement && !pDoc.documentElement.clientWidth)
        )
                width = pDoc.body.scrollWidth;
            else
                width = pDoc.body.offsetWidth;
        }
        return { scrollWidth: width, scrollHeight: height };
    },

    GetWindowSize: function() {
        var innerSize = this.GetWindowInnerSize();
        var scrollPos = this.GetWindowScrollPos();
        var scrollSize = this.GetWindowScrollSize();

        return {
            innerWidth: innerSize.innerWidth, innerHeight: innerSize.innerHeight,
            scrollLeft: scrollPos.scrollLeft, scrollTop: scrollPos.scrollTop,
            scrollWidth: scrollSize.scrollWidth, scrollHeight: scrollSize.scrollHeight
        };
    }
}

var IYWindowSize = new IYWindowSizeManager();



function IYOpacityManager() { }

IYOpacityManager.prototype = {
    SetOpacity: function(elem, nOpacity) {
        var opacityProp = this.GetOpacityProperty();

        if (!elem || !opacityProp) return;

        if (opacityProp == "filter") {
            nOpacity *= 100;
            try { var oAlpha = elem.filters['DXImageTransform.Microsoft.alpha'] || elem.filters.alpha; }
            catch (e) { }
            if (oAlpha) oAlpha.opacity = nOpacity;
            else elem.style.filter += "progid:DXImageTransform.Microsoft.Alpha(opacity=" + nOpacity + ")";
        }
        else elem.style[opacityProp] = nOpacity;
    },

    GetOpacityProperty: function() {
        if (typeof document.body.style.opacity == 'string')
            return 'opacity';
        else if (typeof document.body.style.MozOpacity == 'string')
            return 'MozOpacity';
        else if (typeof document.body.style.KhtmlOpacity == 'string')
            return 'KhtmlOpacity';
        else if (document.body.filters && navigator.appVersion.match(/MSIE ([\d.]+);/)[1] >= 5.5)
            return 'filter';
        else return false;
    }
}

var IYOpacity = new IYOpacityManager();




function IYEventManager() { }

IYEventManager.prototype = {
    Add: function(object, event, handler, useCapture) {
        if (object.addEventListener) object.addEventListener(event, handler, useCapture);
        else if (object.attachEvent) object.attachEvent('on' + event, handler);
        else object['on' + event] = handler;
    },

    Remove: function(object, event, handler, useCapture) {
        if (object.removeEventListener) object.removeEventListener(event, handler, useCapture);
        else if (object.detachEvent) object.detachEvent('on' + event, handler);
        else if (object['on' + event]) object['on' + event] = null;
    }
}

var IYHandler = new IYEventManager();



function IYFadeManager() {
    this.timeout = 10;
    this.opacityRate = 0.03;
    this.fadeInTimerID = null;
    this.fadeOutTimerID = null;
    this.fadeInOpacity = 0;
    this.fadeOutOpacity = 0;
    this.fadeInElement = null;
    this.fadeOutElement = null;
    this.callBack = null;

    this.init = function(timeout, opacityRate) {
        if (timeout) this.timeout = timeout;
        if (opacityRate) this.opacityRate = opacityRate;
    }
}

IYFadeManager.prototype = {
    FadeIn: function(element, callBack) {
        if (!element) return false;
        else {
            this.callBack = callBack;
            this.fadeInElement = element;
            this.fadeInOpacity = 0;
            function StartFadeIn(fader) {
                if (!fader || fader.fadeInTimerID || !fader.fadeInElement) fader._tryToCallBack();
                else {
                    try {
                        var handler = function() {
                            if (fader.fadeInOpacity > 1) {
                                IYOpacity.SetOpacity(fader.fadeInElement, 1);
                                fader._resetFadeInTimer();
                            }
                            else {
                                IYOpacity.SetOpacity(fader.fadeInElement, fader.fadeInOpacity);
                                fader.fadeInOpacity += fader.opacityRate;
                            }
                        }
                        fader.fadeInTimerID = setInterval(handler, fader.timeout);
                    }
                    catch (e) {
                        IYOpacity.SetOpacity(fader.fadeInElement, 1);
                        fader._tryToCallBack();
                    }
                }
            }
            StartFadeIn(this);
        }
    },

    FadeOut: function(element, callBack) {
        if (!element) return false;
        else {
            this.callBack = callBack;
            this.fadeOutElement = element;
            this.fadeOutOpacity = 1;
            function StartFadeOut(fader) {
                if (!fader || fader.fadeOutTimerID || !fader.fadeOutElement) fader._tryToCallBack();
                else {
                    try {
                        var handler = function() {
                            if (fader.fadeOutOpacity < 0) {
                                IYOpacity.SetOpacity(fader.fadeOutElement, 0);
                                fader._resetFadeOutTimer();
                            }
                            else {
                                IYOpacity.SetOpacity(fader.fadeOutElement, Math.round(fader.fadeOutOpacity * 100) / 100);
                                fader.fadeOutOpacity -= fader.opacityRate;
                            }
                        }
                        fader.fadeOutTimerID = setInterval(handler, fader.timeout);
                    }
                    catch (e) {
                        IYOpacity.SetOpacity(fader.fadeOutElement, 0);
                        fader._tryToCallBack();
                    }
                }
            }
            StartFadeOut(this);
        }
    },

    _resetFadeInTimer: function() {
        clearInterval(this.fadeInTimerID);
        this.fadeInTimerID = null;
        this._tryToCallBack();
    },

    _resetFadeOutTimer: function() {
        clearInterval(this.fadeOutTimerID);
        this.fadeOutTimerID = null;
        this._tryToCallBack();
    },

    _tryToCallBack: function() {
        if ((typeof this.callBack) == "function" && !this.fadeInTimerID && !this.fadeOutTimerID)
            this.callBack();
    }
}

var IYFader = new IYFadeManager();




function IYPopUpWindowExt() {
    this._F_size = { width: 800, height: 600 };
    this._F_windowSize = {
        innerWidth: 0, innerHeight: 0,
        scrollLeft: 0, scrollTop: 0,
        scrollWidth: 0, scrollHeight: 0
    };
    this._F_windowPosition = { left: 0, top: 0, centerX: 0, centerY: 0 };
    this._F_container = null;
    this._F_overlay = null;
    this._F_window = null;
    this._F_ajaxLoader = null;
    this._F_savedBackground = {
        image: "",
        position: "",
        repeat: ""
    };
    this._F_cssStyles = {
        containerStyle: "z-index:10000; position:absolute; left:0px; top:0px; display:none;",
        overlayStyle: "z-index:11000; position:absolute; left:0px; top:0px; background-color:#000000;",
        windowStyle: "z-index:12000; position:absolute; background-color:#FFFFFF; display:none;"
    };
    this._F_overlayOpacity = { max: 0.75, current: 0, step: 0.06, delay: 5 };
    this._F_timerID = null;

    this.init = function(width, height) {
        this._F_windowSize = IYWindowSize.GetWindowSize();
        this.__setWindowSize(width, height);
        this.__create();
    }
}

IYPopUpWindowExt.prototype = {
    __create: function() {
        this._F_window = document.createElement("div");
        this._F_window.style.cssText = this._F_cssStyles.windowStyle;
        this._F_window.className = "IYPopUpWindow";
        this._F_overlay = document.createElement("div");
        this._F_overlay.style.cssText = this._F_cssStyles.overlayStyle;
        this._F_container = document.createElement("div");
        this._F_container.style.cssText = this._F_cssStyles.containerStyle;
        this._F_container.appendChild(this._F_overlay);
        this._F_container.appendChild(this._F_window);
        var body = document.getElementsByTagName("body")[0];
        body.appendChild(this._F_container);
        IYOpacity.SetOpacity(this._F_overlay, 1);
        this.__reposition();
    },

    __reposition: function() {
        var leftPosition = (this._F_windowSize.scrollLeft + (this._F_windowSize.innerWidth - this._F_size.width) / 2);
        var topPosition = (this._F_windowSize.scrollTop + (this._F_windowSize.innerHeight - this._F_size.height) / 2);

        leftPosition = Math.min(leftPosition, this._F_windowSize.scrollWidth - this._F_size.width);
        topPosition = Math.min(topPosition, this._F_windowSize.scrollHeight - this._F_size.height);

        this._F_windowPosition.left = (leftPosition > 0 ? leftPosition : this._F_windowSize.scrollLeft);
        this._F_windowPosition.top = (topPosition > 0 ? topPosition : this._F_windowSize.scrollTop);
        this._F_windowPosition.centerX = this._F_windowPosition.left + this._F_size.width / 2;
        this._F_windowPosition.centerY = this._F_windowPosition.top + this._F_size.height / 2;

        this._F_window.style.left = this._F_windowPosition.left + "px";
        this._F_window.style.top = this._F_windowPosition.top + "px";
    },

    __resetTimer: function() {
        clearInterval(this._F_timerID);
        this._F_timerID = null;
    },

    __setWindowSize: function(width, height) {
        if (width && parseInt(width)) this._F_size.width = width;
        if (height && parseInt(height)) this._F_size.height = height;
    },

    SetAjaxLoader: function(fileName) {
        if (fileName && fileName.length > 0)
            this._F_ajaxLoader = fileName.toString();
    },

    ShowAjaxLoader: function() {
        this._F_savedBackground.image = this._F_window.style.backgroundImage;
        this._F_savedBackground.position = this._F_window.style.backgroundPosition;
        this._F_savedBackground.repeat = this._F_window.style.backgroundRepeat;
        this._F_window.style.backgroundImage = "url(\"" + this._F_ajaxLoader + "\")";
        this._F_window.style.backgroundPosition = "center center";
        this._F_window.style.backgroundRepeat = "no-repeat";
    },

    HideAjaxLoader: function() {
        this._F_window.style.backgroundImage = this._F_savedBackground.image;
        this._F_window.style.backgroundPosition = this._F_savedBackground.position;
        this._F_window.style.backgroundRepeat = this._F_savedBackground.repeat;
    },

    GetWindowPosition: function() {
        return this._F_windowPosition;
    },

    GetOverlay: function() {
        return this._F_overlay;
    },

    Clear: function() {
        this._F_window.innerHTML = "";
    },

    SetContent: function(content) {
        this.Clear();
        switch (typeof (content)) {
            case "string": this._F_window.innerHTML = content; break;
            case "object": this._F_window.appendChild(content); break;
        }
    },

    Resize: function(width, height) {
        this.__setWindowSize(width, height);
        this._F_windowSize = IYWindowSize.GetWindowSize();
        this._F_window.style.width = this._F_size.width + "px";
        this._F_window.style.height = this._F_size.height + "px";
        this._F_overlay.style.width = Math.max(this._F_windowSize.scrollWidth, this._F_size.width) + "px";
        this._F_overlay.style.height = Math.max(this._F_windowSize.scrollHeight, this._F_size.height) + "px";
        this.__reposition();
    },

    Open: function() {
        if (this._F_timerID) this.__resetTimer();
        this.Resize();
        function fadeIn(wndObject) {
            wndObject._F_overlayOpacity.current = 0;
            IYOpacity.SetOpacity(wndObject._F_overlay, 0);

            wndObject._F_container.style.display = "block";
            var handler = function() {
                if (wndObject._F_overlayOpacity.current > wndObject._F_overlayOpacity.max) {
                    IYOpacity.SetOpacity(wndObject._F_overlay, wndObject._F_overlayOpacity.max);
                    wndObject.__resetTimer();
                    wndObject._F_window.style.display = "block";
                    if (typeof (wndObject.onopened) == "function") wndObject.onopened();
                }
                else {
                    wndObject._F_overlayOpacity.current += wndObject._F_overlayOpacity.step;
                    IYOpacity.SetOpacity(wndObject._F_overlay, wndObject._F_overlayOpacity.current);
                }
            }
            wndObject._F_timerID = setInterval(handler, wndObject._F_overlayOpacity.delay);
        }
        fadeIn(this);
    },

    Close: function() {
        if (this._F_timerID) this.__resetTimer();
        this._F_window.style.display = "none";
        function fadeOut(wndObject) {
            wndObject._F_overlayOpacity.current = wndObject._F_overlayOpacity.max;

            var handler = function() {
                if (wndObject._F_overlayOpacity.current < 0) {
                    IYOpacity.SetOpacity(wndObject._F_overlay, 0);
                    wndObject.__resetTimer();
                    wndObject._F_container.style.display = "none";
                    if (typeof (wndObject.onclosed) == "function") wndObject.onclosed();
                }
                else {
                    wndObject._F_overlayOpacity.current -= wndObject._F_overlayOpacity.step;
                    IYOpacity.SetOpacity(wndObject._F_overlay, wndObject._F_overlayOpacity.current);
                }
            }
            wndObject._F_timerID = setInterval(handler, wndObject._F_overlayOpacity.delay);
        }
        fadeOut(this);
    },

    onopen: function() { },
    onclose: function() { }
}

function IYFormSubmitter() {
	this.form = null;
	this.txtInput = null;
	this.defaultText = ""; //"Поиск по сайту";

	this.init = function(form, txtInput, defaultText) {
		if (typeof (form) != "object" || typeof (txtInput) != "object") return;
		else {
			this.form = form;
			this.txtInput = txtInput;
			if (defaultText) this.defaultText = defaultText;
			if (!this.txtInput.value) this.txtInput.value = defaultText;
			function setHandlers(thisObj) {
				IYHandler.Add(thisObj.txtInput, "focus",
					function() { SetRequiredTextField(thisObj.txtInput, thisObj.defaultText, "focus"); });

				IYHandler.Add(thisObj.txtInput, "blur",
					function() { SetRequiredTextField(thisObj.txtInput, thisObj.defaultText, "blur"); });

				//thisObj.form.onsubmit = function() { thisObj._preSubmit(thisObj); };
			}
			setHandlers(this);
		}
	}
}

IYFormSubmitter.prototype = {
	_preSubmit: function(thisObj) {
		if (!thisObj) return false;
		else {
			if (thisObj.txtInput.value == thisObj.defaultText) thisObj.txtInput.value = "";
			return thisObj.form.submit();
		}
	}
}

function SetRequiredTextField(txtInput, defaultText, eventType) {
	if (typeof (txtInput) != "object") return;
	else {
		var value = txtInput.value;

		switch (eventType) {
			case "focus":
				if (value == defaultText) txtInput.value = "";
				else txtInput.select();
				break;
			default:
				if (value.replace(" ", "").length == 0) txtInput.value = defaultText;
				break;
		}
	}
}
