$(document).ready(function(event) {

	/* Basically,this drop shadow solution isn't brilliant.
	
	if (jQuery.browser.mozilla || jQuery.browser.opera || jQuery.browser.msie) {
		$("#spotlight").dropShadow();
	}
	*/
	
	window.ImagePreloaders	= [];
	window.PreloadCount		= 0;
	window.MenuData			= {};
	window.contentLoaded	= false;
	
	$("#spotlight").css("backgroundPosition",-548);
	
	//Make XML Request
	jQuery.ajax({
				url:		"xml/images.xml",
				dataType:	"xml",
				success:	function(data,textStatus) { parseData(data); },
				error:		function(XMLHTTPR, Text, ErrorThrown) {
					if (window.console) {
						console.log(Text);
					}
				}
			});
	
	$("#header li.product a").css({paddingTop:0, opacity:0}).removeClass("clickable");
	$("#header li.service a").css({paddingTop:30, opacity:0}).removeClass("clickable");
});

$(window).load(function() {
	$("#spotlight p, #spotlight h3").animate({opacity:0},500,function() {
		$("#spotlight").animate({
									backgroundPosition: "0"
								}, 2000);
		$("#spotlight p").animate({opacity:1},3000);
		$("#spotlight h3").animate({opacity:1},3000);
		$("#header li.product a").animate({paddingTop:30, opacity:1},2000).addClass("clickable");
		$("#header li.service a").animate({paddingTop:1, opacity:1},2000).addClass("clickable");
	});
	
	//event handler
	$("#header li a").mouseover(function(event) {
		var TmpClassData = this.className;
		TmpClassData = TmpClassData.replace(/\s*clickable\s*/,"");
		
		if (window.MenuData[TmpClassData]) {
			var CurrentItem = window.MenuData[TmpClassData];
			
			$("#spotlight h3, #spotlight p, #spotlight").stop();
			
			$("#spotlight h3").animate({opacity:0},200);
			$("#spotlight p").animate({opacity:0},200,function() {
				$("#spotlight h3").html(CurrentItem.title)
				$("#spotlight p").html(CurrentItem.text);
				
				$("#spotlight h3").animate({opacity:1},500);
				$("#spotlight p").animate({opacity:1},500);
			});
			
			if (window.CurrentImageFile != CurrentItem.image) {
				$("#spotlight").animate({ backgroundPosition: -548 }, 500, function() {
					
					// check again whether we've loaded
					ImagePreload = new Image();					
					ImagePreload.src = CurrentItem.image;
					
					if (jQuery.browser.msie) {
						window.CurrentImageFile = CurrentItem.image;
						$("#spotlight").css("backgroundImage","url('" + CurrentItem.image + "')");
						$("#spotlight").animate({backgroundPosition: 0}, 1000);
					} else {
						$(ImagePreload).load(function() {
							window.CurrentImageFile = CurrentItem.image;
							$("#spotlight").css("backgroundImage","url('" + CurrentItem.image + "')");
							$("#spotlight").animate({backgroundPosition: 0}, 1000);
						});
					}
				});
			} else {
				$("#spotlight").css("backgroundImage","url('" + CurrentItem.image + "')");
				$("#spotlight").animate({backgroundPosition: 0}, 1000);
			}
		}
	});
	
	$("#header li a, #spotlight + ul li").mouseout(function(event) {
		if (window.MenuData["home"]) {
			var CurrentItem = window.MenuData["home"];
			
			$("#spotlight h3, #spotlight p, #spotlight").stop();
			
			$("#spotlight h3").animate({opacity:0},200);
			$("#spotlight p").animate({opacity:0},200,function() {
				$("#spotlight h3").html(CurrentItem.title)
				$("#spotlight p").html(CurrentItem.text);
				
				$("#spotlight h3").animate({opacity:1},500);
				$("#spotlight p").animate({opacity:1},500);
			});
			
			if (window.CurrentImageFile != CurrentItem.image) {
				$("#spotlight").animate({ backgroundPosition: -548 }, 500, function() {
					
					// check again whether we've loaded
					ImagePreload = new Image();					
					ImagePreload.src = CurrentItem.image;
				
					if (jQuery.browser.msie) {
						window.CurrentImageFile = CurrentItem.image;
						$("#spotlight").css("backgroundImage","url('" + CurrentItem.image + "')");
						$("#spotlight").animate({backgroundPosition: 0}, 1000);
					} else {
						$(ImagePreload).load(function() {
							window.CurrentImageFile = CurrentItem.image;
							$("#spotlight").css("backgroundImage","url('" + CurrentItem.image + "')");
							$("#spotlight").animate({backgroundPosition: 0}, 1000);
						});
					}
				});
			} else {
				$("#spotlight").css("backgroundImage","url('" + CurrentItem.image + "')");
				$("#spotlight").animate({backgroundPosition: 0}, 1000);
			}
		}
	});
	
	$("#spotlight + ul a").mouseover(function(event) {
		var TmpClassData = this.className;
		TmpClassData = TmpClassData.replace(/\s*/,"");
		
		if (window.MenuData[TmpClassData]) {
			var CurrentItem = window.MenuData[TmpClassData];
			
			$("#spotlight h3, #spotlight p, #spotlight").stop();
			
			$("#spotlight h3").animate({opacity:0},200);
			$("#spotlight p").animate({opacity:0},200,function() {
				$("#spotlight h3").html(CurrentItem.title)
				$("#spotlight p").html(CurrentItem.text);
				
				$("#spotlight h3").animate({opacity:1},500);
				$("#spotlight p").animate({opacity:1},500);
			});
			
			if (window.CurrentImageFile != CurrentItem.image) {
				$("#spotlight").animate({ backgroundPosition: -548 }, 500, function() {
					
					// check again whether we've loaded
					ImagePreload = new Image();					
					ImagePreload.src = CurrentItem.image;
				
					if (jQuery.browser.msie) {
						window.CurrentImageFile = CurrentItem.image;
						$("#spotlight").css("backgroundImage","url('" + CurrentItem.image + "')");
						$("#spotlight").animate({backgroundPosition: 0}, 1000);
					} else {
						$(ImagePreload).load(function() {
							window.CurrentImageFile = CurrentItem.image;
							$("#spotlight").css("backgroundImage","url('" + CurrentItem.image + "')");
							$("#spotlight").animate({backgroundPosition: 0}, 1000);
						});
					}
				});
			} else {
				$("#spotlight").css("backgroundImage","url('" + CurrentItem.image + "')");
				$("#spotlight").animate({backgroundPosition: 0}, 1000);
			}
		}
	});
});


function parseData(data) {
	$("#spotlight p").css({opacity:0});
	$("#spotlight h3").css({opacity:0});
	
	for (MenuItem = 0; MenuItem < data.documentElement.childNodes.length; MenuItem ++) {
		if (data.documentElement.childNodes[MenuItem].nodeType == 1) {
			window.MenuData[data.documentElement.childNodes[MenuItem].nodeName] = {
				title:	data.documentElement.childNodes[MenuItem].getElementsByTagName("title")[0].firstChild.nodeValue,
				text:	data.documentElement.childNodes[MenuItem].getElementsByTagName("text")[0].firstChild.nodeValue,
				image:	data.documentElement.childNodes[MenuItem].getElementsByTagName("image")[0].firstChild.nodeValue
			};
			
			ImagePreloaders[ImagePreloaders.length] = new Image();	  
			ImagePreloaders[ImagePreloaders.length-1].src = data.documentElement.childNodes[MenuItem].getElementsByTagName("image")[0].firstChild.nodeValue;
		}
	}
}