/* ***********************************************************************
TODO
	Automatically add in "[read more]" links if img is wrapped in 
		"<a href>" tag and if truncated add in "..." on the end of 
		string.

LIMITATIONS
	text will "pop on".  may be able to fade in as well - low priority.

	lots of quick clicks will result in the animation having a hard time
		to keep up - not sure of a way to fix it without having the
		old images to just "pop off" - several work arounds provided if
		necessary (none perfect).

	if you have more than ~20 images it may cause display issues for
		feature_selections section.  this can only be fixed by CSS.
************************************************************************ */


// DEFAULTS - edit these for times - 1000(ms) =  1 second
if(!fade_timer) var fade_timer = 7000;
if(!transition_time) var transition_time = 1000;

// DEFAULTS - string length limits
if(!longdesc_limit) var longdesc_limit = 270;
if(!title_limit) var title_limit = 45;

var intval;
function builder()
{
	// rotator will not display unless javascript is enabled
	$('#feature_rotator').css('display', 'block');
	$('#feature_rotator img:first').css('display', 'block').addClass('active');

	// wrap images in div - feature_imgs
	$('#feature_rotator > img').wrapAll('<div class="feature_imgs" />');

	// set up rel for each img
	var item = 1;
	$('#feature_rotator > .feature_imgs > img').each(function(index) 
	{
		$(this).attr('rel', item);
		item++;
	});

	// get initial description (using longdesc) and title attributes	
	var longdesc = $('#feature_rotator img:first').attr('longdesc');
	var title = $('#feature_rotator img:first').attr('title');
	
	//limit string length
	longdesc = longdesc.substr(0,longdesc_limit);
	title = title.substr(0,title_limit);

	// set up the navigation at bottom
	$('#feature_rotator').append('<div class="feature_selections"></div>');
	var items = $('#feature_rotator > .feature_imgs > img').length;
	$('#feature_rotator > .feature_imgs > img').each(function(index) 
	{
		$('#feature_rotator > .feature_selections').append('<a href="#" rel="'+ items +'" title="'+ items +'">'+ items +'</a>');
		items--;
	});

	// highlight the first feature_selection
	$('#feature_rotator > .feature_selections > a[rel|="1"]').addClass('highlight');

	// display initial text with description and titles
	$('#feature_rotator').append('<div class="feature_text"><div class="title"></div><div class="text"></div></div>');
	$('#feature_rotator > .feature_text > .title').html(title);
	$('#feature_rotator > .feature_text > .text').html(longdesc);
}

function fader(slide)
{
	// if slide nav (feature_selections) was clicked
	if(slide != 'auto')
	{
		clearInterval(intval);
		$('#feature_rotator > .feature_imgs > img').removeClass('active');
		var $$ = $('img[rel|="'+ slide +'"]');
		//$('img:animated').stop().not('$$').fadeTo(250, 0); // fix for fast click without white flashes but causes highlighted feature_selections to not work until ready - uncomment if you feel the fix is necessary (other workaround is faster transition_time)
		$('img:animated').stop().fadeTo(250, 0); // fix for fast click but causes white flashes - uncomment if you feel the fix is necessary (other workaround is faster transition_time)
		var next = $$.next();
		if ($$.next().length == 0) 
		{
			next = $('#feature_rotator img:first');
		}
		$$.addClass('active');
		$$.fadeTo(transition_time, 1);
		$('#feature_rotator > .feature_imgs > img').not('.active').fadeTo(transition_time, 0);
		
		// find the next img's description and title
		var longdesc = $$.attr('longdesc');
		var title = $$.attr('title');

		//limit string length
		longdesc = longdesc.substr(0,longdesc_limit);
		title = title.substr(0,title_limit);

		// display the new description and title
		$('#feature_rotator > .feature_text > .text').html(longdesc);
		$('#feature_rotator > .feature_text > .title').html(title);

		// remove other highlights then highlight the current feature_selection
		$('#feature_rotator > .feature_selections > a').removeClass('highlight');
		$('#feature_rotator > .feature_selections > a[rel|="'+ slide +'"]').addClass('highlight');

		//$$.removeClass('active');
		//next.addClass('active');
		
		intval = setInterval ('fader("auto")', fade_timer);
	}
	// if slide is set to auto
	else
	{
		var $$ = $('.active');
		
		var next = $$.next();
		if ($$.next().length == 0) 
		{
			next = $('#feature_rotator img:first');
		}

		var selected_rel = next.attr('rel');		

		next.fadeTo(transition_time, 1);
		$$.fadeTo(transition_time, 0);

		next.addClass('active');
		$$.removeClass('active');

		// remove other highlights then highlight the current feature_selection
		$('#feature_rotator > .feature_selections > a').removeClass('highlight');
		$('#feature_rotator > .feature_selections > a[rel|="'+ selected_rel +'"]').addClass('highlight');

		// find the next img's description and title
		var longdesc = next.attr('longdesc');
		var title = next.attr('title');

		//limit string length
		longdesc = longdesc.substr(0,longdesc_limit);
		title = title.substr(0,title_limit);

		// display the new description and title
		$('#feature_rotator > .feature_text > .text').html(longdesc);
		$('#feature_rotator > .feature_text > .title').html(title);
	}
}

// <![CDATA[
$(document).ready(function() 
{
	// onload build the interface and start the fader timer
	builder();
	intval = setInterval ('fader("auto")', fade_timer);

	$('#feature_rotator > .feature_selections > a').click(function(){
		var roll_num = $(this).attr('rel');
		fader(roll_num);
	});
});
// ]]>

