tvMarsans = {
	urlBusca : 'pub/json/getVideos.php'
	
	,page : 1
	,requests	: null
	
	,addRequest : function(req)
	{
		this.request = req;
	}
	
	,cancelRequest : function()
	{
		if (this.request) this.request.abort();
		this.request = null;
	}
	
	,getDestaque : function()
	{
		this.cancelRequest();
		
		$('div.videos .resultadoBusca ul').empty();
		$('div.videos .resultadoBusca').hide();
		$('div.videos .destaque').show();
		$('div.videos .destaque ul').empty();
		$('.totProdutos').empty().hide();
		$('.paginas').empty();
		$("<span>Carregando...</span>").appendTo('.destaque ul');
		
		tipos = ['ProdutoNacional', 'ProdutoInternacional', 'Navio'];
		
		$.each(tipos, function(i, tipo) {
			tvMarsans.addRequest(
				$.get(WEB_ROOT + tvMarsans.urlBusca,
					{
						"tipo"		: tipo,
						"limit"		: 4
					}, 
					function(data) {
						$('.destaque ul span').remove();
						if (!data.error) {
							if (data.videos.length == 0) {
								$("<span>Nenhum vídeo encontrado.</span>").appendTo('.resultadoBusca ul');
							} else {
								tvMarsans.mostraVideos(data, tipo);
							}
						}
					},'json'
				)
			);
		});
	}
	
	,getBusca : function(resetPage)
	{
		this.cancelRequest();
		
		$('div.videos .destaque ul').empty();
		$('div.videos .destaque').hide();
		$('div.videos .resultadoBusca').show();
		$('div.videos .resultadoBusca ul').empty();
		$('.totProdutos').empty().show();
		$('.paginas').empty();
		$("<span>Carregando...</span>").appendTo('.resultadoBusca ul');
		
		search = $('.buscaVideo').val();
		categoria = $('.filtro select').val();
		this.page = resetPage ? 1 : this.page;
		
		this.addRequest($.get(
			WEB_ROOT + this.urlBusca,
			{
				"page"			: this.page,
				"categoria"		: categoria,
				"search"		: search
			}, 
			function(data) {
				$('.resultadoBusca ul span').remove();
				if (!data.error) {
					if (data.videos.length == 0) {
						$("<span>Nenhum vídeo encontrado.</span>").appendTo('.resultadoBusca ul');
					} else {
						tvMarsans.mostraVideos(data, 'resultadoBusca');
					}
				}
			},'json'
		));
	}
	
	,mostraVideos : function(data, tipo)
	{
		page = parseInt(data.page);
		totPages = parseInt(data.totPages);
		totVideos = parseInt(data.totVideos);
		
		$('.totProdutos').text(data.totVideos+' vídeo(s)');
		
		if (page > 1) {
			$("<a>&laquo;Anterior&nbsp;|&nbsp;</a>")
				.attr('href', '#')
				.attr('onclick', 'return false;')
				.addClass('btAnterior')
				.click(function()
				{
					tvMarsans.page = page-1;
					tvMarsans.getBusca();
					
					$(this).remove();
//					return false;
				})
				.appendTo('.paginas');
		}
		
		if (totPages > 1) {
			$("<span> página "+page+" de "+totPages+" </span>").appendTo('.paginas');
		}
		
		if (page < totPages) {
			$("<a>&nbsp;|&nbsp;Proximo&raquo;</a>")
				.attr('href', '#')
				.attr('onclick', 'return false;')
				.addClass('btProximo')
				.click(function()	
				{
					tvMarsans.page = page+1;
					tvMarsans.getBusca();
					
					$(this).remove();
//					return false;
				})
				.appendTo('.paginas');
		}
		
		$.each(data.videos, function(i, item) {
			$('.videos .'+tipo+' ul').append(
				'<li>'
				+'	<div>'
				+'	<h4>'+item.titulo+'</h4>'
				+'	<p>'+item.legenda+'</p>'
				+'	</div>'
				+'	'
				+'	<a href="'+item.url+'" target="_blank" class="youtube">'
				+'		<img src="'+item.thumbnail+'">'
				+'	</a>' 
				+'</li>'
			)
		})
		
		tvMarsans.preparePopup();
	}
	
	,preparePopup : function()
	{
		$('a.youtube').click(function(e){
			e.preventDefault();
			
			url = $(this).attr('href');
	        videoId = url.replace(new RegExp("http://www.youtube.com/watch\\?v=", "i"), '');
			// a URL abaixo não funciona no IE
	        //videoUrl = 'http://www.youtube.com/v/'+videoId+'&hl=pt_BR&fs=1&rel=1&color1=0x5d1719&color2=0xcd311b&border=1';
			videoUrl = 'http://www.youtube.com/watch_popup?v='+videoId+'&hl=pt_BR&fs=1&rel=1&color1=0x5d1719&color2=0xcd311b&border=1';

			flashWidth	= 640;
			flashHeight	= 385;
			
			winPopup = popUp(videoUrl, 'youtubeVideo', 'toolbars=yes,status=yes,resize=no,scrollbars=yes,width='+flashWidth+',height='+flashHeight+',top=30,left=100');
			winPopup.opener = null;
			winPopup.focus();
		});
	}
};