function submitDefault(){
	//alert("Default");
  if($("#busyDialog").dialog('isOpen'))
	$('#busyDialog').dialog('close');
}

function submitWaiting(){
  if(!$("#busyDialog").dialog('isOpen')){	
	$("#busyDialog").dialog().parents(".ui-dialog").find(".ui-dialog-titlebar").remove();
    $('#busyDialog').dialog('open');
  }
}

$(document).ready(function(){
	//References
	var sections = $(".sf-menu li");
	var loading = $("#loading");
	var content = $("#mainContent");
	var rightContent = $("#rightContent");
	var mlsContent = $("#mlsSearchDiv");
	
	//Manage click events
	sections.click(function(){
	  content = $("#mainContent");
	  rightContent = $("#rightContent");
	  sections = $(".sf-menu li");
	  mlsContent = $("#mlsSearchDiv");
	  submitWaiting();
		//show the loading bar
		//showLoading();
		//load selected section
		
		switch(this.id){
			case "noid":
				submitDefault();
				break;
			case "home":
				content.slideUp();
				content.load("home.php", changeBG2);
				break;
			case "mlsSearch":
				mlsContent.load("searchPage.php", changeBG3);
				break;
			case "helpfulLinks":
				rightContent.slideUp();
				rightContent.load("helpfulLinks.php",changeBG);
				break;
			case "vendorReferrals":
				rightContent.slideUp();
				rightContent.load("vendors.php",changeBG);
				break;
			case "contact":
				rightContent.slideUp();
				rightContent.load("contact.php", changeBG);
				break;
			case "aboutUs":
				rightContent.slideUp();
				rightContent.load("aboutUs.php", changeBG);
				break;
			default:
				//hide loading bar if there is no selected section
				//hideLoading();
				submitDefault();
				break;
		}
	});

function changeBG3(){
		document.getElementById("mlsSearchDiv").style.display="block";
		document.getElementById("rightBottomContent").style.display="none";
		document.getElementById("mainContent").style.display="none";
		
		
		//document.getElementById("rightBottomContent").style.display="none";
	  //document.getElementById("rightContent").style.backgroundColor="#FFFFFF";	
	  //document.getElementById("rightContent").style.border="1px solid #F0B310";
	  //alert("B3");
	  submitDefault();
	  //alert("B3");
	  //mlsContent.slideDown();
	  mlsContent.slideDown('slow', function() {
        $('#container').height("985px");
      });
	 /* var windowHeight = document.getElementById('mainContent').style.height + 100;
	  
	  document.getElementById('container').style.height=windowHeight +"px";
	  alert("bg3: " + windowHeight);*/
	  
	}
	
    function changeBG(){
		document.getElementById("mlsSearchDiv").innerHTML='';  //REMOVE SEARCH FROM DIV SO IT WILL STOP LOADING IF SWITCH LINKS
		document.getElementById("mainContent").style.display="block";
		//document.body.style.backgroundImage="none";
		document.getElementById("mlsSearchDiv").style.display="none";
		document.getElementById("rightBottomContent").style.display="none";
	  document.getElementById("rightContent").style.backgroundColor="#fffbe9";
	  document.getElementById("rightContent").style.backgroundImage="url(images/palmTree.png)";
	  document.getElementById("rightContent").style.border="3px solid #000000";
	  document.getElementById("rightContent").style.backgroundRepeat="no-repeat";
	  document.getElementById("rightContent").style.backgroundPosition="top right";
	  submitDefault();
	  //rightContent.slideDown();
//	  $("#rightContent").height(0);
	  //document.getElementById('rightContent').style.height='0px';
	//  document.getElementById('container').style.height='200px';
	  rightContent.slideDown('slow', function() {
		//alert($("#rightContent").height());
        var windowHeight = $("#rightContent").height() + 250;
	    $('#container').height(windowHeight +"px");
		//alert($("#rightContent").height());
      });
	  
	  /*var windowHeight = document.getElementById('mainContent').style.height + 100;
	  document.getElementById('container').style.height=windowHeight +"px";
	  alert("bg: " + windowHeight);
*/	}
	
	 function changeBG2(){
		 document.getElementById("mlsSearchDiv").innerHTML='';  //REMOVE SEARCH FROM DIV SO IT WILL STOP LOADING IF SWITCH LINK
		 document.getElementById("mainContent").style.display="block";
		 document.getElementById("rightBottomContent").style.display="block";
		 //document.body.style.backgroundImage="none";
		 document.getElementById("mlsSearchDiv").style.display="none";
	  document.getElementById("rightContent").style.backgroundColor="#29261F";	
	  document.getElementById("rightContent").style.border="none";
	  
	  document.getElementById("rightBottomContent").style.backgroundColor="#fffbe9";
	  document.getElementById("rightBottomContent").style.backgroundImage="url(images/palmTree.png)";
	  document.getElementById("rightBottomContent").style.border="3px solid #000000";
	  document.getElementById("rightBottomContent").style.backgroundRepeat="no-repeat";
	  document.getElementById("rightBottomContent").style.backgroundPosition="top right";
	  //content.slideDown();
	  content.slideDown('slow', function() {
										 //alert($("#mainContent").height());
        //var windowHeight = $("#mainContent").height() + 250;
	    $('#container').height("860px");
      });
	  
	  
	  
	 /* var windowHeight = document.getElementById('mainContent').style.height + 100;
	  document.getElementById('container').style.height=windowHeight +"px";
	  alert("bg2: " + windowHeight);*/
	  
	  $.get('../php/selectPropSubType.php',{'type': 'Residential', 'city': ''},
    function(data){
        $("select#propertySubType").html(data);
		$("select#propertySubType").parent()[0].innerHTML += '';
		});

  $.get('../php/selectCity.php',{'type': 'Residential'},
    function(data){
        $("select#city").html(data);
		$("select#city").parent()[0].innerHTML += '';
		});
  submitDefault();
	}
	
	//show loading bar
	function showLoading(){
		loading
			.css({opacity:"1"})
			.css({display:"block"})
		;
	}
	//hide loading bar
	function hideLoading(){
		loading.fadeTo(1000, 0);
		loading.css({display:"none"});
	};
	


});

