$(document).ready(function(){
	var path = "http://www.test3.maniac.com.ua/";
	if (window.location.hostname == "localhost")
	{
		path = "http://localhost/businessbroker.com.ua/www/";
	}
	var langid = $("#langid").attr('value');
	var tbl = $("#tbl").attr('value');
	
	function bbcatajax(param, sel){
		$.ajaxSetup({
      		url: path+'bb_ajax.php?l=' + langid + '&cat=' + param + "&p1=typ" + "&tbl=" + tbl,      
      		type: "GET",
      		dataType : 'xml',
     		success: function(data){
     			$('#bbtyp').empty();
     			$('#bbtyp').append('<option>::all::</option>');
				$(data).find('el').each(function(){
                    var id = $(this).find('id').text();
                    var text = $(this).find('t').text();
                    var selected = "";
                    if (sel == id) {
                    	var selected = " selected";
                    }
					$('#bbtyp').append('<option value="' + id + '"'+selected+'>&nbsp;&nbsp;' + text + '</option>');
				});
				$('#bbtyp').removeAttr("disabled");
				$('#bt').removeAttr("value");
				$('#br').removeAttr("value");
				$('#bbreg').attr("disabled", "disabled");
				$('#bbreg').empty();
      		},
     		cache: false
   		}); 
   		$.ajax({ data: $(this).attr('value') });
	}
	
	function bbtypajax (param, sel){
		$.ajaxSetup({
      		url: path+'bb_ajax.php?l=' + langid + '&cat=' + $("#bbcat").val() + "&p1=reg" + "&t=" + param + "&tbl=" + tbl,
      		type: "GET",
      		dataType : 'xml',
      		timeout: 2000,
     		success: function(data){
     			$('#bbreg').empty();
     			$('#bbreg').append('<option>::all::</option>');
				$(data).find('el').each(function(){
                    var id = $(this).find('id').text();
                    var text = $(this).find('t').text();
                    var selected = "";
                    if (sel == id) {
                    	var selected = " selected";
                    }
					$('#bbreg').append('<option value="' + id + '"'+selected+'>&nbsp;&nbsp;' + text + '</option>');
				});
				$('#br').removeAttr("value");
				$('#bbreg').removeAttr("disabled");
      		},
     		cache: false
   		}); 
   		$.ajax({ data: $(this).attr('value') });
	}
	
	var bbcat2 = $("#bc").attr("value");
	var bbtyp2 = $("#bt").attr("value");
	var bbreg2 = $("#br").attr("value");
	
	if (bbcat2){
		bbcatajax(bbcat2, bbtyp2);
	}
	
	if (bbtyp2){
		bbtypajax(bbtyp2, bbreg2);
//		$("div#ohfilter").find("span").addClass("activespan");
//		$("div#ohcontent").toggle("slow");
	}
	
	$("div#ohfilter").click(function(){
		$(this).find("span").toggleClass("activespan");
//		$("div#ohcontent").toggle("slow");
	});
	
	$("#bbcat").change(function(){
		$("#bc").attr("value", $(this).val());
		bbcatajax($(this).val());
	});
	
	$("#bbtyp").change(function(){
		$("#bt").attr("value", $(this).val());
		bbtypajax($(this).val());
	});
	
	$("#bbreg").change(function(){
		$("#br").attr("value", $(this).val());
	});
	
});