/** 
 * @fileoverview Cross-browser layers.
 *
 * @author Iain White iain.white@aotgroup.com.au
 * @version 2.1
 */

if (window.JSLib) {
    JSLib.addVersion("DhtmlLayer.js", "DHTML Layers", "2.1");
}

/**
 * {array} DhtmlLayer Global variables
 */
var DhtmlLayer_layers = new Array();

/**
 * {boolean} Page needs IFRAME shims
 */
var DhtmlLayer_pageNeedsIFrame = false;

/**
 * DhtmlLayer - Construct a new DhtmlLayer object.
 * @class This is the basic DhtmlLayer class.
 * This is the core DHTML layer object
 * @param {string} objName The name of this created DhtmlLayer instance.
 * @param {string} layerID (optional) The layerID of the layer. (Default: 'DhtmlLayer_layer_0')
 * @constructor
 * @returns A new DhtmlLayer
 */
function DhtmlLayer(objName)
{
    this.layerID = arguments[1] ? arguments[1] : 'DhtmlLayer_layer_' + DhtmlLayer_layers.length;

    // Properties
    this.objName             = objName;
    this.offsetX             = 10;
    this.offsetY             = 10;
    this.isMouseover         = false;
    this.hasIframe           = false;
    this.cssClass            = 'DhtmlLayer';
    this.imgIcon             = '/libimages/icon.gif';
    this.imgIconWidth        = 16;
    this.imgIconHeight       = 16;
    this.isDragable          = false;       // Flag to use for dragable layers
    this.globalId            = DhtmlLayer_layers.length;
    this.onlyOne             = false;
    this.documentHide        = false;
 
    // Only use IFRAME shim if IE5.5 or 6
    if (sniffer.is_ie5_5up && (!sniffer.is_ie7up || DhtmlLayer_pageNeedsIFrame)) {
        // @todo Use a secure page for IFRAME shim if required
//        if (document.location.protocol != "https:") {
            // Force IFRAME shim
            if (DhtmlLayer_pageNeedsIFrame) {
                this.enableIframe();
            } else {
                // Only use shim if there is a SELECT on the page
                var selectTags = document.getElementsByTagName("select");
                //if (selectTags && selectTags.length > 0) {
                    this.enableIframe();
                //}
            }
        //}
    }

    DhtmlLayer_layers[DhtmlLayer_layers.length] = this;
    this.scrollWithPage = false;
    this.sx = 0;
    this.sy = 0;
}

///////////////////// Class methods /////////////////////

/**
 * Sets the cssClass name
 *
 * @param string cssClass - The new class name
 */
DhtmlLayer.prototype.setClassName = function(className)
{
    if (this.getLayer()) {
        this.cssClass = className;
        this.getLayer().className = className;
    } else {
        this.cssClass = className;
    }
}


/**
 * Sets the innerHTML attribute of the layer
 *
 * @param string html - Some html text.
 */
DhtmlLayer.prototype.setHTML = function(html)
{
    this.getLayer().innerHTML = html;
}


/**
 * Adds to the innerHTML attribute of the layer
 *
 * @param html - The text to append
 */
DhtmlLayer.prototype.addHTML = function(html)
{
    this.getLayer().innerHTML += html;
}

/**
 * Set the only 1 layer visable parameter
 *
 * @param bool value - True to switch on
 */
DhtmlLayer.prototype.setOnlyOne = function(value)
{
    this.onlyOne = value;
}

/**
 * Set parameter to allow click anywhere to close layer
 *
 * @param bool value - True to switch on
 */
DhtmlLayer.prototype.setDocumentHide = function(value)
{
    this.documentHide = value;
}

/**
 * Add clickable icon to activate the layer, using HTML.
 * 
 * @param string (optional) - Text to display as ALT on image or as link
 * @param bool (optional) - If true create text link, insted of image, using text as above
 */
