// Sponsor Rotator function + plugin, and Slideshow plugin
// Updated: DAL 18 Apr 2007

// Use existing in-page call to call SponsorControl - sponsor1.0.js 18 Apr 2007
function startRotateSponsors(foo, opt) {
// Ignoring first parameter (number to show) for now, calculating below
	opt = jQuery.extend({
		el: "#sponsors",
		box: "#sponsor_box",
		maxlen: 4,
		scrollasap: true,
		selector: "div",
		height: 85
//		show, speed, time to be calculated below
	}, opt);
	var show = 1;
	// Add the "item" class to each item in the scroller, then save the count
	var items = jQuery(opt.el).find(opt.selector); // May need this below
	if (items) var count = items.addClass("item").size(); // Sneaking addClass in here
	opt.selector = ".item"; // Set new selector	
	// Quit and hide whole box if nothing found
	if (!count || count<1) {
		$(opt.box).hide();
		// alert('No matching elements found - hiding the whole thing!');
		return false;
	}
	else if (count <= opt.maxlen) {
			if(opt.scrollasap){
				show = Math.max(Math.floor(count/2), 1);
			}
			else {
				show = count;
			}
	}
	else {
		// Must be at least as many not showing as showing
		show = Math.min(Math.floor(count/2), opt.maxlen);
	}
	// How long it takes to move the entire group
	// opt.speed = (show*600)+((opt.maxlen-show) * 300);
	opt.speed = (show*500)+500;
	// Time between scrolls, includes the move time; at least 2 sec for showing only one
	// opt.time = Math.max(opt.speed*2, 3000);
	opt.time = (show * 1500)+opt.speed;
	// Not moving less than all at this point.
	opt.group = show;
		
	if (count > show) { // Set up scrolling if necessary
		var controls = '<span><img src="images/left_arrow.gif" border="0" alt="Go Back" id="reverse"><img src="images/center_arrow.gif" border="0" alt="Pause" id="pause"><img src="images/right_arrow.gif" border="0" alt="Go Forward" id="forward"></span>';
		jQuery(controls).prependTo("#sponsor_box_btm");
		jQuery("#sponsors").SponsorControl(show, opt);			
	}
	else { // Show static items
		jQuery(opt.el).css('height',show * opt.height); // Set height of scroller
		items.each( function(i){
			jQuery(this).css('top', i*opt.height); // Place items just once
		});
		jQuery("#sponsor_box_btm div").hide();	// Don't add controls and hide link to 'all' page
		jQuery("#sponsor_box_btm").addClass("sponsor_box_btm_alt");	// Don't add controls and hide link to 'all' page
	}
};

(function($) { // simulate block scope to use $ for jQuery here only



// Temporarily separating this from the ScrollControl; Lots of unoptimized code here. DAL 18 Apr 2007
$.fn.SponsorControl = function(show, settings) { 
	// if (!show) show = 1;
	// These defaults are from the meeting scroll box, are overridden
	settings = jQuery.extend({
		group: show,
		selector: "div",
		height: 80,
		time: 3000,
		speed: 1000,
		startdir: 1, // 1 = scroll up, -1 = scroll down
		forward: "#forward",
		reverse: "#reverse",
		pause: "#pause"
	}, settings);

	// Set properties
	var items = $(this).children(settings.selector);
	var count = items.size();
	var ptr = new Array(); // pointer for stack
	var timer;
	var busy = false;
	
	//	Set up animation targets for moving up or down - see initialization
	var lo = new Array(show*2);
	var hi = new Array(show*2);
	var pps = Math.round(settings.speed / show); // Pixels per second for coordinating scrolling multiple items.
	
	// Methods
	// Increment pointer array according to direction
	function pointer(dir) {	
		if (dir==1) {
			ptr.push(ptr[0]);
			ptr.shift();
		}
		else {
			ptr.unshift(ptr.pop());
		}
	};

	// Common move functionality
	function move(dir) {
		busy=true;
		var dist = settings.group*settings.height;
		for (var i=0; i<settings.group; i++) {			
			var stage = (dir*dist)+(i*settings.height);
			var iin = Math.max(i*settings.height, 1);
			var iout = -(dir*dist)+(i*settings.height);
			var next = ((dir*settings.group)+count+i)%count; // Next if forward, last group if reverse
			// Animate out from top of visible
			items.eq(ptr[i]).animate({top: iout}, settings.speed); 		
			// Stage and animate in from top of hidden
			items.eq(ptr[next]).css('top', stage).animate({top: iin}, settings.speed, function() {
				busy=(i+1<settings.group);	// Must be in callback for timing																										 
			}); 
		}
		for (var i=0; i<settings.group; i++) {
			pointer(dir);
		}
	};	
	// Start timed loops
	function startTimer(dir) {
		timer = setInterval(function(){move(dir);},settings.time);
	};	
	// Rotate when clicked according to direction; no response if busy (already moving)
	function changeDir(dir) {
		if (busy==true) {
			return;
		}
		clearInterval(timer);		
		move(dir); // Rotate once immediately
		startTimer(dir); // Then start timer
	};		
	// Pause button
	 function pause() {
		clearInterval(timer);
	};
	
	// Initialize ===========================================================
	// Will be able to consolidate
	// Pointer moves after using
	for (var i=0; i<(show*2); i++) { // start, end, speed,
		var start = (i==0) ? 1 : i*settings.height;
		var target = ((-show+i)==0) ? 1 : (-show+i)*settings.height;
		hi[i] = [start, target];
	}
	// Pointer moves before using
	for (var i=0; i<(show*2); i++) { // start, end, speed,
		var start = ((-show+i)==0) ? 1 : (-show+i)*settings.height;
		var target = (i==0) ? 1 : i*settings.height;
		lo[i] = [start, target]; // array holds postions for going down
	}
	
	// initialize
	for (var i=0; i<count; i++) {
		ptr[i] = i;
	}
	
	// Set initial position
	items.each( function(i){
		$(this).css('top', i*settings.height);
	});
	
	// Set height of the visible container
	$(this).css('height',show * settings.height);
	
	// Add clickable controls
	$(settings.forward).click(function(){ changeDir(1); });
	$(settings.reverse).click(function(){ changeDir(-1); });
	$(settings.pause).click(function(){ pause(); });
	
	// Crank it up. Move first time sooner (deducting movement time) so all are equal.
	function startup() {
		var temptimer = setInterval(function(){move(settings.startdir); clearInterval(temptimer);startTimer(settings.startdir);},((settings.time)-(settings.speed)));
	};
	
	startup();
	
};



// Doc ready function for Industry Photos
$(function(){
	startRotateSponsors();
});

// end block scope
})(jQuery);
