$(function() {
	var t;
	$("table").tablesorter(); 
	$("select, input[type=checkbox], input").bind('change keyup', function () {	
		//Fischer_Carlyle.showProgressDialog(); //function call
		//showProgressDialog();
	clearTimeout(t);	
	function slowAlert(){
	var getData = $("#filterForm").serialize();
		$.ajax({
		type:"post",
		url: "filter-side-nav.php",
		data: getData,
		dataType: 'json',
			success: function(data){			
    			if(data == null){
						//$('#filterstatus').html('No Match');
						alert("No Match");
    					return false;
    			}
    			    parseReturnedData(data.results);
                    $("table").trigger("update");
			}
		});
		}		
		t = setTimeout(slowAlert, 2000);
		//Fischer_Carlyle.filterSuccess();
	});

	/*
	$( "#dialog-filter" ).dialog({
		autoOpen: false,
		show: "blind",
		hide: "explode",
		modal: true,
		width:200,
		closeText: "hide"
	}).css("background-color","none"); 


;Fischer_Carlyle = { 
		name: 'Fischer_Carlyle',

		showProgressDialog: function(){
			$( "#dialog-filter" ).dialog( "open" );
		},

		filterSuccess: function (){
			$( "#filterstatus" ).html("Searching........");
				setTimeout(function() {
					$('#dialog-filter').dialog( "close" );
			}, 3000);
		}
	}
	*/
});

$(function() {
	$("#reset").click(function() {
	var categoryid = $("input[name=CategoryId]").val();
		$.ajax({
				url: 'filter-side-nav.php',
				dataType: 'json',
				data: {reset:'reset', CategoryId:categoryid},
				success: function(data){			
						if(data == null)
						{
								alert("No Match");
								return false;
						}
						parseReturnedData(data.results);
				}
		});
        $("input:not(:checked, input[name='b']) + span")
        $('input:not(input[name="CategoryId"] , input[type="checkbox"])').each(function() {
             $(this).val('');
        });
        $('select').each(function() {
            $(this).val($('option:first', this).val());
        });

           $('input[id="check1"]').attr('checked','checked');
           $('input[id="check2"]').attr('checked','checked');
           $('input[id="check3"]').removeAttr('checked');

	});
});



function parseReturnedData(returned){
	newPropertyRows = '';	
		var categoryid = $("input[name=CategoryId]").val();
		switch(categoryid){			
			case '31':
				propType = 'rental';
				break;
			case '32':
				propType = 'commercial';
				break;
			case '21':
				propType = 'featured';
				break;	
				}
				$("#featured_listing_tbody").children( 'tr:not(:first)' ).remove();
				counter= 1;
				$.each(returned, function(i, val){	
							newPropertyRows += '<tr>';
									$.each(val, function(key, info){
										var skip = false;
											if(key == "Id")	{											
													Id =  info;
													newPropertyRows += '';
													skip = true;
											}
											if(key == "thumbs"){
													if(info == null){
														info = '<img width="100px" src="uploads/default.jpg" alt="" />';
													}else{
														info = '<img width="100px" src=uploads/properties/'+propType+'/'+Id+'/thumbs/'+info+' />';
													}
													newPropertyRows += '<td class="col'+counter+'"><a href="/featured.php?prop='+Id+'">'+info+'</a></td>';	
													skip = true;
													counter++;		
											}
											if(key == "property_status"){
													if(info == null || info == "")
													{
													newPropertyRows += '<td class="col'+counter+'"><a href="/featured.php?prop='+Id+'">N/A</a></td>';						
													counter++;		
													}else{
														
														if(info == "Available Soon") {
														 info2 = "AvailableSoon";
														}
														else {
															info2 = info;
														}
													newPropertyRows += '<td class="col'+counter+'"><a class="'+info2+'" href="/featured.php?prop='+Id+'">'+info+'</a></td>';						
													counter++;					
													}
													skip = true;
											}
											if(skip == false){
													if(info == null || info == "")
													{
													newPropertyRows += '<td class="col'+counter+'"><a href="/featured.php?prop='+Id+'">N/A</a></td>';						
													counter++;		
													}else{
													newPropertyRows += '<td class="col'+counter+'"><a href="/featured.php?prop='+Id+'">'+info+'</a></td>';						
													counter++;					
													}
											}
											
											info = '';
									});						
									counter = 1;
							newPropertyRows += '</tr>';							
				});						
					$("#featured_listing_tbody").html(newPropertyRows);
}


