/*
 * Rotation Script Done By Andreas Voigt
 * 2009-04-02 - http://voigt.se
 */
(function($) {
	$.fn.extend({
		imageRotator: function(options) {
			var defaults = {
				"speed": 300,
				"wait": 3000,
				"index": 0,
				"controls": true,
				"autoPlay": true,
				"showTimer": false
			};

			var options = $.extend(defaults, options);

			return this.each(function() {
				var prevController = null, nextController = null, timeOut;
				var that = $(this);
				var child = that.children();
				var timer = null;
				var nextIndex = options.index;
				var height = 0, width = 0;

				//Group all children to the same position.
				that.css({
					'position': 'relative'
				}).children().css({
					"position" : "absolute",
					"top": that.css("paddingTop"),
					"left": that.css("paddingLeft"),
					"display": "none"
				}).eq(options.index).css({
					"display": ""
				});

				if(options.controls)
					addController();

				resize(0);

				if(options.autoPlay) {
					animate(next);
					if(options.showTimer)
					{
						timer = $('<div class="progressBar"><div></div></div>').hide();
						$(that).mouseover(function(){ timer.show()}).mouseout(function(){ timer.hide();});
						that.append(timer);
						animeateProgressBar();
					}
				}
				function resize(speed) {
					var childObj = child.eq(nextIndex);
					height = childObj.height();
					width = childObj.width();
					that.animate({
						"height": 190 + "px",
						"width" : 520 + "px" 
					}, speed);

					if(options.controls)
						updateButtonPosition(height, width, speed);
				};
				function next() {
					nextIndex = options.index+1>=child.length ? 0 : options.index+1;
					doAnimation(next);
				};
				function prev() {
					nextIndex = options.index-1<0 ? child.length-1 : options.index-1;
					doAnimation(prev);
				};
				function doAnimation(autoPlayFunction) {
					child.eq(options.index).fadeOut(options.speed);
					child.eq(nextIndex).fadeIn(options.speed);
					resize(options.speed);
					options.index = nextIndex;
					if(options.autoPlay){
						animate(autoPlayFunction);
					}
				};
				function animeateProgressBar(){
					$(".progressBar",that).css({width: width + 'px'});
					$(".progressBar div",that).stop().animate({'width': width + 'px'},0).animate({'width': '0px'}, options.wait);
				};
				function animate(func) {
					if(options.showTimer)
						animeateProgressBar();
					clearTimeout(timeOut);
					timeOut = setTimeout(func, options.wait);
				};
				function addController() {
					prevController	= $('<div class="prev">Previous</div>').click(function() {
						prev();
					}).css({
						"position" : "absolute"
					});
					nextController	= $('<div class="next">Next</div>').click(function() {
						next();
					}).css({
						"position" : "absolute"
					});
					that.append(prevController).append(nextController);
				};
				function updateButtonPosition(height, width, speed) {
					var pos = parseInt((height/2));
					//prevController.animate({ "top": pos + "px"}, speed);
					//nextController.animate({ "top": pos + "px", "right" : that.css("paddingRight")}, speed);
				};
			});
		}
	});
})(jQuery);
