//******************************************************************************
//
// 
// ============================================
//
// Copyright (c) 2004 by Geodata Sistemas S.L.
// http://www.geodata.es
//
// Tab manager
//
// This program is free software. You can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License.
//
//******************************************************************************

tab_manager.prototype.add = function(name, title, src, onload) {

  var tabs = this.tabs.length;
  this.tabs[tabs] = new Object();
  this.tabs[tabs].name = name;
  this.tabs[tabs].title = title;
  this.tabs[tabs].titlediv = "<div class='tabs'>"+title+"</div>";  
 // this.tabs[tabs].titlebold = "<div style='font-family: Trebuchet MS; font-size: 14px; text-align=top;'>"+title+"</div>";    
  this.tabs[tabs].titlebold = "<div class='tabs_active'>"+title+"</div>";    
  this.tabs[tabs].loaded = false;
  if (tabs==0) this.active = name;
  if (src) this.tabs[tabs].src = src;
  if (onload) this.tabs[tabs].onload = onload;
  
  return true;
  
}

tab_manager.prototype.build = function() {

  oParent = document.getElementById(this.parent);
  if (oParent) {

    oDiv = document.createElement("div");
    oDiv.id = "container";
    oDiv.style.zIndex = 5;
    oDiv.style.position = "relative";
    oDiv.style.background = this.backgroundcolor;
    oDiv.style.border = "0px solid "+this.bordercolor;
    oDiv.style.left = 0;
    oDiv.style.top = this.tab_height + this.spacing_height + "px";
    oDiv.style.width = this.width + "px";
    var adjustment = 0;
    oDiv.style.height = this.height - this.tab_height - this.spacing_height - adjustment + "px";
		oDiv.style.display = "block";
		
		//afegit
		html = "";

    for (var index=0; index<this.tabs.length; index++) {
      
      // properties
      var name = this.tabs[index].name;
      
      // tab button
      oTab = document.createElement("div");
      oTab.style.position = "absolute";
      oTab.style.left = index * ( this.tab_width + this.spacing_width) + "px";
      oTab.style.width = this.tab_width + "px";
      oTab.style.height = this.tab_height + "px";
      oTab.id = "tab_"+name;
      oTab.name = name;
      oTab.style.zIndex = 5000;

      oImg = document.createElement("img");
      oImg.style.position = "absolute";
      oImg.parent = this;
      oImg.id = "img_"+name;
      oImg.name = name;
      oImg.style.left = 0;
      oImg.style.top = 0;
      oImg.style.width = this.tab_width - 1 + "px";
      oImg.style.height = this.tab_height + "px";
      oImg.src = this.tab_normal;
      
      oImg.onclick = function(e) { this.parent.activate(this.name); };
      //oImg.onmouseover = function(e) { if (this.name!=this.parent.active) this.src = this.parent.tab_hover; this.parent.activate(this.name);};
      //oImg.onmouseout = function(e) { this.src = (this.name==this.parent.active) ? this.parent.tab_active : this.parent.tab_normal; };
      oTab.appendChild(oImg);

      oTxt = document.createElement("div");
      oTxt.parent = oImg;
      oTxt.id = "txt_"+name;      
      oTxt.style.position = "absolute";
      oTxt.style.left = this.text_padding + "px";
      oTxt.style.top = this.text_padding + "px";
      oTxt.innerHTML = this.tabs[index].titlediv;
      
      oTxt.onclick = function(e) { this.parent.onclick(); };
      //oTxt.onmouseover = function(e) { this.parent.onmouseover(); };
      //oTxt.onmouseout = function(e) { this.parent.onmouseout(); };
      oTab.appendChild(oTxt);

      try {
        oImg.style.cursor = "pointer";
        oTxt.style.cursor = "pointer";
      } catch (e) {
        oImg.style.cursor = "hand";
        oTxt.style.cursor = "hand";
      }

      oParent.appendChild(oTab);

      // iframe code is generated this way cause IE does not recognize IDs from dynamically created iframes!
      var ample = parseInt(oDiv.style.width) +5;
      if (name == 'localizersTab') ample += 12;
      
      html += "<iframe id='"+name+"' name='"+name+"' allowtransparency='true' frameborder='no' ";
      html += "style='position: absolute; border: 0px; left: "+this.padding+"px; top: "+this.padding+"px; ";
      html += "width: "+ample+"px; ";
      html += "height: "+(parseInt(oDiv.style.height) - 2 * this.padding)+"px; display: \"\";'";
      if (this.tabs[index].onload) html += " onload='"+this.tabs[index].onload+"'";
      html += "></iframe>";     
    }
    //afegit
    // load iframes
    oDiv.innerHTML = html;   
    
    // set active tab
    oParent.appendChild(oDiv);
    for (var index=this.tabs.length; index>0; index--) {
    	this.activate(this.tabs[index-1].name);
    }
    
    //this.activate(this.active)
    return true;
    
  }

  return false;
  
}


tab_manager.prototype.activate = function(name) {
	
  for (var index=0; index<this.tabs.length; index++) {
    el = document.getElementById(this.tabs[index].name);
    if (el) {
      if (this.tabs[index].name == name) {
        el.style.display = "";
        if (!this.tabs[index].loaded) if (this.tabs[index].src) el.src = this.tabs[index].src;
        this.tabs[index].loaded = true;
      } else {
        el.style.display = "none";
      }
    }
    el = document.getElementById("tab_"+this.tabs[index].name);
    el.style.zIndex = 10;
    
    el = document.getElementById("img_"+this.tabs[index].name);
    if (el) el.src = (this.tabs[index].name == name) ? this.tab_active : this.tab_normal;

    el = document.getElementById("txt_"+this.tabs[index].name);
		if (el){
    	if (this.tabs[index].name == name){
    		el.innerHTML = this.tabs[index].titlebold;
			}
    	else{
    		el.innerHTML = this.tabs[index].titlediv;
    	}
		}
    this.active = name;
  }
  /*
  if (name == "managerTab"){
  	var el = document.getElementById(name)
  	el.contentWindow.location.reload(true);
  }
  */

}


tab_manager.prototype.src = function(name, src) {

  el = document.getElementById(name);
  if (el) el.src = src;
  
}


tab_manager.prototype.get_tab_index = function(name) {
  
  for (var index=0; index<this.tabs.length; index++)
    if (this.tabs[index].name == name) return index;
  return false;
  
}


//******************************************************************************
// constructor
//******************************************************************************

function tab_manager(parent, width, Height, numTabs) {
  
  // module variables
  this.parent = parent;

  this.backgroundcolor = "ffffff";
  this.bordercolor = "#808080";

  this.width = width - 20; 
  this.height = Height - 10;
  this.padding = 10;
  this.spacing_width = 1;
  this.spacing_height = -1;
  this.text_padding = 6;

  //this.tab_width = 263;
  this.tab_width = this.width / (numTabs-0.151);
  this.tab_height = 24; //23
  this.tab_normal = "img/tab.normal.png";
  this.tab_active = "img/tab.active.png";
  this.tab_hover = "img/tab.hover.png";

  this.active = false;

  // objects  
  this.tabs = Array();
  
  this.first = true;
  
}