var selected_ct = [];
var ct_path = [];
function getCategory(cid, target){
	//$F("sc") = cid;
	$("sc").value = cid;
	if($("ct-" + selected_ct[target-1]) != null) $("ct-" + selected_ct[target-1]).removeClassName("ct-selected");
	$("ct-" + cid).addClassName("ct-selected");
	var url = "/ad/xml/category/c/"+cid;
	new Ajax.Request(url, {
	  method: 'get',
	  contentType: 'application/json',
	  onSuccess: function(transport) {
	  	if(transport.responseJSON.length > 0){
	  		
	  		if($('ct-level' + target) == undefined){
	  			var cont = new Element("div",{id:'ct-level'+target,"class":"ct-container"});
	  			$("ct-selector").insert(cont);
	  		}else{
	  			cont = $('ct-level' + target);
	  			cont.update("");
	  		}
	  		
		  	transport.responseJSON.each(function(s){
		  		if(s.sub > 0) optlabel = s.label + " =>"; else optlabel =  s.label;
		  		var ct = new Element("div",{"id":"ct-" + s.id,"class":"ct"}).update(optlabel);
		  		ct.onclick = function(ev){
		  			getCategory(s.id, target + 1);
		  		}
		  		cont.insert(ct);
		  	});
	  	}
	  	
		getCriterias(cid,target);
		selected_ct[target-1] = cid;
		ct_path[target-1] = cid;
		$("ct-path").update("");
		for(i=0;i<ct_path.length;i++){
			if(i!=0) $("ct-path").insert(" >> ");
			$("ct-path").insert($("ct-"+ct_path[i]).innerHTML);
		}
	  	
	  }
	});
}

function getCriterias(cid,target){
	if($$("[ct="+cid+"]").length != 0) return;
	for(i=4;i>=target-1;i--){
		if(selected_ct[i] != null){
			$$("[ct="+selected_ct[i]+"]").each(function(s){
				s.remove();
			});
		}
	}
	var url = "/ad/xml/searchcriteria/c/"+cid;
	new Ajax.Request(url, {
	  method: 'get',
	  contentType: 'application/json',
	  onSuccess: function(transport) {
	  	if(transport.responseJSON.length > 0){  		
		  	transport.responseJSON.each(function(s){
				var tr_obj = new Element("div",{"class":"row","ct":cid});
				var td_key = new Element("div",{"class":"key"});
				td_key.update(s.label);
				tr_obj.insert(td_key);
				var td_val = new Element("div",{"class":"value clr"});
				td_val.update(s.comp);
				tr_obj.insert(td_val);
				$('price-row').insert({"before":tr_obj});
				//alert("asd");
		  	});
	  	}
	  }
	});
	
}

var cr_types = {};
function getAttr(crid,attrid){
	var url = "/ad/xml/attr/type/1/crid/"+crid+"/attrid/"+attrid;
	new Ajax.Request(url, {
	  method: 'get',
	  contentType: 'application/json',
	  onSuccess: function(transport) {
	  	if(transport.responseJSON.items.length > 0){
		  	var select = new Element("select", {id:"cr_"+crid, "name":"s[cr]["+crid+"]","class":"combobox"});
		  	select.style.width = combo_width;
		  	if(cr_types[crid] == "combo"){
		  		select.multiple = true;
		  		select.size = 5;
		  		//select.
		  	}
		  	//if(transport.responseJSON.sub != 0) 
		  		select.onchange = function(ev){
		  			getAttr(transport.responseJSON.sub,$("cr_"+crid).value);
		  		}
		  	transport.responseJSON.items.each(function(s){
		  		var opt = new Element("option",{value:s.id}).update(s.label);
		  		select.insert(opt);
		  	});
		  	$("span_cr_"+crid).update(select);
	  	}else{
	  		$("span_cr_"+crid).update("Alt Kategori Bulunamadı");
	  	}
	  }
	});
}

function getTown(cid, selected){
	
	var url = "/ad/xml/stown/c/"+cid;
	new Ajax.Request(url, {
	  method: 'get',
	  contentType: 'application/json',
	  onSuccess: function(transport) {
	  	if(transport.responseJSON.length > 0){
	  		
		  	var select = new Element("select", {id:"town", "name":"s[town]", "class":"combobox"});
		  	var opt = new Element("option",{value:0}).update("Tüm İlçeler");
		  	select.insert(opt);
		  	
		  	transport.responseJSON.each(function(s){
		  		var opt = new Element("option",{value:s.id}).update(s.label);
		  		if(selected == s.id) opt.selected = true;
		  		select.insert(opt);
		  	});
		  	
		  	select.style.width = "170px";		  	
		  	select.onchange = function(){
		  		getCounty($("town").value);
		  	}
		  	
		  	$("towns").update(select);
	  	}
	  }
	});
	if(selected != null)
		getCounty(selected);
}

function getCounty(cid){
	if(cid != 0){
		var url = "/ad/xml/scounty/c/"+cid;	
	}else{
		var url = "/ad/xml/scounty/c/"+cid+"/ct/"+$("city").value;
	}
	
	new Ajax.Request(url, {
	  method: 'get',
	  contentType: 'application/json',
	  onSuccess: function(transport) {
	  	if(transport.responseJSON.length > 0){
		  	var select = $("county_s");
		  	var options = select.childElements();

		  	for(i=0;i<options.length;i++)
		  		select.down().remove();
		  	
		  	transport.responseJSON.each(function(s){
		  		var opt = new Element("option",{value:s.id}).update(s.label);
		  		if(s.id.include("0000"))
  					opt.addClassName("soption");
		  		select.insert(opt);
		  	});
	  	}
	  }
	});
}

var added_county = [];
function addCounty(){
	var source = $("county_s");
	var target = $("county_t");
	var options = source.childElements();
	var t_options = target.childElements();
  	for(i=0;i<options.length;i++){
  		if(options[i].selected){
  			var opt = new Element("option",{value:options[i].value}).update(options[i].text);
  			if(options[i].value.include("0000"))
  				opt.addClassName("soption");
  			if(added_county.indexOf(opt.value) == -1){
	  			target.insert(opt);
	  			added_county.push(opt.value);			
  			}
  		}
  			
  	}
  		
}

function removeCounty(){
	var target = $("county_t");
	var options = target.childElements();
  	for(i=0;i<options.length;i++){
  		if(options[i].selected){
  			options[i].remove();
  			added_county = added_county.without(options[i].value);
  		}
  			
  	}
}

function selectCounties(){
	var target = $("county_t");
	var options = target.childElements();
  	for(i=0;i<options.length;i++){
  		options[i].selected = true;  			
  	}
}

function addCountyArray(county){
	var target = $("county_t");
	for(i=0;i<county.length;i++){
		var opt = new Element("option",{value:county[i].value}).update(county[i].text);
		if(county[i].value.include("0000")){
			opt.addClassName("soption");
			opt.text = "Tüm " + opt.text;
		}
		target.insert(opt);
		added_county.push(opt.value);
  	}
}

function scrollToCt(con, el){
	$(con).scrollTop = $(el).offsetTop - 100;
}