// JavaScript Document
var xmlPath="/xml/slideshow.xml";
var destinationDiv="slideImg";
var navHolderDiv="slideNavBar";
var slideshowData;
var slideshow;
var currentInstance;

var SlideshowData = Class.create({
	
	initialize: function(p_xmlPath) {
		this.xmlPath=p_xmlPath;
		this.xml;
		this.data;
		
		var req = new Ajax.Request(this.xmlPath,{
		method:'get',
		onSuccess: function(p_response){
			var response = p_response.responseText || "no response text";
		  	startShow(p_response);
		},
		onFailure: function(){ alert('Error finding XML'); }
	  });
	}
});


var Slideshow = Class.create({
	initialize: function(p_xml, p_div, p_rndStart, p_navHolder){
		this.xmlRef = p_xml;
		this.slides = this.xmlRef.getElementsByTagName("slide");
		
		this.slidesParsed = [];
		this.div = p_div;
		this.slideTimer;
		this.inTransition = false;
		this.navHolder=p_navHolder;
		this.currentSlideIndex = this.slides.length-1;
		this.navItemImg="/images/btnSlide.png";
		this.navItemRollImg="/images/btnSlideRoll.png";
		
		if(p_rndStart){ this.currentSlideIndex = Math.floor(Math.random() * (this.slides.length)); }
			
		this.time=this.xmlRef.getElementsByTagName("slides")[0].attributes[0].value * 1000;
		this.transitionTime=this.xmlRef.getElementsByTagName("slides")[0].attributes[1].value;
		this.loadGraphic=this.xmlRef.getElementsByTagName("slides")[0].attributes[2].value;
		
		var l=this.slides.length;
		var thisObj=this;
		
		for(var i=0;i<l;i++){ 
			var obj = new Object();
			assignPropsToObject(obj,this.slides[i].attributes);
			obj.isLoaded=false;
			this.slidesParsed.push(obj);
		}
		
		this.slidesParsed.reverse();
		
		if(l > 1){
			$('next').observe('click',function(){ thisObj.updateSlide(1); });
			$('prev').observe('click',function(){ thisObj.updateSlide(-1); });
			
			this.buildNav();
			this.animIn();
		}else{
			$('next').remove();
			$('prev').remove();
		}
		
		this.startShow();
	}, 
	
	startShow: function(){
		if(this.slidesParsed[this.currentSlideIndex].ref != undefined){ this.slidesParsed[this.currentSlideIndex].ref.src = this.navItemRollImg; }
		this.showSlide();
		var thisObj = this;
		if(this.slides.length > 1){ this.slideTimer = setInterval(function() { thisObj.slideAction(); }, this.time); }
	},
	
	showSlide: function(){
		var slideObj = this.slidesParsed[this.currentSlideIndex];
		if($("temp") != null){ $("temp").remove(); }
		
		//load image
		//&& !Prototype.Browser.IE
		
		if(!slideObj.isLoaded){
			currentInstance=this;
			//$(this.div).insert("<img id=\"loader\" src=\"" + slideObj.imgPath + "\" style=\"display: none\" onload=\"slideshow.onImgLoadSuccess({}, slideshow.slidesParsed[slideshow.currentSlideIndex])\" />");
			$(this.div).insert("<img id='loader' src='"+slideObj.imgPath+"' style='display: none' onload='currentInstance.onImgLoadSuccess({}, currentInstance.slidesParsed[currentInstance.currentSlideIndex])' />");
			$(this.div).insert("<img id='temp' src='"+this.loadGraphic+"'>");
		}else{
			this.onImgLoadSuccess({}, slideObj);
		}
	},
	
	onImgLoadSuccess: function(p_response, slideObj){
		slideObj.isLoaded=true;
		if($("temp") != null){ $("temp").remove(); }
		
		//$(this.div).setOpacity(0);
		if(slideObj.url != "" && slideObj != null){
			$(this.div).insert("<a id='temp' href='"+slideObj.url+"' target='_blank'><img src='"+slideObj.imgPath+"' border='0'></a>");
		}else{
			$(this.div).insert("<img id='temp' src='"+slideObj.imgPath+"'>");
		}
		
		var thisObj = this;
		new Effect.Opacity(this.div, { from: 0, to: 1, duration:this.transitionTime, afterFinish:function(){ thisObj.onFadeInComplete(); } });
	},
	
	hideCurrentSlide: function(){
		this.inTransition = true;
		var thisObj = this;
		new Effect.Opacity(this.div, { from: 1, to: 0, duration:this.transitionTime, afterFinish:function(){ thisObj.showSlide(); } });
	},
	
	slideAction: function(){
		this.slidesParsed[this.currentSlideIndex].ref.src = this.navItemImg;
		this.updateSlideIndex(-1);
		this.slidesParsed[this.currentSlideIndex].ref.src = this.navItemRollImg;
		this.hideCurrentSlide();
	},
	
	updateSlideIndex: function(p_dir){
		if(p_dir > 0){
			this.currentSlideIndex++;
			if(this.currentSlideIndex >= this.slides.length){ this.currentSlideIndex=0; }
		}else{
			this.currentSlideIndex--;
			if(this.currentSlideIndex < 0){ this.currentSlideIndex=this.slides.length - 1; }
		}
	},
	
	updateSlide: function(p_dir){
		if(!this.inTransition){
			clearInterval(this.slideTimer);
			this.slidesParsed[this.currentSlideIndex].ref.src = this.navItemImg;
			this.updateSlideIndex(p_dir);
			this.slidesParsed[this.currentSlideIndex].ref.src = this.navItemRollImg;
			this.hideCurrentSlide();
		}
	},
	
	onFadeInComplete: function(){
		this.inTransition = false;
	},
	
	selectNavItem: function(p_target){
		//load appropriate slide!
		clearInterval(this.slideTimer);
		this.slidesParsed[this.currentSlideIndex].ref.src = this.navItemImg;
		this.currentSlideIndex=p_target.name;
		this.hideCurrentSlide();
	},
	
	rollNavItem: function(p_target){
		p_target.src = this.navItemRollImg;
	},
	
	rollOutNavItem: function(p_target){
		if(p_target.name != this.currentSlideIndex){
			p_target.src = this.navItemImg;
		}
	},
	
	buildNav: function(){
		var l=this.slidesParsed.length;
		for(var i=0;i<l;i++){
			var id="navItem"+i;
			$(this.navHolder).insert("<a href='javascript:void(0)'><img name='"+i+"' id='"+id+"' src='"+this.navItemImg+"' border='0'></a>");
			
			this.slidesParsed[i].ref=$(id);
			this.slidesParsed[i].index=i;
			
			var thisObj=this;
			$(id).observe('click',function(){thisObj.selectNavItem(this); });
			$(id).observe('mouseover',function(){thisObj.rollNavItem(this); });
			$(id).observe('mouseout',function(){thisObj.rollOutNavItem(this); });
			
			$(id).setOpacity(0);
		}
	},
	
	animIn: function(){
		var l=this.slidesParsed.length;
		for(var i=0;i<l;i++){
			new Effect.Opacity($(this.slidesParsed[i].ref), { from: 0, to: 1, duration:0.5, delay:0.50 + (0.25 * ((l-1) - i)) });
		}
		
		new Effect.Opacity($(navHolderDiv), { from: 0, to: 1, duration:0.5, delay:0 });
		new Effect.Opacity($('next'), { from: 0, to: 1, duration:0.5, delay:0.25 });
		new Effect.Opacity($('prev'), { from: 0, to: 1, duration:0.5, delay:0.50 + (l * 0.25) });
	}
})


function startShow(p_response){
	slideshowData.data = p_response;
	slideshowData.xml = p_response.responseXML;
	
	slideshow = new Slideshow(slideshowData.xml, destinationDiv, false, "slideNav");
}

function assignPropsToObject(p_obj, p_array){
	var l=p_array.length;
	for(var i=0;i<l;i++){ p_obj[p_array[i].name] = p_array[i].value; }
}

document.observe('dom:loaded', function () {

	if($('slideImg')) {
		$('slideImg').setOpacity(0); 
		$(navHolderDiv).setOpacity(0); 
		slideshowData = $('next').setOpacity(0); 
		$('prev').setOpacity(0); 
		new SlideshowData(xmlPath); 
	}
});


