var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-6057631-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);
})();


$(document).ready(function(){
	//Blog pagination
	jQuery('#postPagination a').live('click', function(e){
		e.preventDefault();
		var link = jQuery(this).attr('href');
		jQuery('#blog article').fadeOut(500).load(link + ' #blog article', function(){ jQuery('#blog article').fadeIn(500); });

	});

		
	//
	var indAnim = 1;
	//Get the height of the first item
	$('#mask').css({'height':$('#perfil').height()});	
	//Calculate the total width - sum of all sub-panels width
	//Width is generated according to the width of #mask * total of sub-panels
	$('#sections').width(parseInt($('#mask').width() * $('#sections section').length));
	
	//Set the sub-panel width according to the #mask width (width of #mask and sub-panel must be same)
	//$('#sections section').width($('#mask').width());
	
	//Animated titles
	function animTitle(titleSet){
		indAnim = indAnim + 1;
		$(titleSet).css("z-index", indAnim);
		$(titleSet).css({left: 500});
		$(titleSet).animate({left: 0}, 1000);
	}
	function resizeMask(_target){
		//Get the height of the sub-panel
		var panelheight = $(_target).height();
		var maskHeight = 110 + (panelheight);		
		//Resize the height
		$('#mask').animate({'height':maskHeight},{queue:false, duration:500});			
		//Scroll to the correct panel, the panel id is grabbed from the href attribute of the anchor
		$('#mask').scrollTo(_target, 800);
	}
	//Enable/Disable buttons
	function enableButtons(_target){
		$("#nav a").each(function () {
			if($(this).css("display") == "none"){
				$(this).css("display","inline");
				$(this).css('opacity', 0);
				$(this).stop().fadeTo(300, 1);
			}
		})
		$(_target).css("display","none");
	}
	resizeMask('#perfil');
	$('#nav-perfil a').css("display","none");
	
	//Event buttons
	$('#nav a').click(function (event) {
		resizeMask($(this).attr('href'));
		enableButtons($(this));
		switch($(this).attr('href')){
			case '#perfil': animTitle('.tperf');    _gaq.push(['_trackEvent', 'SessÃµes', 'Perfil']); break;
			case '#portfolio': animTitle('.tport'); _gaq.push(['_trackEvent', 'SessÃµes', 'PortfÃ³lio']); break;
			case '#contato': animTitle('.tcont');   _gaq.push(['_trackEvent', 'SessÃµes', 'Contato']); break;
			case '#blog': animTitle('.tblog');      _gaq.push(['_trackEvent', 'SessÃµes', 'Blog']);break;
		}
		event.preventDefault();
	});
	//
	//$('span').pngFix( );
	
	//BUTTON ANIMATIONS	
	function cssAnim(val){
		$(val[0]).hover(function () { 
		
			$(this).stop().animate(val[1],val[2]); 
		}, function () { 
			$(this).stop().animate(val[3],val[4]);
		});
	}
	//MENU
	var navA = ["#nav a",{opacity: 0}, 300, {opacity: 1}, 450];
	cssAnim(navA);
	//LINK DEVIANT
	var devA = [".link-deviant a",{backgroundColor: "#54360A"}, 300, {backgroundColor: "#C57E16"}, 400];
	cssAnim(devA);
	//SUBMIT FORM
	var subA = ["button",{backgroundColor: "#54360A"}, 300, {backgroundColor: "#C57E16"}, 400];
	cssAnim(subA);
	//SOCIAL MEDIA
	var socA = [".social-media a",{opacity: .5}, 300, {opacity: 1}, 450];
	cssAnim(socA);
	
	//BT PREVIEW ANIMATION
		$(".preview").hover(function () { 
			$(this).stop().animate({backgroundColor: "#D7BF79"}, 400); 
			$(this).children('img').stop().animate({opacity: .5}, 400); 
		}, function () { 
			$(this).stop().animate({backgroundColor: "#F1EAD0"}, 500); 
			$(this).children('img').stop().animate({opacity: 1}, 500); 
		});
		
	//
	$("a.preview").colorbox();
	// Twitter
	$(".tweet").tweet();
	
   // validate
   if(window.localStorage['name'] != null){
	   $('#name').val(window.localStorage['name']);
   }
   if(window.localStorage['email'] != null){
	   $('#email').val(window.localStorage['email']);
   }
	$('#contact').submit(function() {
		var inputs = $('#contact .required');
		var validated;
		inputs.each(function(){
			if($(this).val() == "" || $(this).val() == "Preencha esse campo"){
				$(this).focus();
				$(this).val('Preencha esse campo');
				validated = false;
				return false;
			}else{
				validated = true;
				//localStorage.lastname=$('#nome').val();
				window.localStorage['name'] = $('#name').val();
				window.localStorage['email'] = $('#email').val();
			}
		})
		if(validated){ return true; }
		else{ return false; }
	});
});
