function getById(id)
{
	return document.all? document.all[id] : document.getElementById? document.getElementById(id) : null;
}

var oldElement              = null;         //Needed to track current highlighted element
var menuArray               = new Array();  //Tracks what divs are showing so it knows what to hide when a new span is clicked
var onTextColor             = "#000000";    //Text color with mouseover
var offTextColor            = "#000000";    //Text color with NO mouseover
var onCellColor             = "#d9d9d9";    //Span color with mouseover
var offCellColor            = "#ffffff";    //Span color with nomouseover
var offsetX                 = 0;            //For top level submenu positioning
var offsetY                 = 2;            //For top level submenu positioning
var subOffsetX              = 0;            //For submenu positioning
var subOffsetY              = 0;            //For submenu positioning
var started                 = false;        //Used to track if we just started the menus

var isAway                  = false;        //Used to track if we leave menu away for some time
var timer                   = null;         //
	
// Register onclick event so what if user clicks outside menu, the menus disappear.
document.onclick = hideAllMenus;
document.onkeydown = KeyFilter;

if( !window.opera && document.all && (document.documentElement || document.body) )	// if it is IE
	offsetY = 2;
else
	offsetY = 7;

function KeyFilter()
{
    //if (event.keyCode == 27) hideAllMenus();
}

function startIt(menu,thisItem,level) {                     //menu = menu to display,thisItem=coordinates of item to use,level=current depth of menus
    hideAllMenus();
    started = true;                                     //Lets us know we're coming in for the 1st time
    stateChange(menu,thisItem,level);
}

function UnFlip(eItem)
{
    eItem.flipped = false;
    var eImg = eItem.children["arrow"];
    if (eImg) {
        eImg.src = "images/right_arrow.gif";
    }
    var eParent = eItem.parentElement;
    eParent.flipped = null;
}

function UnFlipItem(eParent)
{
    var eItem = eParent.flipped;
    if (eItem) {
        UnFlip(eItem);
    }
    eParent.flipped = null;
}

function Flip(eItem)
{
    var eImg = eItem.children["arrow"];
    if (eImg) {
        eImg.src = "images/left_arrow.gif";
        var eParent = eItem.parentElement;
        eParent.flipped = eItem;
        eItem.flipped = true;
    }
}

function GetLeft(obj)
{
	var x = 0;
	var o = obj;
	while (o.offsetParent)
	{
		x += o.offsetLeft;
		o = o.offsetParent;
	}
	return x;
}
function GetTop(obj)
{
	var y = 0;
	var o = obj;
	while (o.offsetParent)
	{
		y += o.offsetTop;
		o = o.offsetParent;
	}
	return y;
}

