/*
 *   TopNav.js, by Jeff W. McClung
 *
 *   Purpose - This file should contain all of the javascript nessesary for the top-nav
 *      drop-down menus on www.americas.creative.com.
 *
 *   Componants - 
 *      Function to find co-ordinates of a given anchor ( <a href etc.> ) - anchorposition.js
 *      Function to grab a given object by its name
 *      Function to hide all menu items
 *      Function to show a given menu item
 *      
 *   All this code should be cross-browser compatible.  It should work in IE4+, NS4+, Opera 5, and Mozilla 0.9+.
 *
 *   This code is largely my own creation, but is also largely inspired and in some cases outright borrowed from
 *      the fine work found on these two javascript sites:
 *
 *                  http://www.xs4all.nl/~ppk/js/
 *                  http://www.mattkruse.com/
 *
 *   Thank you, and H.A.N.D.
 *      --Jeff
 */


//browser detection, OS detection (Macintosh)
var IE4=0; var IE5=0; var NS4=0; var NS6=0; var Opera=0; var Mac=0; var Moz=0;
if (navigator.userAgent.indexOf('Opera') != -1) { Opera=1;
} else if(document.getElementById) {
	if ((navigator.userAgent.indexOf('Gecko') != -1)&&(navigator.userAgent.indexOf('etscape') == -1)) { Moz=1;
	} else if (navigator.userAgent.indexOf('Gecko') != -1) { NS6=1;
	} else { IE5=1; }
} else if(document.all) { IE4=1;
} else if(document.layers) { NS4=1; }
if(navigator.appVersion.indexOf('Mac') != -1) { Mac=1; }
//end browser detection


//Global drop-down menu objects
x1 = new Object();  x2 = new Object();  x3 = new Object(); 
x4 = new Object();  x5 = new Object();  x6 = new Object();


//x, y coordinates for the menu objects (used by mmhide [mouse-move hide] function to know when to hide menus)
var Top, Left, Bottom, Right;
Top = 0;  Left = 0;  Bottom = 0; Right = 0;


//grabs an object by name
function getObj(name) {
  if (document.getElementById) {
    return document.getElementById(name).style;
  } else if (document.all) {
    return document.all[name].style;
  } else if (document.layers) {
    return document.layers[name];  }}


//cross-browser variables    //ie = hidden, visible
var hide="hidden";           //ns = hide, show
var show="visible";
if(NS4) { hide="hide"; show="show";       //NS4 vars
document.captureEvents(Event.MOUSEMOVE);  //NS4 event capture
document.onmousemove=mmHide; }
if((NS6)||(Moz)){ document.addEventListener("mousemove", mmHide, false); } //NS6 event capture


function hideAll() {
/* cross-browser object vars (x1, x2, etc. = the pull-down menus) defined after the <divs> in the document. */
  x1.visibility = hide;  x2.visibility = hide;  x3.visibility = hide;
  x4.visibility = hide;  x5.visibility = hide;  x6.visibility = hide; }


function showMenu(name, aName, homePage) {
	if ( ((Mac)&&(homePage)) || ((homePage)&&(!IE4)&&(!IE5)) ) {} else { //the menus won't show over Flash animations in anything but IE.
		hideAll();
		position = getAnchorPosition(aName);
		x = getObj(name);
		//offset positions
		var offsetY, offsetX;
		Top = position.y; Left = position.x;
		//Fixing "Top" and the Top Offset so this looks alike in many browsers
		if ((IE5)||(IE4))			{ offsetY = -5; }
		if ((NS6)||(Moz))			{ Top = Top - 5; offsetY = 10; }
		if (NS4)					{ offsetY = 27; }
		if ((Mac)&&(((IE4)||(IE5)))){ Top = Top + 15; }
		if ((Mac)&&(NS4))			{ Top = Top - 5; offsetY = 5; }
		if (Opera)					{ offsetX = 145; offsetY = 5; Top = Top - 5; }
		if (name != "menu1")		{ Left = Left + 0; offsetX = 186;} else {Left = Left + 0; offsetX = 186; }
		//Top, Left, Right, Bottom used by mmhide to know when to hide the menus (global vars).
		if (name == "menu1") { Right = Left + 400; Bottom = Top + 165; } // Home
		if (name == "menu2") { Right = Left + 165; Bottom = Top + 200; } // Corporate
		if (name == "menu3") { Right = Left + 165; Bottom = Top + 210; } // Products
		if (name == "menu4") { Right = Left + 175; Bottom = Top + 210; } // Support
		if (name == "menu5") { Right = Left + 155; Bottom = Top + 175; } // Shop
		if (name == "menu6") { Right = Left + 165; Bottom = Top + 190; } // Contact Us
		//do the deed
		x.top = Top + offsetY;
		x.left = Left + offsetX; 
		x.visibility = show; } }


function mmHide(e) {  //mouse move hide
/* cross-browser object vars (x1, x2, etc. = the pull-down menus) defined after the <divs> in the document. */
	var mouseX; var mouseY;
	if (Opera) {
		mouseX = window.event.x; mouseY = window.event.y;
	} else if ((IE4)||(IE5)) { 
		mouseX = window.event.x+document.body.scrollLeft; mouseY = window.event.y+document.body.scrollTop; 
	} else if ((NS4)||(NS6)||(Moz)) { 
		mouseX = e.pageX; mouseY = e.pageY; 
	}
  if ((mouseX > Right)||(mouseX < Left)||(mouseY > Bottom)||(mouseY < Top)) {
	hideAll(); 	} 
}


