$(document).ready(function(){
	
	//Create The Tabs
	$( "#tabsSimple" ).tabs();

	//Hide the loading Div
	$("#loading").hide('slow');
	//Show the Tabs
	$("#tabsSimple").show('slow');
	$("#advancedaccordion").accordion({ header: "h3", collapsible: true, active: false });
	$(".expand").colorbox({width:"50%", inline:true, href:"#searchresultsdiv"});
	$('#searchform').ajaxForm({
        dataType: 'xml',
        success: showResultsPane,
	beforeSubmit: function() {
	    $("#working").show();
	    $("#results").html("<p align=center><img src=/images/loading.gif>");
	    $("#tabs2").tabs({ selected: 0 }); // go back to Map
	    firstTime = true;
	}
   	 });

	setupForm();
	$('#searchform').ajaxSubmit({
	dataType: 'xml',
	success: showResultsPane,
	beforeSubmit: function() {
	    firstTime = true;
	    
	    $("#working").show();
	    $("#results").html("<p align=center><img src=/images/loading.gif>");
	}
    });

	

});

function showResultsPane(data)
{
   
    
    var results = $('results', data).text();
    var stats = $('statistics', data).text();
    
    $("#results").html(results);
    

    // if there's nothing in stats, it must be the initial search
    // don't switch if there are no results
    // log.debug(data.documentElement.getElementsByTagName("results")[0].getAttribute("number"));
    if ($("#stats").html() != '' && data.documentElement.getElementsByTagName("results")[0].getAttribute("number") > 0) { $( "#tabs1" ).tabs({ selected: 1 }); }
    $("#stats").html(stats);
    
        
    listings = data.documentElement.getElementsByTagName("listings");
    if (data.documentElement.getElementsByTagName("results")[0].getAttribute("number") == 1)
    {
	firstTime = false;
	ShowDetails(listings[0].getAttribute("first-ls"), listings[0].getAttribute("first-mls"), '', true);
    }
    

    $("#working").hide();
}

function PrevNext(offset, noresults)
{
    $('#searchform').ajaxSubmit({
	data: { pg: offset },
	dataType: 'xml',
	success: showResultsPane,
	beforeSubmit: function() {
	    firstTime = false;
	    $("#tabs2").tabs({ selected: 0 }); // go back to Map
	   
	    $("#results").html("<p align=center><img src=/images/loading.gif>");
	}
    });
}

function ClearAndFocus(e)
{
    e.value = '';
    e.focus();
}

function showResultsDiv(data)
{
	var results = $('results', data).text();
	$("#results").html(results);
}

function mlsDisclaimer(f)
{
    var url = f;
    $.ajax({
	url: url,
	success: function(html) {
	    $("#dialog").html(html);
	    $("#dialog").dialog({
		autoOpen: true,
		modal: true,
		height: 600, width: 600,
		resizable: false,
		draggable: false,
		title: "MLS Disclaimer",
		buttons: {
		    "OK": function() { $(this).dialog("close"); }
		}
	    });
	}
    });
}

