/* globals */
var toplinks = new Array(); /* storage for TabLinks */
var sublinks = new Array();
var comdef; /* default TabLink object */
var parentid; /* selected tab */
var sublinkid; /* selected sub elements */

function TabLink(id, text, showlink){
         this.id = id;
	 this.link = '<a href="javascript:makeCall(\''+id+'\')" id="'+id+'tab">'+text+'</a>';
         this.text = '<b>'+text+'</b>';
         this.container = document.getElementById('def'+id);
        
         if (showlink){
             this.container.innerHTML = this.link;
             this.istext = false;
         }
         else {
             this.container.innerHTML = this.text;
             this.istext = true;
         }
}

TabLink.prototype.switchContent= function(o){
       if (o && o == 'link'){ this.container.innerHTML = this.link; this.istext = false; }
       else { this.container.innerHTML = this.text; this.istext = true; }
}

/* 'static' methods */

var Tabs = {
    setActiveTab:function(id){
	 if (toplinks[id]){ /* if top element */
	    if (id != parentid){ /* new top element -> switch */
	        comdef = toplinks[id];
	        parentid = comdef.id;
	    }
	    comdef.switchContent(); /* show default text */
	 }
         else if (comdef && comdef.istext){ /* if content is text, show link */
		  comdef.switchContent('link');
	 }

         if (sublinks[id]){
             if (sublinkid && sublinks[id]) /* switch content if already opened */ 
             {
                 sublinks[sublinkid].switchContent('link');
             }

             comdef.switchContent('link'); /* make sure parent is set to link */
             sublinks[id].switchContent();
             sublinkid = id;
         }
         else if (sublinkid){
              sublinks[sublinkid].switchContent('link');
              sublinkid = null;
         }
     },

     addTabLink:function(id, name, istop){
         if (istop){
             toplinks[id] = new TabLink(id, name);
         }
         else {
             sublinks[id] = new TabLink(id, name, true);
         }
     }
}

window.onunload = function(){
                  if (typeof cookien == 'undefined'){ return; }
                  if (typeof mapping == 'undefined'){ mapping = new Array(); }
                  if (!toplinks[mapping[section]]){
                      setCookie(cookien, section, 0);
                  }
                  else {
                      setCookie(cookien, sublinkid ? section+sublinkid : section);
                  }

                  var d = new Date();
                  setCookie(cookien+'tm', d.getTime(), 0);
                 }