var oTable;
var bLoadGoogleAd = true;

$(document).ready(function() {	
    var cond = window.location.hash;
    if (qs!=""){cond=qs;}
    if (cond !="")
    {   
        if(cond.substring(0,1)=="#"){cond = cond.substr(1)};
        if(cond.substring(0,1)=="#"){cond = cond.substr(1)};
        $("#actiontype").val(hashvalue(cond,"at="));
        $("#propertytype").val(hashvalue(cond,"pt="));
        $("#select_province").val(hashvalue(cond,"pr="));
	    $.ajax({
		    url: ('ajax_municipalities'),
		    data: 'p=' + hashvalue(cond,"pr="),
		    type: 'GET',

		    success: function( newDatas ) {
			    $('#select_municipality_container').html( newDatas );
			    },
		    complete: function(){
			    $('#select_municipality_container').removeClass("loading_ajax2");
			    $("#select_municipality").val(hashvalue(cond,"mu="));
			    }
	    })        
        
        $("#select_timeframe").val(hashvalue(cond,"tf="));
        $("#select_sort").val(hashvalue(cond,"o="));
        $('#bedrooms').slider("option", "value", hashvalue(cond,"b=")); 
        $("#price").slider("values", 0, hashvalue(cond,"p1="));
        $("#price").slider("values", 1, hashvalue(cond,"p2="));
        $("#bedcount").val(hashvalue(cond,"b="));
        $("#amount").val(milsep($("#price").slider("values", 0)) + ' \u20AC  -  ' + milsep($("#price").slider("values", 1)) + ' \u20AC');
		
	}
	
	oTable = $('#table_results').dataTable( {
		"aoColumns": [ 
			/* Foto */   { "fnRender": function ( oObj ) {
				return "<img src='http://www.inmolia.com/fotos/" + oObj.aData[ oObj.iDataRow ][0] + "_1_tn.jpg' />";
			},
			"bSortable": false,
			"sClass": "col_pic" },
			/* Property-Type */  { "sClass": "col_type"},
			/* Municipality */ { "sClass": "col_municipality"},
			/* Bedrooms */  { "sClass": "tcenter",
							  "sType": "numeric" },
			/* Bathrooms */  { "sClass": "tcenter",
							   "sType": "numeric" },
			/* Meters */  { "sClass": "tcenter",
							"sType": "numeric" },
			/* Price */   { "fnRender": function ( oObj ) {
				return milsep(oObj.aData[ oObj.iDataRow ][6]) + " \u20AC";
			},
			"sClass": "tright",
			"sType": "numeric" },
			/* Date */  { "sClass": "tcenter",
						  "iDataSort": 8 },
			/* Details */   { "fnRender": function ( oObj ) {
				return "<a target='_new' rel='nofollow' href='details?id=" + oObj.aData[ oObj.iDataRow ][8] + "'><img src='images/icon_details.gif' / border='0'></a>";
			},
			"bSortable": false
			},
			{"bVisible": false}
		],		
		"bProcessing": true,
		"bLengthChange": false,
		"iDisplayLength": 11,
		"bFilter": false,
		"aaSorting": [[ 9, "desc" ]],
		"sPaginationType": "full_numbers",
		"bStateSave": false,
		"sAjaxSource": "ajax_json_properties?" + cond,
		"fnDrawCallback": function () {			
			if(bLoadGoogleAd)
			{
                var sGCode = "<iframe src=\"http://www.inmolia.com/inc/adverts?" + qs + "&lang=<%=lang%>\" width=\"100%\" height=\"60\" scrolling=\"no\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\"/>"
                var nRow = document.createElement( 'tr' );
                var nCell = document.createElement( 'td' );
                nCell.colSpan = 9;
                nCell.innerHTML = sGCode;
                nRow.appendChild( nCell );			
                var jqRows = $('#table_results tbody tr');
                var iRows = jqRows.length;
                var iCenter = Math.floor( iRows / 2 );
                var nInsertPoint = jqRows[ iCenter ];
                $(nRow).insertAfter( nInsertPoint );
            }	
		},
		
		"fnInitComplete": function () {			
				bLoadGoogleAd=false;	
		},
		
		"fnHeaderCallback": function( nHead, aasData, iStart, iEnd, aiDisplay ) {
			if(iStart==0 && iEnd!=0)
			{
                bLoadGoogleAd=true;
            }
            else
            {
                bLoadGoogleAd=false;
            }		
		}			
	} );		
} );