﻿if (!SIGI) throw ("SIGI.ContentContainer depends on the following scripts. Please make sure these are included.\nSIGI.js");

var maxItemsDefault = 6;

// === SIGI.ContentContainer === //
SIGI.ContentContainer = function() {
    this._initialDisplay = "";
    this._xslStyleSheet = "";
    this._dataBlockName = "";
    this._xslDOM = null;
    this._selectedIndex = -1;
    this._maxItemsDisplayed = 0;
    this._firstIndexDisplayed = 0;
    this._lastIndexDisplayed = 0;
    this._rotationInterval = 0;
    this._count = 0;
    this._maxIndex = 0;
    this._intervalID = 0;
    this._scrollDelay = 0;
    this._scrollIncrementHeight = 2;
    this._scrollIncrementDelay = 50;
    this._scrollControlHeight = 0;
    this._currentScrollLocation = 0;
    this._scrollingActive = false;
    this._containerCookieName = "";
};

SIGI.ContentContainer.inherits(SIGI.UI.Control);

SIGI.ContentContainer.prototype.get_index = function() {
    return this._selectedIndex;
};

SIGI.ContentContainer.prototype.set_index = function(value) {
    this._selectedIndex = value;
    this.refresh();
};

SIGI.ContentContainer.prototype.set_count = function(value) {
    this._count = value;
    this._maxIndex = this._count - 1;
};

SIGI.ContentContainer.prototype.set_maxItemsDisplayed = function() {
    var max = Number(this.get_element().getAttribute("maxItemsDisplayed"));
    if (max > 0) {
        this._maxItemsDisplayed = max;
    }
    else {
        this._maxItemsDisplayed = -1;
    }
}

SIGI.ContentContainer.prototype.set_maxRotationCount = function() {
    var rotCount = Number(this.get_element().getAttribute("rotationCount"));
    if (rotCount > 0) {
        this.set_count(rotCount);
    }
    else {
        this.set_count(maxItemsDefault);
    }
}

SIGI.ContentContainer.prototype.set_scrollValues = function() {
    var x = Number(this.get_element().getAttribute("scrollDelay"));
    if (x > 0) {
        this._scrollDelay = x * 1000;
        this.set_scrollHeight();
        x = 0;
        x = Number(this.get_element().getAttribute("scrollIncrementHeight"));
        if (x > 0) {
            this._scrollIncrementHeight = x;
        }
        x = 0;
        x = Number(this.get_element().getAttribute("scrollIncrementDelay"));
        if (x > 0) {
            this._scrollIncrementDelay = x;
        }
    }
}

SIGI.ContentContainer.prototype.set_maxRotationCount = function() {
    var rotCount = Number(this.get_element().getAttribute("rotationCount"));
    if (rotCount > 0) {
        this.set_count(rotCount);
    }
    else {
        this.set_count(maxItemsDefault);
    }
}

SIGI.ContentContainer.prototype.set_maxRotationCount = function() {
    var rotCount = Number(this.get_element().getAttribute("rotationCount"));
    if (rotCount > 0) {
        this.set_count(rotCount);
    }
    else {
        this.set_count(maxItemsDefault);
    }
}

SIGI.ContentContainer.prototype.set_RotationInterval = function() {
    var timeout = Number(this.get_element().getAttribute("rotationInterval"));
    if (timeout > 0) {
        code = "$SIGIfind('" + this.get_id() + "').rotate()";
        this._intervalID = setInterval(code, timeout * 1000);
    }
}

SIGI.ContentContainer.prototype.set_scrollHeight = function() {
    this._scrollControlHeight = this.get_element().offsetHeight;
    if (this._scrollControlHeight == 0) {
        this._scrollControlHeight = 200;
    }
    var divi = parseInt(this._scrollControlHeight / this._scrollIncrementHeight);
    this._scrollControlHeight = divi * this._scrollIncrementHeight;
}

SIGI.ContentContainer.prototype.display_prev = function() {
    incr = 0;
    if (this._firstIndexDisplayed == 0) {
        this._lastIndexDisplayed = this._maxIndex;
        this._firstIndexDisplayed = this._maxIndex + 1;
        while (true) {
            this._firstIndexDisplayed--;
            incr++;
            if ((incr == this._maxItemsDisplayed) || (this._firstIndexDisplayed == 0)) {
                break;
            }
        }
    }
    else {
        while (true) {
            this._firstIndexDisplayed--;
            this._lastIndexDisplayed--;
            incr++;
            if ((incr == this._maxItemsDisplayed) || (this._firstIndexDisplayed == 0)) {
                break;
            }
        }
    }
    this._selectedIndex = this._firstIndexDisplayed;
    this.refresh();
};

