var va = "vendita";

$(function(){
	family[0]=$('#regione');	family[1]=$('#provincia');	family[2]=$('#id_citta'); family[3]=$('#id_cat');	family[4]=$('#fascia');
				
	$('.fake_cont').append('<div class="fake_sele"></div><div class="fake_but"><img src="img/transp.gif"></div>');
	$.each(family, function(i,n){
		id = n.attr("id");
		n.append('<input type="hidden" name="'+id+'" value="0" />');
	});
	
	$('.fake_but').children('img').click(
	
		function(){
			$('.fake_list').remove()
			this_cont = $(this).parents().parents('.fake_cont');
			for(f=0;family.length;f++)
				if(this_cont.attr("id") == family[f].attr("id")) break;
			padre='';
			if(f>0)	padre=family[f-1].children('input').val()
			if(f>=dipendenti.length || f==0){
				this_cont.append('<div class="fake_list"></div>');
				this_list = this_cont.children('.fake_list');
				this_list.css({top: this_cont.css("top")})
				this_sel=campi[f];
				for(r=0;r<this_sel.length;r++) this_list.append(this_sel[r]);		
				$('.fake_opt').bind('click', {prox: f}, click_fake_opt);
			 }else if(padre!='0'){
			 	this_cont.append('<div class="fake_list"></div>');
				this_list = this_cont.children('.fake_list');
				this_list.css({top: this_cont.css("top")})
				this_sel=campi[f];
				for(r=0;r<this_sel.length;r++) 
					if(this_sel[r].indexOf(padre)>-1) this_list.append(this_sel[r]);		
				$('.fake_opt').bind('click', {prox: f}, click_fake_opt);
			 }
			 
		});
	
	$('.va').click(function(){
		$('.fake_list').remove();
		$('#fascia').children('input').val(0);
		$('#fascia').children('.fake_sele').html('');
		if($(this).val() == "vendita") {
			campi[4]=prezzi[0]; 
			va = "vendita";
		}	else {
			campi[4]=prezzi[1];
			va = "affitto";
		}
	});
	
	
	$('#vai_ric').click(function(){
		$('.fake_list').remove();
		url = "ricerca/"+va;
		val = "/(";
		$.each(family, function(i,n){
			disp = n.children('input').val();
			seldisp = n.children('.fake_sele').html();
			if(disp != '' && disp != 'qualsiasi' && Number(disp)!= disp) {
				if(i==0)	url+= "/"+disp;
				else url += "/prov-"+disp;
			}
			else if(i==0 )	url+= "/tutte-le-regioni";
			if(i==2 || i ==3){ /* citta o tipologia, costruire la 'chiave' dal valore*/
				if(seldisp !='' && seldisp != 'qualsiasi' )
					url+= "/"+chiave(seldisp);
				else if(i==2) url += "/tutte-le-citta";
				else if(i==3) url += "/ogni-tipologia";
					
			}
			if(Number(disp) == disp) 	val += Number(n.children("input").val())+",";
		});
		val = val.substr(0,val.length-1);
		window.location=url+val+").html";
	});
	
	$('#vai_ric_a').click(function(){
		$('.fake_list').remove();
		action = "ricerca-avanzata/"+va;
		val = "/(";
		$.each(family, function(i,n){
			disp = n.children('input').val();
			seldisp = n.children('.fake_sele').html();
			if(disp != '' && disp != 'qualsiasi' && Number(disp)!= disp) {
				if(i==0)	action+= "/"+disp;
				else action += "/prov-"+disp;
			}
			else if(i==0 )	action+= "/tutte-le-regioni";
			if(i==2 || i ==3){ /* citta o tipologia, costruire la 'chiave' dal valore*/
				if(seldisp !='' && seldisp != 'qualsiasi' )
					action+= "/"+chiave(seldisp);
				else if(i==2) action += "/tutte-le-citta";
				else if(i==3) action += "/ogni-tipologia";
					
			}
			if(Number(disp) == disp) 	val += Number(n.children("input").val())+",";
		});
		val = val.substr(0,val.length-1);
		action+=val+").html";
		$('#form_ricerca').attr("action", action);
		$('#form_ricerca').submit();
	});
	
	function chiave(str){
		var strout = new String("");
		var strin = str.toString();
		strin = strin.replace("<br/>","");strin = strin.replace("<br>","");
		strin = strin.replace("<BR/>","");strin = strin.replace("<BR>","");
		for(s=0;s<strin.length;s++){
			cs = strin.charAt(s);
			if(cs == "'" || cs == " ") strout += '-';
			else if(Number(cs)==cs) strout+=cs;
			else if(cs.toUpperCase() != cs.toLowerCase()) strout+=cs.toLowerCase();
		}
		return strout;
	}

	function click_fake_opt(event){
		val = $(this).attr("name")	;
		disp = $(this).html();
		pre_val = '';
		pre_val = $(this).parent('.fake_list').siblings('input').val();
		$(this).parent('.fake_list').siblings('input').val(val);
		$(this).parent('.fake_list').siblings('.fake_sele').html(disp);
		$('.fake_list').remove();
		if(pre_val != val)
			for(i=event.data.prox+1;i<dipendenti.length;i++){
				family[i].children('input').val('0');
				family[i].children('.fake_sele').html('');
		}		
	}

});
