//fuggosegek: 
if (typeof BROWSERDETECT == "undefined") {
  alert("(TABBEDPANE) BROWSERDETECT include missing");
}
//tobbszoros include:
if (typeof TABBEDPANE != "undefined") {
  alert("TABBEDPANE multiple insert!");
}
TABBEDPANE=true;


/** Fules elrendezesu kontener osztalya
  * @param config parameterek objektuma
  */
function TabbedPane(config) {

  this.tabs=new Array();
  this.tabCount=0;  //ez lehetne this.tabs.count is
  this.classes={};
  this.selected=-1;

  this.setDefaults();
  this.readConfig(config);

  if (this.name == "") {
    throw new Error("tabbedpane: name undefined");
  }

  TabbedPane.addInstance(this.name,this);
}

/** Alapertelmezett ertekek beallitasa
  */
TabbedPane.prototype.setDefaults = function () {
  this.name="";
  this.direction='N';
  this.align='W';
  this.display=0;
  this.autoWidth=0;
  this.autoSep=0;
  this.parentNode=null;
  this.beforeNode=null;
  this.classes.tabContent="tabContent";
  this.classes.tabbedPane="tabbedPane";
  this.classes.tabDiv="tabDiv";
  this.classes.tabVerticalSep="tabVerticalSep";
  this.classes.tabHorizontalSep="tabHorizontalSep";
  this.classes.tabN="tabN";
  this.classes.tabW="tabW";
  this.classes.tabS="tabS";
  this.classes.tabE="tabE";
  this.classes.tabSelectedN="tabSelectedN";
  this.classes.tabSelectedW="tabSelectedW";
  this.classes.tabSelectedS="tabSelectedS";
  this.classes.tabSelectedE="tabSelectedE";
  
}

/** Az ervenyes parameterekkel felulirja az alapertelmezett beallitasokat
  * @param config parameterek objektuma
  */
TabbedPane.prototype.readConfig = function (config) {
  if (typeof config.name == 'string') {
    this.name=config.name;
  }
  if (typeof config.direction == 'string' 
      && (config.direction == 'N' || config.direction == 'E' 
         || config.direction == 'S' || config.direction == 'W' )) {
    this.direction=config.direction;
  }
  if (typeof config.align == 'string' 
      && (config.direction == 'N' || config.direction == 'E' 
         || config.direction == 'S' || config.direction == 'W' || config.direction == 'CENTER')) {
    this.align=config.align;
  } 
  if (typeof config.autoSep == 'number' && 0 <= config.autoSep && config.autoSep <= 1) {
    this.autoSep=config.autoSep;
  } 
  if (typeof config.autoWidth == 'number' && 0 <= config.autoWidth && config.autoWidth <= 1) {
    this.autoWidth=config.autoWidth;
  } 
  if (typeof config.display == 'number' && 0 <= config.display && config.display <= 1) {
    this.display=config.display;
  }

  if (typeof config.parentNode == "object" && null != config.parentNode) {
    this.parentNode=config.parentNode;
  }
  if (typeof config.beforeNode == "object" && null != config.beforeNode) {
    this.beforeNode=config.beforeNode;
  }

  if (typeof config.classes == 'object') {
    //le kell masolni az objektum tartalmat
    for (var i in config.classes) {
      this.classes[i]=config.classes[i];
    }    
  }
 
}

/** Szeparator elemet ad a kontenerhez
  */
TabbedPane.prototype.addSeparator = function () {
  this.tabs[this.tabCount] = {type: "sep"};
  this.tabCount++;
}
/** Egy node-ot ad a kontenerhez
  * @param tabLabel a ful szovege
  * @param divId a tartalom ID-ja
  */
TabbedPane.prototype.addTabDiv = function (tabLabel,divId) {
  this.tabs[this.tabCount] = {
                    label:        tabLabel, 
                    contentNode:  document.getElementById(divId), 
                    tabNode:      null,
                    type:         "tab"
  };
  this.tabCount++;
  if (this.autoSep == 1)  {
    this.addSeparator();
  }
}

/** Html kodot ad a kontenerhez
  * @param tabLabel a ful szovege
  * @param html a tartalom html 
  */
