//
// Набор функций, которые везде пригодятся.
// (ну или почти везде)
//

var aTextInputsVars						= new Object();
	aTextInputsVars["Login"]			= new Object();
	aTextInputsVars["Login"]["InputID"] = 'LoginInput';
	aTextInputsVars["Login"]["Text"]	= 'да свой я, не видишь, что ли?';
	//
	// Глобальный инит, инициализирущий все э-э.. модули. :)
	//
	function GlobalInput()
	{
//		InitTextInputs();
		InitDropMenu();
	}

	//
	// Инициализируем текстовые инпуты для поиска и для подписки
	//
	function InitTextInputs()
	{
		aTextInputsVars["Login"]["Input"]		= document.getElementById(aTextInputsVars["Login"]["InputID"]);
		if( aTextInputsVars["Login"]["Input"] )
		{
			aTextInputsVars["Login"]["Input"].onfocus	= function(){ CheckTextInput( true, this, aTextInputsVars["Login"]["Text"] ); return true }
			aTextInputsVars["Login"]["Input"].onblur		= function(){ CheckTextInput( false, this, aTextInputsVars["Login"]["Text"] ); return true }
			CheckTextInput( false, aTextInputsVars["Login"]["Input"], aTextInputsVars["Login"]["Text"] );
		}
	}
	
	//
	// Проверяем, содержит ли текстовый инпут ключевое слово,
	// или, может он (о, ужас!) вообще пустой?
	function CheckTextInput( bFocus, oInput, sString )
	{
		if( oInput.value == '' || oInput.value == sString )
		{
			oInput.value		= ( bFocus ) ? '' : sString;
			oInput.className	= ( bFocus ) ? '' : 'NotAnySymbol';
		} else if( !bFocus )
			oInput.className = '';
	}


// Несколько переменных для выпадающего меню.
	var sShowedID		= null;

	var MenuItem = Class.create();
		MenuItem.prototype = {
			initialize: function( oNode, iNumber )
			{
				var self = this;
					this.ID			= 'ID' + iNumber;
					this.oLink		= oNode;
					this.oImg		= this.oLink.getElementsByTagName('img')[0];
					this.oSubMenu	= this.oLink.getElementsByTagName('ul')[0];
					if( this.oImg.className == 'Selected' )
					{
						this.bAlreadyParent = true;
						this.sImgHighlight	= this.oImg.getAttribute('src');
						this.sImgShadow		= this.sImgHighlight.substr( 0,  this.sImgHighlight.length - 6 ) + ".gif";
					} else {
						this.sImgShadow		= this.oImg.getAttribute('src');
						this.sImgHighlight	= this.sImgShadow.substr( 0,  this.sImgShadow.length - 4 ) + "_a.gif";
					}
					if( this.oSubMenu )
					{
						this.oSubMenu.style.display		= 'none';
						this.oSubMenu.style.visibility	= 'visible';
						this.oLink.onmouseover = function(){
							self.HighlightMe();
							self.ShowMe();
							return true;
						}
					} else {
						this.oLink.onmouseover = function(){
							self.HighlightMe();
							return true;
						}
						this.oLink.onmouseout = function(){
							self.ShadowMe();
							return true;
						}
					}
					this.bIsDelayHidingOn	= false;	// запущен ли процесс скрытия с задержкой (для onmouseout)
					this.oHidingTimer		= null;		// таймер для задержки скрывания меню при onmouseout
					this.iDelayCountDown	= 0;		// счётчик для таймера
			},
			HighlightMe: function()
			{
				if( !this.bAlreadyParent )
					this.oImg.setAttribute('src', this.sImgHighlight);
			},
			ShadowMe: function()
			{
				if( !this.bAlreadyParent )
					this.oImg.setAttribute('src', this.sImgShadow);
			},
			ShowMe: function() 
			{
				
				var self = this;
				this.bIsDelayHidingOn	= false;
				if( sShowedID )
					aMenuItems[sShowedID].ImmediatelyHideMe();

				this.oLink.onmouseover		= function(){
					self.HighlightMe();
					self.bIsDelayHidingOn = false;
					return true;
				}
				this.oSubMenu.onmouseover	= function(){
					self.ShadowMe();
					self.bIsDelayHidingOn = false;
					return true;
				}
				this.oLink.onmouseout		= function(){
					self.ShadowMe();
					self.TryToHideMe();
					return true;
				}
				this.oSubMenu.onmouseout	= function(){ self.TryToHideMe(); return true; }

				new Effect.Appear( self.oSubMenu, {duration: 0.2});
				sShowedID				= this.ID;
			},
			TryToHideMe: function() 
			{
				if( sShowedID && !this.bIsDelayHidingOn ){
					if( this.oHidingTimer ){
						clearTimeout( this.oHidingTimer );
						this.oHidingTimer = null;
					}
					this.bIsDelayHidingOn = true;
					this.iDelayCountDown = 0;
					this.HideMe();
				}
				var self = this;
			},
			HideMe: function()
			{
				var self = this;
				if( this.bIsDelayHidingOn )
				{
					if( this.iDelayCountDown == 6 ){
						clearTimeout( this.oHidingTimer );
						this.ImmediatelyHideMe();
					} else {
						this.iDelayCountDown++;
						this.oHidingTimer = setTimeout(function(){ self.HideMe(); return false;}, 70);
					}
				} else {
					clearTimeout( this.oHidingTimer );
					this.iDelayCountDown = 0;
					this.oHidingTimer = null;
				}
			},
			ImmediatelyHideMe: function()
			{
				var self = this;
				this.oHidingTimer = null;
				this.bIsDelayHidingOn = false;
				this.iDelayCountDown = 0;
				sShowedID	= null;

				new Effect.Fade( self.oSubMenu, {duration: 0.2});
				this.oLink.onmouseover	= function(){
					self.HighlightMe();
					self.ShowMe();
					return true;
				}
				this.oSubMenu.onmouseover	= function(){ return true; }
				this.oLink.onmouseout		= function(){ return true; }
				this.oSubMenu.onmouseout	= function(){ return true; }
			}
		}


	//
	// Выпадающее меню
	//
	function InitDropMenu()
	{
		oMainMenuContainer	= $('MenuMain');
		var aTemp	= $A( oMainMenuContainer.childNodes );
		var iTemp	= 1;
		aMenuItems	= {};
		
		aTemp.each( function(oNode){
			if( oNode.nodeName == 'LI' )
			{
				aMenuItems["ID" + iTemp] = new MenuItem(oNode, iTemp);
				iTemp++;
			}
		});
	}


// Вешаем на window.onload инициализацию. 
// тут надо быть внимательными с перекрыванием этого события.
window.onload = GlobalInput;

