/**
 * @fileoverview Objekt pro praci s navigaci
 * 
 * @author Oranges.s.r.o. & Laki <info@laki.cz>
 * @version 3.0 <06-09-2009>
 */  

/**
 * Objekt pro dynamickou navigaci CFN/Kinobox
 *
 * <p>Udela dynamickou navigaci CFN/Kinobox v designu V3.</p>
 * 
 * <p>Struktura vstupniho objektu:</p> 
 * <pre>
 *    id     - string - povinny
 *           - identifikator tabulky filmografie, ktera se ma nacist a nahradit
 *             
 *    sort   - array - volitelne - ve vychozim nastaveni se predpoklada "rok desc"
 *           - dvouprvkove pole popisujici vychozi serazeni vstupni tabulky
 *           - viz {@link filmografie#soft}
 *              
 *    filter - array - volitelne - vychozi nastaveni -> nic neni filtrovano
 *           - pole popisujici vychozi nastaveni filtru
 *           - pocet prvku a jejich poradi musi odpovidat poctu filtru a jejich poradi {@link filmografie#filter}
 *           - tj. napr. filtr TVFILM je na druhe pozici, proto napr. filter[1] = "off" 
 *           - kazdy prvek musi nabyvat hodnoty "on" nebo "off"
 *                 
 *    texts  - object - volitelne
 *           - definice objektu textovych promennych - viz {@link filmografie#texts}
 *           - nejsou-li zadany vsechny potrebne vlastnosti, doplni se objekt o tyto vlastnosti s vychozi nastavenim
 * </pre>    
 * 
 * @example Zakladni priklad - aplikuje filmografii na tabulku s ID="film0" a vychozim serazenim sestupne podle roku.
 *   var p = {
 *    id:     "film0", 
 *    sort:   new Array("rok", "desc") };
 *     
 *   var f0 = new filmografie(p);
 *   
 * @example Rozsirujici priklad - pridava vychozi filtrovani tabulky - zobrazovat jen filmy
 *  var p = {
 *    id:     "film0", 
 *    sort:   new Array("rok", "desc"),
 *    filter: new Array("on", "off", "off", "off", "off")  };
 *  var f0 = new filmografie(p); 
 *    
 * @example Rozsirujici priklad - nastaveni vlastnich textovych promennych - nadpis navigacni radky nastavi na "ABC"
 *  var p = {
 *    id:    "film0", 
 *    sort:  new Array("rok", "desc"),
 *    texts: {sort: "ABC"} };
 *  var f0 = new filmografie(p); 
 *  
 * @class filmografie - dynamicka filmografie osoby
 * @extends cfn
 * @version 1.0
 * @constructor
 * @requires events Udalosti
 * @requires hover Nahled miniatur  
 * @param {object} p Objekt vstupnich parametru
 */
function menu(p)
{
  this.debug = false;
  /**
   * Element primarni navigace
   * @type object Element   
   * @private      
   */     
  this.el = null;
  
  this.openIndex = null;
    
  //nalezeni elementu navigace, jinak konec
  if ((!p) || (typeof(p.id) != "string") ) return; 
  this.el = document.getElementById(p.id);
  if (this.el == null) return;
  if (this.el.tagName.toUpperCase() != "UL") return;

  /*
  obj - objekt LI primarniho menu
  txt - text primarniho menu - nadbytecny
  sel - vybrane primarni menu
  sec - obsahuje subnavigaci?  
  */
  this.primary = new Array();   //polozky primarni navigace
  
  for (var i=0; i<this.el.childNodes.length; i++)
  {    
    if (this.el.childNodes[i].childNodes.length > 0)  //prochazet jen uzly LI
    {
      
      this.primary.push( { 
        obj: this.el.childNodes[i], 
        txt: this.getText(this.el.childNodes[i].childNodes[0]), 
        sel: ( (this.getClass(this.el.childNodes[i]) == "on") ? true : false),
        sec: ( (this.el.childNodes[i].getElementsByTagName("UL").length > 0) ? true : false)
      } );
      
    }
  }
  
  for (var i=0; i<this.primary.length; i++)
  {
    this.deb( 
      this.primary[i].obj + " " + 
      this.primary[i].txt + " " + 
      this.primary[i].sel + " " +
      this.primary[i].sec);
      
    if ( (this.primary[i].sec) && (this.getClass(this.primary[i].obj) != "on") )    //jiz vybranou polozku neresit
    {
      events.addEventListener(this.primary[i].obj, "mouseover", this.showSecond, this);
      events.addEventListener(this.primary[i].obj, "mouseout", this.hideSecond, this);
    }
  }

}

// menu je rozsirenim objektu cfn
menu.prototype = new cfn;

//redefinice deb metody
menu.prototype.deb = function(text)
{
  if (this.debug)
  {
    //volej predka s prefixem
    cfn.prototype.deb.call(this, text, "menu-> ");
  } 
}  

//zobrazi sekundarni navigaci
menu.prototype.showSecond = function(e)
{  
  if (!e) var e = window.event;

  if (this.openIndex != null) this.hideSecond(e); //pokud je neco otevreno, okamzite to zavri
 
  for (var i=0; i<this.primary.length; i++)
  {     
    if (this.primary[i].obj.getElementsByTagName("A")[0] == events.eventSource(e))
    {
      this.deb("show " + this.primary[i].txt);
      
      if (this.primary[i].sec)
      {
        this.addClass(this.primary[i].obj, "on2");
      }
      
      this.openIndex = i;
      break;
    }
  }
  
  //zrusit dalsi probublavani udalosti
  e.cancelBubble = true;
  if (e.stopPropagation) e.stopPropagation();
}

//skryje sekundarni navigaci
menu.prototype.hideSecond = function(e)
{
  if (!e) var e = window.event;
 
  if (this.openIndex != null)
  {
  
    if ( ( (events.mouseoutTarget(e).tagName == "UL") && (events.mouseoutTarget(e).parentNode == this.primary[this.openIndex].obj) ) || 
         ( (events.mouseoutTarget(e).tagName == "A") && (events.mouseoutTarget(e).parentNode.parentNode.parentNode == this.primary[this.openIndex].obj) ) ||   //IE
         ( (events.mouseoutTarget(e).tagName == "A") && (events.mouseoutTarget(e).parentNode == this.primary[this.openIndex].obj) ) ) //Mozilla
    {
     
    }
    else
    {
      this.deb("hide " + this.primary[this.openIndex].txt); 
      
      if (this.primary[this.openIndex].sec)
      {
        this.removeClass(this.primary[this.openIndex].obj, "on2");
      }
      
      events.removeEventListener(this.primary[this.openIndex].obj, "mouseover", this.showSecond);
      events.removeEventListener(this.primary[this.openIndex].obj, "mouseout", this.hideSecond);
      
      this.openIndex = null; 
    }
  }
}

