var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-4612616-1']);
  _gaq.push(['_trackPageview']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();
	
function carregaTwitter()
{
	$("#box_conteudo").getTwitter({
		userName: "opticalfaze",
		numTweets: 7,
		loaderText: "Loading tweets...",
		slideIn: true,
		showHeading: true,
		headingText: "Latest Tweets",
		showProfileLink: false
	});
}

function configuraGaleria(){
	$('#galeria a').lightBox({
		overlayBgColor: '#000',
		overlayOpacity: 0.8,
		imageLoading: 'img/lightbox/lightbox-ico-loading.gif',
		imageBtnClose: 'img/lightbox/lightbox-btn-close.gif',
		imageBtnPrev: 'img/lightbox/lightbox-btn-prev.gif',
		imageBtnNext: 'img/lightbox/lightbox-btn-next.gif',
		containerResizeSpeed: 350,
		txtImage: 'Image',
		txtOf: 'of'
	});
}

function configuraEmail()
{
	$('#sendmail').click(function()
	{
		$('#response').removeClass('msg_erro');
		$('#response').removeClass('msg_sucesso');
		
		var valid = '';
		var isr = ' is required.';
		var mail = $('#campo_email').val();
		var text = $('#campo_mensagem').val();
		if (!mail.match(/^([a-z0-9._-]+@[a-z0-9._-]+\.[a-z]{2,4}$)/i))
		{
			valid += '<br />A valid Email'+isr;
		}
		if (text.length<1)
		{
			valid += '<br />Text'+isr;
		}
		if (valid!='')
		{
			$('#response').addClass('msg_erro');
			$('#response').fadeIn('slow');
			$('#response').html( '<strong>Error:</strong>' + valid );
		}
		else
		{
			var datastr = 'mail=' + mail + '&text=' + text;
			$('#response').css('display', 'block');
			$('#response').html('Sending message …. ');
			$('#response').fadeIn('slow');
			setTimeout("send('"+datastr+"')",2000);
		}
		return false;
	});	
}

function send(datastr)
{
	$.ajax({
		type: 'POST',
		url: 'mail.php',
		data: datastr,
		cache: false,
		success: function(html)
		{
			var eh_erro = html.indexOf('erro');
			
			if( eh_erro > 0 )
			{
				$('#response').addClass('msg_erro');
			}
			else
			{
				$('#response').addClass('msg_sucesso');
			}
			
			$('#response').fadeIn('slow');
			$('#response').html('<strong>' + html + '</strong>');
			setTimeout('$("#response").fadeOut("slow")',4000);
		}
	});
}

$(document).ready(function() {
	
	carregaTwitter();
	
	/* Player */
	$('#aba_player').click(
					function()
					{
						var altura = $('#area_player').css('height');
						if( altura == '0px' )
						{
							$(this).fadeOut();
							$('#area_player').animate({height: '225px'});
						}
						else
						{
							$(this).fadeIn();
							$('#area_player').animate({height: '0'});
						}
						
					});
					
	$('#area_player').hover( function(){  }, function(){ $('#area_player').animate({height: '0'}); $('#aba_player').fadeIn(); } );
			
	/*NAVEGACAO COM AJAX*/
	//FIRST: We'll check if the URL has a hash (was bookmarked) and redirect the user to appropriate page
	if( window.location.hash != "" )
	{
		var hashpage = window.location.hash.split( "#" );
		var page = hashpage[ 1 ];
		
		navegaAJAX( page );
	}
	
	function navegaAJAX( page )
	{
		var id_nav = page.split(".");
		id_nav = id_nav[ 0 ];
		
		$('body').attr('id', id_nav);
		
		$("#conteudo").load(
			page + " #conteudo > *",
			function(){
				if( id_nav == 'index' ){
					carregaTwitter();
				}else if( id_nav == 'contact' ){
					configuraEmail();
				}else if( id_nav == 'media' ){
					configuraGaleria();	
				}
				
				$('#carregando').remove();
				
				$("#conteudo").fadeIn(300);
				
			});
		//append hash to the url for bookmarking
		window.location.hash = "#" + page;
	}
	
	//bind click event to the navigation links
	$(".nav").unbind("click").click(function(){
		
		//get href attribute
		var page = $(this).attr( "href" );
		page = page.substr( 30 );
		
		$("#conteudo").fadeOut(
		
			300,
			function(){ navegaAJAX( page );}
		);
		
		$('#conteiner').prepend( "<div id='carregando'>Loading area</div>" );
		
		//cancel browser default so the page doesn't reload
		return false;
		
	});
	
});
