﻿/*
	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.
*/	

//Open JQuery Wrapper
$(document).ready(function(){
//	=================================================================================================================
	$(".menuButton").live("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).live("click", 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).live("click", 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(){
					var thumb = $(this).find("img").text();
					var link = $(this).find("link").text();
					var caption = $(this).find("caption").text();
					
					var pLink = "<br/><br/><p>View the <a href='"+link+"'>"+t+"</a> model</p>";
						
					var img = new Image();
						$(img)
							.load(function(){
								$(this).hide();
								$('#SelectOptionDisplay')
									.removeClass('loading')
									.append(this);
								$(this).show();
							})
							
							.error(function(){
								//alert("there's been an error");
							})
							
							.attr('src', '../'+thumb)
							.addClass('thumbnail')
							.appendTo("#SelectOptionDisplay");
						
						$("#SelectOptionDisplay").append("<div class='desc'><h2><a href='"+link+"'>"+t+"</a></h2><p>"+ caption + "</p>" + pLink + "</dev>");
						
						$("#SelectOptionDisplay").append("<br styl='clear:both;' />");
						//$("#SelectOptionDisplay").append("<a href='"+link+"'><img src='../"+thumb+"' class='thumbnail'/></a>" + "</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>");
					}
*/

				});
			
			}//close success
		
		});//close ajax

	});//close click

});//close jquery