/******************************************************************************************************************/
// COVERFLOW HANDLE FUNCTIONS
/******************************************************************************************************************/

// setting up
var current = 0;
var cfmax = 10;
var ml = 0;
			
function updateCovers(current){
	$('#coverflow .item').removeAttr('id');
	$('#coverflow .item').removeClass('zoom1');
	$('#coverflow .item').removeClass('zoom2');
	$('#coverflow .item').removeClass('zoom3');
	$('#coverflow .item').removeClass('zoom4');
	$('#coverflow .item:eq('+ current +')').attr('id','current');
	$('#coverflow .item:eq('+ (current + 1) +'), #coverflow .item:eq('+ (current - 1) +')').addClass('zoom1');
	$('#coverflow .item:eq('+ (current + 2) +'), #coverflow .item:eq('+ (current - 2) +')').addClass('zoom2');
	$('#coverflow .item:eq('+ (current + 3) +'), #coverflow .item:eq('+ (current - 3) +')').addClass('zoom3');
	$('#coverflow .item:eq('+ (current + 4) +'), #coverflow .item:eq('+ (current - 4) +')').addClass('zoom4');
	$('#coverflow .item img').css({ opacity: 0.2 });
	$('#coverflow .item:eq('+ current +') img').css({ opacity: 1 });
	
	if (current >= 5) ml = 200 - (40 * current);	
	else {
		// questa potrebbe essere migliorata
		mm = new Array(262, 202, 142, 82, 42);
		ml = mm[current];
	}
	
	$('#coverflow .viewport ul').stop().animate({ marginLeft: ml }, 100);
	
	$('#coverflow .nav li').removeClass('sel');
	$('#coverflow .nav li:eq('+ current +')').addClass('sel');
}

function loopCovers(){
	if (current >= cfmax) current = 0;
	else current++;
	
	updateCovers(current);
	
	CoverTimer = window.setTimeout('loopCovers()', 5000);
}

function handleCoveflow(){
	// load ajax coverflow
	$('#coverflow .viewport').html('<div class="loading">loading...</div>');
	
	mylink = "/wp-content/themes/filtersage/ajax/ajax-coverflow.php";
	if ($('#coverflow').hasClass('movies')) mylink = "/wp-content/themes/filtersage/ajax/ajax-coverflow.php?filter=movies";
	if ($('#coverflow').hasClass('music')) mylink = "/wp-content/themes/filtersage/ajax/ajax-coverflow.php?filter=music";
	
	jQuery.ajax({
		url: mylink,
		success: function(html){
			$('#coverflow .viewport').html(html);		
			
			// items handler
			handleItems();
			
			// start loop
			current = -1;
			loopCovers();
			
			// over coverflow
			$('#coverflow').hover(function(){
				clearTimeout(CoverTimer);
			}, function(){
				clearTimeout(CoverTimer);
				if (!openb) window.setTimeout('loopCovers()', 5000);
			});
			
			// over nav
			$('#coverflow .nav li').hover(function(){
				$('.balloon').remove();
				clearTimeout(CoverTimer);
				current = $('#coverflow .nav li').index(this);;
				updateCovers(current);
			}, function(){
				// nothing to do
			});
			
			// item click
			$('#coverflow .item').click(function(){		
				$('.balloon').remove();
				if (!($(this).attr('id') == 'current')) {
					current = $('#coverflow .item').index(this);
					updateCovers(current);					
					return false;
				}
			});
		}
	});
}

/******************************************************************************************************************/
// On document load...
/******************************************************************************************************************/

$(function(){
	// coverflow
	handleCoveflow();
});