//*******************************************************************************************
// CONTEXTUAL MENU SCRIPT	*  VERSION 1.1												   //	
// --------------------------------------------------------------------------------------  //
//																						   //
// Created 09.10.2006 * Modified 18.10.2006	* Respect to Jeremy Meigs, Supersloper.net 	   //
// www.pixilator.com - the designers choice in content management						   //
//																						   //
//																						   //
//*****************************************************************************************//
//																						   //
// CONTEXTUAL MENU SCRIPT	*  VERSION 1.1												   //
// ----------------------------------------------------------------------------------------//




var PixiContextualMenu;
function showMenu (oEvent,PixiContextualMenu)
{
    if (oEvent.button != 2)
    {
       
        return;
    }
    else
    {
    	
    	
    	var oDiv = createDiv(PixiContextualMenu, oEvent);
        drawMenu(PixiContextualMenu);
       
    	
        
    }


}

var NotMenu;

function RemoveMenu (oEvent,sDiv)
{

    if (oEvent.button != 2)
    {
       
        return;
    }
    else
    {
    	
    	NotMenu = sDiv;
    	
     }


}


function drawMenu (sDiv)
{	

	// CALENDAR - ADD EVENT
	if (sDiv == 'PixiCalendar_AddEvent') {
    var oMenu = new Menu(sDiv);
    oMenu.addItem('+ Ny event', 'document.location.href=\'index.php?PModule=15&PScript=65&CalendarID=' + CalendarID + '&PDate=' + PDate + '&HistoryModule=' + PModule + '&HistoryScript=' + PScript + '&CalendarDate=' + CalendarDate + '\'; removeDiv(\'' + sDiv + '\');','MenuItem1',0,'');
    
    oMenu.drawMenu();
    }
    
    // CALENDAR - DELETE EVENT
    if (sDiv == 'PixiCalendar_DeleteEvent') {
    var oMenu = new Menu(sDiv);
    var ConfirmText = 'Slet event?';
   oMenu.addItem('- Slet event', 'document.location.href=\'index.php?PModule=' + PModule + '&PScript=' + PScript + '&PDate=' + PDate + '&CalendarID=' + CalendarID + '&DeleteEventID=' + EventID + '\'; removeDiv(\'' + sDiv + '\');','MenuItem1',0,'');
  
    oMenu.drawMenu();
    }
    
    // DESKTOP DEFAULT MENU
    if (sDiv == 'PixiDesktop_Default') {
    
		if (NotMenu != 'PixiDesktop_Default') { 
		var oMenu = new Menu(sDiv);
		oMenu.addItem('Ny artikel ->', 'document.location.href=\'index.php?PModule=1&PScript=1\'; removeDiv(\'' + sDiv + '\');','MenuItem1',0,'');
		oMenu.addItem('Nyt billede ->', 'document.location.href=\'index.php?PModule=5&PScript=11\'; removeDiv(\'' + sDiv + '\');','MenuItem2',0,'');
		oMenu.addItem('Nyt dokument ->', 'document.location.href=\'index.php?PModule=3&PScript=3\'; removeDiv(\'' + sDiv + '\');','MenuItem3',0,'');
		oMenu.addItem('Ny Flashfilm ->', 'document.location.href=\'index.php?PModule=18&PScript=34\'; removeDiv(\'' + sDiv + '\');','MenuItem4',0,'');
		oMenu.addItem('Nyt link ->', 'document.location.href=\'index.php?PModule=6&PScript=17\'; removeDiv(\'' + sDiv + '\');','MenuItem5',0,'');
		oMenu.addItem('Ny afstemning ->', 'document.location.href=\'index.php?Module=29&PScript=59\'; removeDiv(\'' + sDiv + '\');','MenuItem6',0,'');
		oMenu.addItem('Nyt sp&oslash;rgeskema ->', 'document.location.href=\'index.php?Module=27&PScript=53\'; removeDiv(\'' + sDiv + '\');','MenuItem7',0,'');
		oMenu.addItem('Nyt Dynamisk element ->', 'document.location.href=\'index.php?PModule=2&PScript=2\'; removeDiv(\'' + sDiv + '\');','MenuItem8',0,'');
		oMenu.addItem('Ny kalender ->', 'document.location.href=\'index.php?PModule=15&PScript=72\'; removeDiv(\'' + sDiv + '\');','MenuItem9',0,'');
		oMenu.addItem('Nyt Nyhedsbrev ->', 'document.location.href=\'index.php?PModule=22&PScript=44\'; removeDiv(\'' + sDiv + '\');','MenuItem10',0,'');
		oMenu.addItem('Ny bruger ->', 'document.location.href=\'index.php?PModule=23&PScript=49\'; removeDiv(\'' + sDiv + '\');','MenuItem10',0,'');
		//oMenu.addItem('ANDRE FUNKTIONER ->', 'document.location.href=\'index.php?PModule=23&PScript=49\'; removeDiv(\'' + sDiv + '\');','MenuItem11',1,'');
		
	
		
		//oMenu.addItem('Artikel', 'document.location.href=\'#\'; removeDiv(\'' + sDiv + '\');','MenuItem12',0,'MenuItem11');
	  
		oMenu.drawMenu();
		}
		
    }
    
    
      // CREATE DESKTOP SHORTCUT
    if (sDiv == 'Pixilator_CreateDesktopShortcut') {
    var oMenu = new Menu(sDiv);
    oMenu.addItem('Opret genvej', 'document.location.href=\'index.php?' + LinkVars + '&AddShortCut=' + DesktopShortcutID + '\'; removeDiv(\'' + sDiv + '\');','MenuItem1',0,'');
  
    oMenu.drawMenu();
    }
    
    
    
    // DESKTOP SHORTCUT MENU
    if (sDiv == 'Pixilator_DesktopShortcutMenu') {
   	
    var oMenu = new Menu(sDiv);
    oMenu.addItem('Slet genvej', 'document.location.href=\'index.php?DeleteShortCut=' + DesktopShortcutID + '\'; removeDiv(\'' + sDiv + '\');','MenuItem1',0,'');
  
    oMenu.drawMenu();
    NotMenu ='';
    }

	 
	 
	 // CalendarListItemMenu
    if (sDiv == 'CalendarListItemMenu') {
    var oMenu = new Menu(sDiv);
    oMenu.addItem('Opret genvej', 'document.location.href=\'index.php?' + LinkVars + '&AddShortCut=' + DesktopShortcutID + '\'; removeDiv(\'' + sDiv + '\');','MenuItem1',0,'');
  
    oMenu.drawMenu();
    }



}
/**
 * Add event listener
 * @param      object     target of listener, usually 'document'
 * @param      type       'mousemove', 'mouseover' etc...
 * @param      func       function to call when listener is triggered
 * @param      boolean    should events bubble up ? (don't think this works properly)
 */
