$(function() {
	// Carica la pagina della squadra al variare della select
	$('#menu select').change(function () { window.location = '/squadre/' + $('#menu select option:selected').val() + '/'; } );
	// Inserisce il test di fine avvio
	$('#boh').html(unescape(playlist));
});

// Player
var player;
var curVideo = 0;

function playerReady(obj) {
	var id = obj['id'];
	var version = obj['version'];
	var client = obj['client'];
	player = document.getElementById(id);
	//player.addControllerListener("ITEM","onNextVideo");
	player.addModelListener("ERROR","onVideoError");
	//player.addModelListener("STATE","onChangeState");
};

function onChangeState(obj) {
	if (obj['newstate'] == 'COMPLETED') {
		var nextVideo = 'video_' + obj['index'];
		window.location = $('#lista_video .video:first a').attr('href');
	}
}

function onNextVideo(obj) {
	$('#' + curVideo + ' h2 a').css({ 'color' : '#000' });
	var nextVideo = 'video_' + obj['index'];
	// Se non è il primo filmato, visualizza i controlli video e inserisce gli onclick sul palinsesto
	if (obj['index'] > -1) { 
		$('#player_wrapper').animate( { height: '380px' }, 'normal' );
//			$('.video h2').each(function(i) {
//				this.html('<a href="#" onclick="play(\'' + i + '\'); return false;">' + this.text() + '</a>');
//			});
	}
	$('#' + nextVideo + ' h2 a').css({ 'color' : '#F00' });
	$('#titolo').text($('#' + nextVideo + ' h2 a').text());
	$('#lista_video').scrollTo('#' + nextVideo, 800, { axis : 'y' });
	curVideo = nextVideo;
}

function onVideoError(obj) {
	// Se l'errore è un video not found, carica un video alternativo
	if (obj['message'].substr(0, 15) == 'Video not found') {
		var newUrl = 'http://www.youtube.com/watch?v=' + obj['message'].substr(34, 11);
		player.sendEvent('LOAD', newUrl);
	}
}

function play(id) {
	player.sendEvent('ITEM', id);
}
