var isIE  = (navigator.appVersion.indexOf("MSIE") != -1) ? true : false;
var isWin = (navigator.appVersion.toLowerCase().indexOf("win") != -1) ? true : false;
var isOpera = (navigator.userAgent.indexOf("Opera") != -1) ? true : false;
var hasReqestedVersion = DetectFlashVer(8, 0, 0);

$(function ()  {
   function meuconfirm(message, callback) {
   	$('#confirm').modal({
   		close:false, 
   		overlayId:'confirmModalOverlay',
   		containerId:'confirmModalContainer', 
   		onShow: function (dialog) {
   			dialog.data.find('.message').append(message);
   
   			// if the user clicks "yes"
   			dialog.data.find('.yes').click(function () {
   				// call the callback
   				if ($.isFunction(callback)) {
   					callback.apply();
   				}
   				// close the dialog
   				$.modal.close();
   			});
   		}
   	});
   }

   // ---------------------------------------------------------------------------------------------
   // Verificação do Flash
   // ---------------------------------------------------------------------------------------------
   if (!hasReqestedVersion) {
		meuconfirm("Foi detectado que voce não possui a versão adequada do Flash Player para melhor visualização do site. <br><br> Deseja ir para o site da Adobe e atualizar seu Flash Player?", function () {
			window.open('http://www.adobe.com/go/getflashplayer');
		});
   }   
   // ---------------------------------------------------------------------------------------------      
   
   $('.external').click(function() { window.open(this); return false; });  

   $('.jumpmenu').change(function () { 
      if ($(this).val() == "") {
         return false;
      } else {
         window.open($(this).val()); return false; 
      }
   });   
   $('#acessorapido').change(function () { location.href=$(this).val(); return false; });
   $('#outrosprogramas').change(function () { location.href=$(this).val(); return false; });
   $('#relacionados').change(function () { window.open($(this).val()); return false; });

   $('.indicar').click(function () {
      window.open('index.php?option=com_indica&Itemid=44&url='+$(this).attr('href'),'popup','width=320,height=450');
      return false;
   });
  
   // -----------------------------------------------------------------------------------------------
   // Filtro de Noticias
   // -----------------------------------------------------------------------------------------------
   $("#data_inicial").mask("99/99/9999",{placeholder:" "});
   $("#data_final").mask("99/99/9999",{placeholder:" "});
   // -----------------------------------------------------------------------------------------------

   // -----------------------------------------------------------------------------------------------
   // LIGHTBOX
   // -----------------------------------------------------------------------------------------------
   $('.img_lightbox').lightBox({
		imageLoading: DIR_IMG+'/lightbox-ico-loading.gif',
		imageBtnPrev: DIR_IMG+'/lightbox-btn-prev.jpg',
		imageBtnNext: DIR_IMG+'/lightbox-btn-next.jpg',
		imageBtnClose:	DIR_IMG+'/lightbox-btn-close.jpg',
		imageBlank: DIR_IMG+'/lightbox-blank.gif'		
   });
   // -----------------------------------------------------------------------------------------------


   // -----------------------------------------------------------------------------------------------
   // BOX NOTICIAS
   // -----------------------------------------------------------------------------------------------
   $("#noticias_esq").click(function() {
      if ($(this).attr("class") == "pointer") {
         if ($("#lista_noticias").find(".noticia-mostra")) {
            var pos = parseInt($("#lista_noticias").find(".noticia-mostra").attr("id").split("_")[1]);            
            $("#lista_noticias").fadeOut("normal", function() {
               alternaClasse($("#noticia_"+(pos+0)), "noticia-mostra", "noticia-esconde");
               alternaClasse($("#noticia_"+(pos-1)), "noticia-esconde", "noticia-mostra");
               $("#lista_noticias").fadeIn("normal");
            });
            if ($("#noticia_"+(pos-2)).attr("id") == undefined) { $(this).removeClass("pointer"); }
            if ($("#noticias_dir").attr("class") != "pointer") { $("#noticias_dir").addClass("pointer"); }            
         } else {
            
         }
      }
   });
   // -----------------------------------------------------------------------------------------------
   $("#noticias_dir").click(function() {
      if ($(this).attr("class") == "pointer") {
         var pos = parseInt($("#lista_noticias").find(".noticia-mostra").attr("id").split("_")[1]);
         
         if ($("#noticia_"+(pos+1)).attr('id')) {
            $("#lista_noticias").fadeOut("normal", function() {
               alternaClasse($("#noticia_"+(pos+0)), "noticia-mostra", "noticia-esconde");
               alternaClasse($("#noticia_"+(pos+1)), "noticia-esconde", "noticia-mostra");
               $("#lista_noticias").fadeIn("normal");
            });
            if ($("#noticia_"+(pos+2)).attr("id") == undefined) { $(this).removeClass("pointer"); }
            if ($("#noticias_esq").attr("class") != "pointer") { $("#noticias_esq").addClass("pointer"); }
         } else {
            $("#noticia_"+(pos)).fadeOut();
            $("#noticia_"+(pos)).fadeIn();
         }
         
      }
   });   
   // -----------------------------------------------------------------------------------------------
   // -----------------------------------------------------------------------------------------------      


   $(".mouse-move").mouseenter(function(){
      a = $(this).attr("class").split(" ");
      b = "#" + a[1];
      $(b).show();
      $('ul.lista-acoes > li:first > a').click();
   })
   .mouseleave(function(){
      $(b).hide();
   });


});

