/**
* @fileOverview Scripts gerais
* @name scripts
*/


/**
* Instancia o console do firebug caso não exista
*/
if (!window.console || !console.firebug){
	var names = ["log", "debug", "info", "warn", "error", "assert", "dir", "dirxml", "group", "groupEnd", "time", "timeEnd", "count", "trace", "profile", "profileEnd"];
	window.console = {};
	for (var i = 0; i < names.length; ++i)
		window.console[names[i]] = function() {}
}

/**
* Instancia o track do analytics caso não exista
*/
if (!window._gaq){
	var names = ['_createAsyncTracker', '_getAsyncTracker', 'ld', 'push', 'qd'];
	window._gaq = {};
	for (var i = 0; i < names.length; ++i)
		window._gaq[names[i]] = function() {}
}

/**
* Funções
*
*/
$(document).ready(function() {

	$(".box_fotos_link, a.texto").each(function(){
	var $this = $(this);
		//onclick = this.getAttribute("onclick");
		onclick = this.getAttributeNode('onclick').value;
		var url = onclick.substr(25,16);
		$this.attr('href',  url);
		
		$this.removeAttr("onclick").colorbox({
			iframe:true, 
			innerWidth:650, 
			innerHeight:600,
			title: $this.text()
		
		});

	})
/*
	$(".box_fotos_link").colorbox({
		iframe:true, 
		innerWidth:650, 
		innerHeight:600,
		title:
	
	});
*/
	$("table input,table textarea,table select,table button").uniform();


	$('#votarEnquete').live('click', function(e){
		e.preventDefault();
		var optEnquete = $('input:radio[name=enquete]:checked').val()
		if( !optEnquete){
			$('#msgEnquete').addClass('error').text('Selecione uma opção')
		}else{
			$.ajax({
				type: "POST",
				url: "salvar.php",
				data: "voto="+optEnquete,
				success: function(msg){
					$('#msgEnquete').addClass('success').text('Voto enviado com sucesso')
				}
			});
			
		}
		
	})

	

	//hover de descrição na miniatura de videos
	var $descVideo = $('#linhaListaEpisodios ul li a span, #colEpisodios  ul li a span');
	$descVideo.each(function(i,e){
		$(e).hide()
	});
	$('#linhaListaEpisodios ul li a, #colEpisodios ul li a ').live('hover',function(){
			$(this).children('span').stop(true, true).fadeIn();
	});
	$('#linhaListaEpisodios ul li a, #colEpisodios ul li a ').live('mouseleave',function(){
			$(this).children('span').stop(true, true).fadeOut();
	});
	
	$('.link-programa').live('click', function(e){
		e.preventDefault();
		$('html, body').animate({scrollTop:0}, 'slow');
		$('#mnprograma').trigger('click');
	});
	
	$('.link-episodios').live('click', function(e){
		e.preventDefault();
		$('html, body').animate({scrollTop:0}, 'slow');
		$('#mnepisodios').trigger('click');
	});
	
	$('.link-episodio').live('click', function(e){
		e.preventDefault();
		$('html, body').animate({scrollTop:0}, 'slow');
		e.preventDefault();
		var $this = $(this)
		$('.menu-ativo').removeClass('menu-ativo');
		$('#mnepisodios').addClass('menu-ativo');
		//Track analytics
		_gaq.push(['_trackEvent', 'Episódio: '+$this.children('span').text(), 'clicked'])
		$('#areaVideo').fadeOut(200, function(){
			$header.css('overflow', 'hidden');
			headerAberto = false;
			document.title =  'The Phone - '+$this.text();
			window.location.hash = '#episodio';
			$header.animate({
				height:200
			});
			$("#imgApresentador").animate({
				top:-101
			});
			$("#horario").animate({
				top:107,
				left:645
			});
			buscaPagina($this.attr('href'));
		});
	});
	
	var $header = $("header");
	var headerAberto = true;
	$('nav li a').not('#baixeRingtone').click(function(e){
		e.preventDefault();
		var $this = $(this)
		$('.menu-ativo').removeClass('menu-ativo');
		$this.parent('li').addClass('menu-ativo');
		//Track analytics
		_gaq.push(['_trackEvent', 'Página: '+$this.text(), 'clicked'])
		$('#areaVideo').fadeOut(200, function(){
			$header.css('overflow', 'hidden');
			headerAberto = false;
			document.title =  'The Phone - '+$this.text();
			window.location.hash = $this.attr('id').replace('mn','');
			$header.animate({
				height:200
			});
			$("#imgApresentador").animate({
				top:-101
			});
			$("#horario").animate({
				top:107,
				left:645
			});
			buscaPagina($this.attr('href'));
		});
	});
	$('.link-episodio').click(function(e){
		e.preventDefault();
		var $this = $(this)
		$('.menu-ativo').removeClass('menu-ativo');
		$('#mnepisodios').addClass('menu-ativo');
		//Track analytics
		_gaq.push(['_trackEvent', 'Episódio: '+$this.children('span').text(), 'clicked'])
		$('#areaVideo').fadeOut(200, function(){
			$header.css('overflow', 'hidden');
			headerAberto = false;
			document.title =  'The Phone - '+$this.text();
			window.location.hash = '#episodio';
			$header.animate({
				height:200
			});
			$("#imgApresentador").animate({
				top:-101
			});
			$("#horario").animate({
				top:107,
				left:645
			});
			buscaPagina($this.attr('href'));
		});
	});
	
	if( $('#tituloEpisodio') .size() > 0){
		$('.menu-ativo').removeClass('menu-ativo');
		$('#mnepisodios').addClass('menu-ativo');
		$('#areaVideo').fadeOut(200, function(){
			$header.css('overflow', 'hidden');
			headerAberto = false;
			$header.animate({
				height:200
			});
			$("#imgApresentador").animate({
				top:-101
			});
			$("#horario").animate({
				top:107,
				left:645
			});
			//buscaPagina($this.attr('href'));
		});
	}
	
	
	$('#logo').click(function(e){
		e.preventDefault();
		if(!headerAberto){
			var $this = $(this);
			document.title =  $this.text();
			window.location.hash = '#inicio';
			$("header").animate({
				height:495
			},200, function(){
				$header.css('overflow', 'visible');
				headerAberto = true;
				$('#areaVideo').fadeIn(200);
			});
			$("#imgApresentador").animate({
				top:-24
			});	
			$("#horario").animate({
				top:396,
				left:662
			});
			buscaPagina($this.attr('href'));
		}
	});
	
	var $conteudo = $('#conteudo');
	$(document).bind('mousemove', function(e){
		$('#carregando').css({
			left:  e.pageX,
			top:   e.pageY
		});
	});
	
	function buscaPagina(urlPagina){
		console.log(urlPagina);
		$.ajax({
			url		: urlPagina,
			data		: "&ajax=true",
			statusCode: {
				404: function() {
					alert('page not found');
				}
			},
			beforeSend:function(){
				$('#carregando').fadeIn();
			},
			success: function(data){
				$conteudo.fadeOut(300, function(){
					$conteudo.html(data).fadeIn(300, function(){
						$('#carregando').fadeOut();
						//pog
						var $descVideo = $('#linhaListaEpisodios ul li a span, #colEpisodios  ul li a span');
						$descVideo.each(function(i,e){
							$(e).hide()
						});
						
						
					});
				});
			}
		});
	}//buscaPagina fim


	//se nao for pagina inicial
	if( !(window.location.hash == '')){
		$(window.location.hash.replace('#','#mn')).trigger('click');
	}

});//$(document).ready fim

(function($){

})(this.jQuery);






















