﻿/* Javascript by Daniel Cohen Gindi (c) danielgindi@gmail.com 054-5655765 */
/* Version: 2009-05-31 */

contentAutoMenu = {
    currentId: null,
    itemName: 'contentAutoMenu_item_',
    contentName: 'contentAutoMenu_content_',
    timerInterval: 7000,
    fadeLength: 500,
    fadeFps: 30,
    hasTimer: false,

    toggle: function(item, byTimer, callbackOnEnd) {
        if (!item) return;

        if (this.$IEFilter == undefined) this.$IEFilter = navigator.appVersion.indexOf('MSIE');

        if (this._duringFade) {
          if (item) this._nextItem = [item, callbackOnEnd];
          return;
        }
        this._duringFade = true;
        if (!byTimer) this.clearTimer();

        var elOld, elNew;

        var baseId = item.id.substr(this.itemName.length);
        if (parseInt(this.currentId, 10) == parseInt(baseId, 10)) {
            this._duringFade = false;
            if (callbackOnEnd) setTimeout(callbackOnEnd, this.fadeLength);
            return;
        }
        if (this.currentId !== null) {
            var elItem = document.getElementById(this.itemName + this.currentId);
            elOld = document.getElementById(this.contentName + this.currentId);
            if (elItem) elItem.className = elItem.className.split('_')[0];
        }
        var elItem = document.getElementById(this.itemName + baseId);
        elNew = document.getElementById(this.contentName + baseId);
        if (elItem) elItem.className = elItem.className + '_sel';
        this.currentId = baseId;

        if (elNew) {
            if (this.$IEFilter) elNew.style.filter = 'alpha(opacity=0)';
            elNew.style.opacity = '0';
            elNew.style.MozOpacity = '0';
            elNew.style.KhtmlOpacity = '0';
            elNew.style.display = 'block';
        }
        if (elOld) {
            if (this.$IEFilter) elOld.style.filter = 'alpha(opacity=100)';
            elOld.style.opacity = '1';
            elOld.style.MozOpacity = '1';
            elOld.style.KhtmlOpacity = '1';
        }

        var fadeStart = (new Date()).getTime();
        var fadeEnd = fadeStart + this.fadeLength;
        var fadeInterval = parseInt(1000 / this.fadeFps, 10);
        var thisObj = this;

        var _int = setInterval(function() {
            var curTime = (new Date()).getTime();
            var alpha = (curTime - fadeStart) / thisObj.fadeLength;
            if (alpha > 1) alpha = 1;

            if (elOld) {
                if (thisObj.$IEFilter) elOld.style.filter = 'alpha(opacity=' + ((1 - alpha) * 100) + ')';
                elOld.style.opacity = 1 - alpha;
                elOld.style.MozOpacity = elOld.style.opacity;
                elOld.style.KhtmlOpacity = elOld.style.opacity;
            }
            if (elNew) {
                if (thisObj.$IEFilter) elNew.style.filter = 'alpha(opacity=' + (alpha * 100) + ')';
                elNew.style.opacity = alpha;
                elNew.style.MozOpacity = elNew.style.opacity;
                elNew.style.KhtmlOpacity = elNew.style.opacity;
            }

            if (curTime >= fadeEnd) {
                thisObj._duringFade = false;
                clearInterval(_int);
                if (elOld) elOld.style.display = 'none';
                if (callbackOnEnd) callbackOnEnd();
                if (thisObj._nextItem) {
                  var nextItem = thisObj._nextItem;
                  delete thisObj._nextItem;
                  thisObj.toggle(nextItem[0], false, nextItem[1]);
                }
            }
        }, fadeInterval);
    },
    clearTimer: function() {
        if (this.hasTimer) this.stopTimer = true;
    },
    setTimer: function(list) {
        if (this.hasTimer && this.stopTimer) this.stopTimer = false;
        if (!this._FNexTip) {
            var thisObj = this;
            this._FNextTip = function() { thisObj.nextTip(); };
        }
        this.listTimer = list.split(',');
        while (this.listTimer[this.listTimer.length - 1] == '') this.listTimer.pop();
        if (!this.hasTimer) {
            this.stopTimer = false;
            setTimeout(this._FNextTip, this.timerInterval);
            this.hasTimer = true;
        }
    },
    nextTip: function() {
        if (this.stopTimer) {
            this.hasTimer = false;
            this.stopTimer = false;
            return;
        }

        var nextId = -1;
        for (var idx = 0; idx < this.listTimer.length; idx++) {
            if (this.listTimer[idx] == this.currentId) {
                if (idx == this.listTimer.length - 1) nextId = this.listTimer[0];
                else nextId = this.listTimer[idx + 1];
                break;
            }
        }

        var thisObj = this;
        function callback() {
            if (!thisObj.stopTimer) setTimeout(thisObj._FNextTip, thisObj.timerInterval);
        };

        if (nextId != -1) this.toggle(document.getElementById(this.itemName + nextId), true, callback);
    }
};
