var coverflow = {
	itemWidth : 156,
	totalWidth : 0,
	coverflowSelector : '.coverflow',
	stageSelector : '.coverflow .stage',
	imageSelector : '.coverflow .stage ul img',
	bubbleSelector : '.coverflow-info-bubble',
	bubbleEmpty : '<div class="coverflow-info-bubble" style="display:none"></div>',
	init : function() {
		coverflow.preloadImages();
		coverflow.positionImages();
		jQuery('body').append(coverflow.bubbleEmpty);
		coverflow.initBubbles();
		coverflow.initClones();
		coverflow.initControls();
		jQuery(coverflow.coverflowSelector).removeClass('hidden');
	},
	preloadImages : function() {
		jQuery(coverflow.imageSelector).each(function() {
			var preload = new Image();
			preload.src = jQuery(this).attr('src');
		});
	},
	positionImages : function() {
		var height = Math.round(jQuery(coverflow.stageSelector).height() / 2);
		var width = Math.round(jQuery(coverflow.stageSelector).width() / 3 / 2);
		jQuery(coverflow.imageSelector).each(function() {
			var imageHeight = Math.round(jQuery(this).height() / 2);
			var imageWidth = Math.round(jQuery(this).width() / 2);
			jQuery(this).css({
				'top' : height - imageHeight,
				'left' : width - imageWidth
			});
		});
	},
	initBubbles : function() {
		jQuery(coverflow.imageSelector).bind('mouseover', function(e) {
			var html = jQuery(this).parent().parent().find('.info').clone();
			jQuery(coverflow.bubbleSelector).css({
				'top' : e.pageY - 22,
				'left' : e.pageX + 16
			});
			jQuery(coverflow.bubbleSelector).html(html).fadeIn('fast');
		});
		jQuery(coverflow.imageSelector).bind('mousemove', function(e) {
			jQuery(coverflow.bubbleSelector).css({
				'top' : e.pageY - 22,
				'left' : e.pageX + 16
			});
		});
		jQuery(coverflow.imageSelector).bind('mouseout', function() {
			jQuery(coverflow.bubbleSelector).fadeOut('fast');
		});
	},
	initClones : function() {
		var items = jQuery(coverflow.stageSelector).children('ul').children('li');
		items.filter(':first').before(items.slice(-3).clone(true).addClass('cloned'));
		items.filter(':last').after(items.slice(0, 3).clone(true).addClass('cloned'));
		var newWidth = jQuery(coverflow.stageSelector).children('ul').children('li').length * coverflow.itemWidth,
			newMarginLeft = -3 * coverflow.itemWidth;
		coverflow.totalWidth = newWidth;
		jQuery(coverflow.stageSelector).children('ul').css({
			'width' : newWidth + 'px',
			'margin-left' : newMarginLeft + 'px'
		});
	},
	initControls : function() {
		jQuery(coverflow.coverflowSelector + ' a.next').bind('click', function() {
			var newMarginLeft = parseInt(jQuery(coverflow.stageSelector + ' ul').css('margin-left')) - coverflow.itemWidth;
			jQuery(coverflow.stageSelector + ' ul:not(:animated)').animate({ marginLeft : newMarginLeft + 'px' }, 'fast');
			if (newMarginLeft == (-1 * (coverflow.totalWidth - 3 * coverflow.itemWidth))) {
				jQuery(coverflow.stageSelector + ' ul').queue(function() {
					jQuery(this).css({
						'margin-left' : -1 * (3 * coverflow.itemWidth) + 'px'
					});
					jQuery(this).dequeue();
				});
			}
			return false;
		});
		jQuery(coverflow.coverflowSelector + ' a.prev').bind('click', function() {
			var newMarginLeft = parseInt(jQuery(coverflow.stageSelector + ' ul').css('margin-left')) + coverflow.itemWidth;
			jQuery(coverflow.stageSelector + ' ul:not(:animated)').animate({ marginLeft : newMarginLeft + 'px' }, 'fast');
			if (newMarginLeft == 0) {
				jQuery(coverflow.stageSelector + ' ul').queue(function() {
					jQuery(this).css({
						'margin-left' : -1 * (coverflow.totalWidth - 6 * coverflow.itemWidth) + 'px'
					});
					jQuery(this).dequeue();
				});
			}
			return false;
		});
	}
};
jQuery(document).ready(function() {
	coverflow.init();
});
