HMBannerImage = {
	
	target:null,	
	timeout:3000,
	fade_speed:2000,
	images:['banner_1','banner_rotate_bar','banner_12','banner_rotate_exterior_night','banner_rotate_couple','banner_rotate_terrace'],
	current_image:1,
	current_time:null,
	base_path:'/sites/miramar-bournemouth.com/themes/hotelmiramar/img/banner_images/',

	init:function(){
		this.target = $( '#banner_form_container' );
		
		for( var i = 0; i < this.images.length; i++ ){
			this.images[ i ] = { name:this.images[i], loaded:false };
		}
		
		this.LoadNextImage();
	},
	ChangeImage:function(){
		this.fade_image = $( document.createElement( 'img' ) );
		this.fade_image.css(
			{
				position:'absolute',
				top:'0px',
				left:'0px'
			}
		);
		this.fade_image.hide();
		this.target.append( this.fade_image );

		this.fade_image.attr('src', this.base_path + this.images[ this.current_image ].name + '.jpg' );
		this.fade_image.fadeIn( this.fade_speed, function(){ HMBannerImage.FadeDone() } );
	},
	FadeDone:function(){
		this.target.css( 'background-image', 'url(' + this.base_path + this.images[ this.current_image ].name + '.jpg' + ')' );
		this.fade_image.hide();
		this.fade_image.remove();
		this.fade_image = null;
		if( this.current_image < this.images.length - 1 ){
			this.current_image++;
		} else {
			this.current_image = 0;
		}
		this.LoadNextImage();		
	},
	LoadNextImage:function(){
		this.current_time = new Date().getTime();
		if( !this.images[ this.current_image ].loaded ){
			var image_holder = document.createElement( 'img' );
			image_holder.src = this.base_path + this.images[ this.current_image ].name + '.jpg';
			$(image_holder).bind( 'load', function() { HMBannerImage.NextImageLoaded() } );
		} else {
			this.NextImageLoaded();
		}
	},
	NextImageLoaded:function(){
		this.images[ this.current_image ].loaded = true;
		var time_elapsed = new Date().getTime() - this.current_time;
		if( time_elapsed < this.timeout ){
			setTimeout( function(){ HMBannerImage.ChangeImage() }, this.timeout - time_elapsed );
		} else {
			this.ChangeImage();
		}
	}
}
$(document).ready
	(
		function(){
			HMBannerImage.init();
		}
	);

