// Constants

// Attributes for cells in top table. Initialized in onloadHandler()
var cellArray

// Variables

var lastElem
var lastBackground

// Event handlers

function onloadHandler()
{
	// Install document handlers
	
	document.onclick = onclickHandler
	document.onmouseover = onmouseoverHandler
	document.onmouseout = onmouseoutHandler 

	if (is.ns4)
	{
		function setDocClickHandler(doc, clkHandler)
		{
			for (var l = 0; l < doc.links.length; l++) 
			{ 
				doc.links[l].onclick = clkHandler;
			} 
			for (var i = 0; i < doc.images.length; i++) 
			{ 
				doc.images[i].onmousedown = clkHandler;
				doc.images[i].onmouseover = onmouseoverHandler;
				doc.images[i].onmouseout = onmouseoutHandler;
			}
			for (var y = 0; y < doc.layers.length; y++) 
			{ 
				setDocClickHandler(doc.layers[y].document, clkHandler)
				doc.layers[y].onmousedown = clkHandler;
				doc.layers[y].onclick = clkHandler;
				doc.layers[y].onmouseover = onmouseoverHandler;
				doc.layers[y].onmouseout = onmouseoutHandler;
			}			
			
			doc.onclick = clkHandler
			doc.onmouseover = onmouseoverHandler
			doc.onmouseout = onmouseoutHandler			
		}

		setDocClickHandler(document, onclickHandler)
	}  
	
	toRight = 0;
	toBottom = 0;
	
	// Init cell array
	cellArray = 
	[
		/* ID, type, highlighted background, background, menu, menu x, menu y, click target*/
		
		['FINE ART', 'ART MENU', '#8a8a69', '#ABA64A', window.fw_menu_0, 145+toRight, 114+toBottom, 'http://www.mesart.com/fineart'],
		['CRAFT', 'ART MENU', '#8a8a69', '#CC9900', window.fw_menu_1, 304+toRight, 114+toBottom, 'http://www.mesart.com/craft'],
		['COMMERCIAL ART', 'ART  MENU', '#8a8a69', '#CD7D1E', window.fw_menu_2, 463+toRight, 114+toBottom, 'http://www.mesart.com/commercialart'],
		['SA', 'TOP MENU', '#848684', '#A9A9A9', null, -1, -1, 'http://www.mesart.com/sellart'],
		['RESOURCES', 'TOP MENU', '#848684', '#E7E7E7', null, -1, -1, 'http://www.mesart.com/resources'],
		['BUY ART', 'TOP MENU', '#848684', '#A9A9A9', null, -1, -1, 'http://www.mesart.com/buyart'],
		['EVENTS', 'TOP MENU', '#848684', '#E7E7E7', null, -1, -1, 'http://www.mesart.com/art_forum'],
		['ARTCENTER', 'TOP MENU', '#848684', '#E7E7E7', null, -1, -1, 'http://www.mesart.com/art_center'],
		['SERVICES', 'MAIN MENU', '#E7E7E7', '#FFFFFF', window.fw_menu_3, 145+toRight, 57, 'http://www.mesart.com/services'],
		['COMMUNITY', 'MAIN MENU', '#E7E7E7', '#FFFFFF', window.fw_menu_4, 145+toRight, 0, 'http://www.mesart.com/community.shtml'],
		['SUPPORT', 'MAIN MENU', '#E7E7E7', '#FFFFFF', window.fw_menu_5, 145+toRight, 19, 'http://www.mesart.com/faq'],
		['ABOUT US', 'MAIN MENU', '#8a8a69', '#999999', window.fw_menu_6, 145+toRight, 38, 'http://www.mesart.com/about.shtml'],		
		['ARTIST LISTING', 'RIGHT MENU', '#A4A271', '#CCCCAA', null, -1, -1, 'http://www.mesart.com/artistlisting'],
		['ART LOBBY', 'RIGHT MENU', '#A4A271', '#CCCCAA', null, -1, -1, 'http://www.mesart.com/search']
	]

	// If needed, adjust coordinates for Netscape
	if (is.ns4)
	{
		for (var i = 0; i < cellArray.length; i++) 
		{ 
			cellArray[i][5] -= 1
			cellArray[i][6] -= 1
		}
	}

	// Other operations
	
	if (!(navigator.appVersion.indexOf('Mac') != -1 && navigator.appName.indexOf('Microsoft') != -1 
		&& navigator.appVersion.indexOf('4.0') != -1) ) 
	{ 
		// ap this was causing js errors
		//document.forms.category.blur()
	}
	
	rb()	
}

