//******************************************************************************
//
// 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() {

  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;
    oDiv.style.top = this.tab_height + this.spacing_height + "px";
    oDiv.style.width = this.width + "px";
    oDiv.style.height = this.height - this.tab_height - this.spacing_height + "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 = 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.style.position = "absolute";
      oTxt.style.left = this.text_padding + "px";
      oTxt.style.top = this.text_padding + "px";
      oTxt.innerHTML = this.tabs[index].title;
      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.className = "iframeTabs";
      oIframe.id = name;
      oIframe.name = name;
      oIframe.style.position = "absolute";
      oIframe.style.border = "0px solid #000000";
      oIframe.frameborder = "0px";
      oIframe.scrolling = "no"
      oIframe.style.left = this.padding + "px";
      oIframe.style.top = this.padding + "px";
      oIframe.style.display = "";
      oIframe.style.width = parseInt(oDiv.style.width) - 2 * this.padding + "px";
      oIframe.style.height = parseInt(oDiv.style.height) - 2 * this.padding + "px";
      
      
      oDiv.appendChild(oIframe);
 */
 
       // iframe code is generated this way cause IE does not recognize IDs from dynamically created iframes!
      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: "+(parseInt(oDiv.style.width) - 2 * this.padding)+"px; ";
      html += "height: "+(parseInt(oDiv.style.height) - 2 * this.padding)+"px; display: \"\"; ' ></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);
    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, numTabs) {
  
  // module variables
  this.parent = parent;

  this.backgroundcolor = "#ffffff";
  this.bordercolor = "#808080";

  this.width = width - 2; // 2 pixels for border
  this.height = Height - 2; // 2 pixels for border
  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 +1);
  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();
  
}
