// ================================================
//
// PopupBox
// třída představující popup element
//
// ================================================
function PopupBox(IDTrigger, IDPopup, PopupPosition) {

   this.Trigger = document.getElementById(IDTrigger);
   this.Popup = document.getElementById(IDPopup);
   this.PopupPosition = PopupPosition;
   
   var This = this;
   var IgnoreDocumentClick = false;

   // ================================================
   // Metoda pro prirazeni udalosti prvku
   // ================================================
   this.AttachEvent = function(target, eventType, handler)
   {
      if (target.attachEvent){
         target.attachEvent("on"+eventType, handler);
      }
      else
      {
         if(target.addEventListener){
            target.addEventListener(eventType, handler, false);
         }
      }
   }
   
   
   // ================================================
   // Metoda zjisti XY pozici daneho elementu
   // ================================================
   this.GetElementPosition = function(el)
   {
      var _21 = null;
      var pos = { x:0, y:0 };
      var box;
      if(el.getBoundingClientRect)
      {
         box=el.getBoundingClientRect();
         var _24=document.documentElement.scrollTop||document.body.scrollTop;
         var _25=document.documentElement.scrollLeft||document.body.scrollLeft;
         pos.x=box.left+_25-2;
         pos.y=box.top+_24-2;
         return pos;
      }
      else
      {
         if(document.getBoxObjectFor)
         {
            box=document.getBoxObjectFor(el);
            pos.x=box.x-2;
            pos.y=box.y-2;
         }
         else
         {
            pos.x=el.offsetLeft;
            pos.y=el.offsetTop;
            _21=el.offsetParent;
            if(_21!=el)
            {
               while(_21)
               {
                  pos.x+=_21.offsetLeft;
                  pos.y+=_21.offsetTop;
                  _21=_21.offsetParent;
               }
            }
         }
      }
      if(window.opera)
      {
         _21=el.offsetParent;
         while(_21&&_21.tagName!="BODY"&&_21.tagName!="HTML")
         {
            pos.x-=_21.scrollLeft;
            pos.y-=_21.scrollTop;
            _21=_21.offsetParent;
         }
      }
      else
      {
         _21=el.parentNode;
         while(_21&&_21.tagName!="BODY"&&_21.tagName!="HTML")
         {
            pos.x-=_21.scrollLeft;
            pos.y-=_21.scrollTop;
            _21=_21.parentNode;
         }
      }
      return pos;
   }
   
   
   // ================================================
   // "Kontruktor"
   // ================================================
   this.InitializePopup = function() {
      This.Popup.style.display = "none";
      This.Displayed = false;
   }
   
   
   // ================================================
   // Událost klepnutí na spouštěč
   // ================================================
   this.TriggerClick = function(e) {
      if (!This.Displayed) This.ShowPopup();
      else This.HidePopup(e);
      
      // událost probublává výše až na document, takže
      // příští zpracování se bude ingorovat, aby nedošlo
      // hned ke schování popup boxu
      IgnoreDocumentClick = true;
   }
   
   
   // ================================================
   // Událost kliknutí dovnitř popup boxu
   // ================================================
   this.PopupClick = function(e) {
      e.cancelBubble = true;
   }


   // ================================================
   // Událost kliknutí kamkoliv na stránku
   // ================================================
   this.DocumentClick = function() {
      if (!IgnoreDocumentClick) This.HidePopup();
      IgnoreDocumentClick = false;
   }
   
   
   // ================================================
   // Metoda zobrazí popup box
   // ================================================
   this.ShowPopup = function() {
   
      if (This.PopupPosition == "right") {
         var pos = This.GetElementPosition(This.Trigger);
         This.Popup.style.position = "absolute";

         This.Popup.style.left = pos.x + This.Trigger.offsetWidth + 10 + "px";
         This.Popup.style.top = pos.y + "px";
      }
      
      This.Popup.style.display = "block";
      This.Displayed = true;
   }
   
   
   // ================================================
   // Metoda skryje popup box
   // ================================================
   this.HidePopup = function() {
      This.Popup.style.display = "none";
      This.Displayed = false;
   }
   
   
   // ================================================
   // Přiřazení událostí
   // ================================================
   this.AttachEvent(this.Trigger, "click", this.TriggerClick);
   this.AttachEvent(this.Popup, "click", this.PopupClick);
   this.AttachEvent(document, "click", this.DocumentClick);
   
   // volání konstruktoru
   this.InitializePopup();
}

