var currentFrame = 0;
var maxFrames = 0;

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

}


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();
			this.interval = bannerDuration;
		},
	get_active_frame: function()
		{
			return YAHOO.util.Dom.getElementsByClassName("yui-sldshw-active", null,  this.container)[0];
		},
	choose_next_frame : function()
		{
			if (currentFrame >= (maxFrames-1)) currentFrame = 0;
			else currentFrame = currentFrame + 1;
			
			cached_frames = this.frames;
			// next = cached_frames[Math.floor( Math.random()*cached_frames.length)];
			next = cached_frames[currentFrame];
			
			//possible infinite loop....
			while (next.value==this.active_frame || next.type=="broken")
			{
				next = cached_frames[Math.floor( Math.random()*cached_frames.length)];
			}
			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')
			{
				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
				}
				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();
		},
	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();}, bannerDuration );
		}
 }	


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){
					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){
					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){
					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){
					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){
					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){
					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 }}, bannerTransition/1000, 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){
					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){
					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);
//	}
//}

