//  BlaqMenus
//  v 1.0
//  author: Blake Anderson
//  7/19/05
//  copyright 2005 Blaqware Productions

var MaxMenus = 10
var Menus = new Array(MaxMenus)
NumberOfMenus = 0
var visibleMenu = null

//BlaqMenu Object
function BlaqMenu( id, isVertical, numOfElements, BackgroundColor, OuterBorder, InnerBorder )
{
    if( NumberOfMenus >= MaxMenus )
    {
	alert( "You may only have " + MaxMenus + " menus on a page with this version of BlaqMenus!" )
	return
    }
    this.id = id+".BlaqMenu";
    this.tableID = id;
    this.isVertical = isVertical;
    this.numOfElements = numOfElements;
    this.BackgroundColor = BackgroundColor;
    this.TitleText = new Array( numOfElements );
    this.TitleBGImage = new Array( numOfElements );
    this.TitleBGColor = new Array( numOfElements );
    this.TitleBGImageOver = new Array( numOfElements );
    this.TitleBGColorOver = new Array( numOfElements );
    this.TitleWidth = new Array( numOfElements );
    this.TitleHeight = 25;
    this.SubMenus = new Array( numOfElements );
    this.MenuRef = NumberOfMenus
    this.Link = new Array( numOfElements );
    this.align = ""
    this.x = ""
    this.y = ""
    this.FromLeft = 0
    this.FromRight = 0
    this.XSubMenuTweak = 0
    this.YSubMenuTweak = 0
    this.SwapTitle = -1
    this.outerBorder = OuterBorder
    this.innerBorder = InnerBorder
    this.textColor = new Array( numOfElements );
    this.textColorOver = new Array( numOfElements );
    
    Menus[NumberOfMenus]=this
    NumberOfMenus++;

    document.body.style.overflow = "scroll"
}

//BlaqSubMenu Object
function BlaqSubMenu( numOfElements, BGColor, BGImage, BGColorOver, BGImageOver )
{
    this.Text = new Array( numOfElements )
    this.tableID = ""
    this.Link = new Array( numOfElements )
    this.BGColor = BGColor
    this.BGImage = BGImage
    this.BGColorOver = BGColorOver
    this.BGImageOver = BGImageOver
}

function setBlaqMenuTitle( aBlaqMenu, Element, Text, TitleWidth, TitleHeight, BGColor, BGImage, BGColorOver, BGImageOver, Link )
{
    Element -= 1;
    
    preload = new Image( TitleWidth, TitleHeight )
    preload.src = BGImage
    preloadOver = new Image( TitleWidth, TitleHeight )
    preloadOver.src = BGImageOver
    
    aBlaqMenu.TitleText[Element]=Text
    aBlaqMenu.TitleBGImage[Element]=BGImage
    aBlaqMenu.TitleBGColor[Element]=BGColor
    aBlaqMenu.TitleBGColorOver[Element]=BGColorOver
    aBlaqMenu.TitleBGImageOver[Element]=BGImageOver
    
    TitleWidth-=2;
    
    aBlaqMenu.TitleWidth[Element]= TitleWidth
    TitleHeight+=aBlaqMenu.innerBorder*2+aBlaqMenu.outerBorder*2
    aBlaqMenu.TitleHeight= TitleHeight
    aBlaqMenu.Link[Element] = Link
}

function hideVisibleMenu( )
{
    if( visibleMenu )
    {
	visibleMenu.style.visibility = 'hidden'
    }
}

function showVisibleMenu()
{
    if( visibleMenu )
    {
	    visibleMenu.style.visibility = 'visible'
	    visibleMenu.onmouseout = new Function( 'hideVisibleMenu()' )
    }
}