function onmouseoverHandler(ns_event)
{
	var ev = ns_event ? ns_event : window.event;
	var elem = ns_event ? ev.target : ev.srcElement;	
	var tagName = elem.tagName // TODO: find tag name in Netscape
	var id = elem.id 
	var retValue = true

	// If this is Netscape and we are in a layer...

	if (ns_event && elem.captureEvents)
	{		
		// ...then this is our layer inside a <TD>
		tagName = 'TD'		
		// For our layers that are placed inside inline layers (<ILAYER>):
		if (elem.parentLayer)
		{
			elem = elem.parentLayer
		}
	}

	// If event came into a <FONT>, <A>, <CENTER> or other tags, try to get its parent <TD>:

//alert("elem.tagName::"+elem.tagName+"\n"+"elem.parentElement::"+elem.parentElement+"elem.tagName::"+elem.tagName+"\n"+"elem.parentElement::"+elem.parentElement)
	if (tagName != 'TD')
	{
		var parentTD 
if (is.ie) parentTD = getParentTag(elem, 'TD')
if (is.ns5) parentTD = getParentTagNS6(elem, 'TD')
		if (parentTD) 
		{
			elem = parentTD
			tagName = elem.tagName 
			id = elem.id 
		}
	}

	// Process <TD> tags
	if (tagName == 'TD') 
	{
		 var background
		 var oldBackground 
		 var menu
		 var menux
		 var menuy
		 
		 // Get information about cell
		 
		 var attributes = getCellAttributes(id)
		 if (attributes)
		 {
			background = attributes[2]
			oldBackground = attributes[3]
			menu = attributes[4]
			menux = attributes[5]
			menuy = attributes[6]
		}
		 
		 // Act according to gathered info
		 
		 if (background)
		 {
			// Restore background of last backgrounded element
			// This is needed because sometimes elements do not receive 
			// the onmouceout event
			if (lastElem && lastBackground)
				chgBg(lastElem, lastBackground)

		 	// Now change the background of our element
		 	chgBg(elem, background)

			if (elem.style && elem.style.cursor != (is.ns4? 'pointer' : 'hand')) elem.style.cursor = (is.ns4? 'pointer' : 'hand')
			
			lastElem = elem
			lastBackground = oldBackground
		 }
		 
		 if (menu)
		 {
		 	window.FW_showMenu(menu,menux,menuy)
		 }
	}
	
	return retValue
}

function onmouseoutHandler(ns_event)
{
	var ev = ns_event ? ns_event : window.event;
	var elem = ns_event ? ev.target : ev.srcElement;	
	var tagName = elem.tagName
	var id = elem.id
	var retValue = true

	// If this is Netscape and we are in a layer...
	if (ns_event && elem.captureEvents)
	{
		// ...then this is our layer inside a <TD>
		tagName = 'TD'
		
		// For our layers that are placed inside inline layers (<ILAYER>):
		if (elem.parentLayer)
		{
			elem = elem.parentLayer
			//id = elem.id 
		}
	}
	
	// If event came into a <FONT>, <A>, <CENTER> or other tags, try to get its parent <TD>:
	if (tagName != 'TD')
	{
		var parentTD;
if (is.ie) parentTD = getParentTag(elem, 'TD')
if (is.ns5) parentTD = getParentTagNS6(elem, 'TD')
		if (parentTD) 
		{
			elem = parentTD
			tagName = elem.tagName 
			id = elem.id 
		}
	}

	// Process <TD> tags
	if (tagName == 'TD')
	{
		 var oldBackground 
		 var menu
		 
		 // Get information about cell
		 
		 var attributes = getCellAttributes(id)
		 if (attributes)
		 {
			oldBackground = attributes[3]
			menu = attributes[4]
		}
		 
		 // Act accordingly
		 
		 if (oldBackground)
		 {
		 	chgBg(elem, oldBackground)
		 }
		 
		 if (menu)
		 {
		 	// Remove menu
		 	MM_swapImgRestore()
		 	FW_startTimeout()
		 }
	}
	
	return retValue
}

