$(window).load(function() {	
	
	var act='';
	
	$('#content > ul > li').css({position:'absolute', display:'none'});
	$('#content > ul > li').find('.box1').css({height:'0'})
	
	$('#menu > li > a span').css({opacity:'0'})
	
	$('#menu > li > a').hover(function(){
		$(this).find(' > span').stop().animate({opacity:'1'},600);						   
	}, function(){
		if (!$(this).hasClass('active')) {
			$(this).find(' > span').stop().animate({opacity:'0'},600);						   
		}
	})
	
	$('#menu > li').each(function(num){
		$(this).data({num:num})
	})
	$('#content > ul > li').each(function(num){
		$(this).data({num:num})
	})
	
	if (location.hash.slice(0,3)=='#!/') {
		page=location.hash.slice(3);
		open_page('#'+page);
		fl=false;
	}
	if ((location.hash=='#')||(location.hash=='')) {
		open_page('');
		fl=true;
			$('#content').stop().animate({height:'668'})
	}
	$('a').click(function(){
		if ($(this).attr('href').slice(0,3)=='#!/') {
			page=$(this).attr('href').slice(3);	
			open_page('#'+page);
			return false;
		}
		if ($(this).attr('data-type')=='close') { close_page() }
		if ($(this).attr('data-type')=='about-up') { move_page('#page_About .scroll','up','650') }
		if ($(this).attr('data-type')=='about-down') { move_page('#page_About .scroll','down','650') }
		if ($(this).attr('data-type')=='services-up') { move_page('#page_Services .scroll','up','650') }
		if ($(this).attr('data-type')=='services-down') { move_page('#page_Services .scroll','down','650') }
		if ($(this).attr('data-type')=='home-up') { move_page('#page_Home .scroll','up','650') }
		if ($(this).attr('data-type')=='home-down') { move_page('#page_Home .scroll','down','650') }
		if ($(this).attr('data-type')=='portfolio-up') { move_page('#page_Portfolio .col2','up','148') }
		if ($(this).attr('data-type')=='portfolio-down') { move_page('#page_Portfolio .col2','down','148') }
		
	})

	function open_page(page){
		location.hash='#!/'+page.slice(1);
		$('#menu a').removeClass('active').find(' > span').stop().animate({opacity:'0'},600);
		Cufon.replace('#menu a', { fontFamily: 'Ubuntu', hover:true });
		num=$(page).data('num');
		$('#menu > li').each(function(){					  
			if ($(this).data('num')==num) {
				$(this).find('> a').addClass('active').find('> span').stop().animate({opacity:'1'},600);
				Cufon.replace('#menu a', { fontFamily: 'Ubuntu', hover:true });
			}
		})
		fl=false;
		$('#content').stop().animate({height:'868'})
		if (act!='') {
			$(act).find('.box1').stop().animate({height:'0'},700,'easeOutCirc', function(){
				$(act).css({display:'none'});
				$(page).css({display:'block'}).find('.box1').stop().animate({height:'100%'},700, 'easeOutCirc', function(){
					act=page;	
				});	
			})
		} else {
			$(page).css({display:'block'}).find('.box1').stop().animate({height:'100%'},700, 'easeOutCirc', function(){
				act=page;	
			});		
		}
	}

	function close_page(page){
		$('#menu a').removeClass('active').find(' > span').stop().animate({opacity:'0'},600);
		Cufon.replace('#menu a', { fontFamily: 'Ubuntu', hover:true });
		location.hash='#';
		$(act).find('.box1').stop().animate({height:'0'},700,'easeOutCirc', function(){
			$(act).css({display:'none'});
			act='';
			fl=true;
			$('#content').stop().animate({height:'668'})
		});	
		return false;
	}

	// virtual scroll
	function move_page( element, direction, amount ){		
		var current_position = $( element ).css('top');
		var div_height = parseInt( $( element ).height() );
		if ( current_position == 'auto' ) {
			current_position = '0';
		} else { 
			current_position = parseInt( current_position );
		}
		if ( direction == 'down' ) {
			new_position = current_position - Number(amount);
			if ( ( div_height + new_position ) < 0 ) return;
		} else {
			if ( current_position >= '0') return;
			new_position = current_position + Number(amount);
		}
		$( element ).stop().animate({top:new_position});
	}


	// show/hide submenus
	$('li#menu-item-pittura a').click(function(){
		$('li.pittura').toggle();
	});
	
	
})