function setBlaqMenuSubMenu( )
{
    var a = arguments
    aBlaqMenu = a[0];	Element = a[1];	width = a[2];	    itemHeight= a[3]; TextColor=a[4]; TextColorOver=a[5]; BGColor = a[6]; BGImage = a[7]; BGColorOver = a[8]; BGImageOver = a[9]
    
    Element -= 1;
    
    if( BGImage == null || BGImage == "" )
	BGImage = "''"
    if( BGImageOver == null || BGImageOver == ""  )
	BGImageOver = "''"
	    
    preload = new Image( width, itemHeight )
    preload.src = BGImage
    preloadOver = new Image( width, itemHeight )
    preloadOver.src = BGImageOver
    
    aBlaqMenu.textColor[Element]=TextColor
    aBlaqMenu.textColorOver[Element]=TextColorOver
	    
    aBlaqMenu.SubMenus[Element] = new BlaqSubMenu( (a.length-10)/2, BGColor, BGImage, BGColorOver, BGImageOver )
	
	name = aBlaqMenu.MenuRef+".BlaqSubMenu"+Element
    aBlaqMenu.SubMenus[Element].tableID = name
    document.write( "<table height="+(itemHeight*((a.length-10)/2))+" class=BlaqMenu id="+name+" bgcolor="+aBlaqMenu.SubMenus[Element].BGColor+" cellspacing='0' background="+aBlaqMenu.SubMenus[Element].BGImage+"onSelectStart='return false' style='color: "+aBlaqMenu.textColor[Element]+"; position: absolute; cursor:default; visibility: hidden'" +" width="+width+" onMouseOver='showVisibleMenu()' " + ">" )

    for( x=0; x<(a.length-10)/2; x++ )
    {
	aBlaqMenu.SubMenus[Element].Text[x]=a[2*x+10]
	aBlaqMenu.SubMenus[Element].Link[x]=a[2*x+11]
	
	document.write( "<tr>" )
	document.write( "<td class=BlaqSubMenuCell id="+name+".BlaqSubCell"+x+" onClick='goToFromSub("+'this.id'+")' "+" onMouseOver='mouseInSub("+'this.id'+")' "+" onMouseOut='mouseOutSub("+'this.id'+")' "+" >" )
	document.write( aBlaqMenu.SubMenus[Element].Text[x] )
	document.write( "</td></tr>" )
    }
    document.write( "</table>" )
}

function goToFromTitle( id )
{
    temp = id.indexOf( ".BlaqMenu")
    MenuRef = id.substr(0,temp)
    currentMenu = Menus[MenuRef]
    CellRef = id.substr(temp+9,id.length )
    
    if( currentMenu.Link[CellRef] )
    window.location = currentMenu.Link[CellRef]
}

function goToFromSub( id )
{
    temp = id.indexOf( ".BlaqSubMenu")
    tempb = id.indexOf( ".BlaqSubCell" )
    MenuRef = id.substr(0,temp)
    SubRef = id.substr( temp+12, tempb-(temp+12) )
    CellRef = id.substr( tempb+12, id.length-(tempb+12) )

    currentMenu = Menus[MenuRef]
    if( currentMenu.SubMenus[SubRef].Link[CellRef] )
    window.location = currentMenu.SubMenus[SubRef].Link[CellRef]
}

function mouseInSub( id )
{
    temp = id.indexOf( ".BlaqSubMenu")
    tempb = id.indexOf( ".BlaqSubCell" )
    MenuRef = id.substr(0,temp)
    SubRef = id.substr( temp+12, tempb-(temp+12) )
    CellRef = id.substr( tempb+12, id.length-(tempb+12) )
    
    currentMenu = Menus[MenuRef]
    TheCell =  document.getElementById( id )
    
    TheCell.style.color = currentMenu.textColorOver[SubRef]
    TheCell.style.backgroundColor = currentMenu.SubMenus[SubRef].BGColorOver	
    newImage = "url(" + currentMenu.SubMenus[SubRef].BGImageOver + ")"
    TheCell.style.backgroundImage = newImage
}

