﻿// ================================================
//                                                 
// eCheckBox
// mate�sk� t��da p�edstavuj�c� prvek jako celek   
//                                                 
// ================================================

function eCheckBox(IDcontrol, ImagePath, EnableHover, Enabled, Selected, Width, Height) {

   this.IDcontrol = IDcontrol;
   this.Control = document.getElementById(IDcontrol + "_Wrapper");
   this.Image = document.getElementById(IDcontrol + "_Image");
   this.Label = document.getElementById(IDcontrol + "_Label");
   this.Input = document.getElementById(IDcontrol);

   this.EnableHover = EnableHover;
   this.Enabled = Enabled;
   this.Selected = Selected;
   
   this.Width = Width;
   this.Height = Height;

   this.ImagePath = ImagePath;
   
   
   // na��st obr�zky do cache
   this.ImageChk = new Image(14, 14);
   this.ImageChk.src = this.ImagePath + "checkbox_checked.gif";
   this.ImageUnChk = new Image(14, 14);
   this.ImageUnChk.src = this.ImagePath + "checkbox_unchecked.gif";
   this.ImageDisChk = new Image(14, 14);
   this.ImageDisChk.src = this.ImagePath + "checkbox_disabled_checked.gif";
   this.ImageDisUnChk = new Image(14, 14);
   this.ImageDisUnChk.src = this.ImagePath + "checkbox_disabled_unchecked.gif";
   this.ImageChkHov = new Image(14, 14);
   this.ImageChkHov.src = this.ImagePath + "checkbox_checked_hover.gif";
   this.ImageUnChkHov = new Image(14, 14);
   this.ImageUnChkHov.src = this.ImagePath + "checkbox_unchecked_hover.gif";
   
   
   var This = this;
   

   if (This.Width > 0) This.Image.width = This.Width;
   if (This.Height > 0) This.Image.height = This.Height;


   // ================================================
   // Metoda pro prirazeni udalosti prvku
   // ================================================
   this.AttachEvent = function(target, eventType, handler)
   {
      if (target != null) {
          if (target.attachEvent){
             target.attachEvent("on"+eventType, handler);
          }
          else
          {
             if(target.addEventListener){
                target.addEventListener(eventType, handler, false);
             }
          }
      }
   };



   // ================================================
   // Nastavit obr�zek checkboxu podle stavu
   // ================================================
   this.SetImage = function() {
      if (This.Enabled) {
         if (This.Selected) {
            This.Image.src = This.ImageChk.src;
         } else {
            This.Image.src = This.ImageUnChk.src;
         }
      } else {
          if (This.Selected) {
            This.Image.src = This.ImageDisChk.src;
         } else {
            This.Image.src = This.ImageDisUnChk.src;
         }
      }
   }
   
   
   // ================================================
   // Zv�raznit checkbox
   // ================================================
   this.ImageHighLight = function() {
      if (This.EnableHover && This.Enabled) {
         if (This.Selected) {
            This.Image.src = This.ImageChkHov.src;
         } else {
            This.Image.src = This.ImageUnChkHov.src;
         }
      }
   }
   
   
   // ================================================
   // Zru�it zv�razn�n� checkboxu
   // ================================================
   this.ImageUnHighLight = function() {
      if (This.EnableHover && This.Enabled) {
         This.SetImage();
      }
   }
   
   
   // ================================================
   // Kliknut� na prvek
   // ================================================
   this.Click = function() {
      if (This.Enabled) {
         This.Selected = !This.Selected;
         This.SetImage();
         
         if (This.Input.value == "") This.Input.value = "1";
         else This.Input.value = "";
      }
   }
   
   this.SetImage();


   // ================================================
   // Ud�losti
   // ================================================
   this.AttachEvent(this.Image, "click", this.Click);
   this.AttachEvent(this.Label, "click", this.Click);
   this.AttachEvent(this.Image, "mouseover", this.ImageHighLight);
   this.AttachEvent(this.Image, "mouseout", this.ImageUnHighLight);
}