function stateChange(menu,thisItem,level) {                                 //menu = menu to display,thisItem=name of span item to use,level=current depth of menus
    if(level == "-1")
    {
		isAway = true;
		timer = setTimeout("if(isAway) hideAllMenus();", 500);
    }
    else
    {
		isAway = false;
		clearTimeout(timer);
    }
         
    if(thisItem.className != "clsMenuBarCell")
    {
		 if (oldElement)
		 { 
			 oldElement.style.background = offCellColor;
			 oldElement.style.color = offTextColor;
             //if (oldElement.flipped) UnFlip(oldElement);
             oldElement = null;
		 }    
		//Turn on new item
		oldElement = thisItem;
		thisItem.style.background = onCellColor;
		thisItem.style.color = onTextColor;
    }

    if (menu != "") {
        eMenu = getById(menu);
        eItem = thisItem;
        hideMenu(level);
        menuArray[menuArray.length] = menu; // add to list of open menus.

        var cell = eItem.parentElement;
        var positionX = GetLeft(eItem);
        var left = positionX;
        var positionY = GetTop(eItem);      
        
        // if this is a submenu, add width of submenu item.
        if (eItem.className == "clsMenuItem") 
        {
			positionX += eItem.offsetWidth + subOffsetX;
            positionY += subOffsetY;
        }
        else if(eItem.className == "clsMenuBarCell")
        {
			positionY += eItem.offsetHeight + offsetY;
			positionX += offsetX;
        }
  
        eMenu.style.left = positionX + "px";
        eMenu.style.top = positionY; + "px";
        eMenu.style.display = "block";

        // Now that menu is visible check width (width is 0 initially)
        
        //If screen isn't wide enough to fit menu, then pop it up on the left side.
        if ((positionX + eMenu.offsetWidth - document.body.scrollLeft) >= document.body.clientWidth) {
            if (level == 0 || (left - eMenu.offsetWidth) < 0) {
                eMenu.style.left = document.body.clientWidth - eMenu.offsetWidth + document.body.scrollLeft;
            } else {
                eMenu.style.left = left - eMenu.offsetWidth;            
                Flip(eItem);
            }
        }
        //If the menu is too far to the left to display, bump it to the right some
        if (positionX <= document.body.scrollLeft) {
            eMenu.style.left = document.body.scrollLeft;
        }
        //If the menu is too far down, bump the menu up so the bottom equals the body clientHeight property
        if ((positionY + eMenu.offsetHeight) >= document.body.clientHeight) {
            if (started != true) 
                eMenu.style.top = document.body.clientHeight - eMenu.offsetHeight;
        }
    }
    
    started = false; //After 1st menu, turn of started variable
}

function Select(url, code)
{	
	//alert(code);
	if(code == null)
		return;
		
	var tt = code.split('/')
	if (tt[0] == "T")
		ChgPrama(tt[1])
	else if (tt[0] == "I")
		AddIndicators(tt[1])
	else if (tt[0] == "X")
		ChangeIndex(tt[1])
	else if (tt[0] == "M")
		changeMvg(tt[1])
	else if (tt[0] == "P")
		changeMvg(tt[1])
	else if (tt[0] == "C")
		changeComp(tt[1])
	//else if (tt[0] == "X")
	//	ChangeIndex(tt[1])
	else if (tt[0] == "SZ")
		changeSize(tt[1])
	else if (tt[0] == "F")
		changeFundamental(tt[1])
	else if (tt[0] == "EV")
		AddEvents(tt[1])
	else if (tt[0] == "O")
		changeOption(tt[1])
	else if (tt[0] == "CT")
		changeTypes(tt[1]) 
	else if (tt[0] == "OF")
		changeOFundamental(tt[1])
	else if (tt[0] == "TT")
		changeTotalReturn(tt[1])
	else if (tt[0] == "FV")
		changeStarRating(tt[1])
	else if (tt[0] == "GS")
		changeSize(tt[1])
	else if (tt[0] == "GL")
		changeScale(tt[1])
	else if (tt[0] == "GT")
		changeTypes(tt[1])
	else if (tt[0] == "RS")
		changeReset(tt[1])
	
	//event.returnValue = false;
}

function hideMenu(currentLevel) {
    for (var i = currentLevel; i < menuArray.length; i++) {
        if (menuArray[i] == null) continue;
        var eMenu = getById(menuArray[i]);
        if (eMenu) {
            UnFlipItem(eMenu);
            eMenu.style.display='none';
        }
    }
    menuArray.length = currentLevel;
}

function hideAllMenus() {   
    for (var i = 0; i < menuArray.length; i++) {
        var arrayString = new String(menuArray[i]);
        if (arrayString == "undefined") continue;
        var eMenu = getById(menuArray[i]);
         if(eMenu == null)
			continue;
        eMenu.style.display = "none";
        eMenu.style.left = 0;
        eMenu.style.top = 0;
        UnFlipItem(eMenu);
    }
    if (oldElement) { 
        oldElement.style.background = offCellColor;
        oldElement.style.color = offTextColor;
        oldElement = null;
        menuArray = new Array();    
        currentSpanElement = "";        
    }
}

