fs_menu.spacer = "/_images/pixel.gif";
fs_menu.dingbat_on = '';
fs_menu.dingbat_off = '';
fs_menu.pad_menu = 2;
fs_menu.pad_item = 3;
fs_menu.shadow_size = 0;
fs_menu.shadow_offset = 1;
fs_menu.shadow_color = "#888888";
fs_menu.shadow_png = '';
fs_menu.bgr_color = "#FBFBFB";
fs_menu.bgr_png = '';
fs_menu.delay = 1000;
fs_menu.slide_time = 400;
fs_menu.reference = {top_left:1,top_right:2,bottom_left:3,bottom_right:4};
fs_menu.dir = {down:1,right:2};
fs_menu.lib = [];
fs_menu._maxZ = 100;

fs_menu.supports = function() {
	if( navigator.userAgent.toLowerCase().indexOf( 'gecko' ) + 1 ) return true;
	if( ( navigator.appName.indexOf( 'Microsoft Internet Explorer' ) + 1 ) && document.getElementById ) return true;
	return false;
}

fs_menu.init = function() {
for (var i = 0, menu = null; menu = this.lib[i]; i++) {
menu.init();
}
}

fs_menu.render_all = function() {
var aMenuHtml = [];
for (var i = 0, menu = null; menu = this.lib[i]; i++) {
aMenuHtml[i] = menu.toString();
}
document.write(aMenuHtml.join(""));
}

