// 0.3: 
// cleaned up global variables
// Initialisation parameters passed in an object.
// added slide_selector parameter: function taking 2 parameters: number_of_slides and current frame index, and returns an integer being the index of the next frame.
// default frame selector is now taking the next slide (was random)
// added interval parameter: interval in milliseconds between 2 transitions when looping


YAHOO.namespace("myowndb");
//YAHOO.myowndb.slideshow = function (container, frames, effect) {
YAHOO.myowndb.slideshow = function (container, o) {
	this.container = YAHOO.util.Dom.get(container);
	this.effect = o.effect;
	var frames = o.frames;
	this.frames = [];
	//add cached frames
	var cached_frames = YAHOO.util.Dom.getElementsByClassName("yui-sldshw-frame", null, this.container);
	
	for (var i=0; i<cached_frames.length; i++)
	{
		this.frames[i] = { id: i, type: 'cached', value: cached_frames[i]};
	}

	if (frames != null && frames!=undefined)
	{
		for (var i=0; i<o.frames.length; i++)
		{
			this.frames[i+cached_frames.length] = o.frames[i];
		}
	}

	//set slide selector
	if (! o.slide_selector)
	{
		this.slide_selector = function(number_of_slides, current_index)
			{
				//return Math.floor( Math.random()*number_of_slides);
				return (current_index+1)%number_of_slides;
			}

	}
	else
	{
		this.slide_selector = o.slide_selector;
	}

	if (o.interval)
		this.interval = o.interval
	else
		this.interval = 3000


	if (o.first_run)
		this.first_run = o.first_run;
	else
		this.first_run = 0;

	this.init();

}


