// Fixes setTimeout for IE6
/*@cc_on
(function(f) {
	window.setTimeout = f(window.setTimeout);   // overwrites the global function!
	window.setInterval = f(window.setInterval); // overwrites the global function!
})(function(f) {
	return function(c, t) {
		var a = [].slice.call(arguments, 2);    // gathers the extra args
		return f(function() {
			c.apply(this, a);                   // passes them to your function
		}, t);
	};
});
@*/


var ImageFader = Class.create({
	current_image: null,
	next_image: null,
	loaded_images: 0,
	loading_complete: false,
	
	images: [],
	
	container: null,
		
	initialize: function(options) {
		
		if( typeof(options.container_id) != undefined ) {
			this.container = $(options.container_id);
		}
		
		if( typeof(options.images) != undefined ) {
			this.images = options.images;
		}
		
		(function(fader) {
		
		
		// Preload all of the image elements
		fader.images.each( function(i) {		
		
			// If an image tag is given
			
			if( typeof(i.href) != undefined && i.href.length > 0 ) {
					
				anchor = new Element('a');
				anchor.setStyle( { textDecoration : 'none', position : 'absolute' } );
				anchor.href = i.href
					
				img = new Element('img');
				
				(function(image_obj, obj, fader) {
					image_obj.onload = function(e) {
						fader.loaded_images++;
						fader.check_loading_completed();
						obj.loaded = true;
					};
				})(img, i, fader);
				
				img.setStyle( { border : 'none' } );
				img.src = i.src;
					
				anchor.appendChild(img);
					
				i.element = anchor;
											
			} else {			
				i.element = new Element('img');
				(function(obj, fader) {
					obj.element.onload = function(e) {
						fader.loaded_images++;
						fader.check_loading_completed();
						obj.loaded = true;
					};
				})(i, fader);
				
				i.element.setStyle( { border : 'none', position : 'absolute' } );
				i.element.src = i.src;
			}
			
			i.hover = false;
			(function(obj) {			
				i.element.onmouseover = function(e) {
					obj.hover = true;					
				};			
			})(i);
			(function(obj) {
				i.element.onmouseout = function(e) {
					obj.hover = false;					
				};			
			})(i);
			
		});	
		
		})(this);
				
	},
	
	check_loading_completed: function() {
		if( this.loaded_images >= this.images.length ) {
			this.loading_complete = true;
			
			this.container.innerHTML = "";	
			this.container.position = "relative";
			this.current_image = this.images[ Math.floor(Math.random()*this.images.length) ];	
			currImage = ((this.current_image.element.tagName == 'A')? this.current_image.element.childNodes[0] : this.current_image.element );
			
			this.container.appendChild(this.current_image.element);
			this.container.setStyle( { height : currImage.height+'px' } );	
		}
	},
	
	// Fade between this.current_image and this.next_image
	fade: function(options) {
		if( typeof(options.fade_delay) == undefined ) {
			options.fade_delay = 1.0
		}
		if( typeof(options.transition_delay) == undefined ) {
			options.transition_delay = 1.0
		}
		
		if( this.current_image.hover || !this.loading_complete ) {
		
			setTimeout( function(fader, options) {
				fader.fade(options);
			}, (options.transition_delay/2), this, options);
		
		} else {
		
			if( this.current_image && this.next_image ) {
				
				currImage = ((this.current_image.element.tagName == 'A')? this.current_image.element.childNodes[0] : this.current_image.element );
				nextImage = ((this.next_image.element.tagName == 'A')? this.next_image.element.childNodes[0] : this.next_image.element );
				
				this.container.setStyle( { height : Math.max(currImage.height,nextImage.height)+'px' } );			
			
				currImage.setStyle( { opacity : 1.0 } );		
				nextImage.setStyle( { opacity : 0.0 } );
				this.container.appendChild(this.next_image.element);			
				
				(function(fader) {
				
					new Effect.Tween(null, 0, 1, { 
						duration : options.fade_delay,
						afterFinish: function() {
							fader.container.removeChild( fader.current_image.element );					
							fader.current_image = fader.next_image;					
							fader.run(options);					
						}
					
					}, function(p) {
						currImage.setStyle( { opacity : 1-p } );
						nextImage.setStyle( { opacity : p } );
					});
				
				})(this);
			
			}		
		
		}
		
	},
	
	run: function(options) {

		if( typeof(options.fade_delay) == undefined ) {
			options.fade_delay = 1.0
		}	
		if( typeof(options.transition_delay) == undefined ) {
			options.transition_delay = 1.0
		}
		
		setTimeout( function(fader) {
			
			found_next = false;
			fader.next_image = null;
			for(i=0;i < fader.images.length;i++) {
				if( found_next ) {
					fader.next_image = fader.images[i];
					break;
				}
					
				if( fader.current_image == fader.images[i] ) {
					found_next = true;
				}
			}
			if( fader.next_image == null ) {
				fader.next_image = fader.images[0];
			}

			fader.fade(options);
			
		}, options.transition_delay*1000, this );
		
	}

});

document.observe("dom:loaded", function() {

	var images = [
		{	
			"src" : "images/home_banners/home_banner1.jpg",
			"href" : "http://www.pollstream.com/webinar/Real-Engagement-Webinar-Sept-22-2010.html",
			"element" : null,
			"loaded" : false
		},
		{	
			"src" : "images/home_banners/home_banner2.jpg",
			"href" : "http://www.pollstream.com/social_media_platform.html",
			"element" : null,
			"loaded" : false
		},
		{	
			"src" : "images/home_banners/home_banner3.jpg",
			"href" : "http://www.pollstream.com/sparkplug.html",
			"element" : null,
			"loaded" : false
		},
		{	
			"src" : "images/home_banners/home_banner4.jpg",
			"href" : "http://www.pollstream.com/thehive.html",
			"element" : null,
			"loaded" : false
		},
		{	
			"src" : "images/home_banners/home_banner5.jpg",
			"href" : "http://www.pollstream.com/careers.html",
			"element" : null,
			"loaded" : false
		}
	];

	var banner = new ImageFader( { "container_id" : "fade_banner", "images" : images } );	
	banner.run( { "transition_delay" : 7.0, "fade_delay" : 2.0, "loops" : 10 } );

});

