// Module Buttons

window.onresize = showModuleButtons;

var moduleContainerButtons = new Array();
var moduleItemContainerButtons = new Array();
var moduleButtonsTimeout;
var visibleModuleContainer = true;

function registerModuleContainerButton(id) {
	moduleContainerButtons[moduleContainerButtons.length] = id; 
	clearTimeout(moduleButtonsTimeout);
	moduleButtonsTimeout = setTimeout("showModuleButtons()", 400);
}

function registerModuleItemContainerButton(id) {
	moduleItemContainerButtons[moduleItemContainerButtons.length] = id; 
	clearTimeout(moduleButtonsTimeout);
	moduleButtonsTimeout = setTimeout("showModuleButtons()", 400);
}

function showModuleButtons() {	
		
	var visStyle = "visible";
	if (!visibleModuleContainer) {
		visStyle = "hidden";
	}
		
	for (var i = 0; i < moduleContainerButtons.length; i++) {
		var btid = moduleContainerButtons[i];
		var deDiv = document.getElementById(btid);
		var deButton = document.getElementById("modulecontainerbutton_" + btid);
		var deTable = document.getElementById("modulecontainertable_" + btid);
		var x = findPosXOcms(deDiv);
		var y = findPosYOcms(deDiv);
		var w1 = deDiv.offsetWidth;
		var w2 = deTable.offsetWidth;
		
		x += (w1 - w2);		
		deButton.style.left = x + "px";
		deButton.style.top =  y + "px";				
		deButton.style.visibility = visStyle;
	}
	
	for (var i = 0; i < moduleItemContainerButtons.length; i++) {
		var btid = moduleItemContainerButtons[i];
		var deDiv = document.getElementById(btid);
		var deButton = document.getElementById("moduleitemcontainerbutton_" + btid);
		var deTable = document.getElementById("moduleitemcontainertable_" + btid);
		var x = findPosXOcms(deDiv);
		var y = findPosYOcms(deDiv);
		var w1 = deDiv.offsetWidth;
		var w2 = deTable.offsetWidth;
		
		x += (w1 - w2);		
		deButton.style.left = x - 15 + "px";
		deButton.style.top =  y + "px";				
		deButton.style.visibility = visStyle;
	}
}

function findPosXOcms(obj) {
    var curleft = 0; 
    if (obj.offsetParent) {
        while (obj.offsetParent) {
            curleft += obj.offsetLeft - obj.scrollLeft; 
            obj = obj.offsetParent; 
        } 
    } else if (obj.x) {
        curleft += obj.x; 
    }
    return curleft; 
}

function findPosYOcms(obj) {
    var curtop = 0; 
    if (obj.offsetParent) {
        while (obj.offsetParent) { 
            curtop += obj.offsetTop - obj.scrollTop; 
            obj = obj.offsetParent; 
        }
    } else if (obj.y) {
        curtop += obj.y;
    }
    return curtop;
}

// Module Buttons Hover

function activateOcms(id) {
	var el = document.getElementById(id);
	if (el.className == "editor_norm") {
	    el.className = "editor_over";
	}
	var bt = document.getElementById("bt_" + id);
	if (bt != null) {
		bt.style.backgroundImage = "url(images/buttons/directedit_op.png)";
	}
}

function deactivateOcms(id) {
	var el = document.getElementById(id);
	if (el.className == "editor_over") {
		el.className = "editor_norm";
	}
	var bt = document.getElementById("bt_" + id);
	if (bt != null) {
		bt.style.backgroundImage = "url(images/buttons/directedit_cl.png)";
	}
}

// Module Buttons Context Menus

function ShowModuleContainerContextMenu(button_id, menu_id){
    var bt = document.getElementById(button_id);
	var x = findPosXOcms(bt) - 100;
	var y = findPosYOcms(bt) + 15;
    igmenu_showMenu(menu_id, null, x+"px",y+"px");
}

function ShowModuleItemContainerContextMenu(button_id, menu_id){
    var bt = document.getElementById(button_id);
	var x = findPosXOcms(bt) - 100;
	var y = findPosYOcms(bt) + 15;
    igmenu_showMenu(menu_id, null, x+"px",y+"px");
}
