// ============================ Dropdown links generator ============================ //

// Copyright Paul Tesar 2008 
// paultesar@gmail.com

// ============== Main function / Hlavni funkce ============== // 

function dropdown_handler(ele,divClass,linksArr,limit,delayMs,offset){               
  
  var pointer = document.getElementById(ele);
  var params  = {timeLimit: limit, delay: delayMs, offset_y: offset}; 

  new createDropDown(pointer,divClass,linksArr,params);
} 

var generatedDropdown;

function createDropDown(ele,divClass,linksArr,param)
{  
    this.controlObj = ele;
    
    if(!this.controlObj)
    {
       return false;
    }
                                                            // EN                                                       // CZ
    this.oP = (param) ? param : {};                         // optional parameters                                      // volitelne parametry
                                                                                                                        
    if (!this.oP.timeLimit)  this.oP.timeLimit   =  2000;   // default dropdown expiration time in ms                   //vychozi cas vyprseni v ms.
    if (!this.oP.delay)      this.oP.delay       =  1000;   // default delay before displaying dropdown in ms           //vychozi prodleva pred zobrazenim v ms.
    if (!this.oP.offset_y)   this.oP.offset_y    =  20;     // default top padding from the control object in px        //vychozi odstup od ovladaciho objektu v px.
 
    this.divclass    =  divClass;
    this.newLinks    =  linksArr;    
    this.timer       =  null;
    this.newdiv      =  ""; 

    var pointer = this;    
    
    this.controlObj.onmouseover = function(){return pointer.delayedDisplay();}
    this.controlObj.onmouseout  = function(){return pointer.setTimer();}
    this.controlObj.onclick     = function(){return pointer.kill();}
    
}
   
createDropDown.prototype.delayedDisplay = function()
{
    var pointer = this;
       
    if(this.oP.delay)
    {
        var echo = setTimeout(function(){pointer.newDisplay()},this.oP.delay);
    }
    else
    {
        var echo = this.newDisplay();
    }
        
    return echo;
}

createDropDown.prototype.newDisplay = function()
{

    if(generatedDropdown != null)
    {
        this.clearDropdowns();
    }

    this.newdiv     = "";
    this.timer      = null;  
    var pointer     = this;
    var position    = this.getPosition(this.controlObj);

    this.newdiv     = addDiv("","",this.divclass); 
                                                                
    this.newdiv.style.position = "absolute";                                
    this.newdiv.style.left     = position.x + "px";                                
    this.newdiv.style.top      = position.y + this.oP.offset_y + "px";          
    this.newdiv.onmouseover    = function(){return pointer.resetTimer();} 
    this.newdiv.onmouseout     = function(){return pointer.setTimer();} 
    
    for (x in this.newLinks)
    {
        addLink(this.newdiv,this.newLinks[x][0],this.newLinks[x][1]);
    }
    
    addImg(this.newdiv,"/img/menu_dropdown_bottom.gif",150,8);
    
    generatedDropdown = this.newdiv;        
}

createDropDown.prototype.kill = function()
{
    if(this.newdiv)
    {
       var parent = document.getElementsByTagName("body");
       parent[0].removeChild(this.newdiv);      
    }
      
    generatedDropdown = null;
                         
    if(this.timer)
    {
        clearTimeout(this.timer);
    }
      
    this.timer  = null;  
    return true;
}

createDropDown.prototype.clearDropdowns = function()
{  
   if(generatedDropdown)
   {
      var parent = document.getElementsByTagName("body"); 
      parent[0].removeChild(generatedDropdown);
      
      generatedDropdown = null; 
      this.newdiv  = null;                                                           
   }
}

createDropDown.prototype.setTimer = function()
{
    var pointer = this;

    this.timer  = setTimeout(function(){pointer.kill()},this.oP.timeLimit);
    return true;
} 

createDropDown.prototype.resetTimer = function(lim)
{  
   clearTimeout(this.timer);
   this.timer = null;
}

createDropDown.prototype.getPosition = function(ele)
{
var obj = ele;
var curleft = 0;

if (obj.offsetParent)
{
    while (obj.offsetParent)
    {
       curleft += obj.offsetLeft
       obj      = obj.offsetParent;
    }
}
else if (obj.x)
{
    curleft += obj.x; 
}

var obj    = ele;
var curtop = 0;

if (obj.offsetParent)
{
    while (obj.offsetParent)
    {
        curtop += obj.offsetTop
        obj     = obj.offsetParent;
    }
}
else if (obj.y)
{
    curtop += obj.y; 
}

return {x:curleft, y:curtop}
}

// ============== Associated non prototype functions / Prirazene externi funkce ============== //

function addDiv(trg,val,cls){
    
   var div           =  document.createElement("div");
       div.className = cls;
       div.innerHTML = val;
       if(trg)
       {
          trg.appendChild(div);
       }
       else
       {
          var parent = document.getElementsByTagName("body");
          parent[0].appendChild(div); 
       }
   return div;
}

function addImg(trg,src,w,h){
    
       var img =  document.createElement("img");
       img.setAttribute("src",src);
       img.setAttribute("width",w);
       img.setAttribute("height",h);
       if(trg)
       {
          trg.appendChild(img);
       }
       else
       {
          var parent = document.getElementsByTagName("body");
          parent[0].appendChild(img); 
       }
   return img;
}

function addLink(trg,val,call){
    
   var link = document.createElement("a");
       link.setAttribute("href",call);
       link.setAttribute("title",val);  
       link.innerHTML = "&raquo; "+val;   
       if(trg)
       {
          trg.appendChild(link);
       }
       else
       {
          var parent = document.getElementsByTagName("body");
          parent[0].appendChild(link); 
       }
   return link;
}