TabbedPane.prototype.addTabHtml = function (tabLabel,html) {
  var divId = this.name+"_tabContent_"+this.tabCount;
  document.write("<div class='"+this.classes['tabDiv']+"' id='"+divId+"'>"+html+"</div>");  
  this.tabs[this.tabCount] = {
                    label:        tabLabel, 
                    contentNode:  document.getElementById(divId), 
                    tabNode:      null,
                    type:         "tab"
  };
  this.tabCount++;
  if (this.autoSep == 1)  {
    this.addSeparator();
  }
}

/** Kivalasztja a fulet, a regi tartalmat elrejti, az ujet megjeleniti
  * @param index a ful indexe
  */
TabbedPane.prototype.select = function(index) {
  if (this.selected > -1) {
    //regi normalla alakitasa
    this.tabs[this.selected].tabNode.className=this.classes["tab"+this.direction];
    var contentDiv=this.tabs[this.selected].contentNode;
    if (this.display == 1) {
      contentDiv.style.display="none";
    } else {
      //felkerul 0,0 - ba
      contentDiv.style.top="0px";
      contentDiv.style.left="0px";
      contentDiv.style.visibility="hidden";
      contentDiv.style.position="absolute";    
    }
  }
  
  if (index < this.tabs.length) {
    //kivalasztott atallitasa
    this.selected=index;
    
    this.tabs[index].tabNode.className=this.classes["tabSelected"+this.direction];
    var contentDiv=this.tabs[this.selected].contentNode;
    if (this.display == 1) {
      contentDiv.style.display="block";
      contentDiv.style.visibility="visible";  
      contentDiv.style.position="relative";
      contentDiv.style.top="0px";
      contentDiv.style.left="0px";
    } else {
      contentDiv.style.top="0px";
      contentDiv.style.left="0px";
      contentDiv.style.position="relative";
      contentDiv.style.visibility="visible";  
    }
  }
}

/** Letrehozza a strukturat, majd kirajzolja
  */
TabbedPane.prototype.render = function() {
  document.write("<table class='"+this.classes['tabbedPane']+"' border='0' cellspacing='0' cellpadding='0' id='"+this.name+"_tabbedPane'>");
  if (this.direction == 'N')  {
    document.write("<tr>");
    if (this.align == 'N' || this.align == 'W') {
      document.write("<td align='left'>");
    } else if (this.align == 'CENTER') {
      document.write("<td align='center'>");
    } else if (this.align == 'S' || this.align == 'E') {
      document.write("<td align='right'>");
    }
    this.generateTabs();
    document.write("</td></tr><tr>");
  } else if (this.direction == 'W') {
    document.write("<tr>");
    if (this.align == 'N' || this.align == 'W') {
      document.write("<td valign='top'>");
    } else if (this.align == 'CENTER') {
      document.write("<td valign='center'>");
    } else if (this.align == 'S' || this.align == 'E') {
      document.write("<td valign='bottom'>");
    }
    this.generateTabs();
    document.write("</td>");
  } else if (this.direction == 'S') {
    document.write("<tr>");
  } else if (this.direction == 'E') {
    document.write("<tr>");
  }

  document.write("<td><div class='"+this.classes['tabContent']+"' id='"+this.name+"_tabContent'></div></td>");

  if (this.direction == 'N') {
    document.write("</tr>");
  } else if (this.direction == 'W') {
    document.write("</tr>");
  } else if (this.direction == 'S') {
    document.write("</tr><tr>");
    if (this.align == 'N' || this.align == 'W') {
      document.write("<td align='left'>");
    } else if (this.align == 'CENTER') {
      document.write("<td align='center'>");
    } else if (this.align == 'S' || this.align== 'E') {
      document.write("<td align='right'>");
    }
    this.generateTabs();
    document.write("</td></tr>");
  } else if (this.direction == 'E') {
    if (this.align == 'N' || this.align == 'W') {
      document.write("<td valign='top'>");
    } else if (this.align == 'CENTER') {
      document.write("<td valign='center'>");
    } else if (this.align == 'S' || this.align == 'E') {
      document.write("<td valign='bottom'>");
    }
    this.generateTabs();
    document.write("</td></tr>");
  }
  
  document.write("</table>");


  
  var tabbedPane=document.getElementById(this.name+"_tabbedPane");
  if (this.parentNode) {
    this.parentNode.insertBefore(tabbedPane,this.beforeNode);
  }

  
  //megvan a tablazat es a tabok, most jonnek a tartalmi divek
  var tabContent=document.getElementById(this.name+"_tabContent");
  var width=tabContent.offsetWidth;
  
  for (i=0 ; i < this.tabCount; i++) {
    if (this.tabs[i].type == "tab") {
      var contentDiv=this.tabs[i].contentNode;
      if (contentDiv.offsetWidth > width) {  //itt nem lehet display:none
        width=contentDiv.offsetWidth;
      }
      tabContent.appendChild(contentDiv);
      contentDiv.className=this.classes["tabDiv"];

      if (this.display == 1) {
        contentDiv.style.display='none';
      }
    }
  }
  if (this.autoWidth == 1) {
    tabContent.style.width=width;
//    alert(width);
  }
  tabContent.style.overflow="auto";

  //mozilla 1.6 nem csinalja jol az igazitast display eseten, firefox, opera es IE jo
  if (this.display == 1 && nsDOM) {
//    document.getElementById(this.name+"_tabbedPane").style.display="none";  
//    document.getElementById(this.name+"_tabbedPane").style.display="block";  
//    document.getElementById(this.name+"_tabbedPane").style.width=width;  
    if (this.direction == 'N' || this.direction == 'S') {
      //egesz tablara allitunk be szelesseget
//      document.getElementById(this.name+"_tabbedPane").style.width=width;  
    } else if (this.direction == 'E' || this.direction == 'W') {
      //csak tabcontent-re
//      tabContent.style.width=width;
    }
    
  }

//elsot kivalasztom
  this.select(0);
}

