/**
* Content Rotator / scrolls content inside a frame
* @author Eric Grossnickle
* @url http://mightydream.com
* @version 1.0.1
*/

jQuery.fn.rotator = function(options) {
	var settings = jQuery.extend({
		'controller': '#controller',	// the controller element
		'images': '#images',			// the images element
		'content': '#content',			// the content element, use 'false' if not applicable
		'speed': 1000,					// speed of transition for images (in milliseconds)
		'transition': 'vertical',		// style of transition for the images: 'horizontal', 'vertical', or 'crossfade'
		'easing': 'swing',				// style of easing: 'swing' and 'linear' are built-in, others are available in jQuery UI
		'interval': false,				// use automatic scrolling (stops when a controller button is clicked)
		'intervalDuration': 10000,		// duration of the interval
		'hoverPause': false				// pause interval: 'true' to use images element as hover area or specify other, e.g. '#stack, #content'
	}, options);
	
	// initialize variables
	var $controller = $(settings.controller),
	    $imagesWindow = $(settings.images).find('.images-window'),
	    $images = $(settings.images).find('.images'),
	    $content = $(settings.content),
	    imageH = $images.find('> :first').height(),
	    imageW = $images.find('> :first').width(),
	    current = 1,
	    startAt = (document.location.hash) ? (($images.find(document.location.hash).index() >= 0) ? $images.find(document.location.hash).index() + 1 : 1) : 1,
	    total = $images.children().size(),
	    ie = ($.browser.msie && parseInt($.browser.version) < 9) ? true : false;
	
	settings.intervalId = 0;
	
	// based on the transition style, set the size of the images holder
	if (settings.transition == 'horizontal') {
		$images.width(imageW * total);
	}
	else if (settings.transition == 'vertical') {
		$images.height(imageH * total);
	}
	else if (settings.transition == 'crossfade') {
		$images
			.children()
			.css({
				position: 'absolute',
				float: 'none'
			});
		$images
			.children(':not(:first)')
			.css('display', 'none');
	}
	else return;
	
	function startInterval() {
		settings.intervalId = setInterval(function() {
			if(current >= total) next = 1;
			else next = current + 1;
			
			goto(next);
		}, settings.intervalDuration);
	}
	
	function stopInterval(forGood) {
		if (typeof(forGood) == 'undefined') forGood = false;
		
		clearInterval(settings.intervalId);
		settings.intervalId = 0;
		if (forGood) settings.interval = false;
	}
	
	// start automatic scrolling
	if (settings.interval) {
		startInterval();
	}
	
	// setup the controller actions
	$controller.find('a')
		.each(function(index){
			$(this)
				.unbind('click')
				.click(function() {
					goto(index+1);
					
					// stop automatic scrolling
					if (settings.interval) {
						stopInterval(true);
					}
					
					return false;
				});
		});
	
	// make changes for IE
	if (ie) {
		$controller.find('> :last').addClass('last');
	}
	
	// function for moving to a a specific image, use 'set' to skip the animation
	function goto(index, set) {
		if (typeof(set) == 'undefined') set = false;
		
		if (index == current && !set) return;
		
		
		
		// update controller buttons
		$controller.find('> .active').removeClass('active').find('a').trigger('mouseleave');
		$controller.find('> :nth-child('+index+')').addClass('active').find('a').trigger('mouseenter');
		
		// update image classes
		$images.find('> .active').removeClass('active');
		$images.find('> :nth-child('+index+')').addClass('active');
		
		// transition images
		if (settings.transition == 'horizontal') {
			if (set) {
				$images.css({
					left: -(index-1) * imageW
				});
			}
			else {
				$images.animate({
					left: -(index-1) * imageW
				}, {duration: settings.speed, easing: settings.easing, queue: false});
			}
		}
		else if (settings.transition == 'vertical') {
			if (set) {
				$images.css({
					top: -(index-1) * imageH
				});
			}
			else {
				$images.animate({
					top: -(index-1) * imageH
				}, {duration: settings.speed, easing: settings.easing, queue: false});
			}
		}
		else if (settings.transition == 'crossfade') {
			if (set) {
				$images.children(':nth-child('+index+')').show();
			}
			else {
				$images.children(':nth-child('+current+')').stop().fadeOut(settings.speed);
				$images.children(':nth-child('+index+')').stop().fadeTo(settings.speed, 1);
			}
		}
		
		// transition content
		if (settings.content) {
			if (set) {
				$content.children(':nth-child('+index+')').show();
			}
			else {
				// for IE, simply hide and show
				if (ie) {
					$content.children(':nth-child('+current+')').hide();
					$content.children(':nth-child('+index+')').show();
				} else {
					$content.children(':nth-child('+current+')').stop().fadeOut(settings.speed/2, function() {
						$content.children(':nth-child('+index+')').stop().fadeTo(settings.speed/2, 1);
					});
				}
			}
		}
		
		if (newHash = $images.find('.active:first').attr('id')) {
			if (!set) {
				$images.find('.active:first').attr('id', '');
				document.location.hash = newHash;
				$images.find('.active:first').attr('id', newHash);
			}
		}
		
		current = index;
	}
	
	if (settings.hoverPause) {
		if (settings.hoverPause === true) hoverArea = settings.images;
		else hoverArea = settings.hoverPause;
		
		$(hoverArea).hover(
			function() { stopInterval(); $(this).addClass('hover'); },
			function() { if (settings.interval) startInterval(); $(this).removeClass('hover'); }
		);
	}
	
	goto(startAt, true);
};
