/**
 * SPscroll jQuery plugin
 * Modified 05 Sep 2007 by Briley Hooper: Make initial data parameter optional - if no data given, assumes elements are already in the box
 *
 * SPscroll enables horizontal scrolling of content inside of a container
 * It takes the following parameters:
 * @param   data         Data to populate scroller with.  If no data give, 
 * @param   arrowLeft    REQUIRED. Id of left arrow element
 * @param   arrowRight   REQUIRED. Id of right arrow element
 * @param   fillFn       REQUIRED. Function to call to fill template element with data
 * @param   num          Number of items to scroll at a time
 *                       Default: 3
 * @param   speed        How long the transition animation should take
 *                       Default: 1/2 second
 */
jQuery.fn.SPscroll = function(params) {
	// If any parameters have been omitted, fill with default values
	params = jQuery.extend({num:3, speed:500, data:false, fillFn:function() {}}, params);
	
	// Setup each passed container to scroll
	return this.each(function() {
		var Obj = {};
		var numItems = 0;
		Obj.params = params;
		Obj.el_container = jQuery(this);
		Obj.el_content   = Obj.el_container.children(':eq(0)');
		Obj.el_template  = Obj.el_content.children(':eq(0)');
		// Calculate the final width of the content layer
		Obj.itemWidth = (Obj.el_template.width() + (parseInt(Obj.el_template.css('margin-left')) || 0) + (parseInt(Obj.el_template.css('margin-right')) || 0))  ||  0;
		// IF data was provided, remove the template and fill content box with data
		if (params.data.length >= 0) {
			numItems = params.data.length;
			Obj.el_template.remove();
			// Create a new item for each item in the data
			jQuery.each(params.data, function(i, n) {
				params.fillFn(Obj.el_template.clone(true), n).appendTo(Obj.el_content);
			});
		}
		// ELSE data is already in the HTML, just count how many elements there are
		else {
			numItems = Obj.el_content.children().size();
		}
		// Resize the content box to hold all of the elements on one line
		Obj.contentWidth = Obj.itemWidth * numItems;
		Obj.el_content.css('width', Obj.contentWidth);
		// Initialize arrows
		jQuery(document.getElementById(params.arrowLeft))
			.css('display','none')
			.click(function() {
				newx = jQuery.SPscrollFn(this.SPscroll, 'left');
				if (newx != "no") {
					// See if we need to hide this arrow
					if (newx >= 0) {
						this.style.display = 'none';
					}
					// Whenever we scroll right, we can always scroll back left
					jQuery(document.getElementById(this.SPscroll.params.arrowRight)).css('display', '');
				}
				return false;
			})
			[0].SPscroll = Obj;
		jQuery(document.getElementById(params.arrowRight))
			.css('display',(numItems > params.num ? '' : 'none')) /* Set to none if no data given */
			.click(function() {
				newx = jQuery.SPscrollFn(this.SPscroll, 'right');
				if (newx != "no") {
					// See if we need to hide this arrow
					if (newx + this.SPscroll.el_content.width() < this.SPscroll.el_container.width()) {
						this.style.display = 'none';
					}
					// Whenever we scroll right, we can always scroll back left
					jQuery(document.getElementById(this.SPscroll.params.arrowLeft)).css('display', '');
				}
				return false;
			})
			[0].SPscroll = Obj;
	});
};

jQuery.SPscrollFn = function(SPscroll, dir) {
	if (SPscroll.isAnimating) return "no";
	SPscroll.isAnimating = true;
	dir = dir == 'left' ? -1 : dir == 'right' ? 1 : dir;
	var newx = parseInt(SPscroll.el_content.css('left')) - SPscroll.itemWidth * SPscroll.params.num * dir;
	SPscroll.el_content.animate({'left':newx}, SPscroll.params.speed, 'easeinout', function() { SPscroll.isAnimating = false; });
	return newx;
}

function SPscroll_ff_logos(clone, data) {
	return clone
		.css('background', '#fff url('+data.logo+') center center no-repeat')
		.attr('title', data.name)
		/*.find('[@name=name]').html(data.name).end()*/
		/*.find('[@name=logo]').attr({'src':data.logo, 'alt':data.name}).end()*/
		.click(function() { window.location = data.url; })
	;
};

function SPscroll_ff_projects(clone, data) {
	var name = data.name;
	if (name.length > 40) name = name.substr(0, 37) + "...";
	return clone
		.find('[@name=name]').html(name).end()
		.find('[@name=img]').css('background', 'url('+data.img+') center center no-repeat').end()
		.click(function() { window.location = data.url; })
	;
};