DhtmlLayer.prototype.writeIconHTML = function()
{
    var text = arguments[0] ? arguments[0] : '';
    var url = arguments[1] ? arguments[1] : false;

//        this.objName + '.setLayerPositionToMouse(event); ' + 
//        this.objName + '.keepOnPage(); ' + 
//        this.objName + '.show(); this.blur(); document.stopBubble(event); return false;" class="' +     
    document.write('<a href="#" onClick="' + 
        this.objName + '.IconWriteClick(event); ' + 
            'this.blur(); return false;" class="' + 
        this.cssClass + '_icon" ' +
        'onmouseover="' + this.objName + '.setMouseover(true);" ' +
        'onmouseout="' + this.objName + '.setMouseover(false);">');
    if (!url) {
        document.write('<img src="' + this.imgIcon + '" border="0" alt="' + text + '" width="' + this.imgIconWidth + '" height="' + this.imgIconHeight + '" /></a>');
    } else {
        document.write(text + '</a>');
    }
}

DhtmlLayer.prototype.IconWriteClick = function(e)
{
    obj = this;
    mousePos = getMousePos(e);
    var x = mousePos[0] + obj.offsetX;
    var y = mousePos[1] + obj.offsetY;
    if (sniffer.is_ie) {
        var pScroll = getPagePosScroll(false);
        x = x + pScroll[0];
        y = y + pScroll[1];
    }
    obj.setLayerPosition(x, y);
    obj.keepOnPage();
    obj.show();
    document.stopBubble(e);
    return false;  
}

/**
 * Add clickable icon to activate the layer, using DOM.
 * 
 * @param object (optional) - Element in DOM to add icon to, if not supplied attempt to use layer.
 * @param string (optional) - Text to display as ALT on image or as link
 * @param bool (optional) - If true create text link, insted of image, using text as above
 * @param string (optional) - Keyword on how to position icon relative to parent element
 */
DhtmlLayer.prototype.writeIconDOM = function()
{
    var element = arguments[0] ? arguments[0] : this.getLayer();
    var text = arguments[1] ? arguments[1] : '';
    var url = arguments[2] ? arguments[2] : false;
    var iconPos = arguments[3] ? arguments[3] : "topleft";
    // If we do not have an element then attach to the document root
    if (element == null) {
        element = document.body;
    }
    var a = document.createElement('a');
    a.setAttribute('href', '#');
    a.className = this.cssClass + '_icon';
    a.dhtmlLayer = this.globalId;
    a.onclick = doIconClick;
    a.onmouseover = doMouseOver;
    a.onmouseout = doMouseOut;
    
    function doIconClick(e)
    {
        var obj = DhtmlLayer_layers[this.dhtmlLayer];
        mousePos = getMousePos(e);
        var x = mousePos[0] + obj.offsetX;
        var y = mousePos[1] + obj.offsetY
        obj.setLayerPosition(x, y);
        obj.keepOnPage();
        obj.show();
        this.blur();
        document.stopBubble(e);
        return false;
    }

    function doMouseOver(e)
    {
        var obj = DhtmlLayer_layers[this.dhtmlLayer]
        obj.setMouseover(true);
    }

    function doMouseOut(e)
    {
        var obj = DhtmlLayer_layers[this.dhtmlLayer]
        obj.setMouseover(false);
    }    

    a.style.position = 'absolute';
    if (url) {
        linkText = document.createTextNode(text);
        a.appendChild(linkText);
    } else {
        var img = document.createElement('img');
        img.setAttribute('src', this.imgIcon);
        img.setAttribute('border', '0');
        img.setAttribute('width', this.imgIconWidth);
        img.setAttribute('height', this.imgIconHeight);
        img.setAttribute('alt', text);
        a.appendChild(img);
    }

    if (element != document.body && element != document.documentElement)
    {
        if (iconPos) {
            if (iconPos == "center") {
                var pos = Position.getCenter(element);
            }
            if (iconPos == "topleft") {
                var pos = Position.get(element);
            }
            if (iconPos == "topright") {
                var pos = Position.getVH(element, 'top', 'right');
            }
            if (iconPos == "bottomright") {
                var pos = Position.getVH(element, 'bottom', 'right');
            }
            if (iconPos == "bottomleft") {
                var pos = Position.getVH(element, 'bottom', 'left');
            }
            if (pos.width > 0) {
                //a.style.left = pos.left;
                //a.style.top = pos.top;
                Position.set(a, pos.left, pos.top)
            }
        }
    }
    element.appendChild(a);
}

