﻿
// ================================================
//                                                 
// eMultiSelectItem                                
// t��da p�edstavuj�c� polo�ku v seznamu           
//                                                 
// ================================================
function eMultiSelectItem(Parent, IDitem, ItemValue, ItemText, Selected, Index) {

   this.IDitem = IDitem;
   this.ItemRow = document.getElementById(IDitem);
   this.ItemValue = ItemValue;
   this.ItemText = ItemText;
   this.Parent = Parent;
   this.StartClassName = this.className;
   this.Index = Index;
   this.Selected = Selected;
   var This = this;
   
   
   // ================================================
   // Ozna�en� polo�ky
   // ================================================
   this.SelectItem = function(e) {
      This.Selected = true;
      Parent.RefreshInput();
      if (e != null) e.cancelBubble = true;

      Parent.RefreshDropDown();
      
      This.ItemRow.style.display = "none";
   }
   
   
   // ================================================
   // Odozna�en� polo�ky polo�ky
   // ================================================
   this.UnselectItem = function(e) {
      This.Selected = false;
      Parent.RefreshInput();
      if (e != null) e.cancelBubble = true;

      Parent.RefreshDropDown();

      This.ItemRow.style.display = "";
   }
   

   // ================================================
   // Zv�raznit polo�ku
   // ================================================
   this.HighlightItem = function() {
      for (i = 0; i < This.Parent.Items.length; i++) This.Parent.Items[i].UnHighlightItem();
      This.ItemRow.className = This.Parent.RowSelectedCssClass;
      This.Parent.HighlightedItem = This;
   }
   
   
   // ================================================
   // Zru�it zv�razn�n� polo�ky
   // ================================================
   this.UnHighlightItem = function() {
      This.ItemRow.className = This.StartClassName;
   }
   
   
   // ================================================
   // AutoPostBack
   // ================================================
   this.ItemClick = function() {
      if (This.Parent.AutoPostBack != "") {
         eval(This.Parent.AutoPostBack);
      }
   }
   Parent.AttachEvent(this.ItemRow, "click", this.ItemClick);
}




// ================================================
//                                                 
// eMultiSelectParagraph                           
// t��da p�edstavuj�c� polo�ku v seznamu           
// vybran�ch polo�ek - v lev� ��sti prvku          
//                                                 
// ================================================
function eMultiSelectParagraph(Parent, Item) {

   this.Parent = Parent;
   this.Item = Item;
   this.Output = document.createElement("p");
   this.StartClassName = this.Output.className;

   var TextNode = document.createTextNode(Item.ItemText);
   this.Output.appendChild(TextNode);
   
   this.Input = document.createElement("input");
   this.Input.setAttribute("type", "hidden");
   this.Input.setAttribute("id", this.Parent.HiddenID);
   this.Input.setAttribute("name", this.Parent.UID);
   this.Input.setAttribute("value", this.Item.ItemValue);
   this.Output.appendChild(this.Input);
   
   var This = this;
   
   
   // ================================================
   // Kliknut� na polo�ku
   // ================================================
   this.ItemClick = function() {
      if (Parent.DropDownShowed == true) {
         This.Item.UnselectItem();
         if (This.Parent.AutoPostBack != "") eval(This.Parent.AutoPostBack);
      }
   }
   
   
   // ================================================
   // Zv�raznit polo�ku
   // ================================================
   this.HighlightItem = function() {
      if (Parent.DropDownShowed == true) {
         This.Output.className = This.Parent.ParagraphSelectedCssClass;
      }
   }


   // ================================================
   // Zru�it zv�razn�n� polo�ky
   // ================================================
   this.UnHighlightItem = function() {
      if (Parent.DropDownShowed == true) {
         This.Output.className = This.StartClassName;
      }
   }
   
   Parent.AttachEvent(This.Output, "click", This.ItemClick);
   Parent.AttachEvent(This.Output, "mouseover", This.HighlightItem);
   Parent.AttachEvent(This.Output, "mouseout", This.UnHighlightItem);
}




