﻿var Global = new NS_Global();

function NS_Global() {
    var debugMsg = null;

    var duration = 3;       //seconds
    this.setDuration = function(val) { duration = val; }
    function getDuration() { return duration * 1000; }

    var speed = 10;         //miliseconds
    this.setSpeed = function(val) { speed = val; }
    function getSpeed() { return speed; }
    
    var bgimage = null;
    this.setBgImage = function(val) { bgimage = val; }
    function getBgImage() { return bgimage; }
    
    var currentItemInd = 0; 
    var items = new Array();
    this.addItem = function(elemId) { items.push(new Item(elemId)); }
    function getItem() { return items[currentItemInd]; }
    function getPrevItem() {
        var prevIndx;
        if (items.length == 1) return null;
        if (items.length - 1 == currentItemInd) prevIndx = 0; 
        else if (currentItemInd == 0) prevIndx = items.length - 1;
        else prevIndx = currentItemInd - 1;
        return items[prevIndx]; 
    }
    function advanceItem() { if (items.length - 1 == currentItemInd) currentItemInd = 0; else currentItemInd++; }
    
    var container = null;
    var slideInt = null;
    var initiated = false;
    this.Initiate = function() { 
        debugMsg = new DebugMessage();  
        window.onresize = Resize;
        Resize();
        if (initiated) { restart(); return; }
        initiated = true;
        if (items.length > 1) {
            container = getItem().GetContainer();
            container.onmouseover = Global.Stop;
            container.onmousemove = Global.Stop;
            container.onmouseout = Global.Start;
            setTimeout(_start, getDuration());
        }
    }
    
    function restart() {
        _stop();
        isParking = false; 
        var currItem = null; 
        for (var i=0; i<container.childNodes.length; i++) {
            if (container.childNodes[i].nodeName == 'DIV') { currItem = container.childNodes[i]; break; }
        }
        var ind = 0; 
        for (var i=0; i<items.length; i++) { 
            if (items[i].GetElement() == currItem) { ind = i; break; }
        }
        currentItemInd = ind; 
        _start();
    }
    
    var resizeInt = null;
    function Resize() {
        if (resizeInt) clearTimeout(resizeInt);
        resizeInt = setTimeout(_resize, 10);
        function _resize() {
            var promoCtrl = getItem().GetContainer().parentNode; if (!promoCtrl) return;
            var winSize = GetWinSize();
            promoCtrl.style.height = ((winSize.y > 0) ? winSize.y : 1) + 'px';
            resizeInt = null;
        }
    }

    var isParking = false;
    function onStepHandler() { 
        var height = getItem().GetHeight();
        var top = Number(container.style.top.replace('px',''));
        if (height > 0 && top < -height) {
            _stop();
            if (!isParking) setTimeout(_resetContainer, getDuration());
            isParking = true;
        }
    }
    
    function _resetContainer() {
        getItem().Replace();
        advanceItem();
        container.style.top = '-1px';
        isParking = false;
        _start();
    }

    function _start() { if (isParking) return; if (slideInt) _stop(); slideInt = Effects.SlideV(container, 1, getSpeed(), onStepHandler); }
    function _stop() { slideInt = Effects.SlideStop(slideInt); }

    this.Start = function() { _start(); }
    this.Stop = function() { _stop(); }
}

function Item(elemId) {
    var elem = null;
    function _getElement() { if (!elem) { elem = document.getElementById(elemId); } return elem; }

    this.Replace = function() { if (!_getElement()) return; _getElement().parentNode.appendChild(_getElement()); elem = null; }
    this.GetContainer = function() { return _getElement().parentNode; }
    this.GetHeight = function() { return _getElement().offsetHeight; }
    this.GetElement = function() { return _getElement(); }
}
function GetWinSize() {
    if (IsIE()) {
        var winSize = {x:document.documentElement.clientWidth, y:document.documentElement.clientHeight - 2 };
        if (winSize.y <= 0) 
            winSize = {x:document.body.offsetWidth, y:document.body.offsetHeight - 2 };
        return winSize;
    }
    else
        return {x:window.innerWidth - 20, y:window.innerHeight };
}
function IsIE() {
    return (navigator.appVersion.indexOf("MSIE") != -1) ? true : false;
}