function CM_addEventListener (target, type, func, bubbles)
{
    // mozilla
    if (document.addEventListener)
    {
        target.addEventListener(type, func, bubbles);
    }
    // IE
    else if (document.attachEvent)
    {
        target.attachEvent('on' + type, func, bubbles);
    }
    // others
    else
    {
        target['on' + type] = func;
    }
}

/**
 * Remove event listener
 * @param      object     target of listener, usually 'document'
 * @param      type       'mousemove', 'mouseover' etc...
 * @param      func       function to call when listener is triggered
 * @param      boolean    should events bubble up ? (don't think this works properly)     
 */
function CM_removeEventListener (target, type, func, bubbles)
{
    // mozilla
    if (document.removeEventListener)
    {
        target.removeEventListener(type, func, bubbles);
    }
    // IE
    else if (document.detachEvent)
    {
        target.detachEvent('on' + type, func, bubbles);
    }
    // others
    else
    {
        target['on' + type] = null;
    }
}

/*********************************************************
* MENU OBJECT TO BE USED WITH RIGHT CLICK STUFF
*********************************************************/

/**
 * Menu class for right click menu's
 * @param       string      id of div the menu is going in
 */
function Menu (sDiv)
{
    this.sDiv = sDiv; 
    this.aInnerHtml = new Array();    // Array to hold the menu html
    this.bInnerHtml = new Array();    // Array to hold any sublevel HTML
}

/**
 * Add each menu item
 * @param      string      text for the menu link
 * @param      string      function strings for the onclick
 */





function CreateSubmenuContainer(ThisID,TopOffset,LeftOffset) {

//Get Top, Left and width of Mother Menuitem
var MotherMenuItemTop = getAbsoluteTop(ThisID);

var MotherMenuItemLeft = getAbsoluteLeft(ThisID);
var MotherMenuItemWidth = document.getElementById(ThisID).clientWidth;
var SubMenuContainerTop = MotherMenuItemTop + TopOffset;
var SubMenuContainerLeft = MotherMenuItemLeft + MotherMenuItemWidth + LeftOffset;

var NewSublevelContainer = document.createElement('div');
	NewSublevelContainer.setAttribute('id','SubMenu_MenuItem1');
	NewSublevelContainer.style.position = 'absolute';
	NewSublevelContainer.style.display = 'block';
	NewSublevelContainer.style.top = SubMenuContainerTop; 
	NewSublevelContainer.style.left = SubMenuContainerLeft; 
	NewSublevelContainer.setAttribute('class','ContextSubMenuOuter');
	NewSublevelContainer.setAttribute('className','ContextSubMenuOuter');
	NewSublevelContainer.setAttribute('ContextSubMenu','ThisID');
	//NewSublevelContainer.style.border = 'solid red 10px';
 
	document.body.appendChild(NewSublevelContainer);
	//document.getElementById('SubMenu_'+ThisID).innerHTML = '<div ContextSubMenu=\'1\' id=\'dennis\'>test</div>';
}

