var curSubPage='';
var timeout=0;
var timeout2=0;

$(document).ready(function(){
	
	$('#logo').css('opacity','0.7');
	
	$('#menu li:last').css('border-bottom','none');
	$('#menu li:last a').css('border-bottom','none');
	
	$('#topmenu li').each(function(){
		//$(this).css('margin-left','-'+((Math.random()*1000%200)+50)+'px');
		//$(this).css('margin-top','-'+100+'px');
		$(this).css('margin-top','-'+((Math.random()*10000%500)+50)+'px');
		$(this).css('width','100%');
		$(this).css('height','auto');
		$(this).animate({ 
        
	        opacity: 1,
			marginTop:"0px"
    	  }, 1000,'easeOutQuad',function(){
			  	
				if($('#menu li a').length){
					$('#menu').show();
					$('#menu ul').slideDown('slow').fadeIn('slow');
				}
				/*$('#logo').animate({ 
					marginLeft:"0px"
				  }, 1000,'easeOutQuint');*/
				
			} );
		
	});
	
	$('#topmenu li a').each(function(){
		//$(this).css('margin-left','-'+((Math.random()*1000%200)+50)+'px');
		//$(this).css('width','100%');
		
			
		rel=$(this).attr('rel');
		rel=rel.replace(/menulink/,'');
		eval('obj = '+rel+';');
		curPage=obj.id;	
		$(this).data('id',obj.id);
		$(this).data('title',obj.title.toLowerCase().replace(/ /g,'_'));
		$(this).addClass(obj.id);
		$(this).css('opacity','0.3');
		
		$(this).mouseover(function(){
						
			if(!$(this).data('animateInProgress')){
				$(this).data('animateInProgress',true);								   
								   
				$(this).animate({ 
					opacity: 0.6
				  	}, 500,null,function(){$(this).data('animateInProgress',false);	
				});
			}
			
			if(timeout2){
				clearTimeout(timeout2);
			}
			
			timeout2=setTimeout("switchHeader('"+$(this).data('id')+"')",500);
			
		});
		
		$(this).mouseout(function(){
			if(!$(this).hasClass('active')){
				$(this).animate({ 
					opacity: 0.3
				  }, 700,null,function(){} );
			}
			//$('#header').css('background-image','url(/images/header/0.jpg)');					  						
			$('#topmenu li a.active').mouseover();
		});
		
	});
	
	$('#topmenu li a.active').mouseover();
	
	
	$('#menu li a').each(function(){
		//$(this).css('margin-left','-'+((Math.random()*1000%200)+50)+'px');
		//$(this).css('width','100%');
		$(this).mouseover(function(){
			
			$(this).click(function(){
				this.blur();				   
				return false;				   
			});
			
			
			rel=$(this).attr('rel');
			rel=rel.replace(/menulink/,'');
			eval('var obj = '+rel+';');
			curSubPage=obj.id;	
			$(this).data('id',obj.id);
			
			
			
			if(!$(this).data('animateInProgress')){
				$(this).data('animateInProgress',true);
				$(this).css('color','#333');
				$(this).animate({ 
					opacity: 0.7
				  }, 500,null,function(){$(this).data('animateInProgress',false);} 
				);
			}
			
			if(timeout){
				clearTimeout(timeout);
			}
			
			timeout=setTimeout("showSubPage('"+obj.id+"','"+obj.lang+"')",300);
						
		});
		$(this).mouseout(function(){
			if($(this).data('id')==curSubPage){
				curSubPage='';
				clearTimeout(timeout);
			}
			
  			$(this).css('color','#555');					   
			$(this).animate({ 
				opacity: 1
			  }, 700,null,function(){} );
			
		});
		
	});
	
	
});


function switchHeader(id){
	mi='.'+id;

	if($(mi).data('title')!=$('#header').data('image')){
		$('#header').data('image',$(mi).data('title'));
		$('#header').animate(
			{backgroundPosition:'-600px'},
			500,'easeInQuad',function(){
					$('#header').css('background-image','url(/images/header/'+$(this).data('image')+'.jpg)');			
					$('#header').animate(
						{backgroundPosition:'0px'},
						500,
						'easeOutQuad'
					);
					
			}	
		);
	}	
}


function showSubPage(id,lang){
	if(id!=curSubPage){
		return;	
	}
	
	if(!$('#'+id).length){
		$.ajax({
			url: "/"+lang+"/",
			dataType:"html",
			data: 'page=pages&action=load&object='+id,
			success: function(html){
				$('.ajaxpage').hide();
				$("#content").append(html);	
				$('#'+id).fadeIn();
			},
			error:function (xhr, ajaxOptions, thrownError){
			}  
		});	
	}
	else{
		$('.ajaxpage').hide();
		$('#'+id).fadeIn();
	}
}


