

var _divList = new Array();
 var _currentSelectElement = null;

jQuery.fn.extend({

    jsRightMenu: function(options,divName) {
        options = $.extend({
            menuList: []
        }, options);
        
        return this.each(function() {
            this.oncontextmenu = function() {
            
                var objMenu = $("#"+divName+"");
                if (objMenu.size() == 0) {
                     var menuCount = options.menuList.length;
                    if (menuCount > 0) {
                        var divMenuList = "<div id=\""+divName+"\" class=\"div_RightMenu\"><span id='_eventSrc'></span><ol class='MM'>";
                        for (var i = 0; i < menuCount; i++) {
                            divMenuList += "<Li onclick=\"" + options.menuList[i].clickEvent + 
                            "\"><a href='javascript:void(0)'> " + options.menuList[i].menuName + "</a></li>";
                        }
                        divMenuList += "</ol></div>";
                        $(this).append(divMenuList);
                      
                        objMenu = $("#"+divName+"");
                        _divList.push(divName);
                    }
                }
                for(var index=0;index<_divList.length;index++){
                    var menu = $("#"+_divList[index]+"");
                    if (menu!=objMenu&&menu.size() > 0) menu.hide();
                }
                objMenu.hide();

                var event = arguments[0] || window.event;
                 if(_currentSelectElement!=null){_currentSelectElement.style.backgroundColor="#fff";}
                _currentSelectElement = event.srcElement ? event.srcElement : event.target;
                _currentSelectElement.style.backgroundColor="#f0fff0";
                
                var clientX = event.clientX;
                var clientY = event.clientY;
                
                var redge = document.body.clientWidth - clientX;
                var bedge = document.body.clientHeight - clientY;
                var menu = objMenu.get(0);
                var menuLeft = 0;
                var menuTop = 0;
                if (redge < menu.offsetWidth)
                    menuLeft = (document.documentElement.scrollLeft || document.body.scrollLeft)  + clientX - menu.offsetWidth;
                else
                    menuLeft = (document.documentElement.scrollLeft || document.body.scrollLeft)  + clientX;
                if (bedge < menu.offsetHeight)
                    menuTop =  (document.documentElement.scrollTop || document.body.scrollTop) + clientY - menu.offsetHeight;
                else
                    menuTop =  (document.documentElement.scrollTop || document.body.scrollTop) + clientY;
                    
                objMenu.css({ top: menuTop + "px", left: menuLeft + "px" });
  
                objMenu.show();
                return false;
            }
         
   
            document.onclick = function() {
                for(var index=0;index<_divList.length;index++){
                    var objMenu = $("#"+_divList[index]+"");
                    
                    if (objMenu.size() > 0) objMenu.hide();
                }
            }
        });
    }
});