Menu.prototype.addItem = function (sText, sDoThis,ThisID,Mother,MotherID)
{
	
	
	if (Mother == 0 && MotherID == '') {
	
    this.aInnerHtml.push('<div id=\'' + ThisID + '\' style=\'cursor:pointer;\' class=\'ContextMenu_MenuItem_Top\' onmouseover=\"this.className=\'ContextMenu_MenuItem_Top_Hover\'\" onmouseout=\"this.className=\'ContextMenu_MenuItem_Top\'\" onclick=\"' + sDoThis + '\">');
    this.aInnerHtml.push(sText);
    this.aInnerHtml.push('</div>'); 
    
    } else if (Mother == 1 && MotherID == '') {
    
    this.aInnerHtml.push('<div id=\'' + ThisID + '\' style=\'cursor:pointer;\' class=\'ContextMenu_MenuItem_Top\' onmouseover=\"CreateSubmenuContainer(\'' + ThisID + '\',0,0); this.className=\'ContextMenu_MenuItem_Top_Hover\'\" onmouseout=\"this.className=\'ContextMenu_MenuItem_Top\'\">');
    this.aInnerHtml.push(sText);
    this.aInnerHtml.push('</div>'); 
  
    } else if (MotherID != '') {
    
    	if (Mother == 0) {
			
			this.bInnerHtml.push('<div id=\'' + ThisID + '\' style=\'cursor:pointer;\' class=\'ContextMenu_MenuItem_Top\' onmouseover=\"this.className=\'ContextMenu_MenuItem_Top_Hover\'\" onmouseout=\"this.className=\'ContextMenu_MenuItem_Top\'\" onclick=\"' + sDoThis + '\">');
			this.bInnerHtml.push(sText);
			this.bInnerHtml.push('</div>'); 
		  
  
    	} else {
    	
    		this.bInnerHtml.push('<div id=\'' + ThisID + '\' style=\'cursor:pointer;\' class=\'ContextMenu_MenuItem_Top\' onmouseover=\"CreateSubmenuContainer(\'' + ThisID + '\',0,0); this.className=\'ContextMenu_MenuItem_Top_Hover\'\" onmouseout=\"this.className=\'ContextMenu_MenuItem_Top\'\">');
			this.bInnerHtml.push(sText);
			this.bInnerHtml.push('</div>'); 
  
    	}
    }
}

/**
 * Draw the menu with innerHTML
 */
Menu.prototype.drawMenu = function ()
{
    document.getElementById(this.sDiv).innerHTML = this.aInnerHtml.join("\n");
    
}

/*********************************************************
* RIGHT CLICK STUFF
*********************************************************/

var gClickHandler;

/**
 * Create a div using the DOM
 * @param         string         id of div to be created
 * @param         event          mousedown event
 * @return        obj            new div
 */
function createDiv (sDiv, oEvent)
{
    var obj = document.getElementById(sDiv)
    // if the object is already showing, simply move the position
    if (obj)
    {
        obj.style.top = oEvent.clientY + 'px';
        obj.style.left = oEvent.clientX + 'px';    
    }
    else
    {
        var obj = document.createElement('div');
    	obj.id = sDiv;
        obj.style.position = 'absolute';
        obj.style.top = oEvent.clientY + 'px';
        obj.style.left = oEvent.clientX + 'px';
        obj.setAttribute('className','ContextMenuOuter');
        obj.setAttribute('class','ContextMenuOuter');
        
    }
    document.body.insertBefore(obj, document.body.childNodes[0]);
    
    
    
    //var GetSubMenus = new Array(getElementsByAttribute(document.body, "*", "ContextSubMenu"));
    //alert(GetSubMenus['MenuItem1']);
   // document.getElementById(GetSubMenus[0]).innerHTML = this.bInnerHtml.join("\n");
    
    //GetSubMenus[0].innerHTML = this.bInnerHtml.join("\n");
    //GetSubMenus[0].innerHTML = 'dennis';   
    
    
    gClickHandler = function() { setTimeout('removeDivFromHandler(\'' + sDiv + '\');', 100); }
    
    CM_addEventListener(document, 'keypress', gClickHandler, false);
    CM_addEventListener(document, 'click', gClickHandler, false);
    return obj;
}



function removeDiv (sDiv)
{	

    var obj = document.getElementById(sDiv);
    if (obj) obj.parentNode.removeChild(obj);
}

function removeDivFromHandler (sDiv)
{   
    removeDiv(sDiv);
   CM_removeEventListener(document, 'keypress', gClickHandler, false);
   CM_removeEventListener(document, 'click', gClickHandler, false);
}

// disable the windows right click context menu
document.oncontextmenu = function () { return false };     


// **************************************************************************************** //
//  									END												    //	
// **************************************************************************************** //
