function onLoadTVFutsite(){
	globals['video']['descricao']	= $( "#video_descricao" ).text();
	//configura o onmouseover da descrição
	$( "#video_descricao" ).get(0).onmouseover	= function(){
		if ( $( "#video-informacoes" ).find( ".descricao-completa" ).length == 0 ){
			var p	= document.createElement("p");
			p.className	= "descricao-completa";
			p.innerHTML	= globals['video']['descricao'];
			$( "#video-informacoes" ).append( p );
			if ( $( "#video-informacoes" ).find( ".descricao-completa" ).height() > 60 ){
				$( "#video-informacoes" ).find( ".descricao-completa" ).fadeIn( 1000 );
				$( "#video-informacoes" ).find( ".descricao-completa" ).get(0).onmouseout	= function(){
					if ( $( "#video-informacoes" ).find( ".descricao-completa" ).length == 1 ){
						$( "#video-informacoes" ).find( ".descricao-completa" ).fadeOut( 1000, function(){ 
								if ( $( "#video-informacoes" ).get(0) && $( "#video-informacoes" ).find( ".descricao-completa" ).get(0) ){
									$( "#video-informacoes" ).get(0).removeChild( $( "#video-informacoes" ).find( ".descricao-completa" ).get(0) ); 
								}
							} 
						);
					}
				}
			} else {
				$( "#video-informacoes" ).get(0).removeChild( $( "#video-informacoes" ).find( ".descricao-completa" ).get(0) );
			}
		}
	}
	//limita altura
	limitaAltura( $( "#video_descricao" ), 60 );
	limitaAltura( $( "#destaque1" ).find( "p.descricao" ), 30 );
	limitaAltura( $( "#destaque2" ).find( "p.descricao" ), 30 );
	document.getElementById("tela-grande").onclick	= playerAumentar;
	document.getElementById("tela-pequena").onclick	= playerDiminuir;
	//destaque 1
	var obj1	= {};
	obj1.idvideo		= globals['destaque'][1]['id'];
	obj1.titulo		= globals['destaque'][1]['titulo'];
	obj1.data		= globals['destaque'][1]['data'];
	obj1.descricao	= globals['destaque'][1]['descricao'];
	$( "#destaque1" ).find( "a" ).get(0).onclick	= function(){ loadNextObj( null, obj1, "video" ) };
	limitaAltura( $( "#destaque1" ).find( "h4" ), 20 );
	//destaque 2
	var obj2	= {};
	obj2.idvideo		= globals['destaque'][2]['id'];
	obj2.titulo		= globals['destaque'][2]['titulo'];
	obj2.data		= globals['destaque'][2]['data'];
	obj2.descricao	= globals['destaque'][2]['descricao'];
	$( "#destaque2" ).find( "a" ).get(0).onclick	= function(){ loadNextObj( null, obj2, "video" ) };
	limitaAltura( $( "#destaque2" ).find( "h4" ), 20 );
	//enviar amigo
	$( "#enviar-amigos-link" ).get(0).onclick	= function(){ window.open('index.php?modulo=tvfutsite&op=popup.enviar.amigos&video='+globals['video']['id'],'','scrollbars=no, width=505, height=210'); }
	
	//carrega o canal
	loadObjs( document.getElementById("boxCanal"), 'canal', 0, 0, globals['canal']['limite'], globals['canal']['id'], false );
	loadObjs( document.getElementById("boxPrograma"), 'programa', globals['canal']['id'], 0, globals['programa']['limite'], globals['programa']['id'], false );
	loadObjs( document.getElementById("boxVideo"), 'video', globals['programa']['id'], 0, globals['video']['limite'], globals['video']['id'], false );
	return false;
}
// limita a altura do objeto
function limitaAltura( container, altura, n_caracteres ){
	n_caracteres= n_caracteres ? 4 + n_caracteres : 10;
	container	= $( container );
	if ( container.height() > altura ){
		while ( container.height() > altura ){
			var descricao_texto	= container.html();
			descricao_texto	= descricao_texto.replace( /<br>/g, "\n" );
			if ( descricao_texto.substr( descricao_texto.length - 4, 1 ) == "\n" ){
				descricao_texto	= descricao_texto.substr( 0, descricao_texto.length - 4 ) + "...";
			}
			container.html( descricao_texto.substr( 0, descricao_texto.length - n_caracteres ).replace( /\n/g, "<br />" ) + "..." );
		}
	}
	return false;
}
//imagem de carregando
function createLoading( message, width, height ){
	width	= width ? width : 60;
	height	= height ? height : 60;
	// cria o div principal
	var div	= document.createElement("div");
	div.className	= "TVFutsiteLoading";
	// cria a img de carregando.
	var img	= document.createElement("img");
	img.src	= "img/loading.verde.gif";
	img.width	= width;
	img.height	= height;
	div.appendChild( img );
	// cria o texto
	div.appendChild( document.createTextNode( message ) );
	// retorna o objeto;
	return div;
}
//destroi imagem de carregando
function destroyLoading( container ){
	if ( $( container ).find( ".TVFutsiteLoading" ).length > 0 ){
		var objs	= $( container ).find( ".TVFutsiteLoading" ).get();
		for ( var i = 0; i < objs.length; ++i ){
			container.removeChild( objs[i] );
		}
	}
	return false;
}
//cria o player
function createPlayer( container_name, file, width, height, autostart ){
	var s1 = new SWFObject("img/player.swf","mediaplayer",width,height,"7");
		s1.addParam("allowfullscreen","true");
		s1.addVariable("width",width);
		s1.addVariable("height",height);
		s1.addVariable("file",file+".flv");
		s1.addVariable("autostart",autostart);
		s1.write(container_name);
	return false;
}
//aumentar o tamanho do player.
function playerAumentar(){
	$( document.getElementById("container_tvfutsite") ).addClass("grande");
	$( document.getElementById("player") ).addClass("grande");
	$( document.getElementById("video-informacoes") ).addClass("grande");
	$("#expandir-tela").find("p").text("Diminuir tela");
	createPlayer( "container_tvfutsite",
					"../videos/tvfutsite/filme/"+globals['video']['id'],
					600,
					480,
					true
		);
	return false;
}
//diminui o tamanho do player
function playerDiminuir(){
	$( document.getElementById("container_tvfutsite") ).removeClass("grande");
	$( document.getElementById("player") ).removeClass("grande");
	$( document.getElementById("video-informacoes") ).removeClass("grande");
	$("#expandir-tela").find("p").text("Expandir tela");
	createPlayer( "container_tvfutsite",
					"../videos/tvfutsite/filme/"+globals['video']['id'],
					340,
					220,
					true
		);
	return false;
}
//carregas o objeto (canal, programa, video)
function loadObjs( container, tipo, idpai, indice, limite, selecionado, nextObj ){
	//limpa o container
	var videos	= $( container ).find( "div" ).get();
	for ( var i = 0; i < videos.length; ++i ){
		container.removeChild( videos[i] );
	}
	var links	= $( container ).find( "a" ).get();
	for ( var i = 0; i < links.length; ++i ){
		container.removeChild( links[i] );
	}
	//cria o item de carregando
	container.appendChild( createLoading( "", 50, 50 ) );
	//verifica se está em cache
	if ( globals['cache'][tipo][idpai] == undefined ){
		var loader	= new net.ContentLoader(
				"index.php?modulo=tvfutsite&op=load_"+tipo+"&id="+idpai,
				retornoObjs
			);
		loader.container		= container;
		loader.container.tipo	= tipo;
		loader.tipo				= tipo;
		loader.idpai			= parseInt( idpai );
		loader.indice			= indice ? indice : null;
		loader.limite			= parseInt( limite );
		loader.selecionado		= selecionado;
		loader.nextObj			= nextObj;
	} else {
		destroyLoading( container );
		var nSelecionado = 0;
		if ( globals['cache'][tipo][idpai]['lista'].length > 0 ){
			if ( indice == null ){
				for ( var i = 0; i < globals['cache'][tipo][idpai]['lista'].length; ++i ){
					if ( selecionado != null && $( globals['cache'][tipo][idpai]['lista'][i] ).hasClass( "selecionado" ) ){
						indice = parseInt( i / limite );
						break;
					} else if ( selecionado == null && globals['cache'][tipo][idpai]['lista'][i].padrao == 1 ){
						$( globals['cache'][tipo][idpai]['lista'][i] ).addClass( "selecionado" );
						indice = parseInt( i / limite );
						break;
					} else {
						$( globals['cache'][tipo][idpai]['lista'][i] ).removeClass( "selecionado" );
					}
				}
			}
			for ( var i = ( indice * limite ); i < ( indice * limite ) + limite; ++i ){
				var video	= globals['cache'][tipo][idpai]['lista'][i];
				if ( video != undefined ){
					container.appendChild( video );
					if ( $( video ).hasClass( "selecionado" ) && selecionado != video.idvideo ){
						++nSelecionado;
						selectObj( container, video, tipo, idpai, video.idvideo );
						if ( nextObj ){ loadNextObj( container, video, tipo, idpai, video.idvideo, nextObj ); }
					}
				}
			}
			if ( selecionado == null && nSelecionado == 0 ){
				loadNextObj( container, null, tipo, 0, 0, nextObj );
			}
			loadPaginacao( container, tipo, idpai, indice, limite );
		} else {
			//cria mensagem de que não há objetos
			var div	= document.createElement( "div" );
			div.className	= "msg";
			div.appendChild( document.createTextNode( "Não há "+tipo+" adicionado." ) );
			container.appendChild( div );
			//altera o título
			$( "#titulo_"+tipo ).text( "" );
			//carrega o próximo objeto vazio.
			loadNextObj( container, null, tipo, idpai, 0, nextObj );
		}
	}
	return false;
}
function retornoObjs(){
	//pega as variáveis antigas
	var container	= this.container;
	var tipo		= this.tipo;
	var idpai		= this.idpai;
	var limite		= this.limite;
	var selecionado	= this.selecionado;
	var nextObj		= this.nextObj;
	//atribui as variáveis
	var xml				= this.req.responseXML;
	var objs			= xml.getElementsByTagName( tipo );
	var indice			= 0;
	if ( globals['cache'][tipo][idpai] == undefined ){
		globals['cache'][tipo][idpai]			= new Array();
		globals['cache'][tipo][idpai]['lista']	= new Array();
	}
	//destroi imagem de carregando
	destroyLoading( container );
	//percorre os objetos
	if ( objs.length > 0 ){
		var nSelecionado = 0;
		for ( var i = 0; i < objs.length; ++i ){
			var obj	= objs.item(i);
			var id				= parseInt( obj.getElementsByTagName('id').item(0).firstChild.nodeValue );
			var titulo			= obj.getElementsByTagName('titulo').item(0).firstChild.nodeValue;
			var data_inclusao	= obj.getElementsByTagName('data_inclusao').item(0).firstChild.nodeValue;
			var data_abreviada	= obj.getElementsByTagName('data_abreviada').item(0).firstChild.nodeValue;
			var descricao		= obj.getElementsByTagName('descricao').item(0) ? obj.getElementsByTagName('descricao').item(0).firstChild.nodeValue : false;
			var padrao			= parseInt( obj.getElementsByTagName('padrao').item(0).firstChild.nodeValue );
			if ( selecionado == id || ( selecionado == null && padrao == 1 ) ){
				indice = parseInt( i / limite );
				+nSelecionado;
			}
			if ( padrao == 1 ){ ++nSelecionado; }
			createObj(
						container,
						tipo,
						idpai,
						id,
						titulo,
						data_abreviada,
						descricao,
						padrao,
						"thumb.php?t=tvfutsite_"+tipo+"&id="+id+"&w=50&h=50",
						( selecionado == id || ( selecionado == null && padrao == 1 ) ? true : false ),
						nextObj
				);
		}
		//exibir apenas os objetos do indice selecionado;
		limitaObjs( container, objs.length, indice, limite );
		//carrega a barra de paginação
		loadPaginacao( container, tipo, idpai, indice, limite );
		//carrega o próximo container vazio.
		if ( nSelecionado == 0 && nextObj ){ loadNextObj( container, null, tipo, idpai, 0, nextObj ); }
	} else {
		//cria mensagem de que não há objetos
		var div	= document.createElement( "div" );
		div.className	= "msg";
		div.appendChild( document.createTextNode( "Não há "+tipo+" adicionado." ) );
		container.appendChild( div );
		//altera o título
		$( "#titulo_"+tipo ).text( "" );
		//carrega o próximo objeto vazio.
		loadNextObj( container, null, tipo, idpai, 0, nextObj );
	}
	return false;
}
//cria os objetos (canal, programa, video)
function createObj( container, tipo, idpai, id, titulo, data, descricao, padrao, img, selecionado, nextObj ){
	//cria o div exterior
	var divVideo		= document.createElement( "div" );
	divVideo.className	= "video";
	divVideo.tipo		= tipo;
	divVideo.idvideo	= id;
	divVideo.titulo		= titulo;
	divVideo.data		= data;
	divVideo.descricao	= descricao;
	divVideo.padrao		= padrao;
	container.appendChild( divVideo );
	
	//cria o link
	var link			= document.createElement( "a" );
	link.href			= "javascript:void(0)";
	link.onclick		= function(){
		selectObj( container, divVideo, tipo, idpai, id )
		loadNextObj( container, divVideo, tipo, idpai, id, true );
	};
	divVideo.appendChild( link );
	
	//cria a imagem
	var imgFoto			= document.createElement( "img" );
	imgFoto.src			= img;
	imgFoto.className	= "logo";
	imgFoto.alt			= "logo";
	link.appendChild( imgFoto );
	
	//cria h4 de título
	h4Titulo			= document.createElement( "h4" );
	h4Titulo.appendChild( document.createTextNode( titulo ) );
	link.appendChild( h4Titulo );
	if ( tipo == 'canal' || tipo == 'programa' ) limitaAltura( link.getElementsByTagName("h4").item(0), 40, 1 );
	if ( tipo == 'video' ) limitaAltura( link.getElementsByTagName("h4").item(0), 20, 1 );
	
	//cria texto de data
	pData			= document.createElement( "p" );
	pData.className	= "data";
	pData.appendChild( document.createTextNode( data ) );
	link.appendChild( pData );
	
	//cria div da descriç?o se existir
	if ( descricao != false ){
		pDescricao			= document.createElement( "p" );
		pDescricao.className= "descricao";
		pDescricao.appendChild( document.createTextNode( descricao ) );
		link.appendChild( pDescricao );
		//limita altura da descrição
		var descricao	= $( link.getElementsByTagName("p").item(1) );
		limitaAltura( link.getElementsByTagName("p").item(1), 36 );
	}
	
	//cria o cache
	globals['cache'][tipo][idpai]['lista'][ globals['cache'][tipo][idpai]['lista'].length ] = divVideo;
	if ( selecionado ){
		globals['cache'][tipo][idpai]['ultimo']	= id;
	}
	
	//marca o item selecionado
	if ( selecionado ){
		selectObj( container, divVideo, tipo, idpai, id );
		if ( nextObj ){ loadNextObj( container, divVideo, tipo, idpai, id, nextObj ); }
	}
	return false;
}
//limita os objetos de acordo com o indice
function limitaObjs( container, nObjs, indice, limite ){
	//atribui as variáveis.
	var objs	= container.getElementsByTagName("div");
	for ( var i = nObjs - 1; i >= 0; --i ){
		if ( i < ( indice * limite ) || i >= ( indice * limite ) + limite ){
			if ( objs[i] != undefined ){
				container.removeChild( objs[i] );
			}
		}
	}
}
//carrega a barra de paginação
function loadPaginacao( container, tipo, idpai, indice, limite ){
	//atribui as variáveis
	var nObjs	= globals['cache'][tipo][idpai]['lista'].length;
	//cria o link de voltar.
	if ( indice > 0 ){
		var anterior			= document.createElement("a");
		anterior.className		= "anterior";
		anterior.href			= "javascript:void(0)";
		anterior.onclick		= function(){ loadObjs( container, tipo, idpai, indice-1, limite, globals['cache'][tipo][idpai]['ultimo'] ); };
		anterior.appendChild( document.createTextNode( "<< anterior" ) );
		container.appendChild( anterior );
	}
	//cria o link de próximo
	if ( ( indice * limite ) + limite < nObjs ){
		var proximo				= document.createElement("a");
		proximo.className		= "proximo";
		proximo.href			= "javascript:void(0)";
		proximo.onclick			= function(){ loadObjs( container, tipo, idpai, indice+1, limite, globals['cache'][tipo][idpai]['ultimo'] ); };
		proximo.appendChild( document.createTextNode( "proximo >>" ) );
		container.appendChild( proximo );
	}
	return false;
}
function selectObj( container, obj, tipo, idpai, id ){
	//remove a seleção do cache
	for ( var i = 0; i < globals['cache'][tipo][idpai]['lista'].length; ++i ){
		var video	= globals['cache'][tipo][idpai]['lista'][i];
		if ( video.idvideo == id ){
			video.className	+= " selecionado";
		} else {
			video.className	= new String( video.className );
			video.className	= video.className.replace( / selecionado/g, "" );
		}
	}
	//adiciona a seleção ao objeto
	obj.className	+= " selecionado";
	//altera o título
	$( "#titulo_"+tipo ).text( obj.titulo );
	limitaAltura(  $( "#titulo_"+tipo ), 20, 1 );
	return false;
}
function loadNextObj( container, obj, tipo, idpai, id, nextObj ){
	//carrega os próximos objetos.
	switch ( tipo ){
		case "canal":
			loadObjs( document.getElementById("boxPrograma"), 'programa', id, null, globals['programa']['limite'], null, nextObj );
			break;
		case "programa":
			loadObjs( document.getElementById("boxVideo"), 'video', id, null, globals['video']['limite'], null, nextObj );
			break;
		case "video":
			if ( obj ){
				loadVideo( obj );
			}
			break;
	}
	return false;
}
//carrega o vídeo no player.
function loadVideo( obj ){
	//atribuição das variáveis
	var id			= obj.idvideo;
	var titulo		= obj.titulo;
	var data		= obj.data;
	var descricao	= obj.descricao;
	var video		= "../videos/tvfutsite/filme/"+id;
	var img			= "thumb.php?t=tvfutsite_video&id="+id+"&w=50&h=50";
	
	//seto vídeo atual
	globals['video']['id']	= id;
	
	//seta as informações do vídeo
	$( "#video_titulo" ).text( titulo );
	$( "#video_data" ).text( data );
	$( "#video_descricao" ).html( descricao );
	globals['video']['descricao']	= $( "#video_descricao" ).html();
	limitaAltura( $( "#video_descricao" ), 60 );
	
	setTimeout( "loadVideoCarregaPlayer( '"+video+"' )", 200 );
	
	return false;
}
function loadVideoCarregaPlayer( video ){
	if ( $( "#video_descricao" ).height() < 60 ){
		//crio novo player
		createPlayer( "container_tvfutsite",
					video,
					340,
					220,
					true
		);
	} else {
		setTimeout( "loadVideoCarregaPlayer( '"+video+"' )", 200 );
	}
}
