$(function() {


	var tot = 0;// qtd de registros
	var cur = 0;
	var pageCurrent = 1;
	var speed = 8000;
	var mover = null;

	var im  = {
		titulo: [],
		legenda: [],
		imagem: [],
		website: []
	};

	// Definições
	$pImagem	= $("#imagemaqui");//$("#destaque1-esq-img");
	$pTitulo	= $("#destaque1-esq-desc-txt");
	$pages		= $("#pagesDestaque");
	var lang	= QueryString("lang");
	if(lang == null) lang = 'pt';
	function _iniNoticiasDestaque(){
		$.post("home.json.php",{
			acao: "NOTICIAS-DESTAQUE",
			lang: lang
		}, function(json){
				if(json.report.error == "false"){
					$.each(json.noticiaslist, function(i,row){
						im.titulo[i] = row.titulo;
						im.legenda[i] = row.resumo;
						im.imagem[i] = row.imagem;
						im.website[i] = row.website;
					});

					tot = im.titulo.length;// qtd de registros
					cur = 0;
					pageCurrent = 1;
				 
					// Definições
					$pImagem = $("#imagemaqui");//$("#destaque1-esq-img");
					$pTitulo = $("#destaque1-esq-desc-txt");
					$pages = $("#pagesDestaque");

					// Inicialização
					
					/*$pImagem.html( im.imagem[cur] ).fadeIn();
					$pTitulo.html("<h1>"+  im.titulo[cur] +"</h1><a href=\""+ im.website[cur]  +"\"><p>" + im.legenda[cur] +"</p></a>").fadeIn();
					$pages.html( pageCurrent +"/"+ tot);
					*/

					_exibir();
					_proximo();
					_anterior();
					if(tot >1) _autoPlay();
					 _mouseOver();
					
				}
				return false;
			},"json"
		);
	
	}// _iniNoticiasDestaque
	// ----------------------------------------------------------------------

	function _mouseOver(){
		$pImagem.mouseover(function(){ _stopPlay(); return false; });
		$pImagem.mouseout(function(){ _autoPlay(); return false; });
		$pTitulo.mouseover(function(){ _stopPlay(); return false; });
		$pTitulo.mouseout(function(){ _autoPlay(); return false; });
		$pages.mouseover(function(){ _stopPlay(); return false; });
		$pages.mouseout(function(){ _autoPlay(); return false; });
	}// _mouseOver
	// ----------------------------------------------------------------------

	function _proximo(){
		$("#proximo").click(function(){
			if( cur < ( tot - 1) ) {
				pageCurrent++;
				cur++; 
			} else {
				pageCurrent = 1;
				cur = 0;
			}
			_exibir();
			/*
			$pImagem.fadeTo("fast", 0.1, function(){
				$pImagem.html( im.imagem[cur] ).fadeTo("fast", 1);
			});
			$pTitulo = $("#destaque1-esq-desc-txt");
			$pTitulo.fadeTo("fast", 0.1,function(){
				$pTitulo.html("<h1>"+  im.titulo[cur] +"</h1><p>" + im.legenda[cur] +"</p>").fadeTo("fast", 1);
			});
			$pages.html( pageCurrent +"/"+ tot);
			*/
			return false;
		});// end : proximo : click
		$("#proximo").find('img').mouseover(function(){
			$(this).attr("src", "imgs/seta_direita_o.gif");
			_stopPlay();
			return false;
		});
		$("#proximo").find('img').mouseout(function(){
			$(this).attr("src", "imgs/seta_direita.gif");
			_autoPlay();
			return false;
		});	
	}// _proximo
	// ----------------------------------------------------------------------

	function _anterior(){
		$("#anterior").click(function(){
			if( cur <= 0 ) {
				cur = tot - 1;
				pageCurrent = tot; 
			} else {
				pageCurrent--;
				cur--;
			}
			_exibir();
		});// end : anterior : click
		$("#anterior").find('img').mouseover(function(){
			$(this).attr("src", "imgs/seta_esquerda_o.gif");
			_stopPlay();
			return false;
		});
		$("#anterior").find('img').mouseout(function(){
			$(this).attr("src", "imgs/seta_esq_branca.gif");
			_autoPlay();
			return false;
		});	
	}// _anterior
	// ----------------------------------------------------------------------

	function _exibir(){		
		$pImagem.fadeTo("fast", 0.1, function(){
			$pImagem.html( im.imagem[cur] ).fadeTo("fast", 1);
		});
		$pTitulo = $("#destaque1-esq-desc-txt");
		$pTitulo.fadeTo("fast", 0.1,function(){
			$pTitulo.html("<h1><a href=\""+ im.website[cur]  +"\">"+  im.titulo[cur] +"</a></h1><p><a href=\""+ im.website[cur]  +"\">" + im.legenda[cur] +"</a></p>").fadeTo("fast", 1);
		});									
		$pages.html( pageCurrent +"/"+ tot);
		return false;	
	}// _exibir
	// ----------------------------------------------------------------------

	function _autoPlay(){
		if(tot<=1) {
			_stopPlay();
			return false;
		}

		clearInterval(mover); 
		mover = setInterval(function(){ 
			if( cur < ( tot - 1) ) {
				pageCurrent++;
				cur++; 
			} else {
				pageCurrent = 1;
				cur = 0;
			}
			_exibir();
		},speed);
	}// _autoPlay
	// ----------------------------------------------------------------------

	function _stopPlay(){
		clearInterval(mover);
	}// _stopPlay
	// ----------------------------------------------------------------------

	_iniNoticiasDestaque();

	
});