﻿var SCM = {
	_menus : new Array,
	_attachedElement : null,
	_menuElement : null,
	_preventDefault : true,
	_preventForms : true,

	setup : function (conf) {

		if ( document.all && document.getElementById && !window.opera ) {
			SCM.IE = true;
		}

		if ( !document.all && document.getElementById && !window.opera ) {
			SCM.FF = true;
		}

		if ( document.all && document.getElementById && window.opera ) {
			SCM.OP = true;
		}

		if ( SCM.IE || SCM.FF ) {

			document.onkeydown = SCM._hide;

			if (conf && typeof(conf.preventDefault) != "undefined") {
				SCM._preventDefault = conf.preventDefault;
			}

			if (conf && typeof(conf.preventForms) != "undefined") {
				SCM._preventForms = conf.preventForms;
			}

		}

	},
	attach : function (classNames, menuId) {

		if (typeof(classNames) == "string") {
			SCM._menus[classNames] = menuId;
		}

		if (typeof(classNames) == "object") {
			for (x = 0; x < classNames.length; x++) {
				SCM._menus[classNames[x]] = menuId;
			}
		}

	},
	
	show : function(e)
	{	    
	    SCM._show(e);
	
	},

	hide : function(e)
	{	    
	    SCM._hide();
	
	},

	_getMenuElementId : function (e) {

		if (SCM.IE) {
			SCM._attachedElement = event.srcElement;
		} else {
			SCM._attachedElement = e.target;
		}

		while(SCM._attachedElement != null) {
			var className = SCM._attachedElement.className;

			if (typeof(className) != "undefined") {
				className = className.replace(/^\s+/g, "").replace(/\s+$/g, "");
				var classArray = className.split(/[ ]+/g);

				for (i = 0; i < classArray.length; i++) {
					if (SCM._menus[classArray[i]]) {
						return SCM._menus[classArray[i]];
					}
				}
			}

			if (SCM.IE) {
				SCM._attachedElement = SCM._attachedElement.parentElement;
			} else {
				SCM._attachedElement = SCM._attachedElement.parentNode;
			}
		}

		return null;

	},

	_getReturnValue : function (e) {

		var returnValue = true;
		var evt = SCM.IE ? window.event : e;

		if (evt.button != 1) {
			if (evt.target) {
				var el = evt.target;
			} else if (evt.srcElement) {
				var el = evt.srcElement;
			}

			var tname = el.tagName.toLowerCase();

			if ((tname == "input" || tname == "textarea")) {
				if (!SCM._preventForms) {
					returnValue = true;
				} else {
					returnValue = false;
				}
			} else {
				if (!SCM._preventDefault) {
					returnValue = true;
				} else {
					returnValue = false;
				}
			}
		}

		return returnValue;

	},


	_show : function (e) {

		SCM._hide();
		var menuElementId = SCM._getMenuElementId(e);

		if (menuElementId) {
			var m = SCM._getMousePosition(e);
			var s = SCM._getScrollPosition(e);

			SCM._menuElement = document.getElementById(menuElementId);
			SCM._menuElement.style.left = m.x + s.x + 'px';
			SCM._menuElement.style.top = m.y + s.y + 'px';
			SCM._menuElement.style.display = 'block';
			return false;
		}

		return SCM._getReturnValue(e);

	},


	_hide : function () {

		if (SCM._menuElement) {
			SCM._menuElement.style.display = 'none';
		}

	},


	_getMousePosition : function (e) {

		e = e ? e : window.event;
		var position = {
			'x' : e.clientX,
			'y' : e.clientY
		};

		return position;

	},

	_getScrollPosition : function () {

		var x = 0;
		var y = 0;

		if( typeof( window.pageYOffset ) == 'number' ) {
			x = window.pageXOffset;
			y = window.pageYOffset;
		} else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
			x = document.documentElement.scrollLeft;
			y = document.documentElement.scrollTop;
		} else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
			x = document.body.scrollLeft;
			y = document.body.scrollTop;
		}

		var position = {
			'x' : x,
			'y' : y
		};

		return position;

	}

};