function submenu (id, o) {

	var self			= this;

	var init = function () {
		var object = document.getElementById(id);

		closeOthers(object.parentNode, object);

		if (!object.rel) {
			object.className	= object.className.replace(' closed', ' open');
			object.rel			= true;
		}

	};

	var closeOthers = function (parentNode, object) {
		for ( var i in parentNode.children ) {
			if (parentNode.children[i].className !== undefined) {
				var splitter = parentNode.children[i].className.split(' ');

				if (splitter[0] == 'submenu' && parentNode.children[i] !== object) {
					parentNode.children[i].className = parentNode.children[i].className.replace(' open', ' closed');
					parentNode.children[i].rel = false;
				}
			}
		}
		return;
	};
	
	init();
};


function getMouseXY (e, IE) {
	var tempX = 0;
	var tempY = 0;
	if (IE) { // grab the x-y pos.s if browser is IE
		tempX = event.clientX + document.body.scrollLeft
		tempY = event.clientY + document.body.scrollTop
	} else {  // grab the x-y pos.s if browser is NS
		tempX = e.pageX
		tempY = e.pageY
	}
	  // catch possible negative values in NS4
	if (tempX < 0){tempX = 0}
	if (tempY < 0){tempY = 0}
	  // show the position values in the form named Show
	  // in the text fields named MouseX and MouseY
	var array = [];
		array['x'] = tempX;
		array['y'] = tempY;
	return array;
}

document.onmousemove = function(e) {
	var event = e || window.event;
    var IE = document.all?true:false
	if (!IE) document.captureEvents(Event.MOUSEMOVE)
	var data = getMouseXY(e, IE);
	window.mouseX = data['x'];
	window.mouseY = data['y'];
}


var movementCheck = new function () {
	
	var self		= this;
	
	var submenu_object, li_object;
	var interval, IE;
	
	var init = function () {
		IE = (document.all) ? true : false ;
		
		if (interval === undefined) {
			interval = window.setInterval(function () {
				if (submenu_object !== undefined && li_object !== undefined) {
					if (!intercept(submenu_object) && !intercept(li_object)) {
						self.clearObjects();
					}
				}
			}, 1000); 
		}
	};
	
	// Private functions
	var intercept = function (object) {
		var position	= get_objectPosition(object);
		if (window.mouseX >= position.left && window.mouseX <= position.right && 
			window.mouseY >= position.top && window.mouseY <= position.bottom)
			return true;
			
		return false;
	};
	
	var get_objectPosition = function (object) {
		var data = [];
		data['left'] 	= getRealXPos(object);
		data['top']		= getRealYPos(object);
		
		//if (getStyle(object, ((!IE)? 'margin-left' : 'marginLeft')) !== false) 
			//data.left = data.left + (parseFloat(getStyle(object, ((!IE)? 'margin-left' : 'marginLeft'))) * 2);
		
		data['right']	= data['left'] + object.offsetWidth;
		data['bottom']	= data['top'] + object.offsetHeight;
		
		return data;
	};
	
	var getStyle = function (x, styleProp)
	{	
		if (x.currentStyle !== undefined)
			var y = x.currentStyle[styleProp];
		else if (window.getComputedStyle)
			var y = document.defaultView.getComputedStyle(x,null).getPropertyValue(styleProp);
			
		if (y == '')
			return false;
			
		return y;
	}
	
	var close = function (o) {
		o.className = o.className.replace('open', 'closed');
		return;
	};
	
	var open = function (o) {
		o.className = o.className.replace('closed', 'open');
		return;
	};
	
	// Public functions
	this.setObjects = function (object, object2) {
		if (submenu_object !== undefined)
			close(submenu_object);
		if (li_object !== undefined)
			close(li_object);
			
		this.clearObjects();
	
		submenu_object = object;
		li_object = object2;
		
		init();
		
		open(submenu_object);
		open(li_object);
		
		return;
	};
	
	this.clearObjects = function () {
		if (submenu_object !== undefined) 
			close(submenu_object);
		if (li_object !== undefined) 
			close(li_object);
		
		window.clearInterval(interval);
		interval = undefined;
		
		submenu_object = undefined;
		li_object = undefined;
		return;
	};
	
	this.isObject = function (object, object2) {
		if (object !== undefined && object2 !== undefined) {
			if (object == submenu_object && object2 == li_object)
				return true;
		} else if (object !== undefined && object2 === undefined) {
			if (object === submenu_object)
				return true;
		} else if (object === undefined && object2 !== undefined) {
			if (object2 === li_object)
				return true;
		}
		return false;
	};
}();

function checkMovements (id, o) {
	
	var object;
	var movement;
	
	var init = function () {
		object		= document.getElementById(id);
		movementCheck.setObjects(object, o);
	};
	
	init();
};
