// JavaScript Document
$(document).ready(function(){
	
	$.ajax({
		url: "/photoshow/photoshow.xml",
		type: "GET",
		dataType: "xml",
		success: function(response) {
			$("div.photoshow-nav ul li").remove();
			
			$(response)
				.find("photo")
					.each(function(){
						var photo = $("<img/>").attr({
							"src": $(this).find("location").text(),
							"alt": $(this).find("alt").text()
						});
						
						var li = $("<li/>").append(photo);
						
						$("div.photoshow-nav ul").append(li);
					});
					
			startAdRotator();
		},
		
		error: function() {
			$("div.photoshow-nav").remove();
		}
	});
	
	function startAdRotator() {
		var fadingInOutSpeed = 500;
		var fadingInOutDuration = 7000;
		var shownItem = 0;
		var nextItem = 1;
		var maxItemNo = 0;

		jQuery('.photoshow-nav li').each(function(i){
			jQuery(this).css({ zIndex : 9999-i , opacity : 0 });
			maxItemNo++;
		});

		maxItemNo--;
		jQuery('.photoshow-nav li:first').animate( {opacity : 1}, fadingInOutSpeed);
		setInterval(startAutoAdShowing, fadingInOutDuration);
		
		function startAutoAdShowing(){
			if(maxItemNo==0){
			} else if( nextItem < maxItemNo ){
				jQuery('.photoshow-nav li').eq(nextItem).animate( {opacity : 1}, fadingInOutSpeed, function(){
					jQuery(this).css({ zIndex : 9999 });
				});

				jQuery('.photoshow-nav li').eq(shownItem).animate( {opacity : 0}, fadingInOutSpeed, function(){
					jQuery(this).css({ zIndex : 9000 });
				});

				shownItem = nextItem;
				nextItem++;
			} else{
				jQuery('.photoshow-nav li').eq(nextItem).animate( {opacity : 1}, fadingInOutSpeed, function(){
					jQuery(this).css({ zIndex : 9999 });
				});

				jQuery('.photoshow-nav li').eq(shownItem).animate( {opacity : 0}, fadingInOutSpeed, function(){
					jQuery(this).css({ zIndex : 9000 });
				});

				shownItem = nextItem;
				nextItem = 0;
			}
		}
	}
	
});

