/* COPYRIGHT NOTICE *****************************

Developed and owned by:

Kevin P Miller
kevin_p_miller@hotmail.com

This software is not free!   If you wish to use this package please contact the developer.   
You must have permission to use this application.   More than anything it is so I know where
it is being used, basically I want somthing back from the people that use this software, 
I want to know where it is being used.   If you can do this much for me you can use it, if
not, don't use it or steps to protect this copyright will be taken. 

Thanks and hope to hear from you soon :)

NOTE: This copyright must stay in place in order to use this software!

** COPYRIGHT NOTICE ****************************/

ssmenu.Registry 	= []
ssmenu.mLength 		= 200
ssmenu.hideDelay 	= 1000
ssmenu.minCPUResolution = 10
ssmenu.defaultMenu	= "";

// IE bug fix for this sites menus, our expanding tables resize the window vertically so we dont want to resize or refresh.   This can be fixed with a function very nicely, just dont have time to write it, email me and I will send it as I am sure I will haev it done by the time you read this :)
var stopResize	= false;

function ssmenu(id, dir, left, top, width, height)
{
	// We add this for positioning for this specific site
	//coordinates 	= getAnchorPosition( 'menu1' );
	
	coordinates 	= getAnchorPosition( id );
	left			= left + coordinates.x;
	top				= top + coordinates.y;

	
	//alert( top +'<---->'+ left );

	this.ie  = document.all ? 1 : 0
	this.ns4 = document.layers ? 1 : 0
	this.dom = document.getElementById ? 1 : 0

	if (this.ie || this.ns4 || this.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.mTimer	 		= false
		this.open			= false
		this.over			= false
		this.startTime	 	= 0

		this.mReference 	= "ssmenu_"+id
		eval(this.mReference+"=this")

		ssmenu.Registry[id] = this

		var d = document

        var staticStyle = '<style type="text/css">';
        staticStyle += '#' + this.id + 'Container { visibility:hidden; display: none; '
		staticStyle += 'left:' + left + 'px; '
		staticStyle += 'top:' + top + 'px; '
		staticStyle += 'overflow:hidden; z-index:10000; margin: 0px; }'
		staticStyle += '*html #' + this.id + 'Container {  margin: 0px; }'
		staticStyle += '#' + this.id + 'Container, #' + this.id + 'Content { ';
		staticStyle	+= 'position:absolute; '
		staticStyle += 'width:' + width + 'px; '
		staticStyle += 'height:' + height + 'px; '
		staticStyle += 'clip:rect(0 ' + width + ' ' + height + ' 0); '
		staticStyle += ' }'
        staticStyle += '</style>';

        d.write(staticStyle)

		this.load()
	}
}

ssmenu.setDefaultMenu = function( id )
{
	ssmenu.defaultMenu = id;
	ssmenu.showMenu( ssmenu.defaultMenu );
}
	
ssmenu.prototype.load = function() {
	var d 		= document
	var mId1 	= this.id + "Container"
	var mId2 	= this.id + "Content"
	var obj1 	= this.dom ? d.getElementById(mId1) : this.ie ? d.all[mId1] : d.layers[mId1]

	if (obj1) var obj2 = this.ns4 ? obj1.layers[mId2] : this.ie ? d.all[mId2] : d.getElementById(mId2)

	var temp

	if (!obj1 || !obj2) window.setTimeout(this.mReference + ".load()", 100)
	else {
		this.container	= obj1
		this.menu	= obj2
		this.style	= this.ns4 ? this.menu : this.menu.style
		this.homePos	= eval("0" + this.dirType + this.dim)
		this.outPos	= 0
		this.accelConst	= (this.outPos - this.homePos) / ssmenu.mLength / ssmenu.mLength 

		if (this.ns4) this.menu.captureEvents(Event.MOUSEOVER | Event.MOUSEOUT);
		this.menu.onmouseover = new Function("ssmenu.showMenu('" + this.id + "')")
		this.menu.onmouseout = new Function("ssmenu.hideMenu('" + this.id + "')")

		this.endSlide()
	}
}
	
ssmenu.showMenu = function(id, parent)
{
	var reg = ssmenu.Registry
	var obj = ssmenu.Registry[id]
	//alert(id);
	
	if ( parent ){ 
		var obj2 = ssmenu.Registry[parent]
	}
	
	if (obj && obj.container) {
		obj.over = true

		for (menu in reg){
			if (id != menu && parent != menu) ssmenu.hide(menu)
		}

		if (obj.hideTimer) { reg[id].hideTimer = window.clearTimeout(reg[id].hideTimer) }
		if (parent){
			if (obj2.hideTimer) { reg[parent].hideTimer = window.clearTimeout(reg[parent].hideTimer) }
		}

		if (!obj.open && !obj.mTimer) reg[id].startSlide(true)
	}
}