function CarregaTabela(codigo) {

   $(".loader").ajaxStart(function(){
      $(this).show();
      $(".grafico").hide();
   });
   
   $(".loader").ajaxStop(function(){
      $(this).hide();
      $(".grafico").show();
   });

   $.ajax({
      type: "GET",
      url: "index.php?option=com_programa&task=indicador&Itemid=10000",
      data: { codigo : codigo },
      dataType: "html",
      success: function(data){
         $(".grafico").html(data);
      }
   });

}


function setActiveStyleSheet(title) {
   var i, a, main;
   for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
      if(a.getAttribute("rel").indexOf("style") != -1
      && a.getAttribute("title")) {
         a.disabled = true;
         if(a.getAttribute("title") == title) a.disabled = false;
      }
   }
}

function alternaClasse(elemento, classeAntiga, classeNova) {
   elemento.removeClass(classeAntiga);
   elemento.addClass(classeNova);
}

function flash(src,w,h,string_values) {
   if(string_values==undefined)
   document.write('<object type="application/x-shockwave-flash" data="'+src+'" width="'+w+'" height="'+h+'" wmode="transparent"><param name="movie" value="'+src+'" /><param name="wmode" value="transparent" /></object>');
   else
   document.write('<object type="application/x-shockwave-flash" data="'+src+'" width="'+w+'" height="'+h+'" wmode="transparent"><param name="movie" value="'+src+'" /><param name="wmode" value="transparent" /><param name="FlashVars" value="'+string_values+'" /></object>');
}

tam = 11;
function mudaFonte( tipo )
{
   if( tipo == 'mais' )
   {
      if( tam < 14 ) tam += 1 ;
   }
   else
   {
      if( tam > 9 ) tam -= 1 ;
   }
   if( document.getElementById( 'texto' ) )
   mudaFonteRecursiva( tipo , document.getElementById( 'texto' ) ) ;

   if( document.getElementById( 'texto' ) )
   mudaFonteRecursiva( tipo , document.getElementById( 'texto' ) ) ;
}

function montaDadosForm(form){
   if((form==undefined)||(form.elements.length == 0))
   return "";
   else{
      var retorno = {};
      for(var i = 0; i + 1 < form.elements.length; i++){
         if(((form.elements[i].type != "checkbox")&&(form.elements[i].type != "radio"))||(form.elements[i].checked)){
            retorno[encodeURIComponent(form.elements[i].name)] = encodeURIComponent(form.elements[i].value);
         }
      }
      retorno[form.elements[i].name] = form.elements[i].value;
      return retorno;
   }
}

function mudaFonteRecursiva( tipo , domElement )
{
   for( var i = 0 ; i < domElement.childNodes.length ; i++ )
   {
      mudaFonteRecursiva( tipo , domElement.childNodes.item( i )  ) ;
   }
   if( domElement.style )
   domElement.style.fontSize = tam+'px';
}

/*** MODAL ***/
function abreModal( link , titulo, tipo ){

   if (tipo=="audio") {
      var width = 480;
      var height = 45;
   } else {
      var width = 480;
      var height = 360;
   }
   if (tipo==undefined) {
      tipo = "video";
   }
   
   $('body').append("<div id='modal-"+tipo+"'><a class='media-"+tipo+"' href='" + link + "'>" + titulo + "</a></div>");

   $("#modal-"+tipo).modal({
   	onOpen: modalVideo.open,
   	onClose:modalVideo.close,
   	minHeight: height+60
   });

   $('.media-'+tipo).media({ "width": width, "height": height , autoplay: true, bgColor: '#F2F2F2', id: "PlayerVideo", params: {volume: 100} });

   $('.media-'+tipo+' div').css({'margin':'10px 0','font-weight':'bold'});
}


var modalVideo = {
	open: function (dialog) {
		dialog.overlay.fadeIn('fast', function () {
			dialog.container.fadeIn('fast', function () {
				dialog.data.fadeIn('fast', function () {
					dialog.data.slideDown('fast');
				});
			});
		});
	},
	close: function (dialog) {
	   if (document.getElementById('PlayerVideo')!=null && document.getElementById('PlayerVideo').controls!=undefined) {
	      document.getElementById('PlayerVideo').controls.stop();
	   }
	   
	   $.modal.close();
	   
	   $("#simplemodal-container").remove();
		$("#modal-video-programa").remove();
		$("#modal-video").remove();
		$("#modal-audio").remove();
	}
};