//******************************************************************************
//
// 
// ============================================
//
// 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) {
		voffset=20; //for multiple rows displaying
    oDiv = document.createElement("div");
    oDiv.id = "container";
    oDiv.style.zIndex = 0;
    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 + voffset + "px";
    oDiv.style.width = (this.width +11) + "px"; //align scrollbars
    //var adjustment=190;
    var adjustment=160;
    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");
      oImg = document.createElement("img");
      oTab.style.position = "absolute";
      //breakline=(this.tabs.length/this.nRows)-(0.5);
      //breakline=(this.tabs.length/this.nRows)-(this.tabs.length%this.nRows);
      breakline=Math.floor(this.tabs.length/this.nRows);
      //alert(this.tabs.length);
      //alert(this.nRows);
      //alert(this.tabs.length/this.nRows);
      
      //alert(breakline);
      if(index<breakline){
     // this.tab_width=
      oTab.style.left = index * ( this.tab_width1 + this.spacing_width) + "px";
      oTab.style.width = this.tab_width1 + "px";
      oImg.style.width = this.tab_width1 + "px";
    	}else{
     // oTab.style.left = (index-(breakline)) * ( this.tab_width2 + this.spacing_width) + "px";
      oTab.style.left = (index-(breakline)) * ( this.tab_width2 + (this.spacing_width/2)) + "px";
      oTab.style.top = (this.tab_height) + "px";
      oTab.style.width = this.tab_width2 + "px";
      oImg.style.width = this.tab_width2 + "px";
    	}      
      oTab.style.height = this.tab_height + "px";
      oTab.id = "tab_"+name;
      oTab.name = name;
      oTab.style.zIndex = 5;
      //oTab.style.fontFamily="Verdana, Times, serif";
      //oTab.style.font-size=12;
      //oTab.style.font-weight="bold";

      
      oImg.style.position = "absolute";
      oImg.parent = this;
      oImg.id = "img_"+name;
      oImg.name = name;
      oImg.style.left = 0;
      oImg.style.top = 0;
      
      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.style.position = "absolute";
      oTxt.style.left = this.text_padding + "px";
      oTxt.style.top = this.text_padding + "px";
      oTxt.innerHTML = this.tabs[index].title;
     // oTxt.style.fontFamily="Verdana, Times, serif";
     //oTxt.style.size="6px";
     //oTxt.style.weight="bold";
     
      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>";     
    }
    //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, nRows) {
  
  // module variables
  this.parent = parent;

  //this.backgroundcolor = "#F2F2F2";
  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.nRows=nRows;

	//nRows=2;
  //this.tab_width = 263;
  this.tab_width = this.width / (numTabs+ 0.25)*nRows;
  a=Math.floor(numTabs/nRows);//width for upper tabs
  b=Math.ceil(numTabs/nRows);//width for lower tabs
  
  this.tab_width1=this.width/ (a+0.0);
  this.tab_width2=this.width/ (b+0.0);

  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();
  
}

