(function(){
	
	window.effectDuration = 600;
	window.nextImageWait = 5000;
	
	$ = window.jQuery;
	
	var slideshow = {
		
		init: function(){
			
			if($("#slideshow").length < 1) return false;
			
			$.ajax({
			  type: "GET",
			  url: "js/slideshow.xml?"+Math.random(),
			  dataType: "xml",
			  success: this.parseSlideshow,
			  error: this.forceQuit
			});
			
		},
		
		forceQuit: function(){
			
			clearInterval(window.slideshow.timer);
			$("#slideshow").remove();
			
		},
		
		parseSlideshow: function(xml){
			
			ul = $("#slideshow ul")	
			
			slides = $("slideshow > slide", xml);
			count = slides.length
			
			n = 0;
			
			for(var idx in slides){
				
				if(typeof slides[idx] != "object"){ count--; continue; }

				slide = $(slides[idx]);
				
				if($("image", slide).length != 1){ count--; continue; }
				
				if(n == 0){
					border = ""
				}else{
					border = "border-left:1px solid #000;"
				}
				
				if(n > 0){
					style = "background:#fff url("+$("image",slide).text()+") no-repeat center center;width:60px;"+border+"";
				}else{
					style = "background:#fff url("+$("image",slide).text()+") no-repeat center center;width:635px;"+border+"";
				}
				
				/* text = "<div>"+$("text", slide).text()+"</div>" */
				text = "&nbsp;";
				
				ul.append("<li id=\"slide_"+n+"\" style=\""+style+"\">"+text+"</li>")
				
				n++;
			}
			
			window.slideshow.runSlideshow()
				
		},
			
		runSlideshow: function(){
			
			$("#slideshow").css("background", "#000")
			
			window.slideshow.current = obj = $("#slideshow ul li#slide_0");
			
			$("#slideshow ul li").css("opacity", 0.3);
			obj.css("opacity", 1);
			
			$("#slideshow ul li").click(function(){
				
				/*
				if($(this).attr("id") == $(window.slideshow.current).attr("id"))
				{
					if($("div a", this).attr("href")) window.location = $("div a", this).attr("href");
					return;
				}
				*/
				
				window.slideshow.manualSelection($(this))
				
			});
			
			/* $("div", obj).show().css("opacity", 0.6); */
			
			window.slideshow.timer = setInterval(this.nextImage, window.nextImageWait);
			
			return;
		},
		
		nextImage: function(next){
			
			obj = window.slideshow.current;
			
			if(typeof(next) != "object"){
				
				window.slideshow.next = next = obj.next()
			
				if(next.length < 1 || typeof(next[0]) == "undefined"){
					window.slideshow.next = next = $("#slideshow ul li:first-child");
				}
				/* IE Fix below... */
				if(typeof(next.get(0)) == "undefined"){
					
					window.slideshow.next = next = $("#slideshow ul li#slide_0");
					
				}
				
			}
			
			/* $("div", next).css('opacity', 0); */
			/* $("div", obj).fadeOut(); */
			obj.animate({opacity: 0.3, width: 60}, {duration:window.effectDuration, easing: "linear"});
			next.animate({opacity: 1, width: 635}, {duration:window.effectDuration, easing: "linear"});
			/* $("div", next).show().fadeTo(500, 0.6) */
			
			window.slideshow.current = next;
			
		},
		
		manualSelection: function(obj){
						
			clearInterval(window.slideshow.timer);
			
			window.slideshow.nextImage(obj);
			
			window.slideshow.timer = setInterval(window.slideshow.nextImage, window.nextImageWait);
			
		}
			
		
	}
	
	$(document).ready(function(){
		
		window.slideshow = slideshow;
		
		slideshow.init();
		
	});
	
})();