jQuery(document).ready(function(){
	
	var minheight = 230;
	$('li.property:not(.open)').height(minheight);
	$('li.property').addClass('js');
	$('.fast_select li.property:not(.open) input.not_defined').parent().hide();
	
	//
	// Peidame listis valikud mis ei mahu kuvatava kasti sisse
	//
	$('li.property:not(.open)').each(function () {
				
			var header = $('h2',this).outerHeight( );
			var list = $('ul',this).outerHeight( );
			var more = $('.more',this).outerHeight( );
			var bottom_margin = 2;
			var overflow = minheight - header - list - bottom_margin;
			var show_more = false;
			
			// Kui kogukõrgus on suurem kui lubatud siis peidame üleliigsed valikud ja kuvame "Rohkem valikuid" nupu.
			if ( overflow < 0 )
			{
				var allowed_height = minheight-(header)-bottom_margin;
				var height_counter = 0;
				$('li', this).each(function (){

					if( $(this).is(':visible') ) {
						height_counter += $(this).outerHeight();
					}

					if( ( allowed_height < height_counter ) )
					{
						$(this).hide();
						$(this).addClass('hide_on_blur');
						show_more = true;
					}
					});
			}
			
			// Kuvame "Rohkem valikuid"
			if( show_more ) {
				$(this).children('div.more').show();
			}
		});
		
	//
	// Kastist hiirega üle liikudes kuvame peidetud valikud
	// Pärast peidame jälle ära
	//
	$('li.property').hover(
		function () {
			// Alati avatud
			if ( $(this).hasClass('open') ) {
				return true;
			}
			
			var top = $(this).position().top;
			var left =  $(this).position().left;
			$(this).css('top', top);
			$(this).css('left', left);

			if( $(this).hasClass('wide') ) {
				$(this).before( '<li id="property_placeholder" class="wide">&nbsp;</li>' );
			} else {
				$(this).before( '<li id="property_placeholder">&nbsp;</li>' );
			}
			$(this).addClass( 'hover' );
			if( $('li.hide_on_blur', this).length > 0 ) {
				$(this).height('auto');
				$('li.hide_on_blur', this).show();
			}
			$('div.more', this).hide();
		},
		function () {
			// Alati avatud
			if ( $(this).hasClass('open') ) {
				return true;
			}
			
			$('#property_placeholder').remove();
			$(this).removeClass( 'hover' );
			$(this).css('top', 0);
			$(this).css('left', 0);
			$(this).height(minheight);
			$('li.hide_on_blur', this).hide();
			if ($('li.hide_on_blur', this).length > 0 ) {
				$('div.more', this).show();
			}
		}
		);
		
	//
	// Tegeleme valiku tausta värvimisega
	//
	$("li.property input:checkbox").click(function(){
			if($(this).is(':checked')) {
				$(this).parent().addClass('checked');
				
				var str = $(this).attr('id');
				var not_defined_id = str.replace(/t\d{1,}/g, '');

				if( !$(this).hasClass('not_defined') && $("input#"+not_defined_id+"").is(':checked') ) {
					$("input#"+not_defined_id+"")
						.removeAttr('checked')
						.parent().removeClass('checked');
					$(".property input[id^="+not_defined_id+"t]").parent().removeClass('disabled');
				}
			} else {
				$(this).parent().removeClass('checked');
			}
		})
		.each(function(){
		// Järsku on HTMLis valik checkitud?
			if($(this).is(':checked')) {
				$(this).parent().addClass('checked');
			}
		});

	//
	// Tegeleme valikuga "määramata"
	//
	$("li.property input.not_defined:checkbox")
		.click(check_not_defined)	// Kuulame eventi
		.each(check_not_defined_onload);	// Järsku on HTMLis valik checkitud?
	
	
	//
	// Peidame detailid
	//
	$('ul#search_details').hide();
	
	
	//
	// Kui kirjutatakse midagi hinnavahemiku kastidesse siis kustutame kiirvaliku
	//
	$('input#minPrice, input#maxPrice').change(function(){
			if( $(this).val() > 0 ) {
				$(".property#price_select input").removeAttr('checked').parent().removeClass('checked');
			}
		});
		
	//
	// Kui valitakse kiirvalikust hind siis kustutame hinnavahemiku väärtused
	//
	$('.property#price_select input').change(function(){
		// Järsku on HTMLis valik checkitud?
			if( $(this).is(':checked') ) {
				$('input#minPrice, input#maxPrice').val('');
			}
		});
});

//
//
//
function clean_fields()
{
	$("li.property input:checkbox").removeAttr('checked').parent().removeClass('checked').removeClass('disabled');
}

//
// Valik "määramata"
//
function check_not_defined()
{
	if($(this).is(':checked')) {
		$(this).parent().addClass('checked');
		$(".property input[id^="+$(this).attr('id')+"t]").removeAttr('checked').parent().removeClass('checked').addClass('disabled');
	} else {
		$(this).parent().removeClass('checked');
		$(".property input[id^="+$(this).attr('id')+"t]").attr('checked', 'checked').parent().addClass('checked').removeClass('disabled');
	}
}
function check_not_defined_onload()
{
	if($(this).is(':checked')) {
		$(this).parent().addClass('checked');
		$(".property input[id^="+$(this).attr('id')+"t]").removeAttr('checked').parent().removeClass('checked').addClass('disabled');
	}
}