/**
 * Set the initial DIV CSS properties
 * 
 * 
 */
DhtmlLayer.prototype.initLayer = function()
{
    var layer = this.getLayer();
    if (layer == null) {
        //debugger;
        alert('Cannot initialise layer!\n In obj, ' + this.objName); 
        return false;
    }

    layer.style.zIndex          = '999';
    layer.style.position        = 'absolute';
    layer.style.visibility      = 'hidden';
    if (this.hasIframe) {
        this.writeIframe();
    }
}


/**
 * Writes HTML for layer.
 * 
 */
DhtmlLayer.prototype.writeLayerHTML = function()
{
    var html = '<div class="' + this.cssClass + '" ' + 
        'id="' + this.layerID + '" onmouseover="' + this.objName + 
        '.setMouseover(true)" onmouseout="' + this.objName + 
        '.setMouseover(false)"></div>';

    window.document.write(html);

    this.initLayer();
}

/**
 * Append layer using DOM
 *
 * @param object (optional) - element in DOm to attach layer to
 */
DhtmlLayer.prototype.writeLayerDOM = function()
{
    //element = arguments[0] ? arguments[0] : this.getLayer();
    element = arguments[0] ? arguments[0] : document.body;
    // If no parent element, use document root
    //if (element == null) {
    //    element = document.body;
    //}
    var div = document.createElement('div');
    div.className = this.cssClass;
    div.setAttribute('id', this.layerID);
    div.layerObjName = this.objName;
    div.dhtmlLayer = this.globalId;
    div.onmouseover = doMouseOver;
    div.onmouseout = doMouseOut;
    element.appendChild(div);
    this.initLayer();
    
    function doMouseOver(e)
    {
        var obj = DhtmlLayer_layers[this.dhtmlLayer]
        obj.setMouseover(true);
    }

    function doMouseOut(e)
    {
        var obj = DhtmlLayer_layers[this.dhtmlLayer]
        obj.setMouseover(false);
    }
}


/**
 * Atach a masking Iframe for IE5.5 up browsers to stop pesky selects box showthrough
 * Only works if in IE5.5+ browser and layer dimension has been set using setLayerDimansion()
 * 
 * NOTE: Only needs to be ie compatable.
 * @access protected
 */
DhtmlLayer.prototype.writeIframe = function()
{
    if (!this.getIframe()) {
        var layer  = this.getLayer();
        var iframe = document.createElement('iframe');
        //todo use a secure page for IFRAME content
        if (document.location.protocol == "https:") {
            var blankFile = globPathPrefix + '/globals/blank.html';
	        iframe.setAttribute('src', blankFile);
        } else {
            iframe.setAttribute('src', 'JavaScript:void(0);');
        }
        iframe.setAttribute('id', this.layerID + '_iframe');
        iframe.setAttribute('frameborder', '0');
        iframe.setAttribute('scrolling', 'no');
        iframe.style.position = 'absolute';
        iframe.style.filter = 'progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=0)';
        iframe.style.zIndex = layer.style.zIndex - 1;
        iframe.style.visibility = 'hidden';
        if (layer.nextSibling) {
            layer.parentNode.insertBefore(iframe, layer.nextSibling);
        } else {
            layer.parentNode.appendChild(iframe);
        }
    }
}

/**
 * Enable Iframe Mask, for absolute layers.
 * NOTE: Must supply width and hight
 * 
 */
DhtmlLayer.prototype.enableIframe = function()
{
    this.hasIframe = true;
}

/**
 * Disable Iframe Mask
 * 
 */
DhtmlLayer.prototype.disableIframe = function()
{
    var iframe = this.getIframe().style.visibility = 'hidden';
    this.hasIframe = false;
}

/**
 * Set the css class of this layer (default: 'DhtmlLayer')
 * 
 * @param string cssClass - The CSS class for this layer
 */
DhtmlLayer.prototype.setCssClass = function(cssClass)
{
    this.cssClass = cssClass;
}

/**
 * Set the icon that pop's up this layer
 * 
 * @param string icon - Path to the image icon to display
 * @param integer w - Width of image
 * @param integer h - Height of image
 */
