﻿var ua = navigator.userAgent.toLowerCase();
var isOpera = (ua.indexOf('opera') > -1);
var isIE = (!isOpera && ua.indexOf('msie') > -1);

function getIEVersion()
{
    var rv = -1; // Return value assumes failure.
    if (navigator.appName == 'Microsoft Internet Explorer') {
        var re = new RegExp("msie ([0-9]{1,}[\.0-9]{0,})");
        if (re.exec(ua) != null)
            rv = parseFloat(RegExp.$1);
    }
    return rv;
}

if (getIEVersion() < 0 || getIEVersion() > 6.99) { // test for <IE7 - not all included
    function getScrollTop() { return window.pageYOffset || document.documentElement && document.documentElement.scrollTop || document.body.scrollTop || 0; }
    function getDocumentWidth() { return Math.max(document.compatMode != 'CSS1Compat' ? document.body.scrollWidth : document.documentElement.scrollWidth, getViewportWidth()); }
    function getViewportWidth() { return ((document.compatMode || isIE) && !isOpera) ? (document.compatMode == 'CSS1Compat') ? document.documentElement.clientWidth : document.body.clientWidth : (document.parentWindow || document.defaultView).innerWidth; }
    function getDocumentHeight() { return Math.max(document.compatMode != 'CSS1Compat' ? document.body.scrollHeight : document.documentElement.scrollHeight, getViewportHeight()); }
    function getViewportHeight() { return ((document.compatMode || isIE) && !isOpera) ? (document.compatMode == 'CSS1Compat') ? document.documentElement.clientHeight : document.body.clientHeight : (document.parentWindow || document.defaultView).innerHeight; }

    function PleaseWait(img) {
        if (typeof img == "undefined")
            img = "http://img.address.ua/img/pleasewait.gif";
        var Img = img;
        var nStart = 0;
        this.resize = function() {
            var oWait = document.getElementById("idWait");
            oWait.style.top = (parseInt(getScrollTop(), 10) + ((getViewportHeight() - $(oWait).height()) / 2)) + "px";
            oWait.style.left = (parseInt(document.body.scrollLeft, 10) + ((getViewportWidth() - $(oWait).width()) / 2)) + "px";
        };
        this.Show = function() {
            nStart++;
            if (nStart > 1)
                return;
            if ($("#idWait").length < 1) {

                $("body").append('<div id="idWait" class="fixedsystemDivHolder"><div class="loader"><img src="http://img.address.ua/img/ajax-loader.gif" width="220" height="19" alt="загрузка">Загрузка...</div></div>');
                //old 	            $("body").append('<img id="idWait" alt="Идет загрузка..." src="' + Img + '" style="display:none;position:absolute; z-index:100;"///>');
            }
            document.getElementById("idWait").style.display = "";
            /*	        pleaseWait.resize();
            $(window).bind("resize", pleaseWait.resize);
            $(window).bind("scroll", pleaseWait.resize);*/
        };
        this.Hide = function() {
            if (nStart > 0) {
                nStart--;
                document.getElementById("idWait").style.display = "none";
                //	            $(window).unbind("resize", pleaseWait.resize);
                //	            $(window).unbind("scroll", pleaseWait.resize);
            }
        };
    }

    var pleaseWait = new PleaseWait();
    $(window).unload(pleaseWait.Show); // add to event when window begins to unload
//    $(window).ajaxStart(pleaseWait.Show); // add to event when jQuery ajax starts
//    $(window).ajaxStop(pleaseWait.Hide);  // add to event when jQuery ajax stops

    $(document).ready(function() {
        $("body").ajaxStart(pleaseWait.Show); // add to event when jQuery ajax starts
        $("body").ajaxStop(pleaseWait.Hide);  // add to event when jQuery ajax stops
    });
    //$(document).ready(function() {
    //    $(window).unload(pleaseWait.Show);// add to event when window begins to unload
    //    $(window).ajaxStart(pleaseWait.Show); // add to event when jQuery ajax starts
    //    $(window).ajaxStop(pleaseWait.Hide);  // add to event when jQuery ajax stops
    //})
}
else {
    $(document).ready(function() {

    $($("body").children()[0]).before('<div id="idWait" class="fixedsystemDivHolder"><div class="systemMessage systemMessageIeWarning"><div class="closeSiteEnter closeSearchPopup"><a href="javascript:void($(' + "'#idWait'" + ').hide())"><img height="11" width="12" alt="закрыть" src="http://img.address.ua/img/close_icon.gif"></a></div><img height="110" width="86" alt="ie6" src="/img/ie6.gif"> <span>Address.ua может работать некорректно под браузером <strong>Internet Explorer версии 6</strong> и ниже. Рекомендуем <a target=\"_blank\" href="http://www.microsoft.com/rus/windows/internet-explorer/">обновить программное обеспечение </a></span></div></div>');
    })
}