function mouseOutSub( id )
{
    temp = id.indexOf( ".BlaqSubMenu")
    tempb = id.indexOf( ".BlaqSubCell" )
    MenuRef = id.substr(0,temp)
    SubRef = id.substr( temp+12, tempb-(temp+12) )
    CellRef = id.substr( tempb+12, id.length-(tempb+12) )
    
    currentMenu = Menus[MenuRef]
    TheCell =  document.getElementById( id )
    
    TheCell.style.color = currentMenu.textColor[SubRef]
    TheCell.style.backgroundColor = currentMenu.SubMenus[SubRef].BGColor	
    newImage = "url(" + currentMenu.SubMenus[SubRef].BGImage + ")"
    TheCell.style.backgroundImage = newImage
}

function doResize()
{
    for( x=0; x<NumberOfMenus; x++ )
    {
	aBlaqMenu = Menus[x]
	
	xPos=aBlaqMenu.x
	yPos=aBlaqMenu.y
	
	    //add up width or height of all cells
	    MenuBarWidth=aBlaqMenu.outerBorder*2 + aBlaqMenu.innerBorder
	    for( y=0; y<aBlaqMenu.numOfElements; y++ )
	    {
		if( aBlaqMenu.isVertical )
		{
		    if( aBlaqMenu.TitleWidth[y] > MenuBarWidth )
			MenuBarWidth=aBlaqMenu.TitleWidth[y]
		}
		else //isNotVertical
		{
		    MenuBarWidth+= aBlaqMenu.TitleWidth[y]
		}
		    
		MenuBarWidth+=aBlaqMenu.innerBorder //*2
	    }
		
	    ScreenWidth = document.body.clientWidth
		
	    //adjust for scrollbar
	    ScrollBias = -5;
	    if( navigator.appVersion.indexOf( "MSIE" )!=-1 )
	    {
		if( navigator.userAgent.toLowerCase().indexOf("mac")==-1 )
		    ScrollBias= -5;
		else
		    ScrollBias=-7;
	    }
	    ScreenWidth += 2*ScrollBias
	
	if( aBlaqMenu.alignment == "center" || aBlaqMenu.alignment == "right" )
	{
	    if( aBlaqMenu.alignment=="center" )
	    {
		if( ScreenWidth-MenuBarWidth >= 2*aBlaqMenu.FromLeft )
		    xPos += (ScreenWidth-MenuBarWidth)/2;
		else
		    xPos = aBlaqMenu.FromLeft
	    }
	    
	    if( aBlaqMenu.alignment=="right" )
	    {
		if( ScreenWidth - MenuBarWidth - aBlaqMenu.FromRight > aBlaqMenu.FromLeft )
		    xPos = ScreenWidth - MenuBarWidth - aBlaqMenu.FromRight
		else
		 xPos = aBlaqMenu.FromLeft
	    }
	}
	else
	{
	    if( aBlaqMenu.FromLeft )
		xPos = aBlaqMenu.FromLeft;
	}
	    
	document.getElementById( aBlaqMenu.tableID ).style.left = xPos
	document.getElementById( aBlaqMenu.tableID ).style.top = yPos
	
	MoveOver = aBlaqMenu.outerBorder
	for( z=0;z<aBlaqMenu.numOfElements; z++ )
	{
	    if( aBlaqMenu.SubMenus[z] )
	    {
		document.getElementById( aBlaqMenu.SubMenus[z].tableID ).style.left = xPos+MoveOver+aBlaqMenu.XSubMenuTweak;
		document.getElementById( aBlaqMenu.SubMenus[z].tableID ).style.top = yPos+aBlaqMenu.YSubMenuTweak;
	    }
	    
	    if( aBlaqMenu.TitleWidth[z] )
		MoveOver += aBlaqMenu.TitleWidth[z]+aBlaqMenu.innerBorder*2+2
	}
    }
}