DhtmlLayer.prototype.setIcon = function(imgIcon, w, h)
{
    this.imgIcon = imgIcon;
    if (w) {
        this.imgIconWidth = w;
    }
    if (h) {
        this.imgIconHeight = h;
    }
}

/**
 * Hides this Layer.
 * 
 */
DhtmlLayer.prototype.hide = function()
{
    var el = this.getLayer();
    el.style.visibility = 'hidden';
    if (this.hasIframe) {
        this.getIframe().style.visibility = 'hidden';
    }
    if (this.documentHide) {
//        document.removeOnClickHandler(this.objName + '.hide');
        document.deactivateOnClickHandler(this.objName + '.hide');
    }
}

/**
 * Shows the main div layer
 * 
 */
DhtmlLayer.prototype.show = function()
{
    if (this.onlyOne) {
        DhtmlLayer.hideIfCan();
    }
    this.getLayer().style.visibility = 'visible';
    if (this.hasIframe) {
        this.getIframe().style.width  = this.getLayer().offsetWidth;
        this.getIframe().style.height = this.getLayer().offsetHeight;
        this.getIframe().style.visibility = 'visible';
    }
    if (this.documentHide) {
        document.addOnClickHandler(this.objName + '.hide');
    }
}

/**
 * Sets the X offset to the mouse position
 * that the div appears at.
 *
 * @param integer Xoffset - Number of pixels for horizontal
 *     offset from mouse position
 */
DhtmlLayer.prototype.setOffsetX = function(Xoffset)
{
    this.offsetX = Xoffset;
}

/**
 * Sets the Y offset to the mouse position
 * that the div appears at.
 * 
 * @param integer Yoffset - Number of pixels for vertical
 *     offset from mouse position
 */
DhtmlLayer.prototype.setOffsetY = function(Yoffset)
{
    this.offsetY = Yoffset;
}

/**
 * Position this layer to the mouse position
 *
 * @param string pos - Keyword for positioning relative to mouse
 */
DhtmlLayer.prototype.setLayerPositionToMouse = function(e, pos)
{
    pos = pos ? pos : 'left';
    var mousePos = getMousePos(e);
    var x = mousePos[0];
    var y = mousePos[1];
    if (pos == 'left') {
        x = x + this.offsetX;
        y = y + this.offsetY;
    }
    if (pos == 'right') {
        var layer = this.getLayer();
        var pos = Position.get(layer);
        x = x - pos.width;
        x = x + this.offsetX;
    }
    if (pos == 'center') {
        var layer = this.getLayer();
        var pos = Position.get(layer);
        x = x - (pos.width / 2);
        x = x + this.offsetX;
        y = y - (pos.height / 2);
        y = y + this.offsetY;
    }
    if (pos == 'centertop') {
        var layer = this.getLayer();
        var pos = Position.get(layer);
        x = x - (pos.width / 2);
        x = x + this.offsetX;
    }
    this.setLayerPosition(x, y);
}

/**
 * Position the layer so that it dose not overlap the mouse 
 *
 */
DhtmlLayer.prototype.notOverlapMouse = function(e)
{
    var mousePos = getMousePos(e);
    var layer = this.getLayer();
    var pos = Position.get(layer);
    var x = pos.left;
    var y = pos.top;
    if (mousePos[0] >= pos.left && mousePos[0] <= pos.width) {
        x = mousePos[0] + 1;
    }
    if (mousePos[1] >= pos.top && mousePos[1] <= pos.height) {
        y = mousePos[1] + 1;
    }
    if (y != pos.top || x != pos.left) {
        this.setLayerPosition(x, y);
    }
}

/**
 * Keep the layer visable on the page
 *
 * @param bool nudge - If nudge is true layer is moved only enough to clear
 *                      page edges, if false lay is moved by its entire height /
 *                      width like an OS control.
 */