// ================================================
//                                                 
// eSelect                                         
// mate�sk� t��da p�edstavuj�c� prvek jako celek   
//                                                 
// ================================================
function eMultiSelect(IDcontrol, UID, Enabled, RowSelectedCssClass, TextBoxSelectedCssClass,
                      ParagraphSelectedCssClass, ButtonCloseCssClass, AutoPostBack)
{

   this.IDcontrol = IDcontrol + "_inner";
   this.Control = document.getElementById(IDcontrol + "_inner");
   this.DropDown = document.getElementById(IDcontrol + "_DropDownPlaceHolder");
   this.DropDownInner = document.getElementById(IDcontrol + "_DropDown")
   this.Input = document.getElementById(IDcontrol + "_input");
   this.Button = document.getElementById(IDcontrol + "_button");
   this.Roller = document.getElementById(IDcontrol + "_roller");
   this.HiddenID = IDcontrol;
   this.UID = UID;
   this.Enabled = Enabled;
   
   this.AutoPostBack = AutoPostBack;
   this.AutoPostBack = this.AutoPostBack.replace("\(\'", "(\"");
   this.AutoPostBack = this.AutoPostBack.replace("\)\'", ")\"");
   
   this.RowSelectedCssClass = RowSelectedCssClass;
   this.TextBoxSelectedCssClass = TextBoxSelectedCssClass;
   this.StartTextBoxCssClass = this.Input.className;
   this.ParagraphSelectedCssClass = ParagraphSelectedCssClass;
   this.ButtonCloseCssClass = ButtonCloseCssClass;
   this.StartButtonCssClass = this.Button.className;
   
   this.ClickedInside = false;
   this.OffsetX = 0;
   this.OffsetY = 0;
   this.OffsetScrollTop = 20;
   this.DropDownShowed = false;
   
   this.ScreenWidth;
   this.ScreenHeight;
   this.StartOffsetX;
   this.StartOffsetY;
   
   this.Timer;
   
   this.Items = new Array();
   this.SelectedItem;
   this.HighlightedItem;
   var This = this;
   
   
   // ================================================
   // 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 aktualni velikost obrazovky
   // ================================================
   this.GetViewPortSize = function()
   {
      var width = 0;
      var height = 0;
      var body = document.body;
      if(window.innerWidth)
      {
         width = window.innerWidth;
         height = window.innerHeight;
      }
      else
      {
         if(document.compatMode&&document.compatMode == "CSS1Compat")
         {
            body = document.documentElement;
         }
         width = body.clientWidth;
         height = body.clientHeight;
      }
      width += body.scrollLeft;
      height += body.scrollTop;

      This.ScreenWidth = width - 6;
      This.ScreenHeight = height - 6;
   };
   
   
   
   // ================================================
   // 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;
   };
   
   
   
   // ================================================
   // getXY
   // ================================================
   this.getXY = function(el)
   {
      var _4b=null;
      var pos=[];
      var box;
      if(el.getBoundingClientRect)
      {
         box=el.getBoundingClientRect();
         var _4e=document.documentElement.scrollTop||document.body.scrollTop;
         var _4f=document.documentElement.scrollLeft||document.body.scrollLeft;
         var x=box.left+_4f-2;
         var y=box.top+_4e-2;
         return [x,y];
      }
      else
      {
         if(document.getBoxObjectFor)
         {
            box=document.getBoxObjectFor(el);
            pos=[box.x-1,box.y-1];
         }
         else
         {
            pos=[el.offsetLeft,el.offsetTop];
            _4b=el.offsetParent;
            if(_4b!=el)
            {
               while(_4b)
               {
                  pos[0]+=_4b.offsetLeft;
                  pos[1]+=_4b.offsetTop;
                  _4b=_4b.offsetParent;
               }
            }
         }
      }
      if(window.opera)
      {
         _4b=el.offsetParent;
            while(_4b&&_4b.tagName.toUpperCase()!="BODY"&&_4b.tagName.toUpperCase()!="HTML")
            {
               pos[0]-=_4b.scrollLeft;
               pos[1]-=_4b.scrollTop;
               _4b=_4b.offsetParent;
            }
      }
      else
      {
         _4b=el.parentNode;
         while(_4b&&_4b.tagName.toUpperCase()!="BODY"&&_4b.tagName.toUpperCase()!="HTML")
         {
            pos[0]-=_4b.scrollLeft;
            pos[1]-=_4b.scrollTop;
            _4b=_4b.parentNode;
         }
      }
      return pos;
   };
   
   
   // ================================================
   // Bool, jestli element pretika spodek obrazovky
   // ================================================
   this.ElementOverflowsBottom = function(_64,_65)
   {
      This.GetViewPortSize();
      var _66 = This.GetElementPosition(_65).y + _64.offsetHeight;
      return _66 > This.ScreenHeight;
   };
   
   

   // ================================================
   // Bool, jestli element pretika pravy okraj obrazovky
   // ================================================
   this.ElementOverflowsRight = function(_64)
   {
      var _66 = This.GetElementPosition(_64).x + _64.offsetWidth;
      return _66 > This.ScreenWidth;
   };
   


   // ================================================
   // Rozbalit DropDown nabidku
   // ================================================
   this.ShowDropDown = function() {
      This.GetViewPortSize();
      This.ClickedInside = true;
      This.DropDown.style.display = "block";
      This.RefreshDropDown();
         
      This.DropDownShowed = true;
      
      This.Button.className = This.ButtonCloseCssClass;
   };
   
   
   // ================================================
   // Kliknuti na tlacitko zavreni
   // ================================================
   this.ButtonCloseClicked = function(e) {
      if (This.DropDownShowed == true) {
         This.HideDropDown();
         if (e != null) e.cancelBubble = true;
      }
   }
   
   
   // ================================================
   // Prekreslit DropDown nabidku
   // ================================================
   this.RefreshDropDown = function() {
      var _5c = This.Input;
      var _5d = This.getXY(_5c);
      var x = _5d[0]+This.OffsetX;
      var y = _5d[1]+This.OffsetY;

      this.StartOffsetX = x;
      this.StartOffsetY = y;
   
      This.DropDown.style.top = This.StartOffsetY + "px";
      This.DropDown.style.left = (x + This.Input.offsetWidth + This.Button.offsetWidth) + "px";

      if(This.ElementOverflowsBottom(This.DropDown, This.Input))
      {
         var _62 = y + This.DropDown.offsetHeight - This.ScreenHeight;
         if(_62 > 0) y -= _62;
         This.DropDown.style.top = y + "px";
      }


      if (This.SelectedItem != null)
         This.DropDownInner.scrollTop = This.SelectedItem.ItemRow.offsetTop - This.OffsetScrollTop;
   }
   


   // ================================================
   // Sbalit DropDown nabidku
   // ================================================
   this.HideDropDown = function() {
      This.DropDown.style.top = "";
      This.DropDown.style.left = "";
      This.DropDown.style.display = "none";
      This.DropDownShowed = false;
      This.Button.className = This.StartButtonCssClass;
   }
   
   
   // ================================================
   // Kliknuti mimo prvek
   // ================================================
   this.OutSideClicked = function() {
      if (!This.ClickedInside) {
         This.HideDropDown();
      }
      This.ClickedInside = false;
   };
   
   
   // ================================================
   // Prekresleni a aktualizace hodnot v Input prvku
   // ================================================
   this.RefreshInput = function() {

      This.ClearInput();
      
      for (var i = 0; i < This.Items.length; i++) {
         if (This.Items[i].Selected == true) {
            var Paragraph = new eMultiSelectParagraph(This, This.Items[i]);
            This.Input.appendChild(Paragraph.Output);
         }
      }

      // prizpusobit vysku tlacitka
      // if (window.navigator.appName == "Microsoft Internet Explorer")
      //    This.Button.style.height = This.Input.offsetHeight + "px";
      // else
      
      if (This.Input.offsetHeight > 0) This.Button.style.height = (This.Input.offsetHeight - 2) + "px";
   }
   

   // ================================================
   // Vymaze hodnoty v Input prvku
   // ================================================
   this.ClearInput = function() {
      for (var i = This.Input.childNodes.length - 1; i >= 0; i--) {
         This.Input.removeChild(This.Input.childNodes[i]);
      }
   }
   
   
   // ================================================
   // Najet� my�� na roletu
   // ================================================
   this.InputMouseOver = function() {
      This.Input.className = This.TextBoxSelectedCssClass;
   }
   
   
   // ================================================
   // Odjet� my�� z rolety
   // ================================================
   this.InputMouseOut= function() {
      This.Input.className = This.StartTextBoxCssClass;
   }
   
   
   // ================================================
   // Stisknut� kl�vesy v textboxu
   // ================================================
   this.KeyDown = function(e) {
      if (e.keyCode >= 48 && e.keyCode <= 90) This.KeyLetterPressed(e.keyCode);
      
      if (e.keyCode != 9) {
         if (e.stopPropagation) {
            e.stopPropagation();
            e.preventDefault();
         }
         return false;
      }
   }
   
   
   // ================================================
   // Stiskut� kl�vesy s p�smenem
   // ================================================
   this.KeyLetterPressed = function(keyCode) {
      for (var i = 0; i < This.Items.length; i++) {
         if (This.Items[i].ItemText.length > 0 && This.Items[i].ItemText.charCodeAt(0) == keyCode) {
            
            if ((This.HighlightedItem == null) ||
                (This.HighlightedItem.ItemText.charCodeAt(0) != keyCode) ||
                (This.HighlightedItem.ItemText.charCodeAt(0) == keyCode && i > This.HighlightedItem.Index) ||
                (This.Items.length > This.HighlightedItem.Index + 1 && This.Items[This.HighlightedItem.Index + 1].ItemText.charCodeAt(0) != keyCode))
            {
               This.Items[i].HighlightItem();
               This.DropDownInner.scrollTop = This.Items[i].ItemRow.offsetTop;
               break;
            }
 
         }
      }
   }
   
   
   // ================================================
   // Inicializace jednotlivych polozek
   // ================================================
   this.InitializeItems = function(Items) {
      for (var i = 0; i < Items.length; i++) {
         This.Items[i] = new eMultiSelectItem(This, Items[i][0], Items[i][1], Items[i][2], Items[i][3], i);
         this.AttachEvent(This.Items[i].ItemRow, "click", This.Items[i].SelectItem);
         this.AttachEvent(This.Items[i].ItemRow, "mouseover", This.Items[i].HighlightItem);
         if (Items[i][3] == true) This.Items[i].SelectItem();
      }
      
      // hack kvuli IE
      This.Timer = window.setTimeout(This.RefreshInput, 5);
   };



   // ================================================
   // UD�LOSTI
   // ================================================
   if (This.Enabled) {
       this.AttachEvent(this.Control, "click", this.ShowDropDown);
       this.AttachEvent(this.Button, "click", this.ButtonCloseClicked);
       this.AttachEvent(document, "click", this.OutSideClicked);
       this.AttachEvent(this.Roller, "mouseover", this.InputMouseOver);
       this.AttachEvent(this.Roller, "mouseout", this.InputMouseOut);
       this.AttachEvent(this.Control, "keydown", this.KeyDown);
   }
   this.AttachEvent(document, "load", this.RefreshInput);
};


// ================================================
// VALIDA�N� FUNKCE                                
// ================================================
function eMultiSelectRequireValidate(object, arguments) {
    if (arguments.Value == "") {
        arguments.IsValid = false;
    } else {
        arguments.IsValid = true;
    }
}
