
function popupItemDef( text, action, enabled, render, childPopup )
{
    this.text       = text;
    this.action     = action;
    this.enabled    = enabled;
    this.render     = render;
    this.childPopup = childPopup;
}

function popupDef( name, items, width, render )
{
    this.name       = name;
    this.items      = items;
    this.width      = width;
    this.render     = render;
}

function hideElement( element )
{
    if( element.style === undefined )
    {
        alert( element.tagName );
    }
    else
    {
        if( element.style.display != "none" )
        {
            element.style.display = "none";
        }
    }
}

function showElement( element )
{
    if( element.style.display != "" )
    {
        element.style.display = "";
    }
}

function isDisplayed( element )
{
    var result = false;
    if( element.style.display != "none" )
    {
        result = true;
    }
    return result;
}
/*
function showPopup( popup, x, y )
{
    setElementXY( popup, x, y )
    showElement( popup );
    showDropShadow( popup );
    return false;
}
*/
function getX( element, x )
{
    if( x == null )
    {
        x = 0;
    }
    x += element.offsetLeft;

    if( element != document.body )
    {
        parentOffset = element.offsetParent;
        return getX( parentOffset, x );
    }
    return x;
}

function getY( element, y )
{
    if( y == null )
    {
        y = 0;
    }
    y += element.offsetTop;

    if( element != document.body )
    {
        parentOffset = element.offsetParent;
        return getY( parentOffset, y );
    }
    return y;
}

function setElementXY( element, x, y )
{
    element.style.posLeft = x;
    element.style.posTop = y;
}

function renderPopupShadow( popup, popupWidth, numberOfItems )
{
    var table = document.createElement( "TABLE" );
    table.className = "shadowBlur";
    table.style.position   = "absolute";
    table.style.zIndex     = 1;
    table.style.display    = "none";

    table.border           = "0";
    table.cellpadding      = "0";
    table.cellspacing      = "0";

    var row = document.createElement( "TR" );
    table.appendChild( row );
    row.height = "34";

    var cell = document.createElement( "TD" );
    cell.className  = "popupTopLeftMask";
    cell.width  = "35";
    row.appendChild( cell );

    cell = document.createElement( "TD" );
    cell.className = "popupTopCenterMask";
    cell.width = popupWidth;
    row.appendChild( cell );

    cell = document.createElement( "TD" );
    cell.className = "popupTopRightMask";
    cell.width = "24";
    row.appendChild( cell );

    for( var i = 0; i < numberOfItems; i++ )
    {
        var row = document.createElement( "TR" );
        table.appendChild( row );
        row.height = "24";

        var cell = document.createElement( "TD" );
        cell.className  = "popupMiddleLeftMask";
        row.appendChild( cell );

        cell = document.createElement( "TD" );
        cell.className = "popupMiddleCenterMask";
        row.appendChild( cell );

        cell = document.createElement( "TD" );
        cell.className = "popupMiddleRightMask";
        row.appendChild( cell );
    }

    var row = document.createElement( "TR" );
    table.appendChild( row );
    row.height = "34";

    var cell = document.createElement( "TD" );
    cell.className  = "popupBottomLeftMask";
    row.appendChild( cell );

    cell = document.createElement( "TD" );
    cell.className = "popupBottomCenterMask";
    row.appendChild( cell );

    cell = document.createElement( "TD" );
    cell.className = "popupBottomRightMask";
    row.appendChild( cell );

    return table;
}