SIGI.ContentContainer.prototype.display_next = function() {
    incr = 0;
    if (this._lastIndexDisplayed == this._maxIndex) {
        this._firstIndexDisplayed = 0;
        this._lastIndexDisplayed = -1;
        while (true) {
            this._lastIndexDisplayed++;
            incr++;
            if ((incr == this._maxItemsDisplayed) || (this._lastIndexDisplayed == this._maxIndex)) {
                break;
            }
        }
    }
    else {
        while (true) {
            this._firstIndexDisplayed++;
            this._lastIndexDisplayed++;
            incr++;
            if ((incr == this._maxItemsDisplayed) || (this._lastIndexDisplayed == this._maxIndex)) {
                break;
            }
        }
    }
    this._selectedIndex = this._firstIndexDisplayed;
    this.refresh();
};

SIGI.ContentContainer.prototype.rotate = function() {
    if (this._selectedIndex >= this._maxIndex) {
        this._selectedIndex = 0;
        this._firstIndexDisplayed = 0;
        this._lastIndexDisplayed = this._maxItemsDisplayed - 1;
    }
    else {
        this._selectedIndex++;
        if (this._selectedIndex > this._lastIndexDisplayed) {
            //retain and reset selected index after display_next, which will break that value
            temp = this._selectedIndex;
            this.display_next();
            this._selectedIndex = temp;
        }
    }
    this.refresh();
};

SIGI.ContentContainer.prototype.delayRotationInterval = function(delay) {
    if (this._intervalID > 0) {
        clearInterval(this._intervalID);
        code = "$SIGIfind('" + this.get_id() + "').set_RotationInterval()";
        this._intervalID = setTimeout(code, delay * 1000);
    }
}

SIGI.ContentContainer.prototype.refresh = function() {

    //resolve index with cookie
    var cookieName = "SIGI.ContentContainerPosition_" + this.get_id();
    var cookieVar = "";
    if (this._selectedIndex == -1) {
        cookieVar = getCookie(this._containerCookieName);
        if (cookieVar != "") {
            this._selectedIndex = Number(cookieVar);
        }
    }
    if (this._selectedIndex == -1) {
        this._selectedIndex = 0;
    }
    if (this._selectedIndex > this._maxIndex) {
        this._selectedIndex = this._firstIndexDisplayed;
    }
    setCookie(this._containerCookieName, this._selectedIndex.toString());

    //refresh transform
    var vars = "index:" + this._selectedIndex;
    vars = vars + ";maxItemsDisplayed:" + this._maxItemsDisplayed;
    vars = vars + ";ctlId:" + this.get_id();
    vars = vars + ";minIndex:" + this._firstIndexDisplayed;
    vars = vars + ";maxIndex:" + this._lastIndexDisplayed;
    SIGI.XML.transform(this._dataBlockName, this._xslStyleSheet, this.get_id(), null, vars);
};

SIGI.ContentContainer.prototype.scrollStorySet = function() {
    this.get_element().scrollTop = this._currentScrollLocation;
}

SIGI.ContentContainer.prototype.scrollStory = function() {
    if (this._scrollingActive) {
        return;
    }
    this._scrollingActive = true;
    code = "$SIGIfind('" + this.get_id() + "').scrollStoryIn()";
    this._intervalID = setInterval(code, this._scrollIncrementDelay);
}

SIGI.ContentContainer.prototype.scrollStoryIn = function() {
    this._currentScrollLocation += this._scrollIncrementHeight;
    if (this._currentScrollLocation > this._scrollControlHeight) {
        clearInterval(this._intervalID);
        code = "$SIGIfind('" + this.get_id() + "').scrollStoryOutIncrement()";
        this._intervalID = setTimeout(code, this._scrollDelay);
        this._scrollingActive = false;
    }
    this.scrollStorySet();
}

SIGI.ContentContainer.prototype.scrollStoryOutIncrement = function() {
    code = "$SIGIfind('" + this.get_id() + "').scrollStoryOut()";
    this._intervalID = setInterval(code, this._scrollIncrementDelay);
}

SIGI.ContentContainer.prototype.scrollStoryOut = function() {
    this._currentScrollLocation += this._scrollIncrementHeight;
    if (this._currentScrollLocation > (this._scrollControlHeight * 2)) {
        this._currentScrollLocation = 0;
        this.rotate();
        clearInterval(this._intervalID);
        code = "$SIGIfind('" + this.get_id() + "').scrollStoryIn()";
        this._intervalID = setInterval(code, this._scrollIncrementDelay);
    }
    this.scrollStorySet();
}

SIGI.ContentContainer.prototype.initialize = function() {
    this._initialDisplay = this.get_element().style.display;
    this._xslStyleSheet = this.get_element().getAttribute("xslstylesheet");
    this._containerCookieName = this.get_element().getAttribute("idByPortal");
    this._dataBlockName = this.get_element().getAttribute("dataBlockName");
    this.set_maxItemsDisplayed();
    this.set_maxRotationCount();
    if (this._maxItemsDisplayed == -1) {
        this._maxItemsDisplayed = this._count;
    }
    this.set_RotationInterval();
    this.set_scrollValues();
    this._lastIndexDisplayed = this._maxItemsDisplayed - 1;
    this.refresh();
    if (this._scrollDelay > 0) {
        code = "$SIGIfind('" + this.get_id() + "').scrollStoryIn()";
        this._intervalID = setInterval(code, this._scrollIncrementDelay);
    }
};

