$j(document).ready(function() {

	var checkChange = false;

	//Check if the menu has anything in it and update searchpage dropdowns of the same
	$j("select.SearchMenu option:selected").each(function(){
		$j("#"+$j(this).parent().attr('id').replace('Menu','')+".Search").val($j(this).val());
		if($j(this).val()!='n/a'){
			checkChange = true;
		}
	});

	//This has to be set after the check above to gaurentee proper update :)
	var myJSONObject = createJSONObject("Search");

	$j("select.Search").change(function() {
		myJSONObject = createJSONObject("Search");

		//This updates the MainMenu Search
		$j("#"+$j(this).attr('id')+"Menu.SearchMenu").val($j(this).val());

		if(myJSONObject){
			callJSON(myJSONObject);
		}
	});

	$j(".searchMenuSubmitButt").click(function() {
		document.myForm.submit();
	});
	
	$j("#searchRefresh").click(function(){
		myJSONObject = createJSONObject("Search");

		if(myJSONObject){
			callJSON(myJSONObject);
		}
	});

	if(checkChange) {
		$j("#ajaxLoading").show();
		callJSON(myJSONObject);
	}

	$j("#ajaxLoading").bind("ajaxSend", function(){
		$j(this).show();
	}).bind("ajaxComplete", function(){
		$j(this).hide();
	});

	function callJSON(JSONObj){
		var myJSONData = JSON.stringify(JSONObj);

		$j.ajax({
			type: "POST",
			dataType: "json",
			url: "assets/snippets/DropDownSearch/DropDownSearch.ajax.php",
			data: "ddsData="+myJSONData+"&date="+ new Date().getTime(),
			success: function(data){
				$j("#mainContentRightSearch").empty();
				$j("#mainContentRightContact").hide();
				$j("#mainContentRightSearch").show();

				var totalCount = data.length;

				var totalPages = ((totalCount / 9)<=1)?1:(Math.ceil(totalCount / 9));
				var searchResultNumber = 0;
				var pageNumbers = 1;
				var pageHeight = (totalCount > 9)?705:Math.ceil((totalCount/3))*235;

				if(totalCount){
					$j(createPagination(totalPages,"top")).appendTo("#mainContentRightSearch");

					$j.each(data, function(){
//console.log(this.id+": "+this.pagetitle+": "+this.leftCol_addressBox+": "+this.leftCol_style+": "+this.leftCol_squareFeet+": "+
//this.leftCol_bedrooms+": "+this.leftCol_bathrooms+": "+this.leftCol_topImage+": "+this.community+": "+this.city+": "+this.link+": "+
//this.alias);
						searchResultNumber = (searchResultNumber < 9)?++searchResultNumber:1;

						if(searchResultNumber==1){
							hidePage = (pageNumbers == 1)?"":"style=\"display: none;\"";
							$j("<div id=\"searchResultsPage_"+pageNumbers+"\" " + hidePage + " class=\"searchResultsPage\">").appendTo("#mainContentRightSearch");
						}

						$j("<div class=\"searchResultsOuter\">\n\
								<div class=\"searchResultsImg\">\n\
									"+((this.leftCol_topImage != '')?"<a href=\""+this.link+"\"><img src=\""+this.leftCol_topImage+"\" alt=\"\" /></a>":"")+"\n\
								</div>\n\
								<div class=\"searchResultsText\">\n\
									<strong><a href=\""+this.link+"\">"+this.pagetitle+"</a></strong><br />\n\
									<span>"+this.leftCol_addressBox+((this.community != '' && this.leftCol_addressBox != '')?", ":"")+this.community+((this.city != '' && (this.leftCol_addressBox != '' || this.community != ''))?", ":"")+this.city+"</span>\n\
									<ul>\n\
										"+((this.leftCol_style != '')?"<li>"+this.leftCol_style+"</li>":"")+"\n\
										"+((this.leftCol_bedrooms != '')?"<li>"+this.leftCol_bedrooms+" Bedrooms</li>":"")+"\n\
										"+((this.leftCol_bathrooms != '')?"<li>"+this.leftCol_bathrooms+" Bathrooms</li>":"")+"\n\
										"+((this.leftCol_squareFeet != '')?"<li>"+this.leftCol_squareFeet+" sqft</li>":"")+"\n\
									</ul>\n\
								</div>\n\
							</div>").appendTo("#searchResultsPage_"+pageNumbers+"");

						if( searchResultNumber==9 || (pageNumbers==totalPages && searchResultNumber==totalCount)){
							pageNumbers = (searchResultNumber==9)?++pageNumbers:pageNumbers;

							$j("</div>").appendTo("#mainContentRightSearch");
						}

					});

					$j("<div class=\"clear\"></div>").appendTo("#mainContentRightSearch");

					$j(createPagination(totalPages,"bottom")).appendTo("#mainContentRightSearch");

					$j("<div class=\"clear\"></div>").appendTo("#mainContentRightSearch");

					$j('a.pagStart').click(function(){
						$j('.searchResultsPage').hide();
						$j('#searchResultsPage_1').show();
						$j(".pagCurrent").removeClass("current");
						$j("#pagCurrent_top_1.pagCurrent").addClass("current");
						$j("#pagCurrent_bottom_1.pagCurrent").addClass("current");
					});

					$j('a.pagEnd').click(function(){
						$j('.searchResultsPage').hide();
						$j('#searchResultsPage_'+totalPages).show();

						$j(".pagCurrent").removeClass("current");
						$j("#pagCurrent_top_"+totalPages+".pagCurrent").addClass("current");
						$j("#pagCurrent_bottom_"+totalPages+".pagCurrent").addClass("current");
					});

					$j(".pagCurrent").click(function(){
						var topCheck = ($j(this).attr("id").indexOf("top") != -1)?"top":"bottom";
						var thisID = $j(this).attr("id").replace("pagCurrent_" + topCheck + "_","");
						$j('.searchResultsPage').hide();
						$j('#searchResultsPage_'+thisID).show();

						$j(".pagCurrent").removeClass("current");
						$j("#pagCurrent_top_"+thisID+".pagCurrent").addClass("current");
						$j("#pagCurrent_bottom_"+thisID+".pagCurrent").addClass("current");
					});

					$j(".pagPrev").click(function(){
						var topCheck = ($j(".current").attr("id").indexOf("top") != -1)?"top":"bottom";
						var currentID = parseInt($j(".current").attr("id").replace("pagCurrent_" + topCheck + "_",""));
						var prevID = (currentID!=1)?currentID-1:1;

						$j('.searchResultsPage').hide();
						$j('#searchResultsPage_'+prevID).show();

						$j(".pagCurrent").removeClass("current");
						$j("#pagCurrent_top_"+prevID+".pagCurrent").addClass("current");
						$j("#pagCurrent_bottom_"+prevID+".pagCurrent").addClass("current");
					});
					
					$j(".pagNext").click(function(){
						var topCheck = ($j(".current").attr("id").indexOf("top") != -1)?"top":"bottom";
						var currentID = parseInt($j(".current").attr("id").replace("pagCurrent_" + topCheck + "_",""));
						var nextID = (currentID!=totalPages)?currentID+1:totalPages;

						$j('.searchResultsPage').hide();
						$j('#searchResultsPage_'+nextID).show();

						$j(".pagCurrent").removeClass("current");
						$j("#pagCurrent_top_"+nextID+".pagCurrent").addClass("current");
						$j("#pagCurrent_bottom_"+nextID+".pagCurrent").addClass("current");
					});

					$j('div#mainContentRightSearch').height(parseInt(pageHeight+100));
					
					heightFix();
				} else {
					$j("<div class=\"noResults\">No Results Returned.</div><div class=\"clear\"></div>").appendTo("#mainContentRightSearch");
				}
			}
		});
	}

	function createPagination(totalPages,location){
		var output = '';

		if(totalPages > 1) {
			output = "<div class=\"outerPagination\">\n\
						<a href=\"javascript:void(0);\" class=\"pagStart\"><< Start</a>\n\
						<a href=\"javascript:void(0);\" class=\"pagPrev\">< prev</a> ";

			for(var x=1;x <= totalPages;x++){
				var current = (x==1)?"current":"";
				output += "<a href=\"javascript:void(0);\" id=\"pagCurrent_"+location+"_"+x+"\" class=\"pagCurrent " + current + "\">"+x+"</a> ";
			}

			output += "<a href=\"javascript:void(0);\" class=\"pagNext\">Next ></a>\n\
						<a href=\"javascript:void(0);\" class=\"pagEnd\">End >></a>\n\
					</div>";
		} else {
			output = "<div class=\"outerPagination\">&nbsp;</div>";
		}

		return output;
	}

	function createJSONObject(menuClass){
		var JSONObj = {};
		var isempty = false;

		menuClass = (menuClass)?menuClass:"Search";

		$j("select."+menuClass+" option:selected").each(function(){
			if($j(this).val()!="n/a"){
				JSONObj[$j(this).parent().attr('id')]=$j(this).val();
			}
		});

		//Checking if JSONObj has anything in it
		for (value in JSONObj) { isempty = true; break; }
		
		if(isempty){
			return JSONObj;
		} else {
			$j("#mainContentRightSearch").empty();
			$j("#mainContentRightContact").show();
			$j("#mainContentRightSearch").hide();
			return false;
		}
	}
});