/* plugin name: hoverscroll */

(function($) {
//
// plugin definition
//
$.fn.hoverscroll = function(options) {

	// build main options before element iteration
	var opts = $.extend({}, $.fn.hoverscroll.defaults, options);	
	
	// iterate and reformat each matched element
	return this.each(function() {
		
		var container = $(this);
		var content = $(opts.content, container);
		var contentWidth = _elementWidth(content); 	
		
		container.css({overflow: 'hidden'});
		content.css({position: 'absolute', width: contentWidth});
		
		_setup(container, content, contentWidth);
		$(window).bind('resize', function(){
			_setup(container, content, contentWidth);
		});
	});
};

function _setup(container, content, contentWidth) {
	var containerWidth = container.width();
			
	if(contentWidth > containerWidth) {
		container.mousemove(function(e){
			var _left = (e.pageX - container.offset().left) * (containerWidth - contentWidth) / containerWidth;
			content.css({left: _left});
		});
		//uso Jquery UI draggable, se disponibile
		if($().draggable) {
			content.draggable({ axis: "x" });
		}
	} else {
		content.css({left: '0px'});
		container.mousemove(function(){});
	};
}

function _elementWidth(element) {
	var w = 0;
	$(element).children().each(function(){
		w += $(this).outerWidth();
	});
	return w;
}

//
// plugin settings
//

$.fn.hoverscroll.defaults = {
	content: '.hoverscroll-content'
};

})(jQuery);