function renderPopup( popup, popupWidth, x, y, level )
{
// alert( 'renderPopup:'+popup.name+' width:'+popupWidth+' x:'+x+' y:'+y );
    var newPopup = document.createElement( "DIV" );

    newPopup.id             = popup.name+"Div";
    newPopup.style.display  = "none";
    newPopup.style.zIndex   = 900+level;

    newPopup.setAttribute( "shadow", popup.name+"Shadow" );
//    newPopup.setAttribute( "level", level );

    var popupItems = popup.items;
    if( popupItems != null )
    {
        var itemX = x;
        var itemY = y;

        for( var i = 0; i < popupItems.length; i++ )
        {
            var popupItem = popupItems[i];
            var newItemTable = popupItem.render( popupItem, popupWidth );
            setElementXY( newItemTable, itemX, itemY );
            newPopup.appendChild( newItemTable );
            if( popupItem.childPopup != null )
            {
//                renderPopup( popupItem.childPopup, popupItem.childPopup.width, x-(popupItem.childPopup.width+58), itemY-8, level+1 );
                renderPopup( popupItem.childPopup, popupItem.childPopup.width, x+(popupWidth+58), itemY-8, level+1 );
            }
            itemX += 0;
            itemY += eval(newItemTable.height);
        }
    }

    document.write( newPopup.outerHTML  );
    var popupShadow = renderPopupShadow( popup, popupWidth, popupItems.length-2 );
    popupShadow.setAttribute( "id", popup.name+"Shadow" );

    popupShadow.style.pixelLeft = x-6;
    popupShadow.style.pixelTop  = y-6;

    document.write( popupShadow.outerHTML  );
}

function createPopupItemTable( childPopup )
{
    var popupTable = document.createElement( "TABLE" );

    popupTable.border           = "0";
    popupTable.cellpadding      = "0";
    popupTable.cellspacing      = "0";
    popupTable.style.position   = "absolute";
    popupTable.style.zIndex     = 100;

//  popupTable.style.tableLayout = "fixed";

    if( childPopup != null )
    {
        popupTable.setAttribute( "childPopup", childPopup.name+'Div' );
    }

    return popupTable;
}

function renderPopupTopItem( popupItem, popupWidth )
{
    var popupTable = createPopupItemTable( popupItem.childPopup );

    popupTable.onmouseover = "hideSiblingPopups(this); showChildPopup(this); changePopupClassName(this,'Top','Hover');";
    popupTable.onmouseout  = "changePopupClassName(this,'Top','Normal');";
    popupTable.onclick     = popupItem.action;
    popupTable.height = "34";

    var row = document.createElement( "TR" );
    row.height = "34";
    row.setAttribute( "popupClass", "Top" );

    var cell = document.createElement( "TD" );
    cell.className      = "popupTopLeftNormal";
    cell.width          = "35";
    row.appendChild( cell );

    cell = document.createElement( "TD" );
    cell.className      = "popupTopCenterNormal";
    cell.style.paddingTop = "10px";
    cell.width          = popupWidth;
    cell.innerHTML = popupItem.text;
    row.appendChild( cell );

    cell = document.createElement( "TD" );
    cell.className      = "popupTopRightNormal";
    cell.width          = "24";
    row.appendChild( cell );

    popupTable.appendChild( row );
    return popupTable;
}

function renderPopupMiddleItem( popupItem, popupWidth )
{
    var popupTable = createPopupItemTable( popupItem.childPopup );

    popupTable.onmouseover = "hideSiblingPopups(this); showChildPopup(this); changePopupClassName(this,'Middle','Hover');";
    popupTable.onmouseout  = "changePopupClassName(this,'Middle','Normal');";
    popupTable.onclick     = popupItem.action;
    popupTable.height = "24";

    var row = document.createElement( "TR" );
    row.height = "24";
    row.setAttribute( "popupClass", "Middle" );

    var cell = document.createElement( "TD" );
    cell.className      = "popupMiddleLeftNormal";
    cell.width          = "35";
    row.appendChild( cell );

    cell = document.createElement( "TD" );
    cell.className      = "popupMiddleCenterNormal";
    cell.style.paddingBottom = "2px";
    cell.width          = popupWidth;
    cell.innerHTML = popupItem.text;
    row.appendChild( cell );

    cell = document.createElement( "TD" );
    cell.className      = "popupMiddleRightNormal";
    cell.width          = "24";
    row.appendChild( cell );

    popupTable.appendChild( row );
    return popupTable;
}

