/**
 * prefixy pro komunikaci span v divu => checkbox + label
 * jedine 100% reseni... 
 * IDcka pro inputy: a_01, a_02 atd...
 * IDcka pro spany: sp_a_01, sp_a_02 atd...  
 **/ 
var span_prefix = 'sp_';

var checkbox_prefix = 'a_';

/**
 * zjisteni hodnoty kotvy
 * @zdroj: http://wiki.sovanet.cz/doku.php?id=webmaster:js:kotva2get       
 **/             
function kotva() {
  hash = document.location.hash.replace(new RegExp('^(.*)#'), '');
  var get = {}
  if (hash) {
    hash = hash.split('&');
    for (i in hash) {
      hash[i] = hash[i].split('=');
      get[hash[i][0]] = (hash[i][1] ? hash[i][1] : null);
    }
  }
  return get;
}

/**
 * update hlavniho divu
 * @param object self        
 **/             
function update_parent(self) {
  if (self.checked) {
    elem = document.createElement('span');
    elem.innerHTML = self.nextSibling.innerHTML;
    elem.id = span_prefix + self.id
    elem.onclick = function() {
      spanOnclickFunction(this);
    };
    
    sHandler.Elems.choose.appendChild(elem);
  } else {
    elem = document.getElementById(span_prefix + self.id);
    if (elem !== null) {
      elem.parentNode.removeChild(elem);  
    }
  }
}

/**
 * onklik spenu
 * @param object self
 **/ 
function spanOnclickFunction(self) {
  id = (self.id).replace(span_prefix, '');
  elem = document.getElementById(id); 
  if (elem !== null) {
    elem.checked = false;
  }

  // sebevrazda
  self.parentNode.removeChild(self);  
}

/**
 * simulace selektboxu
 * @param string displayer id nesmyslu, kam se budou vracet hodnoty (do innerHTML) 
 * @param string elem - id odkazu
 * @param string showElem - id toho co budeme naklikavat
 * @param string hidInput - id hidden inputu, kam se budou posilat data
 * @param string prefix - prefix ID, ze kterych se budou parsovat value do hidden inputu   
 **/
function SmallHandler(dislayer, elem, showElem, hidInput, prefix) {

  /**
   * kopie argumentu, co kdyby se hodily
   * @var string   
   **/     
  this.hidInputId = hidInput
  
  this.dislayerId = dislayer;
  
  this.elemId = elem;
  
  this.showElemId = showElem;

  /**
   * objekty prvku
   * @var object   
   **/     
  this.hidInput = document.getElementById(hidInput);
  
  this.displayer = document.getElementById(dislayer);
  
  this.elem = document.getElementById(elem);
  
  this.showElem = document.getElementById(showElem);
  
  this.anchors = null;

  /**
   * prefix idcek odkazu
   * @var string   
   **/     
  this.prefix = prefix;
  
  /**
   * sam svuj metlos
   * @var object   
   **/     
  var that = this;
  
  /**
   * funkce na onclick jednotlivych polozek
   * skryje box, prepise tridy, prepise hodnotu v hlavnim boxu, vrazi ID do hidden inputu   
   **/
  this.close = function(self) {
    that.showElem.style.display = 'none';
    for (i = 0; i < that.anchors.length; i++) {
      that.anchors[i].className = '';
    }
    self.className = 'active';
    that.displayer.innerHTML = self.innerHTML;
    that.hidInput.value = (self.id).replace(new RegExp('^' + that.prefix + '(.+)$'), '$1');
  }
  
  /**
   * konstruktor
   *
   **/
  this.SmallHandler = function() {
    display = kotva().display;
    
    // nevim na co tohle je
    if (display && display == this.showElemId) {
      this.showElem.style.display = 'block';
    }
    
    // pouze onclick klikatka         
    this.elem.onclick = function() {
      that.onclickFunction(this);
    }
    
    // onclick toho druhyho
    this.displayer.onclick = function() {
      that.onclickFunction(that.elem);
    };
    
    // prirazeni akci odkazum
    this.anchors = this.showElem.getElementsByTagName('a');
    for (i = 0; i < this.anchors.length; i++) {
      this.anchors[i].onclick = function() {
        that.close(this);
      }
    }
  };
  
  /**
   * funkce se prirazuje na onclick dvoum prvkum, abych to nemusel mit v kodu 2x
   * 
   **/     
  this.onclickFunction = function(self) {
    if (that.showElem.style.display == 'none') {
      self.className = 'active';
      that.showElem.style.display = 'block';
    } else {
      self.className = '';
      that.showElem.style.display = 'none';
    }
  }
  
  this.SmallHandler();
}