function onclickHandler(ns_event)
{
	var ev = ns_event ? ns_event : window.event;
	var elem = ns_event ? ev.target : ev.srcElement;	
	var tagName = elem.tagName
	var id = elem.id
	var retValue = true
	
	// If this is Netscape, and the target is some layer.document object...
	if (ns_event && elem.captureEvents)
	{
		// Get this document object's layer:
		var parentLayer = getDocParentLayer(document, elem)


		if (parentLayer) 
		{
			elem = parentLayer
			id = elem.id 
		}

		// ...then this is our layer inside a <TD>
		tagName = 'TD'
	}
	
	// If event came into a <FONT>, <A>, <CENTER> or other tags, try to get its parent <TD>:
	if (tagName != 'TD')
	{	
		var parentTD
		if (is.ie) parentTD = getParentTag(elem, 'TD')
		if (is.ns5) parentTD = getParentTagNS6(elem, 'TD')

		if (parentTD) 
		{
			elem = parentTD
			tagName = elem.tagName 
			id = elem.id 
		}
	}
	
	if (tagName == 'TD')
	{
		var clickTarget 
		 
		 // Get information about cell
		 
		var attributes = getCellAttributes(id)
		if (attributes)
		{
			clickTarget = attributes[7]
		}
		 
		// Act accordingly
		 
		if (clickTarget)
		{
			location = clickTarget
			retValue = false
		}
	}

	return retValue
}

// Other functions

// Returns information about an element of our cellArray
function getCellAttributes(id)
{
	var attributes = null
	
	for (i = 0; i < cellArray.length; i++)
		if (cellArray[i][0] == id)
		{

			attributes = cellArray[i]
			break
		}	
	
	return attributes
}

// Traverses upwards until meeting the required tag
// IE only
function getParentTag(elem, requiredTag)
{
	var parent
		
	if (elem)
	{
		parent = elem.parentElement
		
		while (parent && parent.tagName != requiredTag)
			parent = parent .parentElement
	}

	return parent
}
//ach
function getParentTagNS6(elem, requiredTag)
{
	var parent
		
	if (elem)
	{
		parent = elem.parentNode
		
		while (parent && parent.tagName != requiredTag)
			parent = parent .parentNode
	}

	return parent
}
//ach
// Traverses dowards upwards until meeting the paret layer of cupplied doc
// NS only
function getDocParentLayer(document, doc)
{
	for (var i = 0; i < document.layers.length; i++) 
	{ 
		var layer = document.layers[i]
		
		if (doc == layer.document)
			return layer
		
		layer = getDocParentLayer(layer.document, doc)
		
		if (layer)
			return layer
	}
}

function disableTextField (field) {
  if (field==null || field=='undefined') return;
  if (document.all || document.getElementById) 
    field.disabled = true;
  else {
    field.oldonFocus = field.onfocus;
    field.onfocus = skip;
  }
}
function enableTextField (field) {
  if (field==null || field=='undefined') return;
  if (document.all || document.getElementById)
    field.disabled = false;
  else {
    field.onfocus = null;
  }
}
 
function swallowClick() 
{
    if (this.checked) this.checked = false;
}

