var $ = jQuery;
var clickFade = false;


//jQuery.preloadImages = function()
//{
//	for(var i = 0; i<arguments.length; i++)
//	{
//		jQuery("<img>").attr("src", arguments[i]);
//	}
//}



// Плавный переход между страницами
jQuery(document).ready(function() {
	Cufon.set('fontFamily', 'HeliosCompressed').replace('#menu');
	
	Cufon.set('fontFamily', 'helioscond').replace('#lang, #home_link, #back_link');/*#getInfo, #awards, #left_column p, #right_column */
	Cufon.set('fontFamily', 'HeliosCompressed').replace('#left_column h1');/*, strong, b*/
	//Cufon.set('fontFamily', 'HeliosCompressed').replace('');
	
        if (true/*!jQuery.browser.opera*/)
        {
            //jQuery("body").css("display", "none");
            jQuery("body").fadeIn(1000);
            function redirectPage() {
                    window.location.href = linkLocation;
            }
            
            jQuery("a.open").click(function(event){
                    My_href = this.href;
                    event.preventDefault();
                    linkLocation = My_href;
                    clickFade = true;
                    jQuery("body").fadeOut(500, function(){
                            redirectPage();
                            });
                    return false;
            });
        }

	

});



//jQuery.preloadImages = function(preload){ 
//	jQuery(document.createElement('img')).bind('load', function(){ 
//	if(preload[0]) this.src = preload.shift(); 
//	}).trigger('load'); 
//}

// Функция выцисления позиции меню от координат мыши
function menu_pos(pos_x,pos_y,obj)
{
	if(pos_y!=null){
		all_height=jQuery('#wrap').height();
		menu_height=jQuery(obj).height();
		center_y=all_height/2;
		center_menu_y=menu_height/2;
		center_y_prc=pos_y/(all_height/100);
		menu_y_prc=center_menu_y-(menu_height/100)*center_y_prc;
	}
	
	if(pos_x!=null){
		all_width=jQuery('#wrap').width();
		menu_width=jQuery(obj).width();
		center_x=all_width/2;
		center_menu_x=menu_width/2;
		center_x_prc=pos_x/(all_width/100);
		menu_x_prc=center_menu_x-(menu_width/100)*center_x_prc;
	}
	
	if(pos_x!=null && pos_y!=null){
		jQuery(obj).stop(true,true).animate({
		marginTop: menu_y_prc + 'px',
		marginLeft: menu_x_prc + 'px'
		},500,'expoout');
	}
	
	if(pos_x!=null && pos_y==null){
		jQuery(obj).stop(true,true).animate({
		marginLeft: menu_x_prc + 'px'
		},500,'expoout');
	}
	
	if(pos_x==null && pos_y!=null){
		jQuery(obj).stop(true,true).animate({
		marginTop: menu_y_prc + 'px'
		},500,'expoout');
	}

}




function MenuRePosition(OutObj,InObj,e){
	menu_position = jQuery(OutObj).position();
	menu_pos(jQuery(InObj).width()-(jQuery(InObj).width()-(e.pageX-menu_position.left)-((jQuery(InObj).width()/2))),null,jQuery(InObj));
}


// Основыные события
jQuery(document).ready(function(){
	
	jQuery('#home_link, #back_link').animate({
		opacity: 0.5
	},300,'expoout');
	
	jQuery('#home_link, #back_link').hover(function(){
			jQuery(this).stop().animate({
				opacity: 1
				},300,'expoout');
		},function(){
			jQuery(this).stop().animate({
				opacity: 0.5
				},300,'expoout');
		});
	
	
select_menu = false;
	jQuery('#preview').animate({
		opacity: 0 
		},1);
	
	
	if(jQuery('#right_column').length && jQuery('#right_column').height()<jQuery('#wrap').height()){
		razn_y=jQuery('#wrap').height()-jQuery('#right_column').height();
		jQuery('#right_column').css({
			top: (razn_y/2)+'px'
			});
	}else{
		if (jQuery('#right_column').length && jQuery('#right_column').height()>jQuery('body').height()){
			jQuery('#wrap').mousemove(function(e){
			menu_pos(null,e.pageY,jQuery('#right_column'));
			});
		}
	}
	
	
	jQuery('#wrap').mousemove(function(e){
		if(jQuery('#menu').css('display')!='none' && select_menu==false){
			menu_pos(null,e.pageY/2,jQuery('#menu'));
		}
		});
	
	jQuery('#menu li table').mousemove(function(e){
			MenuRePosition(jQuery('#menu'),this,e);
                                //menu_position = jQuery('#menu').position();
                                ////alert(menu_position.left);
                                //menu_pos(jQuery(this).width()-(jQuery(this).width()-(e.pageX-menu_position.left)-((jQuery(this).width()/2))),null,jQuery(this));
		});
	
	hidden_table=true;
	function table_mouse_over(MyObj){
			if(hidden_table==true){
				jQuery(MyObj).stop(true,true).hide("slow");
			}
	}
	
	jQuery('#menu li').not('#menu li ul li').hover(function(){
		
			jQuery(this).addClass('hover').stop(true,false).animate({
				opacity: 1 
				},500);
			jQuery('#menu li:not(.hover)').not('#menu li ul li').stop(true,false).animate({
				opacity: 0.3
				},500);
			
			hidden_table=false;
		},function(){
			jQuery(this).removeClass('hover').stop(true,false).animate({
				opacity: 1 
				},500);
			jQuery('#menu li:not(.hover)').not('#menu li ul li').stop(true,false).animate({
				opacity: 1 
				},500);
                        select_menu=false;
			hidden_table=true;
			MyObj = jQuery(this).children('table');
			jQuery(MyObj).wait({
					msec:3000,
					onEnd:function(){
						table_mouse_over(MyObj);
					}
				    });
			});
	
	jQuery('#menu li a').not('#menu li ul li a').click(function(e){
		
		MyObj = jQuery(this).parent('li').children('table');
		//jQuery(MyObj).mousemove(e);
		
		MenuRePosition(jQuery('#menu'),MyObj,e);
		select_menu=true;
		jQuery('#menu li table').not(MyObj).stop(true,true).hide("fast",function(){
			jQuery(MyObj).stop(true,true).show('slow',function(){
				jQuery('#menu li table td:not(.hover) span').animate({
					opacity: 0.5
					},300);
				});
			
			});

		return false;
		});
	
	
	jQuery('#menu li table td').hover(function(){
		jQuery(this).addClass('hover').stop(true,false).animate({
			opacity: 1 
			},500);
		
		jQuery(this).children('a').children('span').stop(true,false).animate({
			opacity: 1
			},300);
		
		jQuery(this).children("a").children("img").stop(true,false).animate({
			opacity: 0
			},300);
	},function(){
		jQuery(this).removeClass('hover').stop(true,false).animate({
			opacity: 1 
			},500);

		jQuery(this).children('a').children('span').stop(true,false).animate({
			opacity: 0.5
			},300);

		jQuery(this).children("a").children("img").stop(true,false).animate({
			opacity: 1
			},300);
                
		});
	
	jQuery('#menu li table td a').click(function(){
		jQuery('#menu').stop(true,true).fadeOut("fast");
		img_src=jQuery(this).attr('href');
		jQuery("#preview").attr("src",img_src).stop(true,true).animate({
					opacity: 0.1
					},1000,function(){
						jQuery('#loading').fadeIn("slow");
						});
		return false;
		});
	});