ssmenu.hideMenu = function(id)
{
	
	var obj = ssmenu.Registry[id]

	if (obj.container) {
		if (obj.hideTimer) window.clearTimeout(obj.hideTimer)
		if ( ssmenu.defaultMenu != id )
		{
			obj.hideTimer = window.setTimeout("ssmenu.hide('" + id + "', true )", ssmenu.hideDelay);
		}
	}


}

ssmenu.hide = function(id, always )
{
	var obj 	= ssmenu.Registry[id]
	obj.over 	= false

	if (obj.hideTimer) window.clearTimeout(obj.hideTimer)
	
	obj.hideTimer = 0

	if (obj.open && !obj.mTimer) obj.startSlide(false)
	
	if ( always && ssmenu.showMenu != "" ) 
	{
		ssmenu.showMenu( ssmenu.defaultMenu );
	}
}

ssmenu.prototype.startSlide = function(open) {

	this[open ? "onactivate" : "ondeactivate"]()
	this.open 	= open

	if (open) this.setVisibility(true)

	this.startTime = (new Date()).getTime()	
	this.mTimer = window.setInterval(this.mReference + ".slide()", ssmenu.minCPUResolution)
}

ssmenu.prototype.slide = function() {

	var elapsed 	= (new Date()).getTime() - this.startTime

	if (elapsed > ssmenu.mLength) this.endSlide()
	else {
		var d = Math.round(Math.pow(ssmenu.mLength-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)
	}
}

ssmenu.prototype.endSlide = function() {

	this.mTimer 	= window.clearTimeout(this.mTimer)
	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)
	}
}

ssmenu.prototype.setVisibility = function(bShow) { 

	var s 	= this.ns4 ? this.container : this.container.style

	s.visibility = bShow ? "visible" : "hidden"
	s.display = bShow ? "block" : "none"
}

ssmenu.prototype.moveTo = function(p) { 

	this.style[this.orientation == "h" ? "left" : "top"] = this.ns4 ? p : p + "px"
}

ssmenu.prototype.getPos = function(c) {

	return parseInt(this.style[c])
}

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

/*
 * Get the Anchor Position
 */

function getAnchorPosition( anchorname ) {

	var useWindow	= false;
	var coordinates	= new Object();
	var x			= 0;
	var y			= 0;
	var use_gebi	= false;	
	var use_css		= false;
	var use_layers	= false;

	if (document.getElementById) { 
		use_gebi	= true; 
	}
	else if (document.all) { 
		use_css		= true; 
	}
	else if (document.layers) { 
		use_layers	= true; 
	}
	
	if (use_gebi && document.all) {
		x	= AnchorPosition_getPageOffsetLeft(document.all[anchorname]);
		y	= AnchorPosition_getPageOffsetTop(document.all[anchorname]);
	}
	else if (use_gebi) {
		var o=document.getElementById(anchorname);
		x	= AnchorPosition_getPageOffsetLeft(o);
		y	= AnchorPosition_getPageOffsetTop(o);
	}
 	else if (use_css) {
		x	= AnchorPosition_getPageOffsetLeft(document.all[anchorname]);
		y	= AnchorPosition_getPageOffsetTop(document.all[anchorname]);
	}
	else if (use_layers) {
		var found	= 0;
		for (var i = 0; i < document.anchors.length; i++) {
			if (document.anchors[i].name==anchorname) { 
				found=1; 
				break; 
			}
		}
		if (found==0) {
			coordinates.x	= 0; 
			coordinates.y	= 0; 
			return coordinates;
		}
		x	= document.anchors[i].x;
		y	= document.anchors[i].y;
	}
	else {
			coordinates.x	= 0; 
			coordinates.y	= 0; 
			return coordinates;
	}

	coordinates.x	= x;
	coordinates.y	= y;

	return coordinates;
}

function AnchorPosition_getPageOffsetLeft (el) {
	var ol	= el.offsetLeft;
	while ((el=el.offsetParent) != null) { 
		ol 	+= el.offsetLeft; 
	}
	
	return ol;
}

function AnchorPosition_getWindowOffsetLeft (el) {
	return AnchorPosition_getPageOffsetLeft(el)-document.body.scrollLeft;
}	

function AnchorPosition_getPageOffsetTop (el) {
	var ot	= el.offsetTop;
	while((el=el.offsetParent) != null) { 
		ot += el.offsetTop; 
	}
	
	return ot;
}

function AnchorPosition_getWindowOffsetTop (el) {
	return AnchorPosition_getPageOffsetTop(el)-document.body.scrollTop;
}