icms_iMenu.Registry = [];
icms_iMenu.aniLen = 250;
icms_iMenu.hideDelay = 500;
icms_iMenu.minCPUResolution = 10;

// constructor
function icms_iMenu(id, dir, left, top, width, height) {
	var agt = navigator.userAgent.toLowerCase();
	var is_major = parseInt(navigator.appVersion);
    var is_minor = parseFloat(navigator.appVersion);
	
	this.is_ie = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1));
	this.is_nav = ((agt.indexOf('mozilla')!=-1) && (agt.indexOf('spoofer')==-1) && (agt.indexOf('compatible') == -1) && (agt.indexOf('opera')==-1) && (agt.indexOf('webtv')==-1) && (agt.indexOf('hotjava')==-1));
	this.is_nav4 = (this.is_nav && (is_major == 4));

	this.is_dom = document.getElementById ? 1 : 0;
	
	if (this.is_ie || this.is_nav4 || this.is_dom) {
		this.id = id;
		this.dir = dir;
		this.orientation = dir == "left" || dir == "right" ? "h" : "v";
		this.dirType = dir == "right" || dir == "down" ? "-" : "+";
		this.dim = this.orientation == "h" ? width : height;
		this.hideTimer = false;
		this.aniTimer = false;
		this.open = false;
		this.over = false;
		this.startTime = 0;
		this.gRef = "icms_iMenu_"+id;
		
		eval(this.gRef+"=this");
		
		icms_iMenu.Registry[id] = this;
		var d = document;
		var strCSS = '<style type="text/css">';
			strCSS += '#' + this.id + 'Container { visibility:hidden; ';
			strCSS += 'left:' + left + 'px; ';
			strCSS += 'top:' + top + 'px; ';
			strCSS += 'overflow:hidden; z-index:10000; }';
			strCSS += '#' + this.id + 'Container, #' + this.id + 'Content { position:absolute; ';
			strCSS += 'width:' + width + 'px; ';
			strCSS += 'height:' + height + 'px; ';
			strCSS += 'clip:rect(0 ' + width + ' ' + height + ' 0); ';
			strCSS += '}';
			strCSS += '</style>';
		d.write(strCSS);
		this.load();
	}
}

icms_iMenu.prototype.load = function() {
	var d = document;
	var layerId1 = this.id + "Container";
	var layerId2 = this.id + "Content";
	
	var obj1 = this.is_dom ? d.getElementById(layerId1) : this.is_ie ? d.all[layerId1] : d.layers[layerId1];
	if (obj1) var obj2 = this.is_nav4 ? obj1.layers[layerId2] : this.is_ie ? d.all[layerId2] : d.getElementById(layerId2);
	
	var temp;
	
	if (!obj1 || !obj2) {
		window.setTimeout(this.gRef + ".load()", 100);
	} else {
		this.container = obj1;
		this.menu = obj2;
		this.style = this.is_nav4 ? this.menu : this.menu.style;
		this.homePos = eval("0" + this.dirType + this.dim);
		this.outPos = 0;
		this.accelConst = (this.outPos - this.homePos) / icms_iMenu.aniLen / icms_iMenu.aniLen ;
		// set event handlers.
		if (this.is_nav4) this.menu.captureEvents(Event.MOUSEOVER | Event.MOUSEOUT);
		this.menu.onmouseover = new Function("icms_iMenu.showMenu('" + this.id + "')");
		this.menu.onmouseout = new Function("icms_iMenu.hideMenu('" + this.id + "')");
		//set initial state
		this.endSlide();
	}
}

icms_iMenu.showMenu = function(id) {
	var reg = icms_iMenu.Registry;
	var obj = icms_iMenu.Registry[id];
	if (obj.container) {
		obj.over = true;
		for (menu in reg) if (id != menu) icms_iMenu.hide(menu);
		if (obj.hideTimer) {
			reg[id].hideTimer = window.clearTimeout(reg[id].hideTimer);
		}
		if (!obj.open && !obj.aniTimer) reg[id].startSlide(true);
	}
}

icms_iMenu.hideMenu = function(id) {
	var obj = icms_iMenu.Registry[id]
	if (obj.container) {
		if (obj.hideTimer) window.clearTimeout(obj.hideTimer)
		obj.hideTimer = window.setTimeout("icms_iMenu.hide('" + id + "')", icms_iMenu.hideDelay);
	}
}

icms_iMenu.hideAll = function() {
	var reg = icms_iMenu.Registry;
	for (menu in reg) {
		icms_iMenu.hide(menu);
		if (menu.hideTimer) window.clearTimeout(menu.hideTimer);
	}
}

icms_iMenu.hide = function(id) {
	var obj = icms_iMenu.Registry[id];
	obj.over = false;
	if (obj.hideTimer) window.clearTimeout(obj.hideTimer);
	obj.hideTimer = 0;
	if (obj.open && !obj.aniTimer) obj.startSlide(false);
}

icms_iMenu.swapimgrestore = function() {
  var i,x,a=document.icms_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

icms_iMenu.findobj = function(n, d) {
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=icms_iMenu.findobj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

icms_iMenu.swapimage = function() {
  var i,j=0,x,a=icms_iMenu.swapimage.arguments; document.icms_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=icms_iMenu.findobj(a[i]))!=null){document.icms_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

icms_iMenu.prototype.startSlide = function(open) {
	this[open ? "onactivate" : "ondeactivate"]();
	this.open = open;
	if (open) this.setVisibility(true);
	this.startTime = (new Date()).getTime() ;
	this.aniTimer = window.setInterval(this.gRef + ".slide()", icms_iMenu.minCPUResolution);
}

icms_iMenu.prototype.slide = function() {
	var elapsed = (new Date()).getTime() - this.startTime;
	if (elapsed > icms_iMenu.aniLen) {
		this.endSlide();
	} else {
		var d = Math.round(Math.pow(icms_iMenu.aniLen-elapsed, 2) * this.accelConst);
		if (this.open && this.dirType == "-") d = -d;
		else if (this.open && this.dirType == "+") d = -d;
		else if (!this.open && this.dirType == "-") d = -this.dim + d;
		else d = this.dim + d;
		this.moveTo(d);
	}
}

icms_iMenu.prototype.endSlide = function() {
	this.aniTimer = window.clearTimeout(this.aniTimer);
	this.moveTo(this.open ? this.outPos : this.homePos);
	if (!this.open) this.setVisibility(false);
	if ((this.open && !this.over) || (!this.open && this.over)) {
		this.startSlide(this.over);
	}
}

icms_iMenu.prototype.setVisibility = function(bShow) { 
	var s = this.is_nav4 ? this.container : this.container.style;
	s.visibility = bShow ? "visible" : "hidden";
}

icms_iMenu.prototype.moveTo = function(p) { 
	this.style[this.orientation == "h" ? "left" : "top"] = this.is_nav4 ? p : p + "px";
}

icms_iMenu.prototype.getPos = function(c) {
	return parseInt(this.style[c])
}

icms_iMenu.prototype.onactivate = function() { }
icms_iMenu.prototype.ondeactivate = function() { }

function icms_repositioniMenu(menu, offset) {
	var width = document.body.clientWidth != null ? document.body.clientWidth : window.innerWidth;
	width = (width < 780 ) ? 780 : width;
	var newLeft = width / 2 + offset;
	menu.container.style ? menu.container.style.left = newLeft + "px" : menu.container.left = newLeft;
}