﻿/*
	V2 HomeFinder:
	12-10-2009:
	Rob Scheetz:
	
	This version of homefinder implements JQuery event handlers to the different program objects.  It also utilizes the
	AJAX function to make calls to the server much simpler than in version 1.
	
//	-----------------------------------------------------------------------------------------------------------------------		
	
	V2.1 HomeFinder:
	01-06-2009:
	Rob Scheetz:
	
	This version adds a small feature called search box.  This is in the file homefinder.search.js.  It basically is a
	function triggered on keyup event.  It will create AJAX object and search for any homes beginning with the value in
	the text field.
	
//	-----------------------------------------------------------------------------------------------------------------------	
	
	V2.2 HomeFinder:
	01-07-2009:
	Rob Scheetz:
	
	Another add-on for homefinder is the star ratings system.  I'm building this so it can be plugged into different parts
	of the site.  The star ratings should be located in the header bar for each model page.  This is where you'll vote on
	a home.  The star rankings will display next to the header in homefinder and also on the new series page.
	*** Not yet Deployed	

//	-----------------------------------------------------------------------------------------------------------------------	

	V2.3 HomeFinder:
	01-12-2009:
	Rob Scheetz:
	
	Tightened up the style and layout from previous version.  Added JQuery Animate to load image.  Stylized the large bg,
	display for home's dimensions. Built full list of models in a comprehensive XML file.
	*** Didn't deploy with star ratings
	
//	-----------------------------------------------------------------------------------------------------------------------	



*/	
//	======================================================================================================================
//	======================================================================================================================
//	======================================================================================================================









//Open JQuery Wrapper
$(document).ready(function(){






//	======================================================================================================================
	$(".menuButton").bind("click", function(){
		var file = $(this).find("a").attr("id");
		
		$.ajax({
			type: 'GET',
			url: 'xml/'+file+'.xml',
			datatype: 'xml', 
			success: function(xml){
				//1.  clear parent menu
				$('#parentMenu').empty();
				
				//2.  repopulate it with each record in XML
				$(xml).find("series").each(function(){
					var series= $(this).attr("name")
					var seriesHTML = "<option id='" +series+ "' value='" +series+ "' class='parentMenu'>" +series+ " </option>";
					$("#parentMenu").append(seriesHTML);
					});
					
				//3.  clear child menu
				$('#childMenu').empty();
				
				//4.  repopulate it with each record from first parent in XML
				$(xml).find("series:first").find("model").each(function(){
					var model= $(this).text();
					var modelHTML = "<option class='childMenu' id='" +model+ "' value='" +model+ "'>" +model+ " </option>";
					$("#childMenu").append(modelHTML);
					});
					
				//5.  set cookie
				$.cookie("filtertype", file);
					
			}//close success

		});//close ajax
				
	});//close click
//	=================================================================================================================





//	=================================================================================================================
	$("#parentMenu", this).bind("change", function(){
		var s = $(this).val();
		
		var file = $.cookie("filtertype");
		
		$.ajax({
			type: 'GET',
			url: 'xml/'+file+'.xml',
			datatype: 'xml', 
			success: function(xml){
				//1.  clear child menu
				$('#childMenu').empty();
				
				//2.  repopulate it with each record from first parent in XML
				$(xml).find("series[name='"+s+"'] ").each(function(){
					$(this).find("model").each(function(){
						var model= $(this).text();
						var modelHTML = "<option class='childMenu' id='" +model+ "' value='" +model+ "'>" +model+ " </option>";
						$("#childMenu").append(modelHTML);
						});
					});

			}//close success

		});//close ajax

	});//close click
//	=================================================================================================================





//	=================================================================================================================
//	function selectProduct(){}
	$("#childMenu", this).bind("change", function(){
		var t = $(this).val();
		
		$.ajax({
			type: 'GET',
			url: 'xml/models.xml',
			datatype: 'xml',
			success: function(xml){
				$('#SelectOptionDisplay').empty();
				
				$(xml).find("model[name='"+t+"']").each(function(){
				
					//Each time we find a home with the name that's specified,
					//grab this set of information
										
					//Series
					var series = $(this).parent().attr("name");

					//Dimensions
					var area = $(this).find("dimensions").find("area").text();
					var bed = $(this).find("dimensions").find("bed").text();
					var bath = $(this).find("dimensions").find("bath").text();
					
					//File Information				
					var hfthumb = $(this).find("hfthumb").text();
					
					//Ratings
					var rating = $(this).find("rating").text();
					var votes = $(this).find("votes").text();
					
					//Caption
					var caption = $(this).find("caption").text();
					
					//Location
					var link = $(this).find("link").text();
					
					
					//Format Description Fields
						rating = "<img src='gfx/starimages/"+rating+".png' title='"+rating+" points / "+votes+" total votes' style='margin-left: 8px;'/>";
					
					var header = "<div class='header'><h2><a href='"+link+"' title='"+t+" Model'>"+t+"</a>"+"</h2><br/><p>"+series+"</p></div>"
					var caption = "<div class='caption'><p>"+caption+"</p></div>";

					var footer = "<div class='footer'><p>View the <a href='"+link+"'>"+t+"</a> model</p><br/><div><span>"+bed+"bed</span>&nbsp;<span>"+bath+"bath</span>&nbsp<span>"+area+"sqft</span></div></div>";
						
					var img = new Image();
						$(img)
							.load(function(){
								$(this).hide();
								$('#SelectOptionDisplay')
									.append(this);
								$(this).animate({
									'opacity' : 'show'
									}, {'duration' : 1000 });
								})//close load
							.addClass('thumbnail')
							.attr('src', '../'+hfthumb)

							.error(function(){
								})//close error
								
						$("#SelectOptionDisplay").append("<div class='desc'>" + header + caption + footer + "</div>");
												
					//Error Message
/*					if($.browser.msie){
					$("#SelectOptionDisplay").append("<br/><div><p style='font-size: 9pt; font-weight: bold; font-style: italic; color: red;'>*Technical Problems with the HomeFinder are causing problems with Internet Explorer.  To view the graphics, click on the link for the Model Home you selected.  Sorry for the inconvenience, please be patient while we work on this.</p></div>");
					}
*/

/*
					var error = "<p style='font-size: 9pt; color: red;' >Updates are being performed with the homefinder utility today.  Please be patient while these occur.  Some information about your selection may not show up properly.</p>";
					
						$("#SelectOptionDisplay > .desc").append(error);
*/

				});
			
			}//close success
		
		});//close ajax

	});//close click

});//close jquery