DhtmlLayer.prototype.keepOnPage = function(nudge, useOffset)
{
    nudge = nudge ? nudge : false;
    useOffset = useOffset ? useOffset : true;
    var layer = this.getLayer();
    var pos = Position.get(layer);
    var pagePos = getPagePosScroll(true);
    var x = pos.left;
    var y = pos.top;
    // y + h > window height 
    if ((pos.top + pos.height) > pagePos[1]) {
        if (!nudge && (pos.top - pos.height) >= 0) {
            y = y - pos.height;
            if (useOffset) {
                y = y - (this.offsetY + 5);
            }
        } else {
            y = pagePos[1] - (pos.height + 3);
        }
    }
    
    // x + w > window width
    if ((pos.left + pos.width) > pagePos[0]) {
        if (!nudge && (pos.left - pos.width) >= 0)  {
            x = x - pos.width;
//            if (useOffset) {
//                x = x - (this.offsetX + 5);
//            }
        } else {
            x = pagePos[0] - (pos.width + 3);
        }
    }
    
    if (y < 0) {
        y = 0;
    }
    
    if (x < 0) {
        x = 0;
    }
    if (y != pos.top || x != pos.left) {
        this.setLayerPosition(x, y);
    }
}


/**
 * Sets the layers position
 * 
 * @param integer x - 
 * @param integer y - 
 */
DhtmlLayer.prototype.setLayerPosition = function(x, y)
{
    Position.set(this.getLayer(), x, y);
    if (this.hasIframe) {
        // iFrame
        Position.set(this.getIframe(), x, y);
    }
}

/**
 * Position a layer to an element in thE dom
 *
 * @param bool center - If true position on the elements center, otherwise TL
 */
DhtmlLayer.prototype.setLayerPosToElement = function(element, center)
{
    if (center) {
        var pos = Position.getCenter(element);
    } else {
        var pos = Position.get(element);
    }
    this.setLayerPosition(pos);
}

/**
 * Set layer position so that it is static on the page when the page scrolls
 *
 * @todo - remove time when not required
 */
DhtmlLayer.prototype.setLayerPositionNoScroll = function()
{
    if (this.scrollWithPage) {
        var pagePos = getPagePosScroll(false);
        var scrollPos = this.getScrollXY()
        var x = scrollPos[0];
        var y = scrollPos[1];
        x = x + pagePos[0];
        y = y + pagePos[1];
        this.setLayerPosition(x, y);
        var callwrapperL = new CCallWrapper(this, 300, 'setLayerPositionNoScroll');
        CCallWrapper.asyncExecute(callwrapperL);
    }
}

/**
 * Set parameter to allow layer to scroll with the page
 *
 * @param bool value - True to switch on
 * @param integer x - Static left position of layer
 * @param integer y - Static top position of layer
 */
DhtmlLayer.prototype.setScrollWithPage = function(value, x, y)
{
    this.scrollWithPage = value;
    this.sx = x;
    this.sy = y;
}

/**
 * Get parameter to show layer can scroll with the page
 *
 */
DhtmlLayer.prototype.getScrollWithPage = function()
{
    return this.scrollWithPage;
}

/**
 * Get the x y coordinates of the static layer
 *
 */
DhtmlLayer.prototype.getScrollXY = function()
{
    return [this.sx, this.sy];
}

/**
 * Sets the layers dimensions
 *
 * @param string width - (eg: '10em', '10px')
 * @param string height - (eg: '10em', '10px')
 */
DhtmlLayer.prototype.setLayerDimension = function(width, height)
{
    // Test for an integer
    if (isNumeric(width)) {
         width = parseInt(width) + 'px';
    }
    if (isNumeric(height)) {
        height = parseInt(height) + 'px';
    }

    // Layer
    this.getLayer().style.width  = width;
    this.getLayer().style.height = height;
    if (this.hasIframe) {
        // iFrame
        this.getIframe().style.width  = this.getLayer().style.width;
        this.getIframe().style.height = this.getLayer().style.height;
    }
}

/**
 * Set only the width of the layer
 *
 * @param integer width - 
 */
DhtmlLayer.prototype.setLayerWidth = function(width)
{
    // Test for an integer
    if (isNumeric(width)) {
         width = parseInt(width) + 'px';
    }

    // Layer
    this.getLayer().style.width  = width;
    if (this.hasIframe) {
        // iFrame
        this.getIframe().style.width  = this.getLayer().style.width;
    }
}

/**
 * Get the visible state of this layer.
 * 
 * @return boolean
 */
DhtmlLayer.prototype.isVisible = function()
{
    return this.getLayer().style.visibility == 'visible' ? true : false;
}

