/* =========================================================

// jquery.rotator.js

// Date: 2007-09-20
// Author: Jody Mickey
// Company: Palmetto Webworks

// based off of innerfade by Medienfreunde Hofmann & Baldes GbR

// ========================================================= */


(function($) {

var stopRotating = false;

$.fn.rotator = function(options) {

	this.each(function(){ 	
		
		var settings = {
			animatestyle: 'fade', 
			animatespeed: 'normal',
			timeout: 5000
		};
		
		if(options)
			$.extend(settings, options);
		
		var elements = $(this).children('ul.listings').children();
		var main = $(this).children('div.main');
		
		// preload full size images
		$(this).children('ul.listings').find('img').each(function() {
			var img = new Image();
			img.src = $(this).attr('alt');
		});
	
		if (elements.length > 1) {
			$(elements[0]).children('p').children('img').addClass('current');
			setTimeout(function(){
				$.rotator.next(main, elements, settings, 1, 0);
			}, settings.timeout);
		}
		
		// manually pick on thumbnail click
		$(elements).children('p').children('img').click(function() {
			$(elements).each(function() {
				$(this).children('p').children('img').removeClass('current');
			});
			var newMain = $(this).parent().parent().clone();
			newMain.children().children('img').attr('src', $(this).attr('alt')).css('border', 'none');
			$(this).parent().parent().parent().parent().children('.main').html(newMain.html());
			$(this).addClass('current');
			stopRotating = true;
		});
		
		// redirect if click on main and has a .fr-link classed link
		$(main).click(function() {
			var link = $(this).children('.fr-link').children('a').attr('href');
			if(link != null)
				location.href = link;
		});
		
	});
	
};

$.rotator = function() {}
$.rotator.next = function (main, elements, settings, current, last) {
	if(!stopRotating) {
		// set all image borders to black
		$(elements).each(function() {
			$(this).children('p').children('img').removeClass('current');
		});
		
		// clone the LI inner code
		var newMain = $(elements[current]).clone();
		// swap the image src and remove the border
		newMain.children('p').children('img').attr('src', 
			$(elements[current]).children('p').children('img').attr('alt')).css('border', 'none');
		// replace the main content
		$(main).html(newMain.html());
		// set the thumbnail to the current class
		$(elements[current]).children('p').children('img').addClass('current');

		if ( ( current + 1 ) < elements.length ) {
			current = current + 1;
			last = current - 1;
		} else {
			current = 0;
			last = elements.length - 1;
		};
		
		setTimeout((function(){$.rotator.next(main, elements, settings, current, last);}), settings.timeout);
	}
};

})(jQuery);