YAHOO.myowndb.slideshow.prototype = {
	init: function()
		{
			if (! this.effect)
			{
				this.effect= YAHOO.myowndb.slideshow.effects.slideUp;
			}
			
			this.active_frame = this.get_active_frame();
			this.choose_next_frame(-1);
		},
	get_active_frame: function()
		{
			var current_frame =  YAHOO.util.Dom.getElementsByClassName("yui-sldshw-active", null,  this.container)[0];
			return current_frame;
		},
	get_frame_index: function(frame)
		{
			for(var i=0; i<this.frames.length;i++)
			{
				if (this.frames[i].value==frame)
				{
//					alert( 'active is ' + i);
					return i;
				}
			}
			return -1;
		},
	get_frame_index_by_id: function(frame_id)
		{
		
			//passed an frame id this returns
			//index within all frames array
			for(var i=0; i<this.frames.length;i++)
			{
				//original frame is stored in cache
				//in the value field
				//hence value.id is the original frame id
				
				if (this.frames[i].value.id==frame_id)
				{
					return i;
				}
			}

			return -1;

	
		}
	,
	choose_next_frame : function(frame_index)
		{
//			alert('starting to choose');
			var current_index = this.get_frame_index(this.get_active_frame());
//			alert('im on' + current_index + ' goto' + frame_index);

			if ( current_index == frame_index ) // 10-3 without this break, for some reason wrongly determines active slide as n+1 slide
				return FALSE

			
			if (current_index<0)
				current_index=0;
			var all_frames = this.frames;
			
			//frame_index=-1 means choose next according to slide_selector
			//otherwise contains the frame we want next
			if (frame_index <= -1) {
				var next_index = this.slide_selector(all_frames.length, current_index);
//				alert('next is' + next_index);

				// if it's the first slide and the second run through then stop and clear the first-run property
				if (next_index == 1 && this.first_run == 1){
					this.first_run = 0;
					clearInterval(this.loop_interval);
				}
				
				// if it's the second slide and the first run through, then decrement the first_run property to show that next run through it stops
				if (next_index > 1 && this.first_run == 2)
					this.first_run = 1;

				

			}
			else {
			
				if (frame_index < all_frames.length) {

					var next_index = frame_index;
				}
				else {
				//you chose a frame outside the range
				//choose default next
					var next_index = this.slide_selector(all_frames.length, current_index);
				}


			}
		

			
			var next = all_frames[next_index];
			var next_frame;
			//possible infinite loop....
			while (next.value==this.active_frame || next.type=="broken")
			{   
				next = all_frames[this.slide_selector(all_frames.length, next_index)];
			}
			if (next.type=='cached')
			{
				next_frame = next.value;
				YAHOO.util.Dom.replaceClass(next_frame, "yui-sldshw-cached", "yui-sldshw-next");
				this.next_frame = next_frame;
				this.effect.setup(this.next_frame);
			}
			else if ( next.type=='image_url')
			{
				next_frame = document.createElement('img');
				next_frame.setAttribute('src',next.value);
				//next_frame.setAttribute('id','frame_'+next.id);
				next.type='cached';
				next.value=next_frame;
				YAHOO.util.Dom.addClass(next_frame, "yui-sldshw-frame");
				YAHOO.util.Dom.addClass(next_frame, "yui-sldshw-next");
				this.container.appendChild(next_frame);
				this.next_frame = next_frame;
				this.effect.setup(this.next_frame);
			}
			else if (next.type=='remote_html')
			{
				var callback = { 
					success: function(o) {
						var next_frame = document.createElement('div');
						next_frame.innerHTML = o.responseText;
						next_frame.setAttribute('id','frame_'+o.argument.id);
						o.argument.type='cached';
						o.argument.value=next_frame;
						YAHOO.util.Dom.addClass(next_frame, "yui-sldshw-frame");
						YAHOO.util.Dom.addClass(next_frame, "yui-sldshw-next");
						this.container.appendChild(next_frame);
						this.next_frame = o.argument.value;
						this.effect.setup(this.next_frame);
					},
					failure: function(o) {
						this.type='broken';
						this.choose_next_frame();
					},
					scope: this,
					argument: next
				}
				var transaction = YAHOO.util.Connect.asyncRequest('GET', next.value , callback,  null); 
			}
		},
	clean_up_transition : function() 
		{ 
		
			YAHOO.util.Dom.replaceClass(this.active_frame, "yui-sldshw-active", "yui-sldshw-cached");
			YAHOO.util.Dom.replaceClass(this.next_frame, "yui-sldshw-next", "yui-sldshw-active");
			this.active_frame = this.next_frame; 
		    	this.choose_next_frame(-1);
		},
	transition: function()
		{

            var hide = this.effect.get_animation(this.active_frame);
			
			hide.onComplete.subscribe(this.clean_up_transition, this, true);
		    hide.animate();
		}
	,
	loop: function()
		{
			var self;
			self =this;
			this.loop_interval = setInterval( function(){ self.transition();}, this.interval );
		}
	,
	stop_loop: function() 
		{
			this.first_run = 0;
			clearInterval(this.loop_interval);

		}
	,
	go_to_frame: function(frame_id)
		{
			//stop looping
			this.stop_loop();
				
			//cache the frame which was previously chosen
			//as we are going to force a particular frame
			//instead
			YAHOO.util.Dom.replaceClass(this.next_frame, "yui-sldshw-active", "yui-sldshw-cached");

			//set the next frame to our choice
			x = this.get_frame_index_by_id(frame_id);
			this.choose_next_frame(x);
			
			//go to the next frame without a transition
			this.clean_up_transition();

			//initialise the default looping
			//behaviour from this new point
			//this.init();
			//this.loop();

		}
 }	


