function ScrollableRollover(options){

	var defaults = {
		// Whether jscrollpane is used for the item description
		use_jscrollpane: true,
		// How long a user needs to hover over an item before the rollover appears (in seconds)
		hover_delay: 0.5
	}
	options = $.extend(defaults, options);
	
	this.container_id = options['container_id'];
	this.use_jscrollpane = options['use_jscrollpane'];
	this.hover_delay = options['hover_delay'];
	this.rollover_id = this.container_id+'_rollover';
	this.rollover_div_has_focus = false;
	this.item_div_has_focus = false;
	this.hover_timeout = null;
	this.focused_item_id = null;
	
	$('body').append('<div id="'+this.rollover_id+'" class="scrollable_rollover carousel_widget_rollover"></div>');
	this.rollover_div = $('#'+this.rollover_id);
	this.container_div = $('#'+this.container_id);
	
	var self = this;
	
	this.init = function(){
	
		var self = this;
		
		var items = this.container_div.find('.carousel_widget_item_image');
		
		items.mouseenter(function(){
			var item_id = $(this).parents('.carousel_widget_item:first').attr('id');
			self.focused_item_id = item_id;
			self.item_div_has_focus = true;
			self.hover_timeout = setTimeout(show_rollover_after_delay, self.hover_delay * 1000);
		});

		items.mouseleave(function(){
			self.item_div_has_focus = false;
			setTimeout(hide_rollover_if_not_in_focus, 50);
			clearInterval(self.hover_timeout);
		});
		
		this.rollover_div.mouseenter(function(){
			self.rollover_div_has_focus = true;
		});
		
		this.rollover_div.mouseleave(function(){
			self.rollover_div_has_focus = false;
			setTimeout(hide_rollover_if_not_in_focus, 50);
		});
		
		this.rollover_div.find('.rollover_close_button').live('click', function(){
			self.rollover_div.hide();
			return false;
		});

	}
	
	this.show_rollover = function(){
		
		var item = self.container_div.find('#'+self.focused_item_id).find('.carousel_widget_item_image');
		var rollover_content_div = item.siblings('.carousel_widget_rollover_content:first');
		var content = rollover_content_div.html();
		var pos = item.offset();
		var width = item.width();
		var height = item.height();
		
		// Add any classes from the first child of the rollover_content_div to the rollover_div for use in CSS
		var rollover_classes = rollover_content_div.attr('class').split(/\s+/);
		$.each(rollover_classes, function(index, class_name){
			if(class_name != 'carousel_widget_rollover_content'){
				self.rollover_div.removeClass(class_name).addClass(class_name);
			}
		});
		
		self.rollover_div.html(content);
		
		var window_width = $(window).width();
		var rollover_width = self.rollover_div.width();
		var rollover_height = self.rollover_div.height();
		
		var rollover_offscreen_to_right = (pos.left + width + rollover_width) > window_width;
		var more_space_on_left = pos.left + (width / 2) > (window_width / 2);
		if(rollover_offscreen_to_right && more_space_on_left){
			var left = pos.left - rollover_width;
			self.rollover_div.removeClass('carousel_widget_rollover_on_right').addClass('carousel_widget_rollover_on_left');
		}else{
			var left = pos.left + width;
			self.rollover_div.removeClass('carousel_widget_rollover_on_left').addClass('carousel_widget_rollover_on_right');
		}
		
		var top = pos.top + (height / 2) - (rollover_height / 2);
		top = Math.max(top, 0);
		
		self.rollover_div.css({left: left+'px', top: top+'px'}).show();
			
		if(self.use_jscrollpane){
		
			var description_div = self.rollover_div.find('.rollover_description');
		
			description_div.jScrollPane({
				showArrows: true
			});
			
			// If a scroll bar has been added, move the scroll arrows to be above and below the description
			if(self.rollover_div.find('.jspArrowUp').length){
				
				self.rollover_div.find('.jspArrowUp').prependTo(description_div);
			
				var arrow_width = self.rollover_div.find('.jspArrowUp').width();
				self.rollover_div.find('.jspArrowDown').appendTo(description_div).css('position', 'absolute').width(arrow_width);
			
				self.rollover_div.find('.jspVerticalBar').remove();
				
			}
				
			description_div.height(description_div.height() + 10);
		
		}	
	
	}

	function hide_rollover_if_not_in_focus(){
		if(!self.rollover_div_has_focus && !self.item_div_has_focus){
			self.rollover_div.hide();
		}
	}
	
	function show_rollover_after_delay(){
		self.show_rollover();
	}
	
	this.init();
	
}
