// Constants
var DROP_ITEM_OFF_CLASS = "item-off";
var DROP_ITEM_ON_CLASS = "item-on";
var SUBNAV_ITEM_OFF_CLASS = "nav-item-off";
var SUBNAV_ITEM_ON_CLASS = "nav-item-on";
var DROP_DISAPPEAR_MS = 6000

var initDone = false;

// Standalone rollover Images
var subnavArrowOn = new Image();
var subnavArrowOff = new Image();
subnavArrowOn.src = "http://bigfish.fileburst.com/KD/subnav-arrow-on.gif";
subnavArrowOff.src = "http://bigfish.fileburst.com/KD/subnav-arrow-off.gif";

// A Stack to keep track of window timeouts
var timeoutIDs = [];

// Array of objects describing the drop menus and their parent images
var drop = [
	[ 'drop_whoweare', 'nav_whoweare', new Image(), new Image() ],
	[ 'drop_memberservices', 'nav_memberservices', new Image(), new Image() ],
	[ 'drop_community', 'nav_community', new Image(), new Image() ],
	[ 'drop_parents', 'nav_parents', new Image(), new Image() ],
	[ 'drop_scholarship', 'nav_scholarship', new Image(), new Image() ],
	[ 'drop_events', 'nav_events', new Image(), new Image() ],
	[ 'drop_potential', 'nav_potential', new Image(), new Image() ]
];

drop[0][2].src = 'http://bigfish.fileburst.com/KD/nav-who-off.gif';
drop[0][3].src = 'http://bigfish.fileburst.com/KD/nav-who-on.gif';
drop[1][2].src = 'http://bigfish.fileburst.com/KD/nav-services-off.gif';
drop[1][3].src = 'http://bigfish.fileburst.com/KD/nav-services-on.gif';
drop[2][2].src = 'http://bigfish.fileburst.com/KD/nav-community-off.gif';
drop[2][3].src = 'http://bigfish.fileburst.com/KD/nav-community-on.gif';
drop[3][2].src = 'http://bigfish.fileburst.com/KD/nav-parents-off.gif';
drop[3][3].src = 'http://bigfish.fileburst.com/KD/nav-parents-on.gif';
drop[4][2].src = 'http://bigfish.fileburst.com/KD/nav-scholarship-off.gif';
drop[4][3].src = 'http://bigfish.fileburst.com/KD/nav-scholarship-on.gif';
drop[5][2].src = 'http://bigfish.fileburst.com/KD/nav-events-off.gif';
drop[5][3].src = 'http://bigfish.fileburst.com/KD/nav-events-on.gif';
drop[6][2].src = 'http://bigfish.fileburst.com/KD/nav-potential-off.gif';
drop[6][3].src = 'http://bigfish.fileburst.com/KD/nav-potential-on.gif';

// Array of objects describing the secondary nav images
var sec = [
	[ new Image(), new Image() ],
	[ new Image(), new Image() ],
	[ new Image(), new Image() ],
	[ new Image(), new Image() ],
	[ new Image(), new Image() ]
];

sec[0][0].src = 'http://bigfish.fileburst.com/KD/secnav-contact-off.gif';
sec[0][1].src = 'http://bigfish.fileburst.com/KD/secnav-contact-on.gif';
sec[1][0].src = 'http://bigfish.fileburst.com/KD/secnav-shop-off.gif';
sec[1][1].src = 'http://bigfish.fileburst.com/KD/secnav-shop-on.gif';
sec[2][0].src = 'http://bigfish.fileburst.com/KD/secnav-calendar-off.gif';
sec[2][1].src = 'http://bigfish.fileburst.com/KD/secnav-calendar-on.gif';
sec[3][0].src = 'http://bigfish.fileburst.com/KD/secnav-guestbook-off.gif';
sec[3][1].src = 'http://bigfish.fileburst.com/KD/secnav-guestbook-on.gif';
sec[4][0].src = 'http://bigfish.fileburst.com/KD/secnav-exclusively-off.gif';
sec[4][1].src = 'http://bigfish.fileburst.com/KD/secnav-exclusively-on.gif';

function Point(x, y) {
	this.x = x;
	this.y = y;
}

function getObj(id) {
	if(document.layers) {
		return document.layers[id];
	} else if(document.all) {
		return document.all[id];
	} else if(document.getElementById) {
		return document.getElementById(id);
	}
}

function getPosition(el) {
	if(document.all) {
		return getPositionIE(el);
	} else if(document.getElementById) {
		return getPositionNS(el);
	}
}

function getPositionNS(el) {
	return new Point(el.x, el.y);
}

function getPositionIE(el) {
	var r = new Point(el.offsetLeft, el.offsetTop);
	
	if(el.offsetParent) {
		var tmp = getPositionIE(el.offsetParent);
		r.x += tmp.x;
		r.y += tmp.y;
	}

	return r;
}

function subnavOver(obj, imgObj) {
	dropHideAll();
	obj.className = SUBNAV_ITEM_ON_CLASS;
	imgObj.src = subnavArrowOn.src;
}

function subnavOut(obj, imgObj) {
	obj.className = SUBNAV_ITEM_OFF_CLASS;
	imgObj.src = subnavArrowOff.src;
}

function dropItemOver(obj) {
	obj.className = DROP_ITEM_ON_CLASS;
}

function dropItemOut(obj) {
	obj.className = DROP_ITEM_OFF_CLASS;
}

function dropItemClick(obj) {
	document.location.href = obj.href;
}

function navOver(obj, index) {
	if(!initDone) return;
	
	dropHideAll();
	var target = getObj(drop[index][0]);
	target.style.display = 'block';
	timeoutIDs.push([ window.setTimeout('dropHideAll();', DROP_DISAPPEAR_MS), obj, index ]);
	obj.src = drop[index][3].src;
}

function navOut(obj, index) {
	// Noop
}

function dropHide(index) {
	var target = getObj(drop[index][0]);
	target.style.display = 'none';
}

function dropHideAll() {
	for(var i = 0; i < drop.length; i++) {
		var target = getObj(drop[i][0]);
		target.style.display = 'none';
	}
	while(timeoutIDs.length > 0) {
		var timeout = timeoutIDs.pop();
		window.clearTimeout(timeout[0]);
		timeout[1].src = drop[timeout[2]][2].src;
	}
}

function secNavOut(obj, index) {
	obj.src = sec[index][0].src;
}

function secNavOver(obj, index) {
	obj.src = sec[index][1].src;
	dropHideAll();
}

function putObj(parent, target, offsetX, offsetY) {
	var pt = getPosition(parent);
	target.style.top = new String(pt.y + offsetY) + 'px';
	target.style.left = new String(pt.x + offsetX) + 'px';
	return pt;
}

function initDrops() {
	for(var i = 0; i < drop.length; i++) {
		var el = getObj(drop[i][1]);
		var target = getObj(drop[i][0]);
		var pt;
		
		if(i < (drop.length - 2)) {
			pt = putObj(el, target, 0, el.offsetHeight + 1);
		} else {
			pt = getPosition(el);
			var prevState = target.style.display;
			target.style.display = 'block';
			target.style.left = new String((el.offsetWidth + pt.x) - target.offsetWidth) + 'px';
			target.style.top = new String(pt.y + el.offsetHeight + 1) + 'px';
			target.style.display = prevState;
		}
	}
	initDone = true;
}

function init() {
	initDrops();
}