/** A fuleket hozza letre, esemenykezelot keszit kattintasra
  */
TabbedPane.prototype.generateTabs = function() {
  document.write("<table border='0' cellpadding='0' cellspacing='0' >");
  if (this.direction == 'N' || this.direction == 'S') {
    document.write("<tr>");
    var i=0;
    for (i=0 ; i < this.tabCount; i++) {
      var tab=this.tabs[i];
      if (tab.type == "tab") {
        document.write("<td><div id='"+this.name+"_tab_"+i+"' class='"+this.classes["tab"+this.direction]+"' onclick='TabbedPane.getInstance(\""+this.name+"\").select("+i+");' >"+tab.label+"</div></td>");
        this.tabs[i].tabNode=document.getElementById(this.name+"_tab_"+i);
      } else if (tab.type == "sep") {        
        document.write("<td><div class='"+this.classes['tabVerticalSep']+"'></div></td>");
      }
    }
    document.write("</tr>");
  } else if (this.direction == 'E' || this.direction == 'W') {    
    var i=0;
    for (i=0 ; i < this.tabCount; i++) {
      document.write("<tr>");      
      var tab=this.tabs[i];
      if (tab.type == "tab") {
        document.write("<td><div id='"+this.name+"_tab_"+i+"' class='"+this.classes["tab"+this.direction]+"' onclick='TabbedPane.getInstance(\""+this.name+"\").select("+i+");' >"+tab.label+"</div></td>");
        this.tabs[i].tabNode=document.getElementById(this.name+"_tab_"+i);
      } else if (tab.type == "sep") {
        document.write("<td><div class='"+this.classes['tabHorizontalSep']+"'></div></td>");
      }
      document.write("</tr>");
    }
  } 
  document.write("</table>");
}


/** Kiserleti statikus osztaly
  */
/**
  * @param name a tabbedpanehez rendelt cimke
  * @param tabbed maga a tabbedpane peldany
  */
TabbedPane.addInstance = function (name,tabbed) {
  if (TabbedPane.tabbedPanes == null) {
    TabbedPane.tabbedPanes = new Array();
  }
  TabbedPane.tabbedPanes[name]=tabbed;
}

/** Visszaadja a beregisztralt tabbedpanek kozul azt, amelyiknek a cimkejet megkapja
  * @param name a kert tabbedpane cimkeje
  * @return a cimkehez rendelt tabbedpane
  */
TabbedPane.getInstance = function (name) {
  return TabbedPane.tabbedPanes[name];
}

