//******************************************************************************
//
// AMB
// ============================================
//
// 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) {

  var tabs = this.tabs.length;
  this.tabs[tabs] = new Object();
  this.tabs[tabs].name = name;
  this.tabs[tabs].title = title;
  this.tabs[tabs].loaded = false;
  if (tabs==0) this.active = name;
  if (src) this.tabs[tabs].src = src;
  return true;
  
}

tab_manager.prototype.build = function() {
	
	var tmp;
	
  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 = "1px solid "+this.bordercolor;
    oDiv.style.left = 0;
    tmp = (this.separator) ? (this.tab_height + this.spacing_height + this.separator_height) : (this.tab_height + this.spacing_height);
    oDiv.style.top = tmp + "px";
    oDiv.style.width = this.width + "px";
    tmp = (this.separator) ? (this.height - this.tab_height - this.spacing_height - this.separator_height) : (this.height - this.tab_height - this.separator_height);
    oDiv.style.height = tmp + "px";
		oDiv.style.display = "block";
		
		if (this.separator){
	    oDivSep = document.createElement("div");
	    oDivSep.id = "tabSeparator";
	    oDivSep.style.zIndex = 5;
	    oDivSep.style.position = "absolute";
	    oDivSep.style.background = this.separator_backgroundcolor;
	    oDivSep.style.left = 0;
	    oDivSep.style.top = (this.tab_height) + "px";
	    oDivSep.style.width = this.width + "px";
	    oDivSep.style.height = this.separator_height + "px";
			oDivSep.style.fontSize = this.separator_height + "px";
			oDivSep.style.display = "block";
		}

		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 = 5;

      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 + "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; };
      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 = "txtTab_"+index;
      oTxt.style.position = "absolute";
      oTxt.style.left = this.text_padding_left + "px";;
      oTxt.style.top = this.text_padding_top + "px";;
      oTxt.innerHTML = "<b>"+this.tabs[index].title+"</b>";
      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);
/*
      oIframe = document.createElement("iframe");
      oIframe.id = name;
      oIframe.name = name;
      oIframe.style.position = "absolute";
      oIframe.style.border = "0px";
      oIframe.frameborder = "0px";
      oIframe.style.left = this.padding;
      oIframe.style.top = this.padding;
      oIframe.style.display = "";
      oIframe.style.width = parseInt(oDiv.style.width) - 2 * this.padding;
      oIframe.style.height = parseInt(oDiv.style.height) - 2 * this.padding;
      oDiv.appendChild(oIframe);
*/   




      // iframe code is generated this way cause IE does not recognize IDs from dynamically created iframes!
      html += "<iframe scrolling='no' id='"+name+"' name='"+name+"' allowtransparency='true' frameborder='no' ";
      html += "style='position: absolute; border: 0px; left: "+this.padding+"px; top: "+this.padding+"px; ";
      html += "width: "+(parseInt(oDiv.style.width) - 2 * this.padding)+"px; ";
      html += "height: "+(parseInt(oDiv.style.height) - 2 * this.padding)+"px; display: \"\";'"; 
      
      if (this.useMapOnLoad && name=='map'){
  	    html += "onload='_onloaded()'></iframe>";		
  	  } else {
	  		html += "></iframe>";		
  		}
/*
			if (name == "map"){
				hTrans = 30;
				hTransHeader = 10;
				tTransHeader = this.padding + 3;		
				tTrans = tTransHeader + hTransHeader;
				wTrans = 120;
				lTrans = this.padding + (parseInt(oDiv.style.width)) - wTrans - 38;
		
				 html += "<div id='map_trans_header' name='map_trans_header' style='background-color:#08516B; position: absolute; margin-top: "+tTransHeader+"px; margin-left: "+ lTrans +"px; width: "+ wTrans +"px; height: " + hTransHeader + "px; padding:1px;'></div>";
				 html += "<div id='map_trans' name='map_trans' style='position: absolute; margin-top: "+tTrans+"px; margin-left: "+ lTrans +"px; width: "+ wTrans +"px;' class='map_trans'>" + writeLegend() + "</div>";					
			}      
      */
    }
    //afegit
    // load iframes
    oDiv.innerHTML = html;    
    
    // set active tab
    oParent.appendChild(oDiv);
    if (this.separator) oParent.appendChild(oDivSep);
    
		
		
    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.setfocus = function(name) {
	el = document.getElementById(name);
	if (el){
		el.focus();
	}
}



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);
    if (el) el.style.zIndex = (this.tabs[index].name == name) ? 10 : 1;
    el = document.getElementById("img_"+this.tabs[index].name);
    if (el) el.src = (this.tabs[index].name == name) ? this.tab_active : this.tab_normal;
    this.active = name;
  }
}

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) {
  
  // module variables
  this.parent = parent;

  this.backgroundcolor = "#EBEBEB";
  this.bordercolor = "#EBEBEB";
	this.separator_backgroundcolor = "#C9D6DF";
  
  this.width = width - 2; // 2 pixels for border
  this.height = height - 2; // 2 pixels for border
  this.padding = 0;
  this.spacing_width = 1;
  this.spacing_height = -1;
  this.text_padding_top = 3;
  this.text_padding_left = 6;
  this.separator_height = 4;
  

  this.tab_width = 200;
  this.tab_height = 20;
  this.tab_normal = "img/tab.normal.png";
  this.tab_active = "img/tab.active.png";
  this.tab_hover = "img/tab.hover.png";

  this.active = false;
  
  this.separator = true;
  
  this.useMapOnLoad = false;

  // objects  
  this.tabs = Array();
  
}
