YAHOO.namespace("hdrimg");
YAHOO.hdrimg.slideshow = function (container) {
	this.container = YAHOO.util.Dom.get(container);
	this.effect = YAHOO.hdrimg.slideshow.effects.fadeOut;
	this.frames = [];
	//add cached frames
	var cached_frames = YAHOO.util.Dom.getElementsByClassName("hdr_img_frame", null, this.container);
	for (var i=0; i<cached_frames.length; i++){
		this.frames[i] = { id: i, type: 'cached', value: cached_frames[i]};
	}
	
	//set slide selector
	this.slide_selector = function(number_of_slides, current_index) {
			return (current_index+1)%number_of_slides;
	}
	this.interval = 5000
	this.init();
}
YAHOO.hdrimg.slideshow.prototype = {
	init: function(){
			this.active_frame = this.get_active_frame();
			this.choose_next_frame();
	},
	get_active_frame: function(){
		var current_frame =  YAHOO.util.Dom.getElementsByClassName("hdr_img_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) return i;
		return -1;
	},
	choose_next_frame : function(){
		var current_index = this.get_frame_index(this.get_active_frame());
		if (current_index<0) current_index=0;
		var all_frames = this.frames;
		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, "hdr_img_cached", "hdr_img_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.type='cached';
			next.value=next_frame;
			YAHOO.util.Dom.addClass(next_frame, "hdr_img_frame");
			YAHOO.util.Dom.addClass(next_frame, "hdr_img_next");
			this.container.appendChild(next_frame);
			this.next_frame = next_frame;
			this.effect.setup(this.next_frame);
		}
	},
	clean_up_transition : function() { 
		YAHOO.util.Dom.replaceClass(this.active_frame, "hdr_img_active", "hdr_img_cached");
		YAHOO.util.Dom.replaceClass(this.next_frame, "hdr_img_next", "hdr_img_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();}, this.interval );
	}
}	
YAHOO.hdrimg.slideshow.effects = {
	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);
		}
	}  
}