$(function(){
	var act;
	function all_null(){
		$('#content > article').css({position:'absolute', display:'none'});
		$('#content .box').css({width:'0', height:'0', marginTop:'342px', marginLeft:'350px'});
		$('#content .box').css({opacity:'0.9'});
		$('#content .text').css({opacity:'0',display:'none'});
	}
	function null_content(){
		$('#content > article').css({display:'none'});
		$('#content .box').css({width:'0', height:'0', marginTop:'342px', marginLeft:'350px'});
		$('#content .text').css({opacity:'0',display:'none'});
	}
	all_null();
	function open_page(page){
		if (page!='#page_projects') {
			$(page).css({display:'block'}).find('.box').animate({width:'50%', height:'50%', marginTop:'171', marginLeft:'175',  rotate:'+=720deg'},400).animate({width:'100%', height:'100%', marginTop:'0', marginLeft:'0'},400,'easeOutBack', function(){
				$(page).find('.text').css({display:'block'}).animate({opacity:'1'},400,function(){
					act=page;												 
				});																																																										  							 			});
		} else {
			$(page).css({display:'block'});
			$(page).find('.text').css({display:'block'}).animate({opacity:'1'},400,function(){
				act=page;												 
			});	
		}
	}
	page=location.hash.slice(0,-1);
	if ((page!='')&&(page!='#')) {
		var nav=page.substr(6);
		nav='.nav'+nav;
		$(nav).addClass('active');
		$(nav).find('a').unbind('mouseenter, mouseleave').parent().stop().animate({backgroundPosition:'-100px -266px'},400,'easeInSine');
		if (page=='#page_projects') {$('.ban3 a').unbind('mouseenter, mouseleave').find('img').stop().animate({rotate:'-45deg'},1200,'easeOutElastic')}
		open_page(page);
	} else {$('.nav1 a').unbind('mouseenter, mouseleave').parent().stop().animate({backgroundPosition:'-100px -266px'},400,'easeInSine');}
	
	$('a').click(function(){
		page=$(this).attr('href');
		if (page.substr(page.indexOf('#'),6)=='#page_') {
			if (page=='#page_0') {
					menu_hover();
					banners_hover();
					$(this).unbind('mouseenter, mouseleave').parent().stop().animate({backgroundPosition:'-100px -266px'},400,'easeInSine');
					$('#menu > li').removeClass('active').find('> a').mouseleave();
					$('.banners > li').find('> a').mouseleave();
					$(act).find('.text').animate({opacity:'0'},400, function(){
						$(act).find('.text').css({display:'none'});
						if (act!='#page_projects') {
						$(act).find('.box').animate({width:'50%', height:'50%', marginTop:'171', marginLeft:'175'},400,'easeInBack').animate({width:'0', height:'0', marginTop:'342px', marginLeft:'350px',  rotate:'+=720deg'},400, function(){
							null_content();
							location.hash='/';
							act=0
						})	
						} else{
							null_content();
							location.hash='/';
							act=0
						}
					})
			} else {
			menu_hover();
			banners_hover();
			$(this).unbind('mouseenter, mouseleave').parent().stop().animate({backgroundPosition:'-100px -266px'},400,'easeInSine');
			$('#menu > li').removeClass('active').find('> a').mouseleave();
			$('.banners > li').find('> a').mouseleave();
			if (act) {
				$(act).find('.text').animate({opacity:'0'},400, function(){
					$(act).find('.text').css({display:'none'})
					if (act!='#page_projects') {
					$(act).find('.box').animate({width:'50%', height:'50%', marginTop:'171', marginLeft:'175'},400,'easeInBack').animate({width:'0', height:'0', marginTop:'342px', marginLeft:'350px',  rotate:'+=720deg'},400, function(){
						null_content();
						open_page(page);
						location.hash=page+'/';
					})							
					} else {
						null_content();
						open_page(page);
						location.hash=page+'/';
					}
				})
			} else {
				open_page(page);
				location.hash=page+'/';
			}
			}
				return false;
			} else {return true}
	})
})
