// JavaScript Document

var xml = null;
var currentProductionId = null;

$(function()
{   
	$.ajax({type:"GET",
		   	url:"xmlData/catalog.xml",
			dataType:"XML",
			success:parseXmlData,
			error:loadError
			});	 
});



function parseXmlData (xmlData, textStatus, XMLHttpRequest)
{
	xml = xmlData;
	
	$(xmlData).find('product').each(	
									function()
									{
										
										var productName = $(this).find('name:eq(0)').text(); //get only first tag with <name>
										var productId = $(this).attr("id");
										var productSku = $(this).attr("sku");
										var fileName = $(this).find('filename').text();
										var splicedName = fileName.split("."); // remove .jpg extension
										//
										$("#thumbContainer").append('<div class="thumbnail" id="' + productId + '"><a href="?section=home&id='+ productId +'"><img src="catalog/' + splicedName[0] + '_small.jpg" width="70" height="70" border="0" /></a></div>');
									}
								);
	
	$(".thumbnail").hover(over,out);
	/* Set default big photo */
	setDefaultBigPhoto();
}


/* ///////////////////////////// */

function loadError (XMLHttpRequest, textStatus, errorThrown)
{
	alert("!!error "+ XMLHttpRequest.status + " !!e thrown " + errorThrown);
}

function loadComplete (XMLHttpRequest, textStatus)
{
	//alert("Load Complete " + XMLHttpRequest.status + textStatus);
}


/* /////////////////////////////// */

function over(event)
{
	//alert($(this).attr("id"));
	$(this).css("cursor", "pointer");
	$(this).stop().animate({ backgroundColor: "#646464"},400);
	
	var thisProductId = $(this).attr("id");
	
	if(xml != null)
	{
	
      var queryDetails = 'product[id$='+thisProductId+']';
      $(xml).find(queryDetails).each( 
									 function()
									 {
											var productName = $(this).find('name:eq(0)').text(); //get only first tag with <name>
											$("#thumbnailToolTip").append('<div id="title">'+ productName +'</div><div id="info">Click to see details</div>');
      								 }
								);
	}
	
	var offset = $(this).offset();
	var thumbXpos = offset.left + 70;
	var thumbYpos = offset.top - 60;
	$("#thumbnailToolTip").stop().animate({opacity:1,  left: thumbXpos, top:thumbYpos},400);
}

function out(event)
{
	$(this).stop().animate({ backgroundColor: "#0f0f0f"}, 200);
	$("#thumbnailToolTip").stop().animate({opacity:0},100, function() { $this.hide() });
	$("#thumbnailToolTip").empty();
}


//

function getProductName(productId)
{
	
	var productName = "not found";
	
	if(xml != null) {
	
	alert("xml not null");
      var queryDetails = 'product[id$='+productId+']';
      $(xml).find(queryDetails).each(  function()
										{ 
												productName = $(this).find('name:eq(0)').text();
												alert("inner called");
										}
									);
	}
	
	return productName;
	//$("#viewContainer").append('<div>'+ productName +' = success</div>');
}

/* Set default Big Photo */

function setDefaultBigPhoto()
{
	if(currentProductionId == null)
	{
		$("#viewContainer #details h2").append($(xml).find('name:eq(0)').text());	
		$("#viewContainer #details p").append($(xml).find('description:eq(0)').text());
	} else {
		$("#viewContainer #details h2").append(getProductName(currentProductionId));
		$("#viewContainer #details p").append("soon");
	}
}

function setCurrentProductId(idVal)
{
	currentProductionId = idVal;
	alert("id set to "+ currentProductionId);
}

function getDefaultBigPhotoSku()
{
	$("#viewContainer #details #skuNumber").append($(xml).find('product:eq(0)').attr('sku'));
}
