/*
    Disclaimer

    While we make every effort to ensure that this code is fit for its intended purpose, we
    make no guarantees as to its functionality. CoreTrek AS will accept no
    responsibility for the loss of data or any other damage or financial loss caused by use
    of this code.


    Copyright

    This programming code is copyright of CoreTrek AS. Permission to run this code is given to
    approved users of CoreTrek's publishing system CorePublish.

    This source code may not be copied, modified or otherwise repurposed for use by a third
    party without the written permission of CoreTrek AS.

    Contact webmaster@coretrek.com for information.
*/

/*
 * Get the absolute position of a given element
 *
 * This function is used by Appbase Javascript utils.
 * Do not change!
 */
function getAbsolutePos(el){
    for (var lx=0,ly=0;el!=null;
    lx+=el.offsetLeft,ly+=el.offsetTop,el=el.offsetParent);
    return {x:lx,y:ly}
}

/*
 * Set the layer with the given id to
 * display:block.
 *
 * This function is used by Appbase Javascript utils.
 * Do not change!
 */
function displayLayer(layerId) {
    if(typeof $(layerId) != "undefined") {
        $(layerId).style.display = 'block';
    }
}

/*
 * Set the layer with a given id to
 * display:inline, position:absolute
 *
 * This function is used by Appbase Javascript utils.
 * Do not change!
 */
function hoverLayer(layerId) {
    if(typeof $(layerId) != "undefined") {
        $(layerId).style.display = 'inline';
        $(layerId).style.position = 'absolute';
    }
}

/*
 * Set a layer to display:none
 *
 * This function is used by Appbase Javascript utils.
 * Do not change!
 */
function hideLayer(layerId) {
    if(typeof $(layerId) != "undefined") {
        $(layerId).style.display = 'none';
    }
}

/*
 * Delegate function used by the hovermenu to hide
 * all hovering layers.
 *
 * This function is used by Appbase Javascript utils.
 * Do not change!
 */
var hideAllHovermenuItems = function(id) {
    hovermenus = $('hovermenu-' + id).getElementsByClassName('hovermenuitem');
    for(i=0;i<hovermenus.length;i++) {
       hovermenus[i].style.display = 'none';
    }
}

/*
 * Function to get the mouse position from event
 *
 * This function is used by Appbase Javascript utils.
 * Do not change!
 */
function getMousePosition(event) {
    var pos;
    
    if (!event) var event = window.event;
	if (event.pageX || event.pageY) 	{
		posx = event.pageX;
		posy = event.pageY;
	}
    else if (event.clientX || event.clientY) 	{
		posx = event.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
		posy = event.clientY + document.body.scrollTop + document.documentElement.scrollTop;
	}
	
	return {x:posx,y:posy};
}

/*
 * Display a hovering popup layer.
 *
 * This function is used by Appbase Javascript utils.
 * Do not change!
 *
 * Layer will be absolute positioned and positioned
 * according to options. Default is to display the
 * popup with a small right offset to the mouse pointer.
 *
 * Options has to be of type Object, and the following
 * assosiated attributes can be used to configure popup:
 *   - x, override the x offset
 *   - y, override the y offset
 *   - fixedPosition, layer will be positioned at the fixed x/y position
 */
function popupLayerEvent(event, layerId, options) {
    if($(layerId) != null) {
        pos = getMousePosition(event);
        
        if(typeof options == "undefined") {
            var options = new Object();
        }
        
        var offsetY = 18;
        var offsetX = 14;
        if(typeof options['x'] != "undefined") {
            offsetX = options['x'];
        }
        
        if(typeof options['y'] != "undefined") {
            offsetY = options['y'];
        }
        
        var element = $(layerId);
        
        element.style.display = 'block';
        element.style.position = 'absolute';
                
        if(typeof options['fixedPosition'] == "undefined" || options['fixedPosition'] == false) {
            element.style.left = pos['x'] + offsetX + 'px';
            element.style.top = pos['y'] + offsetY + 'px';
        }
    }
}

