
function initSearch(base_url) {
	$(".colorbox").colorbox( {
		photo : true,
		current : '{current} / {total}',
		maxWidth : '100%',
		maxHeight : '100%'
	});

	$(".offer_send_to").colorbox( {
		iframe : true,
		width : "50%",
		height : "80%"
	});

	$("#search_button").click(function() {
		var country = $("input#filter\\.PrevCountry").val();
		var offerType = $("select#filter\\.OfferType").val();
		var realtyType = $("select#filter\\.RealtyType").val();
		var region = $("select#filter\\.Region").val();
		var priceFrom = $("input#filter\\.PriceFrom").val();
		var priceTo = $("input#filter\\.PriceTo").val();
		var paymentUnit = $("select#filter\\.PaymentUnit").val();
		
		var url = base_url + 'offers?offertype=' + offerType
			+ '&realtytype=' + realtyType
			+ '&country=' + country;
		if (region != '*')
		{
			url += '&region=' + region;
		}
		if (priceFrom != '')
		{
			url += '&pricefrom=' + priceFrom;
		}
		if (priceTo != '')
		{
			url += '&priceto=' + priceTo;
		}
		if (paymentUnit != '')
		{
			url += '&paymentunit=' + paymentUnit;
		}
		if ($("div#roomsContainer:visible").length != 0)
		{
			var rooms = $("select#filter\\.Rooms").val();
			if (rooms != '')
			{
				url += '&rooms=' + rooms;
			}
		}
		
		window.location.href = url;
	});

	$("select#filter\\.OfferType").change(function() {
		var offerType = $(this).val();
		var prevPaymentUnit = $("input#filter\\.PrevPaymentUnit").val();
		$.getJSON(base_url + 'vocabularies/paymentunits',
			{ 'offertype': offerType },
			function(data) {
				$("select#filter\\.PaymentUnit").html(fillSelect(data, prevPaymentUnit));
			}
		);
	});

	$("select#filter\\.RealtyType").change(function() {
		var realtyType = $(this).val();
		if (realtyType == 'apartment' ||
			realtyType == 'house' ||
			realtyType == 'office')
		{
			if ($("div#roomsContainer:visible").length == 0)
			{
				$("div#roomsContainer").show(0);
			}
		}
		else if ($("div#roomsContainer:hidden").length == 0)
		{
			$("div#roomsContainer").hide(0);
		}
	});
	
	var prevOfferType = $("input#filter\\.PrevOfferType").val();
	$.getJSON(base_url + 'vocabularies/offertypes',
		function(data) {
			$("select#filter\\.OfferType").html(fillSelect(data, prevOfferType));
			$("select#filter\\.OfferType").change();
		}
	);
	
	var prevRealtyType = $("input#filter\\.PrevRealtyType").val();
	$.getJSON(base_url + 'vocabularies/realtytypes',
		function(data) {
			$("select#filter\\.RealtyType").html(fillSelect(data, prevRealtyType));
			$("select#filter\\.RealtyType").change();
		}
	);
	
	var prevCountry = $("input#filter\\.PrevCountry").val();
	var prevRegion = $("input#filter\\.PrevRegion").val();
	$.getJSON(base_url + 'vocabularies/regions',
		{ 'country': prevCountry },
		function(data) {
			$("select#filter\\.Region").html(fillSelect(data, prevRegion));
		}
	);
	
	var prevRooms = $("input#filter\\.PrevRooms").val();
	$.getJSON(base_url + 'vocabularies/rooms',
		function(data) {
			$("select#filter\\.Rooms").html(fillSelect(data, prevRooms));
		}
	);
}