YAHOO.myowndb.slideshow.effects = {
	slideRight :{
			setup: function(frame){
				YAHOO.util.Dom.setStyle(frame, 'top', '0'); 
				YAHOO.util.Dom.setStyle(frame, 'left', '0'); 
			},
			get_animation: function(frame){
					var region = YAHOO.util.Dom.getRegion(frame);
					return new YAHOO.util.Motion(frame, { points: { by: [region.right-region.left,0] } }, 1, YAHOO.util.Easing.easeOut);
			}
		},
	slideLeft: {
			setup: function(frame){
					YAHOO.util.Dom.setStyle(frame, 'top', '0'); 
					YAHOO.util.Dom.setStyle(frame, 'left', '0'); 
			},
			get_animation: function(frame){
					var region = YAHOO.util.Dom.getRegion(frame);
					return new YAHOO.util.Motion(frame, { points: { by: [region.left-region.right,0] } }, 1, YAHOO.util.Easing.easeOut);
			}
		},
	squeezeLeft: {
			setup: function(frame){
					YAHOO.util.Dom.setStyle(frame, 'width', '100%'); 
			},
			get_animation: function(frame){
					var region = YAHOO.util.Dom.getRegion(frame);
					return new YAHOO.util.Anim(frame, { width: { to: 0 } }, 1, YAHOO.util.Easing.easeOut);
			}
		},
	squeezeRight: {
			setup: function(frame){
					YAHOO.util.Dom.setStyle(frame, 'width', '100%'); 
					YAHOO.util.Dom.setStyle(frame, 'right', '0px'); 
			},
			get_animation: function(frame){
					var region = YAHOO.util.Dom.getRegion(frame);
					YAHOO.util.Dom.setStyle(frame, 'right', '0px'); 
					return new YAHOO.util.Anim(frame, { width: { to: 0 }}, 1, YAHOO.util.Easing.easeOut);
			}
		},
	squeezeUp: {
			setup: function(frame){
					YAHOO.util.Dom.setStyle(frame, 'height', '100%'); 
			},
			get_animation: function(frame){
					var region = YAHOO.util.Dom.getRegion(frame);
					return new YAHOO.util.Anim(frame, { height: { to: 0 }}, 1, YAHOO.util.Easing.easeOut);
			}
		},
	squeezeDown: {
			setup: function(frame){
					YAHOO.util.Dom.setStyle(frame, 'height', '100%'); 
			},
			get_animation: function(frame){
					var region = YAHOO.util.Dom.getRegion(frame);
					YAHOO.util.Dom.setStyle(frame, 'bottom', '0px'); 
					return new YAHOO.util.Anim(frame, { height: { to: 0 }}, 1, YAHOO.util.Easing.easeOut);
			}
		},
	fadeOut: {
			setup: function(frame){
					YAHOO.util.Dom.setStyle(frame, 'opacity', '1'); 
			},
			get_animation: function(frame){
					return new YAHOO.util.Anim(frame, { opacity: { to: 0 }}, 1, YAHOO.util.Easing.easeOut);
			}
		},
	fadeIn: {
			setup: function(frame){
					YAHOO.util.Dom.setStyle(frame, 'opacity', '0'); 
					YAHOO.util.Dom.setStyle(frame, 'z-index', '20'); 
			},
			get_animation: function(frame){
					var region = YAHOO.util.Dom.getRegion(frame);
					return new YAHOO.util.Anim(frame, { opacity: { to: 1 }}, 1, YAHOO.util.Easing.easeOut);
			}
		}  
}
YAHOO.myowndb.slideshow.effects.slideUp={
			setup: function(frame){
					YAHOO.util.Dom.setStyle(frame, 'top', '0'); 
					YAHOO.util.Dom.setStyle(frame, 'left', '0'); 
			},
			get_animation: function(frame){
					var region = YAHOO.util.Dom.getRegion(frame);
					return new YAHOO.util.Motion(frame, { points: { by: [0,region.top-region.bottom] } }, 1, YAHOO.util.Easing.easeOut);
			}
}
//YAHOO.myowndb.effect = {
//	setup: function(){
//			YAHOO.util.Dom.setStyle(next_frame, 'top', '0'); 
//			YAHOO.util.Dom.setStyle(next_frame, 'left', '0'); 
//	},
//	get_animation: function(frame){
//			return new YAHOO.util.Motion(frame, { points: { by: [-800,0] } }, 1, YAHOO.util.Easing.easeOut);
//	}
//}

