$(document).ready(function(){
	var n_filhos = $("#flip li").length;
	var curr = Math.round(n_filhos / 2) - 1;
	var id_video_inicial = $('#flip li').eq(curr).children().attr("src").split('/');
	
	//Customização do player.
	$('#player').youTubeEmbed("http://www.youtube.com/watch?v=" + id_video_inicial[4]);
	
	$("#player").mouseenter(function() {
		$(".content_comandos").stop().animate({'bottom': 0}, 300);
	}).mouseleave(function() {
		$(".content_comandos").stop().animate({'bottom': -22}, 300);
	});
	
	//Add a class ativo no li do thumb atual
	$("#flip li:eq(" + curr + ")").addClass("ativo");
	
	//Configuração do coverflow	
	jQuery( '#flip' ).jcoverflip({
		current: curr,
		beforeCss: function( el, container, offset ){
		return [
		  $.jcoverflip.animationElement( el, { left: ( container.width( )/2 - 130 - 75*offset)+'px', bottom: '40px' }, { } ),
		  $.jcoverflip.animationElement( el.find( 'img' ), { opacity: 0.5, width: Math.max(10,75-10*offset*offset) + 'px' }, {} )
		];
		},
		afterCss: function( el, container, offset ){
		return [
		  $.jcoverflip.animationElement( el, { left: ( container.width( )/2 + 54 + 85*offset)+'px', bottom: '40px' }, { } ),
		  $.jcoverflip.animationElement( el.find( 'img' ), { opacity: 0.5, width: Math.max(10,75-10*offset*offset) + 'px' }, {} )
		];
		},
		currentCss: function( el, container ){
		return [
		  $.jcoverflip.animationElement( el, { left: ( container.width( )/2 - 45 )+'px', bottom: '40px' }, { } ),
		  $.jcoverflip.animationElement( el.find( 'img' ), { opacity: 1, width: '90px' }, { } )
		];
		},           
		change: function(event, ui){             
		jQuery('#scrollbar').slider('value', ui.to*25);           }         });                           
		jQuery('#scrollbar').slider({           
		value: 50,           
		stop: function(event, ui) {             
			if(event.originalEvent) {               
				var newVal = Math.round(ui.value/25);               
				jQuery( '#flip' ).jcoverflip( 'current', newVal );               
				jQuery('#scrollbar').slider('value', newVal*25);             
			}           
		}         
	});
	
	//Descrição
	pTit = $("#flip li:eq(" + curr + ") img").attr('alt');
	$(".thumb_title").append("<span class=\"bg_title_left\" /><span class=\"txt_title\">" + pTit + "</span><span class=\"bg_title_right\" />");
	$("#wrapper").append("<span class=\"seta_canal\" />");
	var width_descri = $(".thumb_title").width();
	var centra = - width_descri / 2;
	$(".thumb_title").css({'margin-left': centra});
	
	//Troca o video quando clica no thumb.
	$("#flip li img").click(function() {
		pTit = $(this).attr("alt");
		pSrc = $(this).attr("src").split('/');
		
		$(".mask_canal").hide();
		
		setTimeout(function() {
			trocaVideo(pSrc[4]);
		}, 100);
		setTimeout(function() {
			$(".mask_canal").fadeIn();	
		}, 2000);
		
		$("#flip li").removeClass("ativo");
		$(this).parent().addClass("ativo");
		
		animaDescri(pTit);
	});
	
	//Botoes de avançar e voltar
	$('.next').click(function(e){
		e.preventDefault();

		if($("#flip li.ativo").next().html() == null){
			pNextTit = $("#flip li:first-child").children().attr("alt");
			pNextSrc = $("#flip li:first-child").children().attr("src").split('/');
			
			trocaVideo(pNextSrc[4]);
			$("#flip li").removeClass("ativo");
			$("#flip li:first-child").addClass("ativo");
		}else{
			pNextTit = $("#flip li.ativo").next().children().attr("alt");
			pNextSrc = $("#flip li.ativo").next().children().attr("src").split('/');
			
			trocaVideo(pNextSrc[4]);
			$("#flip li.ativo").removeClass("ativo").next().addClass("ativo");
		}

		animaDescri(pNextTit);
		
		$('#flip').jcoverflip('next', '1', true);
	});
	$('.prev').click(function(e){
		e.preventDefault();

		if($("#flip li.ativo").prev().html() == null){
			pPrevTit = $("#flip li:last-child").children().attr("alt");
			pPrevSrc = $("#flip li:last-child").children().attr("src").split('/');
			
			trocaVideo(pPrevSrc[4]);
			$("#flip li").removeClass("ativo");
			$("#flip li:last-child").addClass("ativo");
		}else{
			pPrevTit = $("#flip li.ativo").prev().children().attr("alt");
			pPrevSrc = $("#flip li.ativo").prev().children().attr("src").split('/');
			
			trocaVideo(pPrevSrc[4]);
			$("#flip li.ativo").removeClass("ativo").prev().addClass("ativo");
		}

		animaDescri(pPrevTit);
		
		$('#flip').jcoverflip('previous', '1', true);
	});

});


//função de animação da descrição
function animaDescri(descricao) {
	
	$(".seta_canal").stop().animate({'bottom': -7, 'opacity': 0}, 300);
	
	setTimeout(function(){
		$(".thumb_title").stop().animate({'bottom': -5, 'opacity': 0}, 300);
	},200);
	setTimeout(function(){
		$(".thumb_title .txt_title").text(descricao);
		
		var width_descri = $(".thumb_title").width();
		var centra = - width_descri / 2;
		$(".thumb_title").css({'margin-left': centra});
		
		$(".thumb_title").stop().animate({'bottom': 5, 'opacity': 1}, 300);
	}, 600);
	setTimeout(function(){
		$(".seta_canal").stop().animate({'bottom': 4, 'opacity': 1});
	}, 800);	
}

//Função de trocar os videos
function trocaVideo(id) {
	/*$(".flashContainer").css("display", "none");
	$(".flashContainer:first-child").css("display", "block");*/
	
	$(".flashContainer:last-child").remove();
	$('#player').youTubeEmbed("http://www.youtube.com/watch?v=" + id);	
	//$(".flashContainer").append("<object data=\"http://www.youtube.com/apiplayer?enablejsapi=1&amp;version=3\" type=\"application/x-shockwave-flash\" id=\"video_" + id + "\" width=\"470\" height=\"310\"><param name=\"allowScriptAccess\" value=\"always\"><param name=\"wmode\" value=\"transparent\"><param name=\"flashvars\" value=\"video_id=" + id + "&amp;playerapiid=" + id + "\"><param name=\"movie\" value=\"http://www.youtube.com/apiplayer?enablejsapi=1&amp;version=3\"></object>");
}