/**
 * Toggle a layer to visible or hidden
 *
 */
DhtmlLayer.prototype.toggleVisible = function()
{
    if (this.isVisible()) {
        this.hide();
    } else {
        this.show();
    }
}

/**
 * mouse over state. Whether the mouse is over the layer or not.
 *
 * @param boolean status
 */
DhtmlLayer.prototype.setMouseover = function(b)
{
    this.isMouseover = b;
}

DhtmlLayer.prototype.getMouseover = function(b)
{
    return this.isMouseover;
}


/**
 * Returns the layer object
 *
 */
DhtmlLayer.prototype.getLayer = function()
{
    return DhtmlLayer.getElementById(this.layerID);
}


/**
 * Returns the IFrame object
 *
 */
DhtmlLayer.prototype.getIframe = function()
{
    return DhtmlLayer.getElementById(this.layerID + '_iframe');
}

/**
 * Add filters to layer
 *
 * Only work on IE5.5+ on Windows.
 * Taken from old tooltip code
 */
DhtmlLayer.prototype.applyCssFilters = function()
{
    if (sniffer.is_ie6up && sniffer.is_win) {
        var dx = " progid:DXImageTransform.Microsoft.";
        this.getLayer().style.filter = "revealTrans()" + dx + "Fade(Overlap=1.00 enabled=0)" + dx + 
        "Inset(enabled=0)" + dx + "Iris(irisstyle=PLUS,motion=in enabled=0)" + dx + 
        "Iris(irisstyle=PLUS,motion=out enabled=0)" + dx + "Iris(irisstyle=DIAMOND,motion=in enabled=0)" + dx + 
        "Iris(irisstyle=DIAMOND,motion=out enabled=0)" + dx + "Iris(irisstyle=CROSS,motion=in enabled=0)" + dx + 
        "Iris(irisstyle=CROSS,motion=out enabled=0)" + dx + "Iris(irisstyle=STAR,motion=in enabled=0)" + dx + 
        "Iris(irisstyle=STAR,motion=out enabled=0)" + dx + "RadialWipe(wipestyle=CLOCK enabled=0)" + dx + 
        "RadialWipe(wipestyle=WEDGE enabled=0)" + dx + "RadialWipe(wipestyle=RADIAL enabled=0)" + dx + 
        "Pixelate(MaxSquare=35,enabled=0)" + dx + "Slide(slidestyle=HIDE,Bands=25 enabled=0)" + dx + 
        "Slide(slidestyle=PUSH,Bands=25 enabled=0)" + dx + "Slide(slidestyle=SWAP,Bands=25 enabled=0)" + dx + 
        "Spiral(GridSizeX=16,GridSizeY=16 enabled=0)" + dx + "Stretch(stretchstyle=HIDE enabled=0)" + dx + 
        "Stretch(stretchstyle=PUSH enabled=0)" + dx + "Stretch(stretchstyle=SPIN enabled=0)" + dx + 
        "Wheel(spokes=16 enabled=0)" + dx + "GradientWipe(GradientSize=1.00,wipestyle=0,motion=forward enabled=0)" + dx + 
        "GradientWipe(GradientSize=1.00,wipestyle=0,motion=reverse enabled=0)" + dx + 
        "GradientWipe(GradientSize=1.00,wipestyle=1,motion=forward enabled=0)" + dx + 
        "GradientWipe(GradientSize=1.00,wipestyle=1,motion=reverse enabled=0)" + dx + 
        "Zigzag(GridSizeX=8,GridSizeY=8 enabled=0)" + dx + "Alpha(enabled=0)" + dx + 
        "Dropshadow(OffX=3,OffY=3,Positive=true,enabled=0)" + dx + "Shadow(strength=3,direction=135,enabled=0)";
    }
}

/**
 * Stop all filters
 *
 */
DhtmlLayer.prototype.stopFilters = function()
{
    if (sniffer.is_ie6up && sniffer.is_win) {
        layer = this.getLayer();
        for (Index = 28; Index < 31; Index++) { 
            layer.filters[Index].enabled = 0;
        }
        for (s = 0; s < 28; s++) { 
            if (layer.filters[s].status == 2) {
                layer.filters[s].stop();
            }
        }
    }
}