function renderPopupBottomItem( popupItem, popupWidth )
{
    var popupTable = createPopupItemTable( popupItem.childPopup );

    popupTable.onmouseover = "hideSiblingPopups(this); showChildPopup(this); changePopupClassName(this,'Bottom','Hover');";
    popupTable.onmouseout  = "changePopupClassName(this,'Bottom','Normal');";
    popupTable.onclick     = popupItem.action;
    popupTable.height = "33";

    var row = document.createElement( "TR" );
    row.height = "33";
    row.setAttribute( "popupClass", "Bottom" );

    var cell = document.createElement( "TD" );
    cell.className      = "popupBottomLeftNormal";
    cell.width          = "35";
    row.appendChild( cell );

    cell = document.createElement( "TD" );
    cell.className      = "popupBottomCenterNormal";
    cell.style.paddingBottom = "12px";
    cell.width          = popupWidth;
    cell.innerHTML = popupItem.text;
    row.appendChild( cell );

    cell = document.createElement( "TD" );
    cell.className      = "popupBottomRightNormal";
    cell.width          = "24";
    row.appendChild( cell );

    popupTable.appendChild( row );
    return popupTable;
}

function showChildPopup( itemTable )
{
    var childPopupID = itemTable.childPopup;
    if( childPopupID != null)
    {
        var childPopup = popupDiv.children[childPopupID]

        if( childPopup != null)
        {
            showElement( childPopup );
            showDropShadow( childPopup );
        }
    }
}

function hideSiblingPopups( itemTable )
{
    var parentDiv = itemTable.parentElement;
    for( var i = 0; i < parentDiv.children.length; i++ )
    {
        var childPopupID  = parentDiv.children[i].childPopup;
        if( childPopupID != null)
        {
            var childPopup = popupDiv.children[childPopupID]
            if( childPopup != null)
            {
                hideElement( childPopup );
                hideDropShadow( childPopup );
                hideSiblingPopups( childPopup.children[0] );
            }
        }
    }
}

function showDropShadow( element )
{
    var shadow = document.getElementById( element.shadow )
    if( shadow != null )
    {
        showElement( shadow );
    }
}

function hideDropShadow( element )
{
    var shadow = document.getElementById( element.shadow )
    if( shadow != null )
    {
        hideElement( shadow );
    }
}

function renderNothing()
{
    return document.createElement("SPAN");
}


function hideTopLevel( element )
{
    for( var i = 0; i < element.children.length; i++ )
    {
        var child = element.children[i];
        var popup = element.getAttribute( "popup" );
        if( popup == "true" )
        {
            hideElement( element );
            hideDropShadow( element );
        }
        else
        {
            var popupitem = element.getAttribute( "popupitem" );
            if( popupitem == "true" )
            {
                hideElement( element );
                hideDropShadow( element );
            }
        }
        hideTopLevel( child );
    }
}

function renderMainItem( popupItem, popupWidth )
{
    var popupTable = createPopupItemTable( popupItem.childPopup );

    popupTable.onmouseout       = "changeButtonClassName(this,'Normal');"
    popupTable.onmouseup        = "changeButtonClassName(this,'Hover');"
    popupTable.onmousedown      = "changeButtonClassName(this,'Click');"
    popupTable.onmouseover      = "hideSiblingPopups(this); showChildPopup(this); changeButtonClassName(this,'Hover');";
    popupTable.onclick          = popupItem.action;
    popupTable.height = "26";
    popupTable.style.zIndex     = 0;

    var row = document.createElement( "TR" );
    row.height = "26";

    var cell = document.createElement( "TD" );
    cell.className      = "buttonLeftNormal";
    cell.width          = "28";
    row.appendChild( cell );

    cell = document.createElement( "TD" );
    cell.className      = "buttonCenterNormal";
    cell.style.paddingTop = "0px";
    cell.innerHTML = popupItem.text;
    row.appendChild( cell );

    cell = document.createElement( "TD" );
    cell.className      = "buttonRightNormal";
    cell.width          = "17";
    row.appendChild( cell );

    popupTable.appendChild( row );
    return popupTable;
}

