/**
 * SPscroll jQuery plugin
 *
 * SPscroll enables horizontal scrolling of content inside of a container
 * It takes the following parameters:
 * @param   data         REQUIRED. Data to populate scroller with
 * @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
 * @param	onScroll	 Function to call whenever the scroller is scrolled
 */
jQuery.fn.SPscroll = function(params) {
	// If any parameters have been omitted, fill with default values
	params = jQuery.extend({num:3, speed:500, onScroll:function() {}, content:false}, params);
	
	// Setup each passed container to scroll
	return this.each(function() {
		var Obj = {};
		Obj.params = params;
		Obj.el_content   = jQuery(this);
		Obj.el_container = Obj.el_content.parent();
		Obj.el_template  = jQuery(Obj.el_content.children()[0]);
		Obj.numItems = params.data.length;
		Obj.currentItemIndex = 0;
		// Calculate the final width of the content layer, then remove template from document
		Obj.itemWidth = parseInt(Obj.el_template.css('width')) + (parseInt(Obj.el_template.css('margin-left')) || 0) + (parseInt(Obj.el_template.css('margin-right')) || 0);
		Obj.contentWidth = Obj.itemWidth * params.data.length;
		Obj.el_content.css('width', Obj.contentWidth);
		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);
		});
		// Initialize arrows
		Obj.el_arrowLeft = params.arrowLeft
			.css('display','none')
			.click(function() { jQuery.SPautoScrollFn(this.SPscroll, 'left'); return false; });
		Obj.el_arrowLeft[0].SPscroll = Obj;
			
		Obj.el_arrowRight = params.arrowRight
			.css('display',(params.data.length ? '' : 'none')) /* Set to none if no data given */
			.click(function() { jQuery.SPautoScrollFn(this.SPscroll, 'right'); return false; });
		Obj.el_arrowRight[0].SPscroll = Obj;
			
		Obj.params.onScroll(Obj.el_content.children()[0]);
		this.SPscroll = Obj;
	});
};


jQuery.SPautoScrollFn = function(SPscroll, dir) {
	newx = jQuery.SPscrollFn(SPscroll, dir);
	var switchdir = false;

	var thisArrow = dir == 'left' ? SPscroll.el_arrowLeft : SPscroll.el_arrowRight;
	var otherArrow = dir == 'left' ? SPscroll.el_arrowRight : SPscroll.el_arrowLeft;
	
	if (newx != "no") {
		if (dir == 'left') {
			// See if we need to hide this arrow
			if (SPscroll.currentItemIndex == 0) {
				switchdir = true;
			}
		}
		else if (dir == 'right') {
			// See if we need to hide this arrow
			if (SPscroll.currentItemIndex + SPscroll.params.num == SPscroll.numItems) {
				switchdir = true;
			}
		}
		
		otherArrow.css('display', '');
		if (switchdir) {
			thisArrow.css('display', 'none');
		}
	}
	return switchdir;
}

jQuery.SPscrollFn = function(SPscroll, dir) {
	if (SPscroll.isAnimating) return "no";
	SPscroll.isAnimating = true;
	dir = dir == 'left' ? -1 : dir == 'right' ? 1 : dir;
	SPscroll.currentItemIndex += SPscroll.params.num * dir;
	var newx = parseInt(SPscroll.el_content.css('left')) - (SPscroll.itemWidth * SPscroll.params.num * dir);
	
	SPscroll.params.onScroll(SPscroll.el_content.children()[SPscroll.currentItemIndex]);
	
	SPscroll.el_content.animate({'left':newx}, SPscroll.params.speed, 'easeinout', function() { SPscroll.isAnimating = false; });
	$(window).trigger('SPscroll_onMove');
	return newx;
}

jQuery.SPscrollCheck = function(SPscroll) {
	if (SPscroll.isAnimating) return false;
	var x = parseInt(SPscroll.el_content.css('left'));
//	SPscroll.
}

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 = '/brands/projects/project_summary.php?PROJ_ID='+data.id; })
	;
};
