//******************************************************************************
//
// 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;
    oDiv.style.width = this.width;
    oDiv.style.height = this.height - this.tab_height - this.spacing_height;
    
    html = "";

    for (var index=0; index<this.tabs.length; index++) {
      
      // properties
      var name = this.tabs[index].name;
      
      // tab button
      oTab = document.createElement("div");
      oTab.className="tab";
      oTab.style.left = this.width - ( (index+1) * ( this.tab_width + this.spacing_width));
      oTab.style.width = this.tab_width;
      oTab.style.height = this.tab_height;
      oTab.id = "tab_"+name;
      oTab.name = name;

      oImg = document.createElement("img");
      oTab.className="tabimage";
      oImg.parent = this;
      oImg.id = "img_"+name;
      oImg.name = name;
      oImg.style.left = 0;
      oImg.style.top = 0;
      oImg.style.width = this.tab_width;
      oImg.style.height = this.tab_height;
      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.className = "tabtitle";
      oTxt.style.left = this.text_padding;
      oTxt.style.top = this.text_padding;
      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);

      // 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>";

    }
    
    // load iframes
    oDiv.innerHTML = html;

    // set active tab
    oParent.appendChild(oDiv);
    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 (!el.src) 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, activate) {

  document.frames[name].src = src;
  if (activate) this.activate(name);
  return false;
  
  el = document.getElementById(name);
  if (el) {
    el.src = src;
    if (activate) this.activate(name);
  }
  
}

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 = "#f0f0eb";
  this.bordercolor = "#888";

  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 = 179;
  this.tab_height = 25;
  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();
  
}