
function Tab(id){

    var this_tab = this;
    
    clickedontab  = "";
    tabcontentIDs = new Array();
    ul = document.getElementById(id);
    li = ul.getElementsByTagName("li"); // Array contetente gli elementi LI all' interno del tag UL
    
    this.enabletabpersistence=0;       // Abilita la persistenza dei tab tramite cookies per mantere traccia dei tab selezionati
                                        // Settato a false per default
    
    this.class_tab         = "shadetabs";
    this.class_tab_content = "tabcontentstyle";
    this.selectedTab       = null;
    
    this.onchangetab = function(){  };
    
    function expandContent(a, self){
                
        for (var i=0; i< this.li.length; i++){
            li[i].className=""  // Deseleziona tutti i tabs
            //_id = this.li[i].id.slice(4);            
            //document.getElementById("chiusura_tcontent"+_id).src="/immagini/chiudi_off.jpg";
            
            if (typeof this.tabcontentIDs[i] != "undefined")                         // Se l' indice dell' array corrispondente al contenuto del tab esiste (exception: More tabs than there are tab contents)        
                document.getElementById(this.tabcontentIDs[i]).style.display="none" // Nasconde tutti i contenuti dei tab
        }
        
        a.parentNode.className="selected"  // highlight currently clicked on tab                
        document.getElementById(a.getAttribute("rel")).style.display="block" // Espande il contenuto del tab
        //document.getElementById("chiusura_"+linkobj.getAttribute("rel")).src="/immagini/chiudi_on.jpg";
        
        saveSelectedTabId(a.parentNode.id);
        saveSelectedTabContentId(a.getAttribute("rel"));    
    };
    
    function getCookie(Name){ 
        var re=new RegExp(Name+"=[^;]+", "i");                  // Crea una RegularExpression per cercare la coppia nome/valore
        if (document.cookie.match(re))                          // Se il coockie e' stato trovato
            return document.cookie.match(re)[0].split("=")[1]; // ritorna il valore
        return "";
    };
    
    function getUlListLinkById(tabcontentid) { // returns a tab link based on the ID of the associated tab content        
        for( var i=0; i < li.length; i++){
            if( li[i].getElementsByTagName("a")[0].getAttribute("rel") == tabcontentid){
                return li[i].getElementsByTagName("a")[0]
                break
            }
        }
    };
    
    function saveTabContentIds(relattribute){ // Salva gli id dei divs tab content
        tabcontentIDs[tabcontentIDs.length]=relattribute;
    };

    this.removeGarbage = function() {        
        for(var i=0 ; i < li.length ; i++ ) {
            _id = li[i].id.slice(4);
            if(_id==0) ul.removeChild(li[i]);
        }        
    };
    
    
    function saveSelectedTabId(selectedtabid){
        this_tab.selectedTab = selectedtabid;
    };
    
    function saveSelectedTabContentId(selectedtabid){ //set id of clicked on tab as selected tab id & enter into cookie        
        if ( this.enabletabpersistence==1) //if persistence feature turned on
            setCookie(ul.id, selectedtabid);
    };
    
    function setCookie(name, value){
        document.cookie = name+"="+value //cookie value is domain wide (path=/)
    };
        
    this.Init = function() {
    
        var index = 0;
        if(arguments.length==1) index = parseInt(arguments[0]);

        //ul.setAttribute("class", this.class_tab);
        
        for (var i=0; i < li.length; i++){ //loop attraverso ciascun elemento LI
            var a = li[i].getElementsByTagName("a")[0];
            if( a.getAttribute("rel") ){  // Deve essere definito il Tag A all' interno di ciascun tag LI
            
                saveTabContentIds( a.getAttribute("rel") ); // Salva l' ID di ciscun tab content
                
                a.onclick =  function() { expandContent(this); this_tab.onchangetab(); return false;}
                //a.onclick =  this.onchangetab;
                
                if( li[i].className=="selected" && clickedontab=="") // Se un tab e' inizializzato 'selected' per default
                    expandContent(a);                                 // carica automaticamente il tab corrente selezionato
            }
            
        }
        
        if( clickedontab != "" ){                // if a tab has been previously clicked on per the cookie value
            var culistlink=getUlListLinkById(clickedontab);
            if (typeof culistlink!="undefined")
                expandContent(culistlink);       // auto load currenly selected tab content
        } else {                                //  else if no match found between tabcontent id and rel attribute value (cookie mis-association)
            if( li.length > 0 )
                expandContent( li[index%li.length].getElementsByTagName("a")[0]); //just auto load first tab instead
                this.onchangetab();
        }
            
    };
    
}
