/**
 * Scrollbar Controller
 * Created: Friday 19 February 2010 16:43 PM EST
 *
 * Dependencies:
 * jQuery 1.4.1
 * jQuery UI 1.7.2 with 'Draggable' functionality
 */
function Scrollbar($root) {
	
	$root = $root || $('.scrollbar');

	// Abort if there are no elements on which to operate
	if($root.size() == 0) return;

	var self = this;
	this.position = 0;
	this.eventDispatcher = new EventDispatcher;

	// Build structure for scrollbar widget
	$root.append('<div><div><div><span class="scroller"><span><span></span></span></span></div></div></div>');

	var $bounds = $root.find('div>div>div');
	var $scroller = $root.find('.scroller');

	// Initialize jQuery Draggable component
	$scroller.draggable({
		axis: 'x',
		containment: 'parent',
		drag: drag
	});

	this.setWidth = function(percent) {
		$scroller.width($bounds.width() * percent);
	};
	
	this.scrollTo = function(percent, duration) {
		duration = duration || 350;
		
		$scroller.animate({
			left: ( percent * ($bounds.width() - $scroller.width()) )
		}, {
			queue: false,
			duration: duration
		});
	};

	function drag(e, ui) {
		self.position = getPosition(ui);
		self.eventDispatcher.dispatchEvent('drag');
	}

	/**
	 * Returns the position of the scrollbar as a float between 0 and 1.
	 * Argument is the UI object returned from a jQuery UI draggable event.
	 */
	function getPosition(ui) {
		return ui.position.left / ( $bounds.width() - $scroller.width() );
	}
}


/**
 * Album Scrolling Controller
 * Created: Friday 19 February 2010 16:43 PM EST
 *
 * Dependencies:
 * jQuery 1.4.1
 */
function AlbumScroller() {
	
	// Abort if there are no elements on which to operate (i.e. we're not
	// on the albums page).
	if($('#albums').size() <= 0) return;

	var self = this;
	var scrollbar = new Scrollbar;
	var $content = $('#albums>.thumbs'); // content to be scrolled
	var $container = $content.parent(); // container holding content

	// Resize content to the actual width of the contents (which should be
	// longer than the container)
	(function() {
		var albumsWidth = 0;

		$content.find('img').each(function() {
			albumsWidth += $(this).width() + 7;
		});

		$content.width(albumsWidth + 61);
	})();
	
	function setScrollbarSize() {
		scrollbar.setWidth( $container.width() / $content.width() );
	};
	
	/**
	 * Reposition album container element per a given percent. Percent
	 * should be a float between 0 and 1.
	 */
	function setPosition(percent) {
		var overflow = $content.width() - $container.width();
		$content.css('left', -(overflow * percent));
	}
	
	// Listen for scrollbar dragging and scroll content accordingly
	scrollbar.eventDispatcher.addListener('drag', function() {
		setPosition(scrollbar.position);
	});
	
	setScrollbarSize();
	$(window).resize(setScrollbarSize);
}


/**
 * EventDispatcher
 * Created: Friday 19 February 2010 16:43 PM EST
 *
 * Dependencies: None
 */
function EventDispatcher() {

	var events = [];

	this.addListener = function(event, callback) {
		if(!events[event]) {
			events[event] = [];
		}
		events[event].push(callback);
	};

	this.dispatchEvent = function(event) {
		if(events[event]) {
			for(var e in events[event]) events[event][e]();
		}
	};
}
