﻿/*
	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
	
//	-----------------------------------------------------------------------------------------------------------------------	


	V2.4 HomeFinder:
	09-22-2010:
	Rob Scheetz:
	
	Adding a status attribute to a model home.  The default status will be active.  Other statuses include 'discontinued' &
	'promotional'.  Engine will be updated to include a check for default status.
	
	//Added a JQuery wrap(<a>) around the <img> tag so the use can link to the model by clicking on the image or graphic.
	
//	-----------------------------------------------------------------------------------------------------------------------	


	V2.5 HomeFinder:
	09-28-2010:
	Rob Scheetz:
	
	Used autocomplete jquery plugin to make a basic search feature.  Also added <frame> & <keyword> tags to the product
	catalog.
	
	Added page tracking to menuButtons, and childMenu(change): /homefinder/filter, /homefinder/search, homefinder/result
	
	***NEED TO RETRIEVE SEARCH STRING SO WHEN USER COMES BACK TO THIS PAGE, THEIR SEARCH RESULT STILL LOADS THE SAME LIST
	***WOULD ALSO BE NICE TO RETRIEVE THE MODEL THE PERSON JUST HAD DISPLAYED
	
	***ALSO WOULD LIKE TO IMPLEMENT DEFENSIVE AJAX SOLUTIONS TO SET RETRY COUNT AND TIMEOUT.  SECURE REQUESTS?
		http://www.ajaxprojects.com/ajax/tutorialdetails.php?itemid=715
	
	
	
//	-----------------------------------------------------------------------------------------------------------------------	






*/	
//	======================================================================================================================
//	======================================================================================================================
//	======================================================================================================================




//Open JQuery Wrapper
$(document).ready(function(){


//	======================================================================================================================
	$(".menuButton").bind("click", function(){
		$("#parentMenu").removeAttr('disabled');
		
		var file = $(this).find("a").attr("id");
		//thirdTracker._trackPageview("/homefinder/filter/"+file+"");
		
		$.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(){
		
	//	searchTerm = $("#search").val();	
	//	thirdTracker._trackPageview("/homefinder/search/"+searchTerm+"");
	//	thirdTracker._trackPageview("/homefinder/result/"+$(this).val()+"");
		
	
		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();
					var frame = $(this).find("dimensions").find("frame").attr('width') + "x" + $(this).find("dimensions").find("frame").attr('length');
					
					//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 class='link' href='"+link+"' title='"+t+" Model'>"+t+"</a>"+"</h2><br/><p>"+series+"&nbsp;Series</p></div>";
					var caption = "<div class='caption'><p>"+caption+"</p></div>";

					var footer = "<div class='footer'><p>View the <a class='link' href='"+link+"'>"+t+"</a> model</p><br/><div><span>"+bed+"bed</span>&nbsp;<span>"+bath+"bath</span>&nbsp<span>"+area+"sqft</span></div><div><span>"+frame+"&nbsp;Frame</span></div></div>";
						
					var img = new Image();
					$(img)
						.addClass('thumbnail')
						.attr('src', hfthumb)
						.load(function(){
							$(this).hide();
							$('#SelectOptionDisplay').append(this);
							$(this).wrap("<a href='"+link+"'></a>");
							$(this).animate({
								'opacity' : 'show'
								}, {'duration' : 1000 });
							})//close load
						.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;' >We're making some changes :-) to our Homefinder utility.  Please be patient while these changes take place.</p>";


					var error = "<p style='font-size: 9pt; color: red;' >*Please note, some frame sizes may be rounded up to the nearest foot.  Check with a sales associate for dimensions.</p>";
					
						$("#SelectOptionDisplay > .desc").append(error);
*/


				});
			
			}//close success
		
		});//close ajax

	});//close click

});//close jquery
