//image rotator initialization - Ben Willard for Lawrence University ITS - 7/14/2009
/* sets up image rotating mechanism and controls. Currently based on jQuery and jQuery cycle plugin.
*/
/* Revisions:
	8/05/2009 - JJD - Added starting_slide random determination and startingSlide option assignment lines.
	9/01/2009 - JJD - Changed function calls to jQuery() from $() so that Scriptaculous and other Prototype-based scripts can coexist with this script.
	10/12/2009 - JJD - Made progression random!
	11/23/2009 - JJD - Added override for arrow buttons to change slide sequentially when clicked (random progression remains for timeout event)
						-- NOTE: This is because the advance() and go() functions DOUBLY add 1 to "nextSlide" before checking against
							the randomMap[nextSlide], so n/2 slides get skipped.  The backward function WORKS CORRECTLY and seems to only
							adjust nextSlide by 1 in total, so every slide is hit.  The bug only affects forward *CLICKED* movement (not timeout event).
*/

jQuery(document).ready( function() {
	var starting_slide = Math.floor(Math.random()*jQuery('#icon>*').length);
	var opts = { 
		fx: 'scrollHorz',
		prev: '#controlPrev',
		next: '#controlNext',
		pager: '#controlContainer',
		pause: 1,
		speed: 500,
		random: 1,
		timeout: 20000,
		startingSlide: starting_slide
	};
	jQuery('#icon').cycle(opts);
	//Remove the onClick advancement binds from the cycle plugin:
	jQuery(opts.next + ", " + opts.prev).unbind('click');
	//Add our custom binds:
	jQuery(opts.next).click(function(){
		var data = jQuery("#icon").data('cycle.opts');
		var nextSlide = (data.currSlide == data.elements.length - 1) ? 0 : data.currSlide + 1;
		jQuery(opts.pager + " a:eq(" + nextSlide + ")").click();
		return false; //Prevent the link from changing the document location
	});
	jQuery(opts.prev).click(function(){
		var data = jQuery("#icon").data('cycle.opts');
		var nextSlide = (data.currSlide == 0) ? data.elements.length - 1 : data.currSlide - 1;
		jQuery(opts.pager + " a:eq(" + nextSlide + ")").click();
		return false; //Prevent the link from changing the document location
	});
});