function showMyListings(){
	alert("Shawn");
}

function loadSearchPage(agentID){
	//showLoading();
	submitWaiting();
  var params = "bedrooms="+document.getElementById("bedrooms").value+"&bathrooms="+document.getElementById("bathrooms").value+"&propertySubType="+encodeURIComponent(document.getElementById("propertySubType").value)+"&maximumPrice="+document.getElementById("maximumPrice").value+"&minimumPrice="+document.getElementById("minimumPrice").value+"&city="+encodeURIComponent(document.getElementById("city").value)+"&agentMLSID="+agentID;
	//alert(params);
	
  if (window.XMLHttpRequest) {
    var xmlhr = new XMLHttpRequest();
  } else {
	  try{ xmlhr = new ActiveXObject("MSXML2.XMLHTTP.6.0");  }catch(e){}
      try{ if(!xmlhr) xmlhr = new ActiveXObject("MSXML2.XMLHTTP");  }catch(e){}
  }
  
  xmlhr.open('POST', '../searchPage.php', true);
  xmlhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); 
  xmlhr.setRequestHeader("Content-length", params.length);
  xmlhr.setRequestHeader("Connection", "close");

 
  xmlhr.onreadystatechange = function() {
    if (xmlhr.readyState == 4 && xmlhr.status == 200) {
		//alert("Good");
		//alert(xmlhr.responseText);
      if (xmlhr.responseText) {
		  document.getElementById("mlsSearchDiv").innerHTML = xmlhr.responseText;
		  //alert("Default1");
		  document.getElementById("mlsSearchDiv").style.display="block";
		//alert("B1");
		document.getElementById("rightBottomContent").style.display="none";
		//alert("B2");
		document.getElementById("mainContent").style.display="none";
		
		$('#container').height("950px");
		
		  submitDefault();
		  //changeB3();
		  //alert("Default2");
		  //alert(document.getElementById("mainContent").innerHTML);
		  //hideLoading();
      }
    } else if (xmlhr.readyState == 4) {
      alert('Invalid response received - Status: ' + xmlhr.status);
	  //hideLoading();
    } 
  }
  xmlhr.send(params);
}

function loadCommunity(){
	//showLoading();
	submitWaiting();
  var params = "community="+encodeURIComponent("Heritage Palms");
	//alert(params);
	
  if (window.XMLHttpRequest) {
    var xmlhr = new XMLHttpRequest();
  } else {
	  try{ xmlhr = new ActiveXObject("MSXML2.XMLHTTP.6.0");  }catch(e){}
      try{ if(!xmlhr) xmlhr = new ActiveXObject("MSXML2.XMLHTTP");  }catch(e){}
  }
  
  xmlhr.open('POST', '../searchPage.php', true);
  xmlhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); 
  xmlhr.setRequestHeader("Content-length", params.length);
  xmlhr.setRequestHeader("Connection", "close");

 
  xmlhr.onreadystatechange = function() {
    if (xmlhr.readyState == 4 && xmlhr.status == 200) {
		//alert("Good");
		//alert(xmlhr.responseText);
      if (xmlhr.responseText) {
		  document.getElementById("mlsSearchDiv").innerHTML = xmlhr.responseText;
		  //alert("Default1");
		  document.getElementById("mlsSearchDiv").style.display="block";
		//alert("B1");
		document.getElementById("rightBottomContent").style.display="none";
		//alert("B2");
		document.getElementById("mainContent").style.display="none";
		  submitDefault();
		  //changeB3();
		  //alert("Default2");
		  //alert(document.getElementById("mainContent").innerHTML);
		  //hideLoading();
      }
    } else if (xmlhr.readyState == 4) {
      alert('Invalid response received - Status: ' + xmlhr.status);
	  //hideLoading();
    } 
  }
  xmlhr.send(params);
}