function fs_menu(v_trig, v_dir, v_lft, v_top, v_ref, parent_set) {

this.toString = toString;
this.init = init;
this.isOpen = false;
this.show = show;
this.hide = hide;
this.items = [];

this.onactivate = new Function();
this.ondeactivate = new Function();
this.onmouseover = new Function();
this.onqueue = new Function();

this.index = fs_menu.lib.length;
fs_menu.lib[this.index] = this;
var id = "fs_menu" + this.index;
var child_set = null;
var animating = false;
var childMenus = [];
var elts = null;
var ready = false;
var _this = this;
var a = null;
var pos = v_dir == fs_menu.dir.down ? "top" : "left";
var dim = null;

	this.add_item = function( v_txt, v_url ) {
		var f = new fs_menu_item( v_txt, v_url, this );
		f._index = this.items.length;
		this.items[ f._index ] = f;
	}

	this.add_menu = function( v_tgtItem ) {
		if (!v_tgtItem.parentMenu == this) throw new Error("Cannot add a menu here");
		if (child_set == null) child_set = new fs_menu_set(fs_menu.dir.right, -10, 2, fs_menu.reference.top_right);
		var m = child_set.add_menu(v_tgtItem);
		childMenus[v_tgtItem._index] = m;
		m.onmouseover = child_mouseover;
		m.ondeactivate = child_deactivate;
		m.onqueue = child_queue;
		return m;
	}

function init() {
	var menu = document.getElementById(id);
	var all = menu.all ? menu.all : menu.getElementsByTagName("*");
	elts = {};
	elts["clip"] = menu;
	elts["item"] = [];
	for (var i = 0, elm = null; elm = all[i]; i++) {
		switch (elm.className) {
			case "item":
				elm._index = elts["item"].length;
				elts["item"][elm._index] = elm;
				break;
			default:
				elts[elm.className] = elm;
				break;
		}
	}

	_this.elts = elts;
	for (var i = 0, item = null; item = elts.item[i]; i++) {
		item.onmouseover = item_mouseover;
		item.onmouseout = item_mouseout;
		item.onclick = item_click;
	}

	if (typeof v_trig.tagName != "undefined") {
		v_trig.onmouseover = trigger_mouseover;
		v_trig.onmouseout = trigger_mouseout;
	}

	elts["content"].onmouseover = content_mouseover;
	elts["content"].onmouseout = content_mouseout;

    	var ow = elts["items"].offsetWidth;
		var oh = elts["items"].offsetHeight;
		var ua = navigator.userAgent.toLowerCase();
		elts["clip"].style.width = ow + fs_menu.shadow_size + 2 + "px";
		elts["clip"].style.height = oh + fs_menu.shadow_size + 2 + "px";
		elts["content"].style.width = ow + fs_menu.shadow_size + "px";
		elts["content"].style.height = oh + fs_menu.shadow_size + "px";
		dim = ( v_dir == fs_menu.dir.down ? oh : ow ) + fs_menu.shadow_size;
		elts["content"].style[pos] = -dim - fs_menu.shadow_size + "px";
		elts["clip"].style.visibility = "hidden";

		if( ua.indexOf( 'mac' ) == -1 || ua.indexOf( 'gecko' ) > -1 ) {
			var e = elts["background"].style;
			e.width = ow + "px";
			e.height = oh + "px";
			e.backgroundColor = fs_menu.bgr_color;
			e = elts["shadowRight"].style;
			e.left = ow + "px";
			e.height = oh - (fs_menu.shadow_offset - fs_menu.shadow_size) + "px";
			e.backgroundColor = fs_menu.shadow_color;
			e = elts["shadowBottom"].style;
			e.top = oh + "px";
			e.width = ow - fs_menu.shadow_offset + "px";
			e.backgroundColor = fs_menu.shadow_color;
		} else {
			var e = elts["background"].firstChild;
			e.src = fs_menu.bgr_png;
			e.width = ow;
			e.height = oh;
			e = elts["shadowRight"];
			e.firstChild.src = fs_menu.shadow_png;
			e.style.left = ow + "px";
			e.firstChild.width = fs_menu.shadow_size;
			e.firstChild.height = oh - (fs_menu.shadow_offset - fs_menu.shadow_size);
			e = elts["shadowBottom"];
			e.firstChild.src = fs_menu.shadow_png;
			e.style.top = oh + "px";
			e.firstChild.height = fs_menu.shadow_size;
			e.firstChild.width = ow - fs_menu.shadow_offset;
		}

	ready = true;
}
	function show() {
		if( ready ) {
			_this.isOpen = true;
			animating = true;
			setContainerPos();
			elts["clip"].style.visibility = "visible";
			elts["clip"].style.zIndex = fs_menu._maxZ++;
			slideStart();
			_this.onactivate();
		}
	}
	function hide() {
		if ( ready ) {
//if( console ) console.log( this );
			_this.isOpen = false;
			animating = true;
			for (var i = 0, item = null; item = elts.item[i]; i++) light_off(item);
			if (child_set) child_set.hide();
			slideStart();
			_this.ondeactivate();
		}
	}
	function setContainerPos() {
		
		
		var sub = v_trig.constructor == fs_menu_item;
		var act = sub ? v_trig.parentMenu.elts["item"][v_trig._index] : v_trig;
		var el = act;
		var x = 0;
		var y = 0;
		var minX = 0;
		var maxX = (window.innerWidth ? window.innerWidth : document.body.clientWidth) - parseInt(elts["clip"].style.width);
		var minY = 0;
		var maxY = (window.innerHeight ? window.innerHeight : document.body.clientHeight) - parseInt(elts["clip"].style.height);
		while (sub ? el.parentNode.className.indexOf("fs_menu_css") == -1 : el.offsetParent) {
//alert( el.offsetParent.tagName + ' ' + ' ' + el.tagName + ' ' + el.offsetLeft );
			x += el.offsetLeft;
			y += el.offsetTop;
			if (el.scrollLeft) x -= el.scrollLeft;
			if (el.scrollTop) y -= el.scrollTop;
			el = el.offsetParent;
		}
		if (v_trig.constructor == fs_menu_item) {
			x += parseInt(el.parentNode.style.left);
			y += parseInt(el.parentNode.style.top);
		}
		switch( v_ref ) {
			case fs_menu.reference.top_left:
				break;
			case fs_menu.reference.top_right:
				x += act.offsetWidth;
				break;
			case fs_menu.reference.bottom_left:
				y += act.offsetHeight;
				break;
			case fs_menu.reference.bottom_right:
				x += act.offsetWidth;
				y += act.offsetHeight;
				break;
		}
		x += v_lft;
		y += v_top;

		x = Math.max(Math.min(x, maxX), minX);
		y = Math.max(Math.min(y, maxY), minY);
		elts["clip"].style.left = x + "px";
		elts["clip"].style.top = y + "px";
		
			// START PETER HACK
			if(el==$j('body')[0])
			{
				var pos = $j(act).position();
				
				$j(elts["clip"]).css({
					left:	pos.left,
					top:	pos.top + $j(act).height()
				});
			}
			// END PETER HACK
	}
	function slideStart() {
		var x0 = parseInt(elts["content"].style[pos]);
		var x1 = _this.isOpen ? 0 : -dim;
		if (a != null) a.stop();
		a = new anime( x0, x1, fs_menu.slide_time, -1 );
		a.onframe = slideFrame;
		a.onend = slideEnd;
		a.start();
	}
	function slideFrame(x) {
		elts["content"].style[pos] = x + "px";
	}
	function slideEnd() {
		if (!_this.isOpen) elts["clip"].style.visibility = "hidden";
		animating = false;
	}
	function light_on(v_tgt) {
		v_tgt.className = "item hover";
	}
	function light_off(v_tgt) {
		v_tgt.className = "item";
	}
	function item_mouseover() {
		if (!animating) {
			light_on(this);
			if (childMenus[this._index]) child_set.show_menu(childMenus[this._index]);
			//else if (child_set) child_set.hide();
		}
	}
	function item_mouseout() {
		light_off(this);
		if (!animating) {
			if (childMenus[this._index]) child_set.hide_menu(childMenus[this._index]);
		}
	}
	function item_click() {
		if (!animating) {
			if (_this.items[this._index].url)
				location.href = _this.items[this._index].url;
		}
	}
	function trigger_mouseover() {
		parent_set.show_menu(_this);
	}
	function trigger_mouseout() {
		parent_set.hide_menu(_this);
	}
	function content_mouseover() {
		if (!animating) {
			parent_set.show_menu(_this);
			_this.onmouseover();
		}
	}
	function content_mouseout() {
		if (!animating) parent_set.hide_menu(_this);
	}
	function child_mouseover() {
		if (!animating) parent_set.show_menu(_this);
	}
	function child_deactivate() {
		for (var i = 0; i < childMenus.length; i++) {
			if (childMenus[i] == this) {
				light_off(elts["item"][i]);
				break;
			}
		}
	}
	function child_queue() {
		parent_set.hide_menu(_this);
	}
	function toString() {
		var aHtml = [];
		var css_cls = "fs_menu_css" + (v_trig.constructor != fs_menu_item ? " top" : "");
		for (var i = 0, item = null; item = this.items[i]; i++) {
			aHtml[i] = item.toString(childMenus[i]);
		}

		return '<div style="text-align:left" id="' + id + '" class="' + css_cls + '"><div class="content"><table class="items" cellpadding="0" cellspacing="0" border="0"><tr><td colspan="2"><img src="' + fs_menu.spacer + '" width="1" height="' + fs_menu.pad_menu + '"></td></tr>' +
		aHtml.join('') + '<tr><td colspan="2"><img src="' + fs_menu.spacer + '" width="1" height="' + fs_menu.pad_menu + '"></td></tr></table><div class="shadowBottom"><img src="' + fs_menu.spacer + '" width="1" height="1"></div>' +
		'<div class="shadowRight"><img src="' + fs_menu.spacer + '" width="1" height="1"></div><div class="background"><img src="' + fs_menu.spacer + '" width="1" height="1"></div></div></div>';
	}
}