function renderMainPopup( popup, popupWidth, x, y, level )
{
    var newPopup = document.createElement( "DIV" );

    newPopup.id             = popup.name+"Div";
    newPopup.style.display  = "";

//    newPopup.setAttribute( "level", level );

    var popupItems = popup.items;
    if( popupItems != null )
    {
        var itemX = x;
        var itemY = y;

        for( var i = 0; i < popupItems.length; i++ )
        {
            var popupItem = popupItems[i];
            var newItemTable = popupItem.render( popupItem, popupWidth );
            setElementXY( newItemTable, itemX, itemY );
            newPopup.appendChild( newItemTable );
            if( popupItem.childPopup != null )
            {
 //               popupItem.childPopup.render( popupItem.childPopup, popupItem.childPopup.width, itemX-10, y+22, level+1 );
            }
            itemX += 90;
// alert( ' x:'+itemX+' y:'+itemY+' width:'+eval(newItemTable.Width) );
        }
    }

    document.write( newPopup.outerHTML  );
//    alert( newPopup.outerHTML );
}


function renderHomePopup( popup, popupWidth, x, y, level )
{
    var newPopup = document.createElement( "DIV" );

    newPopup.id             = popup.name+"Div";
    newPopup.style.display  = "";

    var popupItems = popup.items;
    if( popupItems != null )
    {
        var itemX = x;
        var itemY = y;

        for( var i = 0; i < popupItems.length; i++ )
        {
            var popupItem = popupItems[i];
            var newItemTable = popupItem.render( popupItem, popupWidth );
            setElementXY( newItemTable, itemX, itemY );
            newPopup.appendChild( newItemTable );
            if( popupItem.childPopup != null )
            {
                popupItem.childPopup.render( popupItem.childPopup, popupItem.childPopup.width, itemX+100, itemY, level+1 );
            }
            itemY += 40;
        }
    }

    document.write( newPopup.outerHTML  );
}


function movedMouse( evt )
{
//		        if( !popupDiv.contains( event.srcElement ) && !rootPopupTable.contains( event.srcElement )   )
	if( !popupDiv.contains( event.srcElement ) )
	{
		for( var i = popupDiv.children.length-1; i >= 1; i-- )
		{
		    if( popupDiv.children[i].id != "rootPopupDiv" )
		    {
		        hideElement( popupDiv.children[i] );
		    }
		}
	}
}


function pageLoaded()
{
	document.body.onmousemove = movedMouse;

	// Space out main menu evenly

	for( var i = 1; i <  rootPopupDiv.children.length; i++ )
	{
		var siblingWidth = rootPopupDiv.children[i-1].offsetWidth;
		var siblingX     = rootPopupDiv.children[i-1].offsetLeft;
        rootPopupDiv.children[i].style.posLeft = siblingX+siblingWidth;
	}
}

function changePopupClassName( table, prefix, suffix )
{
	table.children[0].children[0].children[0].className = 'popup'+prefix+'Left'+suffix;
	table.children[0].children[0].children[1].className = 'popup'+prefix+'Center'+suffix;
	table.children[0].children[0].children[2].className = 'popup'+prefix+'Right'+suffix;
}

function changeButtonClassName( table, suffix )
{
// alert( row.children[0].children[0].tagName );
	table.children[0].children[0].children[0].className = 'buttonLeft'+suffix;
	table.children[0].children[0].children[1].className = 'buttonCenter'+suffix;
	table.children[0].children[0].children[2].className = 'buttonRight'+suffix;
}


/*
function addPopup( popup, source )
{
    var oldDiv = document.getElementById( popup.name+"Div" );

    if( oldDiv == null )
    {
		var popupMenu = renderPopup(popup, getX( source )-240, getY( source )-2 );
		popupDiv.innerHTML = popupMenu.outerHTML;
//   			    popupDiv.appendChild( renderPopup(popup, source) );
	}
}
*/