window.onload = function() {
  
  /**
   * prvni instance naklikavani
   *    
   **/
  window.loc = new SmallHandler('loc', 'a_loc', 'localisation', 'category', 'treba_');

  /**
   * ovladac na filtrovaci formular
   *    
   **/
  window.sHandler = {
  
    Elems : {
      // content : document.getElementById('content'),
      search : document.getElementById('search_div'),
      choose : document.getElementById('choose'),
      a_choose : document.getElementById('a_choose'),
      close : null
    },
    
    Enable : function(kotva) {
      // this.Elems.content.style.display = 'none';
      this.Elems.search.style.display = 'block';
      this.Elems.choose.className = 'active';
      this.Elems.a_choose.className = 'active';
      if (this.Elems.choose.innerHTML == choose_title) {
        this.Elems.choose.innerHTML = '';
      }
      
      // prvky maleho selektu
      loc.showElem.style.display = 'none';
      loc.elem.className = '';
    },
    
    Disable : function(kotva) {
      // this.Elems.content.style.display = 'block';
      this.Elems.search.style.display = 'none';
      this.Elems.choose.className = '';
      this.Elems.a_choose.className = '';
      if (this.Elems.choose.innerHTML == '') {
        this.Elems.choose.innerHTML = choose_title;
      }
      
      // prvky maleho selektu
      loc.showElem.style.display = 'none';
      loc.elem.className = '';
    }

  }; // sHandler end
  
  // krizku dame funkci na zavreni
  sHandler.Elems.close = sHandler.Elems.search.getElementsByTagName('a')[0];
  sHandler.Elems.close.onclick = function() {
    sHandler.Disable(true);
  };
  
  // hlavnimu tlacitku dame funkci na skryti / odkryti formu
  sHandler.Elems.a_choose.onclick = function() {
    if (sHandler.Elems.search.style.display == 'none') {
      sHandler.Enable(true);
    } else {
      sHandler.Disable(true);
    }
  };
  
  // velkemu divu dame funkci na skryti / odkryti formu
  if (sHandler.Elems.choose.innerHTML == choose_title || sHandler.Elems.choose.innerHTML == '') {
    sHandler.Elems.choose.onclick = function() {
      if (sHandler.Elems.choose.innerHTML == choose_title) {
        sHandler.Enable(true);
      } else if (sHandler.Elems.choose.innerHTML == '') {
        sHandler.Disable(true);
      }
    };
  }
  
  // existuje-li kotva, mame okno aktivni
  if (kotva().display == 'obor') {
    sHandler.Enable(false);
  }
  
  // pokud uz tam neco je, pridajo se temu funkce
  pSpans = (sHandler.Elems.choose).getElementsByTagName('span');
  if (pSpans.length) {
    for (i = 0; i < pSpans.length; i++) {
      pSpans[i].onclick = function() {
        spanOnclickFunction(this, sHandler, this.innerHTML);
      };
    }
  }
  
  // prida akci vsem checkboxum a take submitu uvnitr
  var inputs = sHandler.Elems.search.getElementsByTagName('input');
  //for (i in inputs) {
  for (var i = 0; i<inputs.length; i++){
    if (typeof inputs[i] == 'object') {
      if (inputs[i].type == 'button') {
        inputs[i].onclick = function() {
          sHandler.Disable(true);
        };
      } else if (inputs[i].type == 'checkbox') {
        inputs[i].onclick = function() {
          update_parent(this, sHandler);
        }
      }
    }
  }
  
  // osetreni odeslani formulare, nic duleziteho
  document.getElementById('peklo').onsubmit = function() {
    if (keyword_def_value !== undefined && bonus_action !== undefined) {
      if (document.getElementById('key-word').value == keyword_def_value) {
      possible_del = sHandler.Elems.choose.getElementsByTagName('span');
        if(possible_del.length <2){
          this.action = bonus_action;
        }
      }
    }
  };
  
};