function milsep(number) {
	number = '' + number;
	if (number.length > 3) {
	var mod = number.length % 3;
	var output = (mod > 0 ? (number.substring(0,mod)) : '');
	for (i=0 ; i < Math.floor(number.length / 3); i++) {
	if ((mod == 0) && (i == 0))
	output += number.substring(mod+ 3 * i, mod + 3 * i + 3);
	else
	output+= '.' + number.substring(mod + 3 * i, mod + 3 * i + 3);
	}
	return (output);
	}
	else return number;
}
function hashvalue(h, f)
{
    var pos1 = h.toString().indexOf(f);
    if (pos1>=0)
        {var pos2 = h.toString().indexOf("=",pos1)+1;}
    else
        {return "";}
    if (pos2>0)
        {var pos3 = h.toString().indexOf("&",pos2);}
    else
        {return "";}
    if (pos3>pos2)
        {return h.toString().substr(pos2,pos3-pos2);}
    else
        {return "";}
}
            				

$(function() {
	$("#price").slider({
		range: true,
		min: 0,
		max: 1000000,
		values: [0, 1000000],
		step: 10000,
		animate: true,
		slide: function(event, ui) {
			$("#amount").val(milsep(ui.values[0]) + ' \u20AC  -  ' + milsep(ui.values[1]) + ' \u20AC');
		}				
	});
	$("#amount").val(milsep($("#price").slider("values", 0)) + ' \u20AC  -  ' + milsep($("#price").slider("values", 1)) + ' \u20AC');
});

$(function() {
	$("#bedrooms").slider({
		range: false,
		min: 0,
		max: 9,
		value: 0,
		step: 1,
		animate: true,
		slide: function(event, ui) {
			$("#bedcount").val(ui.value);
		}			
	});
	$("#bedcount").val($("#bedrooms").slider('option', 'value'));
});	



$(function() {
	$('#select_province').change(function(){
		$('#select_municipality_container').html("");
		$('#select_municipality_container').addClass("loading_ajax2");
		$.ajax({
			url: ('ajax_municipalities'),
			data: 'p=' + $(this).val(),
			type: 'GET',

			success: function( newDatas ) {
				$('#select_municipality_container').html( newDatas );
				},
			complete: function(){
				$('#select_municipality_container').removeClass("loading_ajax2");
				}
		})
	});
});


$(document).ready(function(){
	$('.btn').each(function(){
		var b = $(this);
		var tt = b.text() || b.val();
		if ($(':submit,:button',this)) {
			b = $('<a>').insertAfter(this). addClass(this.className).attr('id',this.id);
			$(this).remove();
		}
		b.text('').css({cursor:'pointer'}). prepend('<i></i>').append($('<span>').
		text(tt).append('<i></i><span></span>'));
	});
	
	
    $('#reload').click(function(){
		var beds = $("#bedrooms").slider('option', 'value');
		var price_min = $("#price").slider("values", 0);
		var price_max = $("#price").slider("values", 1);
		var province = $("#select_province").val();
		var municipality = $("#select_municipality").val();
		var timeframe = $("#select_timeframe").val();
		var order = $("#select_sort").val();
		var action_type = $("#actiontype").val();
		var property_type = $("#propertytype").val();
		var savesearch = $('#check_savesearch:checked').val();
		var cond = "b=" + beds + "&p1=" + price_min + "&p2=" + price_max + "&pr=" + province + "&mu=" + municipality + "&o=" + order + "&tf=" + timeframe + "&at=" + action_type + "&pt=" + property_type + "&ss=" + savesearch;
        if (qs!=""){document.location.href="http://www.inmolia.com#" + cond;}
        var url = "ajax_json_properties?" + cond;
		window.location.hash = "#" + cond;
		oTable.fnReloadAjax(url); 
	    $.ajax({
		    url: ('ajax_tableheader'),
			data: cond,
			type: 'GET',

		    success: function( newDatas ) {
			    $('#table_header').html( newDatas );		    
			    }
	    })				
    });      	
});

function lastsearch(cond) 
{ 
    var url = "ajax_json_properties?" + cond;
    window.location.hash = "#" + cond;
    oTable.fnReloadAjax(url); 
}


$.fn.dataTableExt.oApi.fnReloadAjax = function ( oSettings, sNewSource )
{
    oSettings.sAjaxSource = sNewSource;
    this.fnClearTable( this );
    this.oApi._fnProcessingDisplay( oSettings, true );
    var that = this;
    $.getJSON( oSettings.sAjaxSource, null, function(json) {
        for ( var i=0 ; i<json.aaData.length ; i++ )
        {
            that.oApi._fnAddData( oSettings, json.aaData[i] );
        }
         
        oSettings.aiDisplay = oSettings.aiDisplayMaster.slice();
        that.fnDraw( that );
        that.oApi._fnProcessingDisplay( oSettings, false );
    } );
}

