var KT_popups = {};

function stub() {
}

function KT_popupConfigure(popupId, closeLinkId, initialDelay, closeLinkDelay, showTime, showAgainAfter, animationSpeed) {
    if (KT_popups[popupId]) {
        return;
    }
    KT_popups[popupId] = new KT_popup(popupId, closeLinkId, initialDelay, closeLinkDelay, showTime, showAgainAfter, animationSpeed);
}

function KT_popup(popupId, closeLinkId, initialDelay, closeLinkDelay, showTime, showAgainAfter, animationSpeed) {
    this._popup = document.getElementById(popupId);

    if (!this._popup) {
        return;
    }
    if (this._readCookie('KT_popup_' + popupId)) {
        return;
    }

    this._popup.style.position = 'absolute';
    this._popup.style.left = -10000 + 'px';
    this._popup.style.top = -10000 + 'px';
    this._popup.style.zIndex = KT_popup.Z_INDEX;
    this._popup.style.visibility = 'hidden';
    this._popup.style.display = 'block';

    this._popupCurPos = 0;
    this._visible = false;
    this._iframe = null;

    if (this._isIE()) {
        this._iframe = document.createElement('DIV');
        this._iframe.innerHTML = '<iframe src="javascript:\'\'" scrolling="no" marginheight="0" marginwidth="0" frameborder="1" width="100%" height="100%"></iframe>';
        this._iframe.style.position = 'absolute';
        this._iframe.style.left = -10000 + 'px';
        this._iframe.style.top = -10000 + 'px';
        this._iframe.style.width = this._popup.offsetWidth + 'px';
        this._iframe.style.height = this._popup.offsetHeight + 'px';
        this._iframe.style.zIndex = KT_popup.Z_INDEX - 1;

        document.getElementsByTagName('BODY')[0].appendChild(this._iframe);
    }

    KT_popup.Z_INDEX += 2;

    this._interval = setInterval('KT_popups[\'' + popupId + '\']._animatePopup()', animationSpeed ? animationSpeed : 30);

    if (initialDelay > 0) {
        setTimeout('KT_popups[\'' + popupId + '\']._show()', initialDelay * 1000);
    } else {
        this._show();
    }

    if (showTime > 0) {
        setTimeout('KT_popups[\'' + popupId + '\']._hide()', (showTime + (initialDelay > 0 ? initialDelay : 0)) * 1000);
    }

    this._closeLink = document.getElementById(closeLinkId);
    if (this._closeLink) {
        this._closeLink.onclick = function() {
            KT_popups[popupId]._hide();
        };
        if (closeLinkDelay > 0) {
            setTimeout('KT_popups[\'' + popupId + '\']._showCloseLink()', (closeLinkDelay + (initialDelay > 0 ? initialDelay : 0)) * 1000);
        }
    }

    if (showAgainAfter) {
        this._createCookie('KT_popup_' + popupId, 1, showAgainAfter);
    }
}

KT_popup.prototype._show = function() {
    this._popup.style.visibility = 'visible';
    this._popupCurPos = this._getRequiredPos();
    this._moveTo(this._popupCurPos);
    this._visible = true;
}

KT_popup.prototype._showCloseLink = function() {
    this._closeLink.style.visibility = 'visible';
}

KT_popup.prototype._hide = function() {
    this._popup.style.visibility = 'hidden';
    this._popupCurPos = [-10000, -10000];
    this._moveTo(this._popupCurPos);
    this._visible = false;
}

KT_popup.prototype._animatePopup = function() {
    if (!this._visible) {
        return;
    }
    var rpos = this._getRequiredPos();

    var stepY = Math.abs(this._popupCurPos[1] - rpos[1]) / 7;
    if (stepY < 2) {
        stepY = 2;
    }
    var stepX = Math.abs(this._popupCurPos[0] - rpos[0]) / 7;
    if (stepX < 2) {
        stepX = 2;
    }
    if (this._popupCurPos[1] > rpos[1]) {
        this._popupCurPos[1] = this._popupCurPos[1] - stepY > rpos[1] ? this._popupCurPos[1] - stepY : rpos[1];
    } else {
        this._popupCurPos[1] = this._popupCurPos[1] + stepY < rpos[1] ? this._popupCurPos[1] + stepY : rpos[1];
    }
    if (this._popupCurPos[0] > rpos[0]) {
        this._popupCurPos[0] = this._popupCurPos[0] - stepX > rpos[0] ? this._popupCurPos[0] - stepX : rpos[0];
    } else {
        this._popupCurPos[0] = this._popupCurPos[0] + stepX < rpos[0] ? this._popupCurPos[0] + stepX : rpos[0];
    }
    this._moveTo(this._popupCurPos);
}

KT_popup.prototype._moveTo = function(pos) {
    this._popup.style.left = pos[0] + 'px';
    this._popup.style.top = pos[1] + 'px';
    this._popup.style.visibility = 'visible';

    if (this._isIE()) {
        this._iframe.style.left = pos[0] + 'px';
        this._iframe.style.top = pos[1] + 'px';
    }
}

KT_popup.prototype._isIE = function() {
    return (navigator.userAgent.indexOf("MSIE") != -1) && (navigator.userAgent.indexOf("Opera") == -1);
}

KT_popup.prototype._getRequiredPos = function() {
    var size = this._getDocumentSize();
    var scroll = this._getBodyScroll();
    var x = (size[0] - this._popup.offsetWidth) / 2 + scroll[0];
    var y = (size[1] - this._popup.offsetHeight) / 2 + scroll[1];
    return [x, y];
}

KT_popup.prototype._getDocumentSize = function() {
    var w = 0;
    var h = 0;
    if (window.innerWidth) {
        w = window.innerWidth;
        h = window.innerHeight;
    } else if (document.documentElement && document.documentElement.clientWidth) {
        w = document.documentElement.clientWidth;
        h = document.documentElement.clientHeight;
    }
    return [w, h];
}

KT_popup.prototype._getBodyScroll = function() {
    var scrOfX = 0;
    var scrOfY = 0;
    if (document.body && (document.body.scrollLeft || document.body.scrollTop)) {
        scrOfY = document.body.scrollTop;
        scrOfX = document.body.scrollLeft;
    } else if (document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop)) {
        scrOfY = document.documentElement.scrollTop;
        scrOfX = document.documentElement.scrollLeft;
    }
    return [scrOfX, scrOfY];
}

KT_popup.prototype._createCookie = function(name, value, seconds) {
    var expires = '';
    if (seconds) {
        var date = new Date();
        date.setTime(date.getTime() + (seconds * 1000));
        expires = '; expires=' + date.toGMTString();
    }
    document.cookie = name + '=' + value + expires + '; path=/';
}

KT_popup.prototype._readCookie = function(name) {
    var nameEQ = name + '=';
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') {
            c = c.substring(1, c.length);
        }
        if (c.indexOf(nameEQ) == 0) {
            return c.substring(nameEQ.length, c.length);
        }
    }
    return null;
}

KT_popup.Z_INDEX = 10000;
