$(
	function(){
		
		// $('#IMG_load').bind('load', function(){
		// 	 	$("#close, #home, #feedback").css('overflow','hidden');	
		// 		$("#home").css('top','0');	
		// 		$("#feedback").css('margin-left','10px');
		// 		$("#feedback").css('top','0px');
		// 
		// 		 	// $("#close,#home,#feedback").css({'overflow':'hidden', 'white-space':'nowrap'} );		
		// 	}); 
		
		

		
		document.documentElement.className += '';
		/*
		 * ������� ������������� ����
		 * @param container {JQueryElement} ������� � ������� �������������
		 */ 
		function Menu( container ){
			if( container.length < 1 )
				return;
			
			var starTime = (new Date()).getTime();
				
			/*
			 * ���������� ������� ����
			 */
			function show( isFirst ){
				//���� ������������ ����� �� ����� ����, ��� ������ �������� ���� �� ������ ���� �������.
				if( !isFirst && ((new Date()).getTime() - starTime < 1000) ){
					startTime = -1;
					return false;
				}
				var that = $(this), popup = that.parent().find('.popup');

				if( timeouts[this.parentNode.id] ){
					clearTimeout(timeouts[this.parentNode.id]);
					timeouts[this.parentNode.id] = 0;
				}
				that.parent().addClass('hover');
				var clientWidth = isFixWidth ? Math.floor(getClientWidth()/2) : Math.round(getClientWidth()/2);
				var top = parseInt(popup.css('padding-top').replace('px',''));
				if( isFirst !== 1 )
					that.parent().removeClass('hide');
					
				that.next().removeClass('popup-last').css({
					overflow: 'hidden',
					height: $('#layout')[0].clientHeight - top
				}).parent().addClass('selected');
	
				that.next().find('.photo').css({
					left: -Math.round(that.next().find('img').width() / 2) + clientWidth - that.offset().left,
					top: $('#fon>img').offset().top + (that.parent().attr('id') == 'main' ? -7 : 65)
				});
				
	            /*
				if( this.parentNode.id == 'main' ){
					that.next().find('> li > ul').removeClass('hidden');
				}
	            */
				return false;
			}
	
			/*
			 * �������� ������� ����
			 */
			function hide(){
				var that = this;
				$(this).parent().removeClass('hover');
				if( timeouts[this.parentNode.id] ){
					clearTimeout(timeouts[this.parentNode.id]);
					timeouts[this.parentNode.id] = 0;
				}
	
				timeouts[this.parentNode.id] = setTimeout(function(){
					if( that.parentNode.id == 'main' ){
						$(that).parent().addClass('hide');
						/*
						$(that).next().find('> li > ul').each(
							function(){
								if( !$(this).find('.selected:first').length && !$(this).parent().find('>b.selected').length )
									$(this).addClass('hidden');	
							}
						)
						*/
					}
					else{
						$(that).next().css('overflow', 'hidden').css('height',0).addClass('popup-last');
	                    if( bodyId != that.parentNode.id + '_page' ){
							$(that).parent().removeClass('selected');
						}
					}
				},100);
			}
			
			/*
			 * ������� ������������ ��� ��������� �� ����� ����
			 */
			function popupOver(){
				if( $(this).parent().hasClass('hide') )
					return false;
	
				var element = $(this).prev()[0];
				if( timeouts[element.id] ){
					clearTimeout(timeouts[element.id]);
					timeouts[element.id] = 0;
				}
				else
					$(this).prev().mouseover();
			}
			
			/*
			 * ������ ������ ���������� ������� ��������
			 */
			function getClientWidth(){
				return document.compatMode=='CSS1Compat' && !window.opera?document.documentElement.clientWidth:document.body.clientWidth;
			}
	
			var bodyId = $('body:first').attr('id'), timeouts = {}, 
			isFixWidth = /(msie (6|5)|webkit|opera)/.test(navigator.userAgent.toLowerCase());
				
			$('#topmenu').find('>li>.nav').mouseover(show).mouseout(hide).next().addClass('popup-init').mouseover(popupOver).mouseout(
				function(){
					$(this).prev().mouseout();
				}
			).next().mouseover(popupOver).mouseout(
				function(){
					$(this).prev().mouseout();
				}
			);
			
			$('#logo > *').mouseover(
				function(){
					$('#main>.nav').mouseover();
				}
			).mouseout(
				function(){
					$('#main>.nav').mouseout();	
				}
			);
			show.apply($('#main>.nav')[0],[1]);
			hide.apply($('#main>.nav')[0]);
		}	
		
		Menu( $('#menu') );
		
		function changeStars(){
			starsContainer.css('background-image', 'url('+stars.src+')');
			stars_ie.show();
			
			setTimeout(
				function(){
					starsContainer.css('background-image', 'none');
					stars_ie.hide();
					setTimeout(changeStars, Math.round(7500 * Math.random()));
				},
				500
			);
		}
		
		

		// var
		// 			stars = new Image(),
		// 			starsContainer = $('#layout'),
		// 			stars_ie = $('<div class="png" />');
		// 		
		// 		if( !/msie 6/.test(navigator.userAgent.toLowerCase()) ){
		// 			$(stars).load(
		// 				function(){
		// 					changeStars();
		// 				}
		// 			);
		// 		} else {
		// 			stars.width = 1547;
		// 			stars.height = 1160;
		// 			
		// 			$(stars).load(function(){
		// 				starsContainer.prepend(stars_ie.css({position: 'absolute', width: 1547, height: 1160, background: 'url(' + stars.src + ')'}));
		// 				starsContainer.prepend(stars_ie.clone());
		// 				changeStars();
		// 			});
		// 		}
		// 
		// 			stars.src = '/f/1/global/stars-some.png';
	}
);

/*
 *  ��������� ��������� � ����������
 */
$(document).keydown(
	function(e){
		if(e.ctrlKey){
			var href = false;
			switch (e.keyCode ? e.keyCode : e.which ? e.which : null){
				case 0x25:
					href = $('#prevLink').attr('href');
				break;
				case 0x27:
					href = $('#nextLink').attr('href');
				break;
	       	    		case 0x26:
					href = $('#upLink').attr('href');
				break;
				case 0x28:
					href = $('#downLink').attr('href');
				break;
				case 0x24:
					href = $('#homeLink').attr('href');
				break;
			}
			if( href )
				document.location = href;
		}
	}
);
