function Populate(init, cur){
  $$('.AutoPop').each(function(el){
    
    var cls = el.className.substring(11,el.className.length);
    var secs = cls.split("_");
    var major = parseInt(secs[0]);
    var minor = parseInt(secs[1]);
    
    
    var fmajor , fminor;
    if(!init){
      cls = cur.className.substring(11,el.className.length);
      secs = cls.split("_");
      fmajor = parseInt(secs[0]);
      fminor = parseInt(secs[1]);
    }
    
    if(init || (major == fmajor && minor > 0 && minor > fminor)){
    	
      clear_element(el);
      
      var path = get_path_for(major,minor);
      
      var items = by_path(path);
      
      var type = get_type(major);
      
      //console.log([major ,path ,type , items] );
      
      populate(el,type,items);
      
      if(init && $$('.Cat'+major+'_'+(minor+1)).length > 0){
        el.addEvent("change",function(){
          Populate(false, this);
        });
      }
      
    }
  
  });
}

function clear_element(el){
  if(el.getTag() == "select"){
    el.options.length = 1;
  }
}

function by_path(path){
  var secs = path.split("/");
  var true_path = "";
  
  for (i in secs){
    if (typeof(secs[i]) != 'string') { continue; }
    if(secs[i] == "") return [];
    true_path += "[" + secs[i] + "]['children']";
  }
  
  var res = eval("cats" + true_path);
  var fres = [];
  
  for (i in res){
    if (typeof(res[i]) != 'object') { continue; }
    fres[fres.length] = [res[i]['id'],res[i]['title']]
  }
  return fres;
  
}

function get_path_for(major,minor){
  var path = major + "/";
  for(var i = 0 ; i < minor ; i++){
    $$('.Cat'+major+'_'+i).each(function(el){
      path = path + el.value + "/";
    });
  }
  
  return path.substring(0,path.length - 1);
}

function get_type(major){
  return eval("cats[" + major + "]['type']");
}

function populate(el , type , items){
    //if(el.getTag() != "select") return false;
//alert(window.location.href);
  if(el.getTag() == "select"){
    items.each(function(item){
      el.options[el.options.length] = new Option(item[1],item[0]);
      
      var regex = escape("cat[]") + "=" + item[0] + "&";
      
      if(window.location.href.indexOf(regex) != -1){
        //el.selectedIndex = el.options.length - 1;
        el.options[el.options.length - 1].selected = true;
        el.options[0].selected = false;
      }
    });
  } else {
    items.each(function(item){
      //el.options[el.options.length] = new Option(item[1],item[0]);
	 
	  
      var checked = null;
      var regex = escape("cat[]") + "=" + item[0] + "&";
      var ie_option = "cat[]=" + item[0] + "&";
      if(window.location.href.indexOf(regex) != -1 || window.location.href.indexOf(ie_option) != -1){
        checked = "checked";
      }
	  
	  if(item[0]>0)
      var the_checkbox = new Element('input', {type: 'checkbox', name: 'cat[]', value: item[0] , 'checked':checked});
	
	
	 if(checked && item[0]>0){
      
      	the_checkbox.defaultChecked=true;
      	the_checkbox.checked = true;
      	//console.log(the_checkbox);
	  }
      //the_checkbox.checked = checked;
	  if(item[0]>0 && item[1]!='עם פנימיה')
      the_checkbox.injectInside(new Element('label').injectInside(el)).getParent().appendText(" " + item[1]);
	  else if(item[1]=='עם פנימיה')
	  {
	     the_checkbox.injectInside(new Element('label').injectInside($('pnimia'))).getParent().appendText(" " + item[1]);
	  }
	  
      
      
    });
    
  }
}

window.addEvent("domready",function(){
  //Get the auto populating 
  Populate(true,this);
});
