// JavaScript Document

function ImageRotator ( container, settings ) {
	
	this.container = $(container);
	this.rotatingIID = -1;
	this.imageCount = 0;
	this.imageIndex = 0;
	this.container.data('rotator', this);
	
	if (this.container.attr('id') !== '') {
		this.containerID = this.container.attr('id');
	} else {
		this.containerID = 'uid_' + new Date().getTime();
		this.container.attr('id', this.containerID);
	}
	
	this.settings = {
		width: '',
		height: '',
		arrowLeft: '&lt;',
		arrowRight: '&gt;',
		delay: 2000,
		rotateTime: 750,
		arrowPadding: 10
	}
	if (settings !== undefined) { $.extend(this.settings, settings); } 
	
	
	this.check = function() {
		var valid = true;
		// Check to see if it appropriate to build an image rotator from the passed container
		// Contains more than one image
		if ($('img', this.container).length < 2) { valid = false; }
		
		if (!valid) { return false; }
		this.init();
	}
	
	
	this.init = function() {

		// Get all images in the container
		var view = $('<div id="imageRotatorView"></div>').css({overflow:'hidden',margin: '0 auto'});
		var pane = $('<div id="imageRotatorPane"></div>').css({float:'left'}).appendTo(view);//
		$('img', this.container).appendTo(pane).wrap('<div class="imageRotatorImage" style="float:left; overflow:hidden;"></div>');
		this.container.html('');
		view.appendTo(this.container);
		
		// Set container, view, pane and image holder widths;
		if (isNaN(this.settings.width)) {
			switch(this.settings.width) {
				case 'largest':
					var imageWidth = -1;
					$('.imageRotatorImage').each(function(){
						imageWidth = Math.max($(this).width(), imageWidth);
					});
					this.settings.width = imageWidth;
					break;
				default:
					this.settings.width = '';
			}
		}
	
		if (this.settings.width !== '') {
			view.width(this.settings.width);
			$('.imageRotatorImage').width(this.settings.width);
		}
		
		// Set container, view, pane and image holder heights;
		if (isNaN(this.settings.height)) {
			switch(this.settings.height) {
				case 'largest':
					var imageHeight = -1;
					$('.imageRotatorImage').each(function(){
						imageHeight = Math.max($(this).height(), imageHeight);
					});
					this.settings.height = imageHeight;
					break;
				default:
					this.settings.height = '';
			}
		} 
		
		if (this.settings.height !== '') {
			this.container.height(this.settings.height);
			view.height(this.settings.height);
			pane.height(this.settings.height);
			$('.imageRotatorImage').height(this.settings.height);
		}
		
		// Center small images
		$('.imageRotatorImage img').each(function(){
			var image = $(this);
			if (image.width() < image.parent().width()) { image.css('padding-left', (image.parent().width() - image.width())/2); }
			if (image.height() < image.parent().height()) { image.css('padding-top', (image.parent().height() - image.height())/2); }
		});
		
		//Copy first image to end and set width of pane;
		$('.imageRotatorImage:first', pane).clone().appendTo(pane);
		this.imageCount = $('.imageRotatorImage').length - 1;
		this.imageIndex = 0;
		if (this.settings.width !== '') {
			pane.width((this.imageCount + 1) * this.settings.width);
		}
		
		// Add back and forward arrows
		$('<div id="imageRotatorArrowRight">' + this.settings.arrowRight + '</div>')
			.css({float:'right', cursor:'pointer', border:'none', padding:this.settings.arrowPadding})
			.bind('click', {rotator:this}, function(e){
				e.data.rotator.rotate('previous');
			})
			.prependTo(this.container);
			
		$('<div id="imageRotatorArrowLeft">' + this.settings.arrowLeft + '</div>')
			.css({float:'left', cursor:'pointer', border:'none', padding:this.settings.arrowPadding})
			.bind('click', {rotator:this}, function(e){
				e.data.rotator.rotate('next');
			})
			.prependTo(this.container);
			
		// Fix container and arrow sizes
		var arrowWidths = $('#imageRotatorArrowRight').outerWidth() + $('#imageRotatorArrowLeft').outerWidth();	
		this.container.width(( this.settings.width + arrowWidths )).css('margin','0 auto');
		$('#imageRotatorArrowRight, #imageRotatorArrowLeft').css('margin-top', ( this.settings.height - $('#imageRotatorArrowLeft').height() )/2);	

		// Start rotating
		this.rotatingIID = setInterval("$('#"+this.containerID+"').data('rotator').rotate('next')", this.settings.delay );
		
	}
	
	this.rotate = function(index) {
		clearInterval(this.rotatingIID);
		if ($('#imageRotatorPane').data('rotating')) return false;
		
		// Change index
		if (index == 'next' || index === '') {
			if (this.imageIndex == this.imageCount) {
				$('#imageRotatorPane').css( 'margin-left', 0 );
				this.imageIndex = 1;
			} else {
				this.imageIndex++;
			}
		} else if (index == 'previous') {
			if (this.imageIndex == 0) {
				$('#imageRotatorPane').css( 'margin-left', (-1 * this.settings.width * this.imageCount) );
				this.imageIndex = this.imageCount - 1;
			} else {
				this.imageIndex--;
			}
		} else {
			if (index < 0 || index > this.imageCount) {
				this.imageIndex = 0;
			} else {
				this.imageIndex = index;
			}
		}
									
		// Move pane to new location
		$('#imageRotatorPane')
			.data('rotating',true)
			.animate({ marginLeft: -1 * this.settings.width * this.imageIndex }, this.settings.rotateTime, 'linear' )
			.data('rotating',false);	
		
		// Now automate it
		this.rotatingIID = setInterval("$('#"+this.containerID+"').data('rotator').rotate('next')", this.settings.delay );
	}
	
	this.check();
}

