//plugin
jQuery.fn.topLink = function(settings) {
	settings = jQuery.extend({
		min: 1,
		fadeSpeed: 200
	}, settings);
	return this.each(function() {
		//listen for scroll
		var el = $(this);
		el.hide(); //in case the user forgot
		$(window).scroll(function() {
			if($(window).scrollTop() >= settings.min)
			{
				el.fadeIn(settings.fadeSpeed);
			}
			else
			{
				el.fadeOut(settings.fadeSpeed);
			}
		});
	});
};

$(function(){
	
	// Magic Line code:
	
	var $el, leftPos, $mainNav = $("#navTop .nav");
	var $current = $('li.current-menu-item a');
	
	if(!$current.offset()){$current = $('.nav li:first a');}
		
	$mainNav.append("<li id='indicator'></li>");
	var $indicator = $("#indicator");
	
	$indicator.css("left", $current.offset().left + ($current.width() * .5) - ($indicator.width() * .5))
		.data("origLeft", $indicator.offset().left);
			
	$(".nav li a").hover(
	function(){
		$el = $(this);
		leftPos = $el.offset().left + ($el.width() * .5) - ($indicator.width() * .5);
		$indicator.stop().animate({
			left: leftPos
		})
	},
	function(){
		$indicator.stop().animate({
			left: $indicator.data("origLeft")
		})
	});
	
	// End of Magic Line code.
	
	// Set the top-link
	$('#top-link, #top-linkEN, #top-linkES').topLink({
		min: 420,
		fadeSpeed: 500
	});
	
	// Smoothscroll to top
	$('#linkToTop').click(function(e) {
		e.preventDefault();
		$.scrollTo(0,300);
	});
		
	// When not Home, scroll to content
	if(!$('body').hasClass('home')) $.scrollTo('#centralArea', 300);
	
	// Pega os parametros da URL
	
	function getUrlVars()
	{
	    var vars = [], hash;
	    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
	    for(var i = 0; i < hashes.length; i++)
	    {
	        hash = hashes[i].split('=');
	        vars.push(hash[0]);
	        vars[hash[0]] = hash[1];
	    }
	    return vars;
	}
	
});

