var caxtonImageChanger = Class.create();

caxtonImageChanger.prototype = {
	
	
	initialize: function( initId, IdList, randomise ) {	
	
		this.activeImage = initId;
		
		this.nextImage = initId;
		
		this.lastHovered = initId;
		
		this.animate = true;
		
		this.pointer = 0;
		
		if( randomise ) this.randomise = true;

		this.detectIE();

		if( IdList ) { 
			
			this.imageList = IdList;
		
			this.showImage();
						
		}
			
	},


	detectIE: function() {	
		
		this.isIE = (/MSIE (5\.5|6\.)/.test(navigator.userAgent) ) ? true : false;
		
		if( this.isIE && this.isPNG( $(this.activeImage).src ) ) {

			im = document.getElementById( this.activeImage );
			
			im.style.display = "none";
			
			$('content').style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + im.src + "', sizingMethod='scale')";
			
		}
		
	},
	
	
	isPNG: function( src ) {
	
		return (/\.png/.test(src) ) ? true : false;
	
	},


	hideActiveImage: function() {	
		
		if( this.activeImage ) {
		
			if( this.isIE && this.isPNG( $(this.activeImage).src ) ) this.showImage();
			else {
				
				im = document.getElementById( this.activeImage );
				
				new Effect.Opacity(im, {duration:1, from:1, to:0, queue: {position:'end', scope: 'galleryScope'}, afterFinish: this.showImage.bind( this ) });		
		
			}
		
		}
		
	},


	showImage: function() {	
		
		if( this.activeImage ) document.getElementById( this.activeImage ).style.display = "none";

		im = document.getElementById( this.nextImage );
		
		Element.setOpacity( im, 0 );
		
		im.style.display = "block";
		
		if( this.isIE && this.isPNG( im.src ) ) {
		
			$('content').style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + im.src + "', sizingMethod='scale')";
		
			this.allowAnimation();
		
		} else new Effect.Opacity(im, {duration:1, from:0, to:1, queue: {position:'end', scope: 'galleryScope'}, afterFinish: this.allowAnimation.bind( this ) });		

		this.activeImage = this.nextImage;
		
		if( this.imageList ) this.autoPlay();
				
	},
	
	
	allowAnimation: function() {
	
		this.animate = true;
		
		if( this.lastHovered != this.activeImage ) this.changeImage( this.lastHovered );
	
	},


	changeImage: function( imageId ) {	
		
		if( imageId != this.activeImage && this.animate == true ) {
		
			this.animate = false;
			
			this.nextImage = imageId;
			
			this.hideActiveImage();		
			
		}	
		
		this.lastHovered = imageId;
		
	},
	
	
	autoPlay: function() {
	
		if( this.randomise ) var nextImageId = this.getRandomNextImage();
		else var nextImageId = this.getNextImage();
	
		this.timeoutId = setTimeout( "myCaxtonImageChanger.changeImage('" + nextImageId + "')", 5000 );
	
	},
	
	
	getRandomNextImage: function() {
	
		var selectedImage = this.activeImage;
		
		while( selectedImage == this.activeImage ) {
		
			var randomNumber = Math.round( Math.random()*(this.imageList.length - 1) );
		
			selectedImage = this.imageList[ randomNumber ];
		
		}	
		
		return selectedImage;
		
	},
	
	
	getNextImage: function() {
	
		this.pointer = this.pointer + 1;		

		if( this.pointer == this.imageList.length ) this.pointer = 0;
				
		return this.imageList[ this.pointer ];
	
	},
	
	
	stop: function() {
	
		if( this.timeoutId ) clearTimeout ( this.timeoutId );
	
	}

	
}


function initCaxtonImageChanger( initId, IdList, randomise ) { myCaxtonImageChanger = new caxtonImageChanger( initId, IdList, randomise ); }