var PMenu = Class.create();
PMenu.prototype = {
	initialize:function (vID)
	{
		this.myID = vID;
		this.myObj = $(vID);
		this.disabled = true;
		this.opacity = 0;
		this.TimerFade = null;
		this.TimerAppear = null;
		this.action = "";
		PopMenus.items[ this.myID ] = this;
		PopMenus.IDs.push( this.myID );


		vLinks = this.myObj.select( "a" );

		 this.myObj.observe( 'mouseout' , PopMenus.tryHideMenu.bindAsEventListener(this.myObj , vID ) );
		 this.myObj.observe( 'mouseover' , PopMenus.stopHide.bindAsEventListener(this.myObj , vID ) );

	},
	showMenu:function( posX, posY )
	{
		PopMenus.hideOtherMenus( this.myID );
		this.disabled = false;
		clearTimeout( this.TimerFade );

		this.myObj.style.top = posY + "px";
		this.myObj.style.left = posX + "px";

		this.startAppear()

	},

	startFade:function()
	{
		this.disabled = true;
		clearTimeout( this.TimerAppear );		
		this.fade();
	},
	fade:function()
	{
		if( this.disabled )
		{	
			this.opacity = this.opacity - 0.1
		
			if( this.opacity <= 0 )
				{this.opacity = 0;}


			this.myObj.style.MozOpacity = this.opacity;
			this.myObj.style.opacity = this.opacity;
			this.myObj.style.filter= ("alpha(opacity="+ parseInt(this.opacity*100)+")")
		
			if( this.opacity <= 0 )
			{
				this.myObj.style.display = "none";
				this.action = "";
			}else {
				this.TimerFade = setTimeout( "PopMenus.items['" + this.myID + "'].fade()" , 15);
			}
		} else {
			clearTimeout( this.TimerFade );
			this.startAppear();
		}
	},
	startAppear:function()
	{
		this.disabled = false;
		this.myObj.style.display = "block";
		PopMenus.hideOtherMenus( this.myID );
		this.action = "appear";
		clearTimeout( this.TimerFade ) ;
		this.appear();
	},
	appear:function(pe)
	{
		if( this.disabled == false)
		{		
			this.opacity = this.opacity + 0.05

			if( this.opacity >= 1 )
				{this.opacity = 1;}
		
			vOBJ = $(this.myID);
			vOBJ.style.display = "block";
			vOBJ.style.MozOpacity = this.opacity;
			vOBJ.style.opacity = this.opacity;
			vOBJ.style.filter= ("alpha(opacity="+ parseInt(this.opacity*100)+")")

			if( this.opacity >= 1 )
				{this.action = "";}			
			else {
				this.TimerAppear = setTimeout( "PopMenus.items['" + this.myID + "'].appear()" , 30);
			}
		} else {
			clearTimeout( this.TimerAppear ) ;
			this.startFade()
		} 
	}
}



PopMenus = {
	items:[],
	IDs: [],
	tryHideMenu: function( event, MenuID ) {

		PopMenus.items[MenuID].TimerFade = setTimeout( "PopMenus.items['" + MenuID + "'].startFade()" , 500);
	},
	stopHide: function(event, MenuID) {
		clearTimeout( PopMenus.items[MenuID].TimerFade );
		PopMenus.items[MenuID].startAppear();
	},
	hideOtherMenus: function ( vID )
	{
		for( i = 0 ; i < PopMenus.IDs.length ; i ++ )
		{
			if( PopMenus.IDs[ i ] != vID )
			{
				clearTimeout( PopMenus.items[PopMenus.IDs[ i ]].TimerAppear );
				PopMenus.items[PopMenus.IDs[ i ]].startFade();
			}
		}
	},
	showMenu: function ( event, MenuID ) {
		
		vCaller = Event.element( event )
		vSize = vCaller.getDimensions();
		vPosition = vCaller.cumulativeOffset();
		posY = vSize.height + vPosition.top + 10;
		posX = vPosition.left + -38;

		PopMenus.items[ MenuID ].showMenu( posX, posY )
	}
}

function configMenu() {
	$$( ".Popmenu" ).each( function( DOMMenu ) {
		x = new PMenu( DOMMenu.id )
		vSelector = "*[rel=" + DOMMenu.id + "]"
		$$( vSelector ).each( function ( DOMLink ) {

			DOMLink.observe( 'mouseover' , PopMenus.showMenu.bindAsEventListener( DOMLink, DOMMenu.id ) );
			DOMLink.observe( 'mouseout' , PopMenus.tryHideMenu.bindAsEventListener( DOMLink , DOMMenu.id ) );


		})
	})

}

document.observe( "dom:loaded" , configMenu );

