// IBC javascript for dhtml functionality / v 1.3
// j.blecher / digital pulp

var dom = document.getElementById;

// stupid opera can't handle object initializers correctly, even though it understands "JavaScript1.3" so we have to test for opera here
// this is really ugly :-(
var uaStr = navigator.userAgent.toLowerCase();
var uaVer = parseInt(navigator.appVersion);
var opera = (uaStr.indexOf("opera") > -1);
var opera7 = (opera && uaVer > 6);
function isCapable() { if (dom) { if (opera) if (opera7) return true; else return false; return true; } else return false; }
var good2go = isCapable();


// some variables
var sSection = "";
var sLastMenu = "";
var oMenuContainer, iTimeout;

// some constants (const keyword has ...uh... issues)
var MENU_MARGIN_WIDTH = 0; // px
var MENU_BORDER_WIDTH = 2; // px
var MENU_PADDING_WIDTH = 0; // px
var MENU_ITEM_PADDING_WIDTH = 20; // px
var MENU_BG_COLOR_0 = "#E5E5E5";
var MENU_BG_COLOR_1 = "#FFF";
var MENU_CONTAINER_ID = "dhtmlMenuContainer";
var MENU_CLASS_NAME = "dhtmlMenuItems";
var MENU_ID_SUFFIX = "Menu";
var MENU_HIDE_TIME = 1000; // ms
var DEFAULT_IMG_PATH = "/images/";

var DEFAULT_FILE_EXT = "html";
var DEFAULT_INDEX_FILE = "index";


// this is the menu data. adjust as necessary
if (good2go) var menuData = {
	navAbout : new Menu(120, [
			new MenuItem("About IBC Home", "/about/"),
			new MenuItem("History", "/about/history"),
			new MenuItem("Locations", "/about/locations"),
			new MenuItem("Partners", "/about/partners"),
			new MenuItem("Employment", "/about/employment")
			],
		"nav.about.0.gif",
		"nav.about.1.gif",
		"about"
		),

	navServices : new Menu(120, [
			new MenuItem("Services Home", "/services/"),
			new MenuItem("Courier Services", "/services/courier"),
			new MenuItem("Mail Services", "/services/mail"),
			new MenuItem("Cargo", "/services/cargo"),
			new MenuItem("Brokerage", "/services/brokerage"),
			new MenuItem("IT Services", "/services/it"),
			new MenuItem("Industry Solutions", "/services/industry")
			],
		"nav.services.0.gif",
		"nav.services.1.gif",
		"services"
		),

	navRates : new Menu(0, [],
		"nav.rates.0.gif",
		"nav.rates.1.gif",
		"rates"
		),

	navContact : new Menu(0, [],
		"nav.contact-rep.0.gif",
		"nav.contact-rep.1.gif",
		"contact"
		),

	navTools : new Menu(170, [
			new MenuItem("Tools Home", "/tools/"),
			new MenuItem("Forms", "/tools/forms"),
			new MenuItem("Domestic Airbill", "/airbill-dom/"),
			new MenuItem("International Airbill", "/airbill-int/"),
			new MenuItem("Order Shipping Materials", "/tools/order-shipping-materials"),
			new MenuItem("International Time Zones", "/tools/popup.map.tz", "popMapTz"),
			new MenuItem("Standard Transit Time", "/tools/standard-transit-time", "popStdTransitTime"),
			new MenuItem("Dimensional Weight Calculator", "/tools/dimensional-weight-calculator"),
			new MenuItem("Shipping Overseas FAQs", "/tools/shipping-overseas-faqs"),
			new MenuItem("Holiday Schedule", "/tools/holiday-schedule")
			],
		"nav.tools.0.gif",
		"nav.tools.1.gif",
		"tools"
		),

	navTracking : new Menu(0, [],
		"nav.tracking.0.gif",
		"nav.tracking.1.gif",
		"tracking"
		)
	};



