﻿/*
	V2 Liquidation:
	06-08-2010:
	Rob Scheetz:
	
	I rewrote this version to correct some things that weren't originally built.  I restructured the functions so that the
	status isn't changed after the code and the image isn't dependant on a list of if/else.  Instead, this version will check
	the status against an array and display the overlay dynamically.  Additional status options can be declared in the array
	without changing any other code, as long as the xml <status> tag and the filename all match.  The code is case sensitive.
//	-----------------------------------------------------------------------------------------------------------------------

*/	
//	======================================================================================================================
//	======================================================================================================================
//	======================================================================================================================


//Add status options to this array.  Seperate using commas, use single quotes around text, is case sensitive.
//These items should match the text from the XML tag <status></status>
var statusARR = [
	'sold',
	'pending',
	'reduced',
	'7900',
	'no19',
	'doublewide'
	];



//	Only edit above this line
//	!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

$(document).ready(function(){
	$.ajax({
        type: 'GET',
		url: 'xml/liquidation.xml',
		dataType: 'xml',
		success: function(xml){
			$(xml).find("home").each(function(xml){
				var id = $(this).attr("id");
				var model = $(this).attr("model");
				var thumbnail = $(this).find("thumbnail").text();
				var bed = $(this).find("bed").text();
				var bath = $(this).find("bath").text();
					bedbath = bed + " bed " + bath + " bath";
				var statusVal = $(this).find("status").text();
				
				//Check if the status is in the array (-1 means not in array, otherwise will be its position)
				var statusN = ($.inArray(statusVal, statusARR));  
				//var statusN = status.indexOf(statusVal);
				
				//alert(statusN + "\t" + statusVal + "\t");

				if(statusN != -1){
					$('#menu').append("<div class='itempreview' onclick=\"displayItem('"+model+"');\"><img src='gfx/liquidation/"+statusVal+".png' id='Sold"+model+"' class='itempreviewSold' width='80px' height='60px' alt='' /><img alt='"+model+"' id='Thumb"+model+"' src='gfx/liquidation/"+model+"/"+thumbnail+"' title='Click for Information about Model #"+model+"' />"  + "<p>"+bedbath+"</p></div>");
					}

				else{
					$('#menu').append("<div class='itempreview' onclick=\"displayItem('"+model+"');\"><img alt='"+model+"' id='Thumb"+model+"' src='gfx/liquidation/"+model+"/"+thumbnail+"' title='Click for Information about Model #"+model+"' />"  + "<p>"+bedbath+"</p></div>");
				}
								
			});
				
		},
		error:function(xhr, ajaxOptions, thrownError){
			$("#label").append("<p class='contentSM'><font color='red'>There was an error with the listing, please contact a sales associate [800-965-6821] for information regarding closeouts. ("+xhr.status + "&nbsp;" + thrownError+")</font></p>");
			}
		
	});//Close Ajax

});//Close JQuery





function displayItem(id){
	$(document).ready(function(){
		$.ajax({
		        type: 'GET',
				url: 'xml/liquidation.xml',
				dataType: 'xml',
				success: function(xml){
					$(xml).find("home").each(function(xml){
						var modelName = $(this).attr("model");
						var HomeID = $(this).attr("id");
						if(modelName == id){
						
						$(this).find("descriptions").children().each(function(){
							var tmpValue = $(this).text();
							var descArray = new Array();
								descArray.push(this.nodeName, tmpValue);
								//alert(this.nodeName + tmpValue);
								
								$.each(descArray, function(val1, val2, val3){
								//alert(val1 + val2 + val3);
								});
							});
									
							var make = $(this).find("make").text();
							var year = $(this).find("year").text();
							var frame = $(this).find("frame").text();
							var area = $(this).find("area").text();
							var bed = $(this).find("bed").text();
							var bath = $(this).find("bath").text();
							
							var description = $(this).find("description").text();
							var oldprice = $(this).find("oldprice").text();
							var currentprice = $(this).find("currentprice").text();
							var call = $(this).find("call").text().replace("<br/>", "\n");
							
							$('#id').html(HomeID);
							$('#model').html(modelName);
							$('#make').html(make);
							$('#year').html(year);

							$('#frameSize').html("Frame: <b>" + frame + "</b>");
							$('#area').html("Area: <b>" + area + "</b>");
							$('#bed').html("Bed: <b>" + bed + "</b>");
							$('#bath').html("Bath: <b>" + bath + "</b>");

							$('#description').html(description);
							$('#oldprice').html(oldprice);
							$('#currentprice').addClass("currentprice").text(currentprice);
							$('#call').html(call);
							
							$('#photo1').fadeTo("fast", .1);
							$('#photo2').fadeTo("fast", .1);
							
							//$("#photo1 > .photos", this).remove();

							var photo1 = "gfx/liquidation/" + id + "/" + $(this).find("photo1").text();
							var photo2 = "gfx/liquidation/" + id + "/" + $(this).find("photo2").text();
							
							$('#photo1').attr("src", photo1).fadeTo("slow", 1);
							$('#photo2').attr("src", photo2).fadeTo("slow", 1);
						

/*
							var img = new Image();
								$(img)
									.load(function(){
										$(this).hide();
										$('#picture1')
											.removeClass('loading')
											.html(this);
										$(this).animate({
											'opacity' : 'show'
											}, {'duration' : 1000 });
										})//close load
									.addClass('photos')
									.attr('src', photo1 )
		
									.error(function(){
										alert(photo1);
										})//close error

*/

							var statusVal = $(this).find("status").text();

							//Check if the status is in the array (-1 means not in array, otherwise will be its position)
							var statusN = ($.inArray(statusVal, statusARR));  
							//var statusN = status.indexOf(statusVal);
							

							if(statusN != -1){
								$('#sold_photoEXT').attr("src", "gfx/liquidation/"+statusVal+"Big.png");
								}
			
							else{
								$('#sold_photoEXT').attr("src", "gfx/spacer.gif");
								}

							}

						});
					
				},//close success function for ajax();
				error:function(){
					//$("#label").append("<p class='contentSM'><font color='red'>There was an error with the listing, please contact a sales associate [800-965-6821] for information regarding closeouts. </font></p>");
					}
			
		});//close ajax() function;

	});//close jquery wrapper;
}
