// JavaScript Document
var destinationDiv="rotator";
var captionDiv = "caption";
var captionTitleDiv = "title";
var slideshow;
var currentInstance;
var slideTime = 7000;
var transitionTime = 0.5;
var randomStart = false;
var autostart = true;

var SlideshowData = Class.create({
	
	initialize: function() {
		/*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_imgs, p_div,p_captionTitleDiv, p_captionDiv, p_rndStart, p_autoStart){
		this.slides = p_imgs;
		this.div = p_div;
		this.captionTitleDiv = p_captionTitleDiv;
		this.captionDiv = p_captionDiv;
		this.slideTimer;
		this.inTransition = false;
		//this.navHolder=p_navHolder;
		this.currentSlideIndex = 0;
		this.slidesParsed=[];
		//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;
		*/
		
		this.time=slideTime;
		this.transitionTime=transitionTime;
		
		
		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.imgPath = this.slides[i].imageurl;
			obj.caption = this.slides[i].caption;
			obj.captionTitle = this.slides[i].caption_title;
			obj.isLoaded=false;
			this.slidesParsed.push(obj);
		}
		
		if(p_autoStart){ this.startShow(); }
		this.showSlide();
	}, 
	
	startShow: function(){
		var thisObj = this;
		if(this.slidesParsed.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='currentInstance.onImgLoadSuccess({}, currentInstance.slidesParsed[currentInstance.currentSlideIndex])' />");
		}else{
			this.onImgLoadSuccess({}, slideObj);
		}	
	},
	
	onImgLoadSuccess: function(p_response, slideObj){
		slideObj.isLoaded=true;
		if($("temp") != null){ $("temp").remove(); }
		if($("tempCaption") != null){ $("tempCaption").remove(); }
		if($("tempCaptionTitle") != null){ $("tempCaptionTitle").remove(); }
		
		$(this.div).setOpacity(0);
		$(this.captionDiv).setOpacity(0);
		$(this.captionTitleDiv).setOpacity(0);
		
		$(this.div).insert("<img id='temp' class='background' src='"+slideObj.imgPath+"'>");
		$(this.captionDiv).insert("<div id='tempCaption'>"+slideObj.caption+"</div>");
		$(this.captionTitleDiv).insert("<div id='tempCaptionTitle'>"+slideObj.captionTitle+"</div>");

		
		var thisObj = this;
		new Effect.Opacity(this.div, { from: 0, to: 1, duration:this.transitionTime, afterFinish:function(){ thisObj.onFadeInComplete(); } });
		new Effect.Opacity(this.captionDiv, { from: 0, to: 1, duration:this.transitionTime });
		new Effect.Opacity(this.captionTitleDiv, { from: 0, to: 1, duration:this.transitionTime });
		
	},
	
	hideCurrentSlide: function(){
		
		this.inTransition = true;
		var thisObj = this;
		new Effect.Opacity(this.div, { from: 1, to: 0, duration:this.transitionTime, afterFinish:function(){ thisObj.showSlide(); } });
		new Effect.Opacity(this.captionDiv, { from: 1, to: 0, duration:this.transitionTime});
		new Effect.Opacity(this.captionTitleDiv, { from: 1, to: 0, duration:this.transitionTime});
	},
	
	slideAction: function(){
		this.updateSlideIndex(1);
		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; }
		}
	},
	
	onFadeInComplete: function(){
		this.inTransition = false;
	},
	
	updateSlide: function(p_num){
		//console.log("UPDATE SLIDE ::: "+p_num);
		
		//reset timer
		if(this.slideTimer != ""){ clearInterval(this.slideTimer); }
		if(!this.inTransition){
			this.updateSlideIndex(p_num);
			this.hideCurrentSlide();
		}
		this.startShow();
	}
})

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 () {
	//console.log(_images);
	slideshow = new Slideshow(_images, destinationDiv,captionTitleDiv, captionDiv, randomStart, autostart);
	$('btnNext').observe('click',function(){ slideshow.updateSlide(1); });
	$('btnPrev').observe('click',function(){ slideshow.updateSlide(-1); });
});


