﻿// FoldierMenu.js File

/////////////////////////////////////////////////////////////////////////////////////////////////////////
// (c) 2007-2008 foldier inc.
// This software is property of foldier inc. Use or reporduction without permision is prohibited  
// Created by : Michele Ursino
/////////////////////////////////////////////////////////////////////////////////////////////////////////

function TestFoldierMenu()
{
    alert('foldierMenu imported');
}

var g_currentMenu = null;   // Current visible menu
var g_currentMenuButton = null;   // Current visible menu

// Constructor function
//
function FoldierMenu( htmlSpan )
{
    this.m_span = htmlSpan;
}

FoldierMenu.prototype.SetX = function( xPos )
{
    this.m_span.style.left = xPos+'px';
}
FoldierMenu.prototype.SetY = function( yPos )
{
    this.m_span.style.top = yPos+'px';
}

FoldierMenu.prototype.Hide = function( e )
{
	if ( g_currentMenuButton!=null )
	{
		g_currentMenuButton.className = "smallButtonMenu";
		g_currentMenuButton = null;
	}

    g_currentMenu.style.display = 'none';
    g_currentMenu = null;
}

FoldierMenu.prototype.HideIfOut = function( e, elName )
{
    if (!e) 
    {
        e = window.event;
    }
    var relTargetElem = (e.relatedTarget) ? e.relatedTarget : ( ( e.toElement )? e.toElement : null );
    var targetElem = e.target;

    if ( targetElem.id == elName && relTargetElem.id == elName) // Check if this is a real leave event
    { 
        this.m_span.style.display = 'none';
        g_currentMenu = null;
        
		if ( g_currentMenuButton!=null )
		{
			g_currentMenuButton.className = "smallButtonMenu";
			g_currentMenuButton = null;
		}
        
    }
}

FoldierMenu.prototype.SetPositionByElement = function( elementName, offX, offY )
{
	var x = 0 ,y = 0;
	var e = document.getElementById(elementName);
	while(e.offsetParent) {
		x += e.offsetLeft;
		y += e.offsetTop;
		e = e.offsetParent;
	}
	
	x += e.offsetLeft;
	y += e.offsetTop;

	this.m_span.style.left = (x+offX)+'px';
    this.m_span.style.top = (y+offY)+'px';
    //alert( "XPos = "+x + "    Offset = "+ offX );

}

FoldierMenu.prototype.ShowByButton = function( buttonElName, deltaX, deltaY )
{
	this.SetPositionByElement(buttonElName, -16, 18 );
	this.Show();
	this.m_span.focus();
	
	if ( g_currentMenu != null )
	{
		var btEl = document.getElementById(buttonElName);
		btEl.className = "smallButtonMenuSelected";
		g_currentMenuButton = btEl;
	}
}

FoldierMenu.prototype.Show = function()
{
    //if (!e) e = window.event;
    
    // Determine the position of the click
    //var x = e.clientX; 
    //var y = e.clientY; 
    //if ( window.pageXOffset )
    //{
    //   x += window.pageXOffset; 
    //   y += window.pageYOffset; 
    //}
    //else
    //{
    //    x += document.documentElement.scrollLeft; 
    //    y += document.documentElement.scrollTop ; 
    //}
    
    //alert( 'Y = '+y);
    
    if ( g_currentSFPopup!=null )
        g_currentSFPopup.Hide();

    if ( g_currentMenu != null )
    {
        g_currentMenu.style.display = 'none';
		if ( g_currentMenuButton!=null )
		{
			g_currentMenuButton.className = "smallButtonMenu";
			g_currentMenuButton = null;
		}
        
        if ( g_currentMenu ==  this.m_span )
        {
            g_currentMenu = null;
            return; // Click on the same button disable the existing menu
        }
    }
      
    //this.SetX( x );
    //this.SetY( y+4 );
    
    this.m_span.style.display = 'block';
    g_currentMenu = this.m_span;
    
    if ( g_currentMainPopup != null )
    {
        g_currentMainPopup.style.display = 'none';
        g_currentShadPopup.style.display = 'none';
        g_currentMainPopup = null;
        g_currentShadPopup = null;
    }
}
