tabSet = new Class.create();

tabSet.prototype = {
    initialize : function(tabset_code, box_id, channel, onDate)
    {
        this.tabset_code        = tabset_code;

        this.setTabsetBoxId(box_id);

        this.currentTabId       = -1;
        this.nextTabId          = -1;
        this.previousTabId      = -1;

        this.channel            = channel;
        this.onDate             = onDate;

        this.autoReloader       = false;
        this.doAutoReload       = false;
        this.autoReloadTime     = 5000;
        this.timeOut            = null;
        this.callAllowed        = true;

        this.scriptName         = '';
    },


    go : function()
    {
        if (this.container_obj == null) {
            this.container_obj = $(this.container_name);
        }

        if (this.header_obj == null) {
            this.header_obj = $(this.header_name);
        }

        this.reloadQueue();
    },


    setHtml : function(html)
    {
        if (this.container_obj == null) {
            return;
        }

        $(this.container_obj).update(html);
    },


    setHeaderHtml : function(html)
    {
        if (this.header_obj == null) {
            return;
        }

        $(this.header_obj).update(html);
    },


    pause : function()
    {
        window.clearTimeout(this.timeOut);
        this.doAutoReload = false;
    },


    play : function()
    {
        clearTimeout(this.timeOut);
        this.doAutoReload = this.autoReloader;
        this.playNextTab();
    },


    playOrPause : function()
    {
        if (! this.doAutoReload) {
            if (this.autoReloader) {
                this.play();
            }
        } else {
            this.pause();
        }
    },


    playNextTab : function()
    {
        this.getTab(this.nextTabId, 0, true);
    },


    reloadQueue : function()
    {
        if (this.doAutoReload) {
            this.timeOut = setTimeout(this.tabset_code + '.playNextTab()', this.autoReloadTime);
        }
    },


    getTab : function(tabId, shift, doReload)
    {
        if (typeof(shift) == "undefined") { var shift = 0; }
        if (typeof(doReload) != "boolean") { var doReload = false; }
        if (this.scriptName == '') { return; }

        if (! this.callAllowed) {
            return
        }
        this.callAllowed = false;

        var url = this.scriptName +
                (this.scriptName.indexOf("?") > 0 ? "" : "?") +
                '&method='          + encodeURIComponent('get_tab') +
                '&tabset_box_id='   + encodeURIComponent(this.tabsetBoxId) +
                '&channel='         + encodeURIComponent(this.channel) +
                '&on_date='         + encodeURIComponent(this.onDate) +
                '&tab_id='          + encodeURIComponent(tabId);

        var MyAjax = new Ajax.Request(
            url,
            {
                'method'        : 'get',
                'parameters'    : '',
                onComplete      : function(result) {
                    this.callAllowed = true;
                    this.getTab_callback(result, doReload);
                }.bind(this)
             }
        );
    },


    getTab_callback : function(result, doReload)
    {
        var callResult = result.responseText.evalJSON();

        if (typeof(callResult.err) != 'undefined' && callResult.err) {
            try {
                alert(callResult.message);
            } catch (e) {}
        }

        if (typeof(callResult.resource.tabHeader) != "undefined") {
            this.setHeaderHtml(callResult.resource.tabHeader);
        }

        if (typeof(callResult.resource.tabContent) != "undefined") {
            this.setHtml(callResult.resource.tabContent);
        }

        if (typeof(callResult.resource.currentTab) != "undefined") {
            this.setCurrentTab(callResult.resource.currentTab);
        }

        if (typeof(callResult.resource.nextTabId) != "undefined") {
            this.setNextTabId(callResult.resource.nextTabId);
        }

        if (typeof(callResult.resource.previousTabId) != "undefined") {
            this.setPreviousTabId(callResult.resource.previousTabId);
        }

        if (typeof(callResult.resource.tab_js_code) != "undefined") {
            eval(callResult.resource.tab_js_code);
        }

        if (doReload) {
            this.reloadQueue();
        } else {
            this.pause();
        }
    },


    getNextTab : function()
    {
        this.getTab(this.nextTabId);
    },


    getPreviousTab : function()
    {
        this.getTab(this.previousTabId);
    },


    setTabsetBoxId : function(id)
    {
        this.tabsetBoxId = id;

        this.container_name     = 'tabset_container_'   + this.tabsetBoxId;
        this.header_name        = 'tabset_header_'      + this.tabsetBoxId;

        this.container_obj      = null;
        this.header_obj         = null;
    },


    setScriptName : function(scriptName)
    {
        this.scriptName = scriptName;
    },


    setCurrentTab : function(tabId)
    {
        this.currentTabId = tabId;
    },


    setNextTabId : function(tabId)
    {
        this.nextTabId = tabId;
    },


    setPreviousTabId : function(tabId)
    {
        this.previousTabId = tabId;
    },


    setChannel : function(channel)
    {
        this.channel = channel;
    },


    setAutoReloader : function(autoReloader)
    {
        this.autoReloader = autoReloader;
        this.doAutoReload = autoReloader;
    },


    setAutoReloadTime : function(autoReloadTime)
    {
        this.autoReloadTime = autoReloadTime;
    }
}


tabset2 = new Class.create();
tabset2.prototype = {
    initialize : function(obj_name, div_box_id)
    {
        this.obj_name 			= obj_name;
        this.box_container_name = div_box_id;
        this.box_container_obj  = null;
    },

    fetch_tab : function(url)
    {
        var MyAjax = new Ajax.Request(
                url,
                {
                    'method' 		: 'post',
                    'parameters'	: null,
                    onComplete		: function(result) {
                        this.ajax_callback(result);
                    }.bind(this)
                 }
            );
    },

    ajax_callback : function(result)
    {
        if (this.box_container_obj == null) {
            this.box_container_obj = $(this.box_container_name);
        }

        var content = result.responseText.evalJSON();
        $(this.box_container_obj).update(content);
    }
}