/**
 * Play a transition filter
 *
 * @param integer transition - Transition number
 * @param integer duration - Duration
 */
DhtmlLayer.prototype.doTransition = function(transition, duration)
{
    if (sniffer.is_ie6up && sniffer.is_win) {
        layer = this.getLayer();
        if (transition == 51)  {
            transition = parseInt(Math.random() * 50);
        }
        var applyTrans = (transition > -1 && transition < 24 && duration > 0)? 1 : 0;
        var advFilters = (transition > 23 && transition < 51 && duration > 0)? 1 : 0;
        var which = (applyTrans) ? 0 : (advFilters) ? transition - 23 : 0;
        if (applyTrans || advFilters) {
            if (applyTrans) {
                layer.filters[0].transition = transition;
            }
            layer.filters[which].duration = duration;
            layer.filters[which].apply();
        }
        if (applyTrans || advFilters) {
            layer.filters[which].play();
        }    
    }
}

/**
 * Enable the shadow filter
 *
 * @param string colour - Colour code e.g. #C0C0C0
 * @param integer shadowType - 1 = normal, 2 = complex
 */
DhtmlLayer.prototype.enableShadow = function(colour, shadowType)
{
    if (sniffer.is_ie6up && sniffer.is_win) {
        if (shadowType = 1) {
            filterNo = 29;
        } else {
            filterNo = 30;
        }
        this.getLayer().filters[filterNo].enabled = 1;
        this.getLayer().filters[filterNo].color = colour;
    }
}

/**
 * Set layer opacity filter
 *
 * @param integer amount - Opacity 0 - 99
 */
DhtmlLayer.prototype.enableOpacity = function(amount)
{
    if (sniffer.is_ie6up && sniffer.is_win) {
        if (amount > 0 && amount < 100) {
            this.getLayer().filters[28].enabled = 1;
            this.getLayer().filters[28].opacity = amount;
        }
    }
}

////////////////// Static Methods /////////////////

/**
 * Returns the layer object
 *
 * @return DOMElement
 * @deprecated - Means old, do not use, use getElementById()
 */
DhtmlLayer.getElementById = function(id)
{
    return getElementById(id);
}

/**
 * Hide all the layers
 *
 */
DhtmlLayer.hideAll = function()
{
    for (var i = 0; i < DhtmlLayer_layers.length; i++) {
        obj = DhtmlLayer_layers[i];
        obj.hide();
    }
}

/**
 * Hide all the layers that can be hidden on a document click
 *
 */
DhtmlLayer.hideIfCan = function()
{
    for (var i = 0; i < DhtmlLayer_layers.length; i++) {
        obj = DhtmlLayer_layers[i];
        if (obj.documentHide) {
            obj.hide();
        }
    }
}

/**
 * Tidy up layers on page exit
 *
 */
DhtmlLayer.tidyOnExit = function()
{
    for (var i = 0; i < DhtmlLayer_layers.length; i++) {
        obj = DhtmlLayer_layers[i];
        if (obj.getLayer()) {
            var layer = obj.getLayer();
            //layer = null;
            discardElement(layer);
        }
        obj = null;
    }
}

/**
 * Get the position of an object
 *
 */
