/**
*
*  Simple Context Menu
*  http://www.webtoolkit.info/
*
**/

var SimpleContextMenu = {

    // private attributes
    _menus : new Array,
    _attachedElement : null,
    _menuElement : null,
    _preventDefault : true,
    _preventForms : true,
    _menuWidth : null,
    _menuHeight : null,


    // public method. Sets up whole context menu stuff..
    setup : function (conf) {

	var agt=navigator.userAgent.toLowerCase();

        if ( document.all && document.getElementById && !window.opera ) {
            SimpleContextMenu.IE = true;
        }

        if ( !document.all && document.getElementById && !window.opera ) {
            SimpleContextMenu.FF = true;
        }

        if ( document.all && document.getElementById && window.opera ) {
            SimpleContextMenu.OP = true;
        }

	// L.K.: added a check for Safari 
        if (agt.indexOf("safari") != -1)  {
            SimpleContextMenu.SA = true;

        }

	// edited to make positioning work in Safari as well
        if ( SimpleContextMenu.IE || SimpleContextMenu.FF || SimpleContextMenu.SA) {

            document.oncontextmenu = SimpleContextMenu._show;
            document.onclick = SimpleContextMenu._hide;

            if (conf && typeof(conf.preventDefault) != "undefined") {
                SimpleContextMenu._preventDefault = conf.preventDefault;
            }

            if (conf && typeof(conf.preventForms) != "undefined") {
                SimpleContextMenu._preventForms = conf.preventForms;
            }

        }

    },


    // public method. Attaches context menus to specific class names
    attach : function (classNames, menuId) {

        if (typeof(classNames) == "string") {
            SimpleContextMenu._menus[classNames] = menuId;
        }

        if (typeof(classNames) == "object") {
            for (x = 0; x < classNames.length; x++) {
                SimpleContextMenu._menus[classNames[x]] = menuId;
            }
        }


    },


    // private method. Get which context menu to show
    _getMenuElementId : function (e) {

        if (SimpleContextMenu.IE) {
            SimpleContextMenu._attachedElement = event.srcElement;
        } else {
            SimpleContextMenu._attachedElement = e.target;
        }

        while(SimpleContextMenu._attachedElement != null) {
            var className = SimpleContextMenu._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 (SimpleContextMenu._menus[classArray[i]]) {
                        return SimpleContextMenu._menus[classArray[i]];
                    }
                }
            }

            if (SimpleContextMenu.IE) {
                SimpleContextMenu._attachedElement = SimpleContextMenu._attachedElement.parentElement;
            } else {
                SimpleContextMenu._attachedElement = SimpleContextMenu._attachedElement.parentNode;
            }
        }

        return null;

    },


    // private method. Shows context menu
    _getReturnValue : function (e) {

        var returnValue = true;
        var evt = SimpleContextMenu.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 (!SimpleContextMenu._preventForms) {
                    returnValue = true;
                } else {
                    returnValue = false;
                }
            } else {
                if (!SimpleContextMenu._preventDefault) {
                    returnValue = true;
                } else {
                    returnValue = false;
                }
            }
        }

        return returnValue;

    },


    // private method. Shows context menu
    _show : function (e) {

        SimpleContextMenu._hide();
        var menuElementId = SimpleContextMenu._getMenuElementId(e);

        if (menuElementId) {
            var mousePos = SimpleContextMenu._getMousePosition(e);

            var windowL = SimpleContextMenu._getPageLeft();
            var windowT = SimpleContextMenu._getPageTop();
            var windowR = SimpleContextMenu._getPageRight();
            var windowB = SimpleContextMenu._getPageBottom();
            
            SimpleContextMenu._menuElement = document.getElementById(menuElementId);
            var menuL = mousePos.x;
            var menuT = mousePos.y;
            
            // Make the menu visible
            SimpleContextMenu._menuElement.style.left = menuL + 'px';
            SimpleContextMenu._menuElement.style.top = menuT + 'px';
            SimpleContextMenu._menuElement.style.display = 'block';
            
            // Now we can get the rendered size
            SimpleContextMenu._menuElement.style.width="auto";
            SimpleContextMenu._menuElement.style.height="auto";
            
            // If it would show off-screen, move it back
            var menuR = menuL + SimpleContextMenu._menuElement.offsetWidth;
            if (menuR > windowR) {
            	var offsX = menuR - windowR;
            	menuL -= offsX;
	            SimpleContextMenu._menuElement.style.left = menuL + 'px';
            }
            var menuB = menuT + SimpleContextMenu._menuElement.offsetHeight;
            if (menuB > windowB) {
            	var offsY = menuB - windowB;
            	menuT -= offsY;
    	        SimpleContextMenu._menuElement.style.top = menuT + 'px';
            }
            
            return false;
        }

        return SimpleContextMenu._getReturnValue(e);

    },


    // private method. Hides context menu
    _hide : function () {

        if (SimpleContextMenu._menuElement) {
            SimpleContextMenu._menuElement.style.display = 'none';
        }

    },


    // private method. Returns mouse position
    _getMousePosition : function (e) {

        if (SimpleContextMenu.IE) {
            if (document.documentElement.scrollTop) {
                var scrollTop = document.documentElement.scrollTop;
                var scrollLeft = document.documentElement.scrollLeft;
            } else {
                var scrollTop = document.body.scrollTop;
                var scrollLeft = document.body.scrollLeft;
            }
        }

        if (SimpleContextMenu.FF || SimpleContextMenu.SA) {
            			var scrollTop = window.pageYOffset;
            			var scrollLeft = window.pageXOffset;
        }
 

  	var evt = SimpleContextMenu.IE ? window.event : e;
	
	//L.K.: Safari is clever and knows mouse position after scrolling
     	if (SimpleContextMenu.SA) {
	var mX = evt.clientX;
        var mY = evt.clientY;
	}
		else {
        		var mX = scrollLeft + evt.clientX;
        		var mY = scrollTop + evt.clientY

		}


        	return { 'x' : mX, 'y' : mY };
    },
    
    // Browser Window Size and Position
	// Developed from open source code authored by Stephen Chapman

	_getPageWidth : function () {
		var w = (window.innerWidth != null) 
				? window.innerWidth 
				: (document.documentElement && document.documentElement.clientWidth)
					? document.documentElement.clientWidth 
					: (document.body != null)
						? document.body.clientWidth 
						: null;

		// have to allow for scrollbars on firefox etc
		if (navigator.appName == "Netscape") w -= 16;

		return w;
	},
	
	_getPageHeight : function () {
		var h = (window.innerHeight != null)
				? window.innerHeight 
				: (document.documentElement && document.documentElement.clientHeight)
					?  document.documentElement.clientHeight 
					: (document.body != null)
						? document.body.clientHeight 
						: null;
						
		// have to allow for scrollbars on firefox etc
		if (navigator.appName == "Netscape") h -= 16;

		return h;		
	},
	
	_getPageLeft : function () {
		return (typeof window.pageXOffset != 'undefined')
				? window.pageXOffset 
				: (document.documentElement && document.documentElement.scrollLeft)
					? document.documentElement.scrollLeft 
					: (document.body.scrollLeft)
						? document.body.scrollLeft 
						: 0;
	},
	
	_getPageTop : function () {
		return (typeof window.pageYOffset != 'undefined')
				?  window.pageYOffset 
				: (document.documentElement && document.documentElement.scrollTop)
					? document.documentElement.scrollTop 
					: (document.body.scrollTop)
						? document.body.scrollTop 
						: 0;
	},
	
	_getPageRight : function () {
		return SimpleContextMenu._getPageLeft() + SimpleContextMenu._getPageWidth();
	},
	
	_getPageBottom : function () {
		return SimpleContextMenu._getPageTop() + SimpleContextMenu._getPageHeight();
	}
                    
}