function drawBlaqMenu( aBlaqMenu, xPos, yPos, XSubMenuTweak, YSubMenuTweak, align, StopFromLeft, StopFromRight )
{
    document.write( "<table height="+aBlaqMenu.TitleHeight+" class=BlaqMenuBar id="+aBlaqMenu.tableID+" cellspacing='0' bgcolor="+aBlaqMenu.BackgroundColor+" style='position: absolute; cursor: default' >" )
    if( !aBlaqMenu.isVertical )
	document.write( "<tr>" )
    for( x=0;x<aBlaqMenu.numOfElements;x++ )
    {
	name = aBlaqMenu.MenuRef+".BlaqMenu"+x
	
	if( aBlaqMenu.isVertical )
	    document.write( "<tr>" )
	document.write( "<td class=BlaqMenuBarCell id="+name+" background="+aBlaqMenu.TitleBGImage[x]+" bgcolor="+aBlaqMenu.TitleBGColor[x]+" onClick='goToFromTitle("+'this.id'+")' "+" onMouseOver='mouseInTitle("+'this.id'+")' "+" onMouseOut='mouseOutTitle("+'this.id'+")' "+" width="+aBlaqMenu.TitleWidth[x] +" >" )
	document.write( "<p onSelectStart='return false'>"+aBlaqMenu.TitleText[x]+"</p>" )
	document.write( "</td>")
	if( aBlaqMenu.isVertical )
	    document.write( "</tr>" )
    }
	if( !aBlaqMenu.isVertical )
	    document.write( "</tr>" )
    document.write( "</table>" )
    
    if( align )
	aBlaqMenu.alignment = align
    if( StopFromLeft )
	aBlaqMenu.FromLeft = StopFromLeft 
    if( StopFromRight )
	aBlaqMenu.FromRight = StopFromRight
    if( XSubMenuTweak )
	aBlaqMenu.XSubMenuTweak = XSubMenuTweak
    if( YSubMenuTweak )
	aBlaqMenu.YSubMenuTweak = YSubMenuTweak
    aBlaqMenu.x = xPos
    aBlaqMenu.y = yPos
	    
    if( navigator.appVersion.indexOf( "MSIE" )!=-1 )
	document.body.onresize = new Function( 'doResize()' )
    else
	self.onresize= new Function( 'doResize()' )
	    
    doResize();
}

function swapTitleForCurrentPage( aBlaqMenu, Element )
{
    Element-=1
    aBlaqMenu.SwapTitle = Element
    
    TheCell = document.getElementById( aBlaqMenu.MenuRef+".BlaqMenu"+Element )
    TheCell.style.backgroundColor = aBlaqMenu.TitleBGColorOver[ Element ]	
    newImage = "url(" + aBlaqMenu.TitleBGImageOver[Element] + ")"
    TheCell.style.backgroundImage = newImage
}

function mouseInTitle( id )
{
    temp = id.indexOf( ".BlaqMenu")
    MenuRef = id.substr(0,temp)
    CellRef = id.substr(temp+9,id.length )
    currentMenu = Menus[MenuRef]
    TheCell =  document.getElementById( id )
    
    if( currentMenu.SwapTitle != CellRef )
    {
	TheCell.style.backgroundColor = currentMenu.TitleBGColorOver[ CellRef ]	
	newImage = "url(" + currentMenu.TitleBGImageOver[CellRef] + ")"
	TheCell.style.backgroundImage = newImage
    }
    
    hideVisibleMenu()
    TheSubMenu = document.getElementById( currentMenu.MenuRef+".BlaqSubMenu"+CellRef )
    visibleMenu = TheSubMenu
    showVisibleMenu()
}

function mouseOutTitle( id, Menuid )
{
    temp = id.indexOf( ".BlaqMenu")
    MenuRef = id.substr(0,temp)
    CellRef = id.substr(temp+9,id.length)
    currentMenu = Menus[MenuRef]
    TheCell =  document.getElementById( id )
    
    if( currentMenu.SwapTitle != CellRef )
    {
	TheCell.style.backgroundColor = currentMenu.TitleBGColor[ CellRef ]
	newImage = "url(" + currentMenu.TitleBGImage[CellRef] +")"
	TheCell.style.backgroundImage = newImage
    }
}