var Position = (function() 
    {
    // Resolve a string identifier to an object
    function resolveObject(s)
        {
        if (document.getElementById && document.getElementById(s) != null) {
            return document.getElementById(s);
        } else if (document.all && document.all[s] != null) {
            return document.all[s];
        } else if (document.anchors && document.anchors.length && document.anchors.length > 0 && document.anchors[0].x) {
            for (var i = 0; i < document.anchors.length; i++) {
                if (document.anchors[i].name == s) { 
                    return document.anchors[i];
                }
            }
        }
    }

    var pos = {};
     
    /**
     * pos.set - Set the position of an object.
     * @param {string/object} o Object to position
     * @param {integer} left Left position
     * @param {integer} top Top position
     * @returns {boolean} true on sucess
     * @extends pos
     */
    pos.set = function(o, left, top)
    {
        if (typeof(o) == "string") {
            o = resolveObject(o);
        }
        if (o == null || !o.style) {
            return false;
        }
        // If the second parameter is an object, it is assumed to be the result of getPosition()
        if (typeof(left) == "object") {
            var pos = left;
            left = pos.left;
            top = pos.top;
        }
        o.style.left = left + "px";
        o.style.top = top + "px";
        return true;
    };

    // Retrieve the position and size of an object
    pos.get = function(o)
    {
        var fixBrowserQuirks = true;
        // If a string is passed in instead of an object ref, resolve it
        if (typeof(o) == "string") {
            o = resolveObject(o);
        }
        if (o == null) {
            return null;
        }
        var left = 0;
        var top = 0;
        var width = 0;
        var height = 0;
        var parentNode = null;
        var offsetParent = null;
        offsetParent = o.offsetParent;
        var originalObject = o;
        var el = o; // "el" will be nodes as we walk up, "o" will be saved for offsetParent references
        while (el.parentNode != null) {
            el = el.parentNode;
            if (el.offsetParent == null) {
            } else {
                var considerScroll = true;
                /*
                In Opera, if parentNode of the first object is scrollable, then offsetLeft/offsetTop already 
                take its scroll position into account. If elements further up the chain are scrollable, their 
                scroll offsets still need to be added in. And for some reason, TR nodes have a scrolltop value
                which must be ignored.
                */
                if (fixBrowserQuirks && window.opera) {
                    if (el == originalObject.parentNode || el.nodeName == "TR") {
                        considerScroll = false;
                    }
                }
                if (considerScroll) {
                    if (el.scrollTop && el.scrollTop>0) {
                        top -= el.scrollTop;
                    }
                    if (el.scrollLeft && el.scrollLeft>0) {
                        left -= el.scrollLeft;
                    }
                }
            }
            // If this node is also the offsetParent, add on the offsets and reset to the new offsetParent
            if (el == offsetParent) {
                left += o.offsetLeft;
                if (el.clientLeft && el.nodeName != "TABLE") { 
                    left += el.clientLeft;
                }
                top += o.offsetTop;
                if (el.clientTop && el.nodeName != "TABLE") {
                    top += el.clientTop;
                }
                o = el;
                if (o.offsetParent == null) {
                    if (o.offsetLeft) {
                        left += o.offsetLeft;
                    }
                    if (o.offsetTop) {
                        top += o.offsetTop;
                    }
                }
                offsetParent = o.offsetParent;
            }
        }

        if (originalObject.offsetWidth) {
            width = originalObject.offsetWidth;
        }
        if (originalObject.offsetHeight) {
            height = originalObject.offsetHeight;
        }

        return {'left':left, 'top':top, 'width':width, 'height':height};
    };

    // Retrieve the position of an object's center point
    pos.getCenter = function(o)
    {
        var c = this.get(o);
        if (c == null) {
            return null;
        }
        c.left = c.left + (c.width / 2);
        c.top = c.top + (c.height / 2);
        return c;
    };

    pos.getVH = function(o, v, h)
    {
        var c = this.get(o);
        if (c == null) {
            return null;
        }
        if (h == 'left') {
            c.left = c.left;
        }
        if (h == 'right') {
            c.left = c.left + (c.width);
        }
        if (h == 'center') {
            c.left = c.left + (c.width / 2);
        }
        if (v == 'top') {
            c.top = c.top;
        }
        if (v == 'bottom') {
            c.top = c.top + (c.height);
        }
        if (v == 'center') {
            c.top = c.top + (c.height / 2);
        }
        return c;
    };

    return pos;
})();

/**
 * Get objects left position
 *
 * @param object obj -
 * Obsolute
 */
function findPosX(obj)
{
    var pos = Position.get(obj);
    return(pos.left);
}

/**
 * Get objects top position
 *
 * @param object obj -
 * Obsolute
 */
function findPosY(obj)
{
    var pos = Position.get(obj);
    return(pos.top);
}

DhtmlLayer.setPageNeedsIFrame = function(val)
{
        DhtmlLayer_pageNeedsIFrame = val;
}

DhtmlLayer.getPageNeedsIFrame = function()
{
        return DhtmlLayer_pageNeedsIFrame;
}

if (window.JSLib) {
    JSLib.setLoaded("DhtmlLayer.js");
}

