/*
	#################################################
	#    Auto: Thiago Silva (Pedreiro Digital)      #
	#         www.pedreirodigitalweb.com.br         #       
	#################################################
*/

var request = 'http://gdata.youtube.com/feeds/api/videos?alt=json';
var limite;
var usuario = 'fdbOficial';
var imagem_video = new Image();
var thumbnail = new Image();
var thumbs = new Array();
var imagens_videos = new Array();
var titulos_videos = new Array();
var urls_videos = new Array();
var idVideo=0;

var videos = function()
{
}

videos.prototype.slide = function(limite)
{
	$.ajax
	(
		{
			type: "GET",
			url: request + '&max-results=' + limite + '&author=' + usuario,
			dataType: 'jsonp',
			success: videos.prototype.carregado,
			error: videos.prototype.erro
		}
	);
}

videos.prototype.carregado = function (result)
{
	var feed = result.feed;						
	var rfeed = feed.entry || [];
	
	if(rfeed.length > 0)
	{
		$(rfeed).each
		(
			function(i) 
			{
				imagens_videos.push(rfeed[i].media$group.media$thumbnail[1].url);
				titulos_videos.push(rfeed[i].title.$t);
				var id_video = rfeed[i].link[0].href.substring(rfeed[i].link[0].href.indexOf('=')+1,rfeed[i].link[0].href.length)
				var explode = id_video.split('&');
				urls_videos.push(explode[0]);
			}
		);
	}
	else
	{
		$('.albuns_video').empty();
		$('.albuns_video').html('erro na conexão com o youtube');
	}
	
	
	videos.prototype.load_foto();
	
	if(rfeed.length != 1)
	{
		$('.ant_video').css({opacity:0.5});
		$('.ant_video').attr('disabled', true);
	}
	else
	{
		$('.ant_video').css({opacity:0.5});
		$('.ant_video').attr('disabled', true);
		$('.pro_video').css({opacity:0.5});
		$('.pro_video').attr('disabled', true);
	}
	
	
	$('.foto_video').click
	(
		function()
		{
			videos.prototype.ver_video(urls_videos[idVideo]);
		}
	);
}

videos.prototype.load_foto = function()
{
	imagem_video.src = imagens_videos[idVideo];
	imagem_video.onload = videos.prototype.exibir_foto;
}

videos.prototype.exibir_foto = function()
{
	$('.legenda_video').html(titulos_videos[idVideo]);
	$('.preloader_video').animate({opacity:0});
	$('.foto_video').css({background:'url('+imagem_video.src+')'});	
}

videos.prototype.passarVideo = function(direcao)
{
	switch(direcao)
	{
		case 'ant_video':
			idVideo--;
			if(idVideo <= 0)
			{
				$('.ant_video').css({opacity:0.5});
				$('.ant_video').attr('disabled', true);
			}
			$('.pro_video').css({opacity:1});
			$('.pro_video').attr('disabled', false);
		break;
		case 'pro_video':
			idVideo++;
			if(idVideo >= (imagens_videos.length-1))
			{
				$('.pro_video').css({opacity:0.5});
				$('.pro_video').attr('disabled', true);
			}
			$('.ant_video').css({opacity:1});
			$('.ant_video').attr('disabled', false);
		break;
	}
	$('.preloader_video').animate({opacity:1},videos.prototype.load_foto);
}

videos.prototype.erro = function (result)
{
	alert('erro ao carregar o youtube');
}

videos.prototype.galeria = function()
{
	$.ajax
	(
		{
			type: "GET",
			url: request + '&author=' + usuario,
			dataType: 'jsonp',
			success: videos.prototype.galeria_carregado,
			error: videos.prototype.erro
		}
	);
}

videos.prototype.galeria_carregado = function (result)
{
	var feed = result.feed;						
	var rfeed = feed.entry || [];
	var thumbs = '<ul>';
	if(rfeed.length > 0)
	{
		$(rfeed).each
		(
			function(i) 
			{
				/*imagens_videos.push(rfeed[i].media$group.media$thumbnail[0].url);
				titulos_videos.push(rfeed[i].media$group.media$description.$t);*/
				var id_video = rfeed[i].link[0].href.substring(rfeed[i].link[0].href.indexOf('=')+1,rfeed[i].link[0].href.length)
				var explode = id_video.split('&');
				var texto = rfeed[i].title.$t;		
				
				
				thumbs += '<li>';
				thumbs += '<img src="'+rfeed[i].media$group.media$thumbnail[0].url+'" class="thumb_foto" onclick="videos.prototype.ver_video(\''+explode[0]+'\')" />';				
				thumbs += '<em>'+texto+'</em>';
				thumbs += '</li>';
			}
		);
		thumbs += '</ul>';
		$('#videos').html(thumbs);
	}
	else
	{
		alert('este canal não foi encontrado');
	}
}

videos.prototype.ver_video = function (url)
{
	window.location = host + 'multimedia/video/'+url;
}

videos.prototype.loader_thumb = function()
{
	var a = 0;
	$('.thumb_foto').each
	(
		function()
		{
			thumbs[a] = $(this);	
			$(fotos[a]).hide();
			a++;
		}
	);
	
	for(i=0; i<thumbs.length; i++)
	{
		 thumbnail.src=thumbs[i].attr('src');
	}
	
	thumbnail.onLoad = videos.prototype.thumb_completo();
}

videos.prototype.thumb_completo = function()
{
	for(i=0; i<thumbs.length; i++)
	{
		$(thumbs[i]).fadeIn();
	}
}