fs_menu_set.lib = [];

function fs_menu_set( v_dir, v_lft, v_top, v_ref ) {
	// local vars
	var menus = [];
	var _this = this;
	var current = null;
	// props
	this.index = fs_menu_set.lib.length;
	fs_menu_set.lib[this.index] = this;
	// inline methods
	this.add_menu = function( v_trig ) {
		var m = new fs_menu(v_trig, v_dir, v_lft, v_top, v_ref, this);
		menus[ menus.length ] = m;
		return m;
	}
	this.show_menu = function( v_tgt ) {
		if (v_tgt != current) {
			if ( current != null ) this.hide( current );
//			if ( current != null ) scheduleHide( current );
			current = v_tgt;
			v_tgt.show();
		} else {
			cancelHide(v_tgt);
		}
	}
	this.hide_menu = function( v_tgt ) {
		if ( current == v_tgt && v_tgt.isOpen) {
			if (!v_tgt.hideTimer) scheduleHide(v_tgt);
		}
	}
	this.hide = function( v_tgt ) {
		if (!v_tgt && current) v_tgt = current;
		if (v_tgt && current == v_tgt && v_tgt.isOpen) {
//		if (v_tgt && v_tgt.isOpen) {
			cancelHide(v_tgt);
			current = null;
			v_tgt.hideTimer = null;
			v_tgt.hide();
		}
	}
	this.render = function() {
		var aMenuHtml = [];
		for (var i = 0, menu = null; menu = menus[i]; i++) {
			aMenuHtml[i] = menu.toString();
		}
		document.write(aMenuHtml.join(""));
	}

	// private methods
	function scheduleHide( v_tgt ) {
		v_tgt.onqueue();
		v_tgt.hideTimer = window.setTimeout("fs_menu_set.lib[" + _this.index + "].hide(fs_menu.lib[" + v_tgt.index + "])", fs_menu.delay);
	}
	function cancelHide( v_tgt ) {
		if (v_tgt.hideTimer) {
			window.clearTimeout(v_tgt.hideTimer);
			v_tgt.hideTimer = null;
		}
	}
}

