$(document).ready(function() {
	// fold out banner
//	$('.trigger').toggle(function(){
//		$('.banner').stop().animate({ right:0 });
//	},function(){
//		$('.banner').stop().animate({ right:'-250px' });
//	});
	
	// submenu
	$('.main-menu').find('li').on({
		mouseenter: function(){
			if ($(this).find('ul').is(':animated')){
				return false;
			}
			else{
				$(this).find('ul').slideDown();	
			}
		},
		mouseleave: function(){
			$(this).find('ul').slideUp();	
		}
	});
	

	// js for home
	if($('body').hasClass('home')){	
		$.supersized({
			slide_interval: 3500,
			transition: 1,
			transition_speed: 2000,
			slide_links: 'blank',
			slides:[
				{image: 'img/home/foto_camarao.jpg'},
				{image: 'img/home/foto_polvo.jpg'},
				{image: 'img/home/foto_vista.jpg'}
			]					
		});
	}
	
	// other than home
	else {
		// adjust slider height
		var contentHeight = $('.content').height(),
			windowHeight = $(window).height();
		
		// 
		if (windowHeight < contentHeight){
			$('.slider').animate({height: windowHeight});
		}
		else {
			$('.slider').animate({height: 100 + contentHeight});
		}
		
		// on scroll change slider height
		$(window).scroll(function(){
			var topDistance = $(window).scrollTop();
				
			if (topDistance > 160){
				$('.slider').css({position: 'fixed', top: '0'});
				$('.content').css({float: 'right'});
			}
			else {
				$('.slider').css({position: 'relative'});
			}
		});
	}
});

$(window).load(function(){

	// slider
	if($('body').hasClass('home')){
		// fold in after 3sec
		//$('.banner').delay(10000).animate({ right:'-250px' });
		// init supersized

	}
	else {
		var slider = $('.slider div'),
			sliderHeight = $('.slider').height(),
			imgs = $('.slider').children('img'),
			imgsCount = $('.slider').children('img').length,
			scroll = 0,
			distance = parseInt($('.slider div').css('top'), 10)*1;
			
		$('.slider').wrapInner('<div></div>');
		
		//get sum of heigth of all imgs = distance
		imgs.each(function(){ scroll += $(this).height() });
		
		// stop, resume on mouse events
		$('.slider').mouseenter(function(){
			$(this).children('div').stop();
		});
		$('.slider').mouseleave(function(){
			var scrolldist = sliderHeight - imgsCount*5 - scroll;
			var t = parseInt($('.slider div').css('top'), 10);
			var dist = scrolldist - t;
			sliderHeight = $('.slider').height();
			if (up == 1){
				$('.slider div').animate({
					top: sliderHeight - imgsCount*5 - scroll
				}, scroll-dist*30, 'linear', function(){ callback(); });
			}
			else{
				$('.slider div').animate({
					top: 0
				}, scroll-t*30, 'linear', function(){ slide(); });
			}
		});
		
		// animate!
		function slide() {
			up = 1;
			sliderHeight = $('.slider').height();
			$('.slider div').animate({
				top: sliderHeight - imgsCount*5 - scroll
			}, scroll*30, 'linear', function(){ callback(); });
		}
		// now go back
		function callback() {
			up = 0;
			$('.slider div').animate({
				top: 0
			}, scroll*30, 'linear', function(){ slide(); });
		}
		slide();
	
	
		// fader
		var timer = setInterval(fade, 3500);
		function fade(){
			if ( $(".fader a:last").is(':visible') ) {
				$(".fader a:first").fadeIn(1500, function(){
					$(".fader a:not(:first)").delay(3500).fadeOut(1500);
				});
			}
			else {
				$(".fader a:hidden:first").fadeIn(1500);
			}
		}
		fade();
	}
	
})
