var Tabs = Class.create();
Tabs.Instance = null;

Tabs.GetLog = function()
{
    //return jslog;
    return nulllog;
};
Tabs.DefaultOptions = 
{
    className:"tabHeader",
    classActiveName:"tabHeaderActive",
    tabContentArea:"tabContent"
};

Tabs.prototype = 
{
    initialize:function(options)
    {
        this.log = Tabs.GetLog();
        this.log.info("Inicializando...");
        
        this.options = Object.extend(Tabs.DefaultOptions, options);
        var activeName = this.options.classActiveName;
        var tabClassName = this.options.className;
        
        this.tabContentArea = $(this.options.tabContentArea);
           
        var elms = $$("."+activeName);
        if(elms.length >= 0)
        {
            this.currentSelectedElement = elms[0];
        } else 
        {
            this.currentSelectedElement = null;
        }
        this.log.info("Procurando tabs " + tabClassName); 
        this.tabs = $$("."+tabClassName);
        //this.log.info("tabs encontradas " + this.tabs.length); 
        //this.links = $$("."+tabClassName +" a");
        //this.tabs.invoke("observe", "click", this.clickTab.bind(this));
        //this.links.invoke("writeAttribute", "onclick", "javascript:return false;");
        this.drawTabs(this.currentSelectedElement);
        
    },
/*    clickTab:function(elm)
    {
		alert("abrindo tab");
		try{
			this.log.info("clickou no link" + elm.href);
			var url = elm.getAttribute("href");
			var tabActive = elm.up().id;
			this.openTab(tabActive, url);
			Effect.ScrollTo('abas');
		} catch(e)
		{
			alert(e.message);
		}
        return false;
    },*/
    openTab:function(tabId, url)
    {
        this.log.info("Sumindo conteudo...");
        $(this.tabContentArea).hide();
        
        //Effect.Fade(this.tabContentArea, {duration: .5});
        new Ajax.Request(url, 
        {
          method: 'get',
          onSuccess: function(transport) 
          {
            this.log.info("Aparecendo conteudo...");
            this.tabContentArea.update(transport.responseText);
            Effect.Appear(this.tabContentArea, { duration: .5});
            this.drawTabs($(tabId));
          }.bind(this)
        });
        
        
    
    },
    drawTabs:function(selected)
    {
        if(selected == null) return;
        
        this.log.info("Desenhado tab" + selected.id);
        //jslog.info(selected);
                
        this.tabs.each( function(elm){
            this.log.info("verificando selecao para " + elm.id);
            if(selected != null && elm.id == selected.id){
                this.log.info("SELECIONAR " + elm.id + ": " + this.options.classActiveName);
                elm.addClassName(this.options.classActiveName);
				this.currentSelectedElement = elm;
            }else{
                this.log.info("tab nao selecionada " + elm.id);
                elm.removeClassName(this.options.classActiveName);
            }
        }.bind(this));
        
    }
    
}

document.observe("dom:loaded", function(){ var tb = new Tabs(); Tabs.Instance = tb; });


/*

function toggleDisp() {
    for (var i=0;i<arguments.length;i++){
        var d = $(arguments[i]);
        if (d.style.display == 'none')
            d.style.display = 'block';
        else
            d.style.display = 'none';
    }
}
/*-----------------------------------------------------------
    Toggles tabs - Closes any open tabs, and then opens current tab
    Input:     1.The number of the current tab
                    2.The number of tabs
                    3.(optional)The number of the tab to leave open
                    4.(optional)Pass in true or false whether or not to animate the open/close of the tabs
    Output: none 
    ---------------------------------------------------------
function toggleTab(num,numelems,opennum,animate) {
    if ($('tabContent'+num).style.display == 'none'){
        for (var i=1;i<=numelems;i++){
            if ((opennum == null) || (opennum != i)){
                var temph = 'tabHeader'+i;
                var h = $(temph);
                if (!h){
                    var h = $('tabHeaderActive');
                    h.id = temph;
                }
                var tempc = 'tabContent'+i;
                var c = $(tempc);
                if(c.style.display != 'none'){
                    if (animate || typeof animate == 'undefined')
                        Effect.toggle(tempc,'appear',{duration:0.8, queue:{scope:'menus', limit: 3}});
                    else
                        toggleDisp(tempc);
                }
                
            }
        }
        var h = $('tabHeader'+num);
        if (h)
            h.id = 'tabHeaderActive';
        h.blur();
        var c = $('tabContent'+num);
        c.style.marginTop = '2px';
        if (animate || typeof animate == 'undefined'){
            Effect.toggle('tabContent'+num,'appear',{duration:1, queue:{scope:'menus', position:'end', limit: 3}});
        }else{
            toggleDisp('tabContent'+num);
        }
    }
}
*/