function fs_menu_item( v_txt, v_url, v_parent ) {
	this.txt = v_txt;
	this.url = v_url;
	this.parentMenu = v_parent;
}

fs_menu_item.prototype.toString = function( v ) {
	var dingbat = v ? fs_menu.dingbat_off : fs_menu.spacer;
	var pad_edge = fs_menu.pad_item + fs_menu.pad_menu;
	var pad_left = "padding:" + fs_menu.pad_item + "px; padding-left:" + pad_edge + "px;"
	var pad_right = "padding:" + fs_menu.pad_item + "px; padding-right:" + pad_edge + "px;"
//	return '<tr class="item"><td nowrap style="' + pad_left + '">' + this.txt + '</td><td width="15" style="' + pad_right + '"><img src="' + dingbat + '" width="15" height="15"></td></tr>';
	return '<tr class="item"><td nowrap style="' + pad_left + pad_right + '">' + this.txt + '</td></tr>';
}

function anime(from, to, time, zip) {
	if (typeof zip == "undefined") zip = 0;
	if (typeof unit == "undefined") unit = "px";
	this.x0 = from;
	this.x1 = to;
	this.dt = time;
	this.zip = -zip;
	this.unit = unit;
	this.timer = null;
	this.onend = new Function();
	this.onframe = new Function();
}

anime.prototype.start = function() {
	this.t0 = new Date().getTime();
	this.t1 = this.t0 + this.dt;
	var dx = this.x1 - this.x0;
	this.c1 = this.x0 + ((1 + this.zip) * dx / 3);
	this.c2 = this.x0 + ((2 + this.zip) * dx / 3);
	anime._add(this);
}

anime.prototype.stop = function() {
	anime._remove(this);
}

anime.prototype._paint = function(time) {
	if (time < this.t1) {
		var elapsed = time - this.t0;
		this.onframe(anime._getBezier(elapsed/this.dt,this.x0,this.x1,this.c1,this.c2));
	}
	else this._end();
}

anime.prototype._end = function() {
	anime._remove(this);
	this.onframe(this.x1);
	this.onend();
}

anime._add = function(o) {
	var index = this.instances.length;
	this.instances[index] = o;

	if (this.instances.length == 1) {
		this.timerID = window.setInterval("anime._paintAll()", this.targetRes);
	}
}

anime._remove = function(o) {
	for (var i = 0; i < this.instances.length; i++) {
		if (o == this.instances[i]) {
			this.instances = this.instances.slice(0,i).concat( this.instances.slice(i+1) );
			break;
		}
	}
	if (this.instances.length == 0) {
		window.clearInterval(this.timerID);
		this.timerID = null;
	}
}

anime._paintAll = function() {
	var now = new Date().getTime();
	for (var i = 0; i < this.instances.length; i++) {
		this.instances[i]._paint(now);
	}
}

anime._B1 = function(t) { return t*t*t }
anime._B2 = function(t) { return 3*t*t*(1-t) }
anime._B3 = function(t) { return 3*t*(1-t)*(1-t) }
anime._B4 = function(t) { return (1-t)*(1-t)*(1-t) }

anime._getBezier = function(percent,startPos,endPos,control1,control2) {
	return endPos * this._B1(percent) + control2 * this._B2(percent) + control1 * this._B3(percent) + startPos * this._B4(percent);
}

anime.instances = [];
anime.targetRes = 10;
anime.timerID = null;