/* -- Menu constructor ---------------------------------------- */
function Menu(width, items, img, imgOver, section) {
	// we may add as many properties as we'd like here, but the first 2 are required
	this.width = width;
	this.items = items;

	// silly object initializer for ie/win
	this.img = (img) ? new Image() : {};
	this.img.src = (img) ? DEFAULT_IMG_PATH + img : "";
	this.imgOver = (imgOver) ? new Image() : {};
	this.imgOver.src = (imgOver) ? DEFAULT_IMG_PATH + imgOver : "";

	// so we can determine what sect we are in
	this.section = section;

	// the method of the menu to create its items
	this.instantiateMenu = function(container) {
		var thisMenu;

		// create the menu DIV container element
		thisMenu = document.createElement('div');
		thisMenu.className = MENU_CLASS_NAME;
		thisMenu.id = this.theAnchor.id + MENU_ID_SUFFIX;

		thisMenu.onmouseover = function() { clearTimeout(iTimeout); };
		thisMenu.onmouseout = function() { clearTimeout(iTimeout); iTimeout = setTimeout("menuData['" + this.menuName + "'].hideMenu()", MENU_HIDE_TIME); };
		thisMenu.onclick = function(e) { thisEvent = (e) ? e : ((event) ? event : null); thisEvent.cancelBubble = true; };

		// for each menu item, create and append the pTag
		for (var i=0; i < this.items.length; i++) thisMenu.appendChild(this.createMenuItem(this.items[i].title, this.items[i].href, this.items[i].onclick));

		// finally append the menu to the container element
		container.appendChild(thisMenu);
		}

	this.showMenu = function() {
		if (sLastMenu != "") {
			if (menuData[sLastMenu].items.length > 0) menuData[sLastMenu].theMenu.style.visibility = "hidden";
			menuData[sLastMenu].theAnchor.childNodes[0].src = menuData[sLastMenu].img.src;
		}
		this.theAnchor.childNodes[0].src = this.imgOver.src;
		if (this.items.length > 0) this.theMenu.style.visibility = "visible";

		sLastMenu = (this.items.length > 0) ? this.theMenu.menuName : "";
		};

	this.hideMenu = function() {
		if (this.items.length > 0) this.theMenu.style.visibility = "hidden"; // could check by child nodes, too
		this.theAnchor.childNodes[0].src = this.img.src;
		};


	// creates and returns the P + A tags for the info passed
	this.createMenuItem = function(theTitle, theHref, theOnclick) {
		var pTag;

		pTag = document.createElement("p");
		pTag.onmouseover = function() { this.style.backgroundColor = MENU_BG_COLOR_1; };
		pTag.onmouseout = function() { this.style.backgroundColor = MENU_BG_COLOR_0; };
		pTag.onclick = function() {
			if (theOnclick) {
				eval(theOnclick + "()");
				return false;
			} else {
				window.location.href = (theHref.lastIndexOf("/") == (theHref.length-1)) ? theHref : theHref + "." + DEFAULT_FILE_EXT;
			}
		};
		pTag.style.width = (this.width - MENU_ITEM_PADDING_WIDTH) + "px"; // for ie/win
		pTag.appendChild(document.createTextNode(theTitle));

		return pTag;
		};


	// adds the item to the menuData data structure, then appens the pTag
	this.addNewMenuItem = function(theTitle, theHref) {
		this.items.push(new MenuItem(theTitle, theHref));
		this.theMenu.appendChild(this.createMenuItem(theTitle, theHref));
		};

	return this;
	}


/* -- Menu item constructor ----------------------------------- */
function MenuItem(title, href, onclick) {
	this.title = title;
	this.href = href;
	this.onclick = onclick;

	return this;
	}



/* -- Menu initializer ---------------------------------------- */
function initMenus() {
	var iMenusWidth;

	oMenuContainer = document.getElementById(MENU_CONTAINER_ID);
	iMenusWidth = oMenuContainer.offsetWidth - MENU_MARGIN_WIDTH - MENU_BORDER_WIDTH - MENU_PADDING_WIDTH;

	for (o in menuData) {
		// attach the events to the anchors
		menuData[o].theAnchor = document.getElementById(o);

		menuData[o].theAnchor.onmouseover = function() { clearTimeout(iTimeout); if (sSection != menuData[this.id].section) menuData[this.id].showMenu(); };
		menuData[o].theAnchor.onmouseout = function() { 
			clearTimeout(iTimeout); 
			if (sSection != menuData[this.id].section) {
				if (menuData[this.id].items.length > 0) {
					iTimeout = setTimeout('menuData["' + this.id + '"].hideMenu()', MENU_HIDE_TIME); 
				} else {
					menuData[this.id].hideMenu();
				}
			}
		};

		// if we have items in this menu
		if (menuData[o].items.length > 0) {

			// create the menu DIV
			menuData[o].instantiateMenu(oMenuContainer);

			// set the properties of the DIV
			menuData[o].theMenu = document.getElementById(o + "Menu");
			menuData[o].theMenu.menuName = o;
			menuData[o].theMenu.style.visibility = "hidden";
			menuData[o].theMenu.style.position = "absolute";
			menuData[o].theMenu.style.display = "block";
			menuData[o].theMenu.style.width = menuData[o].width + "px";
			menuData[o].theMenu.style.left = ( (menuData[o].theAnchor.offsetLeft + parseInt(menuData[o].theMenu.style.width) ) >= iMenusWidth) ? (iMenusWidth - parseInt(menuData[o].theMenu.style.width)) + "px" : menuData[o].theAnchor.offsetLeft + "px";
			}
		}

	// hide menus on click of document
	document.onclick = function(e) {
		thisEvent = (e) ? e : ((event) ? event : null);
		if (thisEvent) {
//			thisEl = (thisEvent.target) ? thisEvent.target : ((thisEvent.srcElement) ? thisEvent.srcElement : null);
//			for (o in menuData) menuData[o].hideMenu();
			if (sLastMenu != "") menuData[sLastMenu].hideMenu();
			}
		};

	}



/* -- page initalizer ---------------------------------------- */
function init() {
	initMenus();
	}


// start this crazy thing up
if (good2go) window.onload = init;


