﻿// FoldierPopup File

/////////////////////////////////////////////////////////////////////////////////////////////////////////
// (c) 2007-2008 foldier inc.
// This software is property of foldier inc. Use or reporduction without permision is prohibited  
// Created by : Michele Ursino
/////////////////////////////////////////////////////////////////////////////////////////////////////////

var g_toolTipWin = null;
var g_currentMainPopup = null;   // Current visible popup window
var g_currentShadPopup = null;   // Current visible popup window
var g_currentDragDrop = null; // Current object being dragged
var g_currentDropped = null; // Last dropped obkject
var g_oldmovehandler = null;
var g_currentDragDropMover = function(e) { if (g_currentDragDrop != null) g_currentDragDrop.moveHandler(e); };
var g_currentDragDropStopper = function(e) { if (g_currentDragDrop != null) g_currentDragDrop.upHandler(e); };
var g_registeredTargets = new Array();
var g_itemPopups = new Array();

// =========================================================================================================
// =========================================================================================================
// =========================================================================================================

// var positionObject = getScrollPositions();
// var scrollLeftPosition = positionObject.left;
// var scrollTopPosition = positionObject.top;
// ---------------------------------------------------------------------------------------------------------
function getScrollPositions() {
    var scrollLeftPosition = 0;
    var scrollTopPosition = 0;

    if ((document.documentElement != null) && (document.documentElement.scrollLeft != null))
        scrollLeftPosition = document.documentElement.scrollLeft;
    else if ((document.body != null) && (document.body.scrollLeft != null))
        scrollLeftPosition = document.body.scrollLeft;
    else if (window.pageXOffset != null)
        scrollLeftPosition = window.pageXOffset;
    else if (window.scrollX != null)
        scrollLeftPosition = window.scrollX;

    scrollLeftPosition = parseInt(scrollLeftPosition);
    if ((isNaN(scrollLeftPosition) == true) || (scrollLeftPosition < 0))
        scrollLeftPosition = 0;

    if ((document.documentElement != null) && (document.documentElement.scrollTop != null))
        scrollTopPosition = document.documentElement.scrollTop;
    else if ((document.body != null) && (document.body.scrollTop != null))
        scrollTopPosition = document.body.scrollTop;
    else if (window.pageYOffset != null)
        scrollTopPosition = window.pageYOffset;
    else if (window.scrollY != null)
        scrollTopPosition = window.scrollY;

    scrollTopPosition = parseInt(scrollTopPosition);
    if ((isNaN(scrollTopPosition) == true) || (scrollTopPosition < 0))
        scrollTopPosition = 0;

    return { left: scrollLeftPosition, top: scrollTopPosition }
}

// Check if the dragged object is on a potential target element
// -----------------------------------------------------------------------------------
function checkDropOn(targetElement, e) {
    if (!e) e = window.event;  // IE Event Model
    var curleft = curtop = 0;
    var targetCheck = targetElement;

    if (!targetElement.offsetParent)
        return false;

    do {
        curleft += targetCheck.offsetLeft;
        curtop += targetCheck.offsetTop;
    } while (targetCheck = targetCheck.offsetParent);

    var dimW = 0;
    var dimH = 0;
    if (targetElement.offsetHeight) {
        dimW = targetElement.offsetWidth;
        dimH = targetElement.offsetHeight;
    }
    else if (targetElement.style.pixelHeight) {
        dimW = targetElement.pixelWidth
        dimH = targetElement.pixelHeight;
    }

    var mouseX = e.clientX;
    var mouseY = e.clientY;
    if (window.pageXOffset != null) {
        mouseX += window.pageXOffset;
        mouseY += window.pageYOffset;
    }
    else {
        mouseX += document.documentElement.scrollLeft;
        mouseY += document.documentElement.scrollTop;
    }


    if (mouseX > curleft && mouseX < curleft + dimW)
        if (mouseY > curtop && mouseY < curtop + dimH) {
        targetElement.className = "DropTargetHit";
        return true;
    }

    return false;

}

// Register a HTML element as potential drop target for a drag & drop action
// -----------------------------------------------------------------------------------
function registerDropTarget(targetElement) {
    g_registeredTargets[g_registeredTargets.length] = targetElement;
}

// Stop the current dragging operations
// -----------------------------------------------------------------------------------
function stopCurrentDragging(e) {
    if (!e) e = window.event;  // IE Event Model

    if (g_currentDragDrop != null) {
        g_currentDragDrop.Hide(event);
    }

    // We've handled this event. Don't let anybody else see it.  
    if (event.stopPropagation) event.stopPropagation();  // DOM Level 2
    else event.cancelBubble = true;                      // IE

    // Now prevent any default action.
    if (event.preventDefault) event.preventDefault();   // DOM Level 2
    else event.returnValue = false;                     // IE
}

// FoldierDragItem Contructor
// -------------------------------------------------------------------------------------------------------
function FoldierDragItem(fatherElement, itemID, itemName, itemIconUrl, sourceName)
{
    this.m_divName = 'FoldierDrag_' + itemID;
    this.m_itemID = itemID;
    this.m_itemName = itemName;
    this.m_sourceName = sourceName;

    // Frame DIV	
    this.m_mainDiv = document.createElement("div");
    this.m_mainDiv.id = this.m_divName;
    this.m_mainDiv.className = "FoldierDrag";

    // Title DIV	
    this.m_thumbDiv = document.createElement("div");
    this.m_thumbDiv.className = "FoldierDragThumb";
    this.m_thumbDiv.innerHTML = "<img class='FoldierDragThumb' alt='' src='" + itemIconUrl + "' /><div><div class='FoldierDragName'>" + itemName + "</div>";

    this.m_mainDiv.appendChild(this.m_thumbDiv);
    fatherElement.appendChild(this.m_mainDiv);
}
// var alertedOnce = false;

// Change the position of this DragItem to follow the current mouse position
// ---------------------------------------------------------------------------------------------
FoldierDragItem.prototype.MousePos = function(e) {
    if (!e) e = window.event;  // IE Event Model

    /**************
    var mouseX = 0;
    var mouseY = 0;

    if (e.pageX || e.pageY) 
    {
    mouseX = e.pageX;
    mouseY = e.pageY;
    }
    else
    {
    //alert('Page Mode: e.clientX = '+e.clientX+'   e.pageX'+e.pageX);
    var de = document.documentElement;        
    var b = document.body;
    mouseX = e.clientX;// + (de.scrollLeft || b.scrollLeft) - (de.clientLeft || 0);
    mouseY = e.clientY;// + (de.scrollTop || b.scrollTop) - (de.clientTop || 0);
    }
    ****/

    var mouseX = e.clientX;
    var mouseY = e.clientY;


    // Move the element to the current mouse position, adjusted as
    // necessary by the offset of the initial mouse-click.
    this.m_mainDiv.style.left = (mouseX - 30) + "px";
    this.m_mainDiv.style.top = (mouseY - 30) + "px";

}

// Hide the current DragItem and detach the events
// ---------------------------------------------------------------------------------------------
FoldierDragItem.prototype.Hide = function(e) {
    if (!e) e = window.event;  // IE Event Model


    g_currentDropped = g_currentDragDrop;

    // Unregister the capturing event handlers.
    if (document.removeEventListener)   // DOM event model
    {
        document.removeEventListener("mousemove", g_currentDragDropMover, true);
        document.removeEventListener("mouseup", g_currentDragDropStopper, true);
    }
    else if (document.detachEvent)   // IE 5+ Event Model
    {
        document.detachEvent("onmousemove", g_currentDragDropMover);
        document.detachEvent("mouseup", g_currentDragDropStopper);
        g_currentDragDrop.m_mainDiv.releaseCapture();
    }
    else {  // IE 4 Event Model
        // Restore the original handlers, if any
        document.onmousemove = g_oldmovehandler;
        g_oldmovehandler = null;
    }

    this.m_mainDiv.style.display = 'none';
    g_currentDragDrop = null;

    showAllFlash();

}


// ---------------------------------------------------------------------------------------------
FoldierDragItem.prototype.Show = function(e) {
    var self = this;
    if (!e) e = window.event;  // IE Event Model

    // The mouse position (in window coordinates)
    // at which the drag begins 
    // var startX = event.clientX, startY = event.clientY;    

    hideAllFlash();

    self.MousePos(e);
    this.m_mainDiv.style.display = 'block';
    this.m_mainDiv.style.visibility = 'visible';

    g_currentDragDrop = this;

    if (document.addEventListener) // DOM Level 2 event model
    {
        document.addEventListener("mousemove", g_currentDragDropMover, true);
        document.addEventListener("mouseup", g_currentDragDropStopper, true);
    }
    else if (document.attachEvent) // IE 5+ Event Model
    {
        g_currentDragDrop.m_mainDiv.setCapture();
        document.attachEvent("onmousemove", g_currentDragDropMover);
        document.attachEvent("mouseup", g_currentDragDropStopper);
    }
    else // IE 4 Event Model
    {
        g_oldmovehandler = document.onmousemove; // used by upHandler() 
        document.onmousemove = g_currentDragDrop.moveHandler;
    }

    // We've handled this event. Don't let anybody else see it.  
    if (e.stopPropagation) e.stopPropagation();  // DOM Level 2
    else e.cancelBubble = true;                      // IE

    // Now prevent any default action.
    if (e.preventDefault) e.preventDefault();   // DOM Level 2
    else e.returnValue = false;                     // IE


}

// --------------------------------------------------------------------------------------- 
// This is the handler that captures mouseup events when an element
// is being dragged. It is responsible for sending the event to the overlapping
// location so they can receive the event and act
// --------------------------------------------------------------------------------------- 
FoldierDragItem.prototype.upHandler = function(e) {
    if (!e) e = window.event;  // IE Event Model

    if (g_currentDragDrop != null)
        g_currentDragDrop.Hide(e);

    // And don't let anyone else see this event.
    if (e.stopPropagation) e.stopPropagation();  // DOM Level 2
    else e.cancelBubble = true;                  // IE

    // Dispatch the event to the overlapping drop location so it can capture drop action
    for (var i = 0; i < g_registeredTargets.length; i++) {
        var potTarget = g_registeredTargets[i];
        if (checkDropOn(potTarget, e)) {
            if (potTarget.dispatchEvent)  // Firefox, Chrome and Safari
            {
                var dispEvent;
                dispEvent = document.createEvent("MouseEvents");
                dispEvent.initMouseEvent("mouseup", e.bubbles, e.cancelable, e.view, e.detail, e.screenX, e.screenY, e.clientX, e.clientY, e.ctrlKey, e.altKey, e.shiftKey, e.metaKey, e.button, null);
                potTarget.dispatchEvent(dispEvent);
            }
            else if (potTarget.fireEvent) // IE 7 
            {
                potTarget.fireEvent("onmouseup", dispEvent);
            }
        }
    }


}

// --------------------------------------------------------------------------------------- 
// This is the handler that captures mousemove events when an element
// is being dragged. It is responsible for moving the element and its shadow.
// --------------------------------------------------------------------------------------- 
FoldierDragItem.prototype.moveHandler = function(e) {
    if (!e) e = window.event;  // IE Event Model

    var self = this;

    if (document.attachEvent) // IE 7
    {
        if (e.button != 1) {
            self.upHandler(e);
            return;
        }
    }


    g_currentDragDrop.MousePos(e);

    // Dispatch the event to the overlapping drop location so it can capture drop action
    for (var i = 0; i < g_registeredTargets.length; i++) {
        var potTarget = g_registeredTargets[i];
        if (checkDropOn(potTarget, e))
            potTarget.className = "DropTargetHit";
        else
            potTarget.className = "DropTarget";

    }


    // We've handled this event. Don't let anybody else see it.  
    if (e.stopPropagation) e.stopPropagation();  // DOM Level 2
    else e.cancelBubble = true;                      // IE

    // Now prevent any default action.
    if (e.preventDefault) e.preventDefault();   // DOM Level 2
    else e.returnValue = false;                     // IE

    // And don't let anyone else see this event.
    //if (e.stopPropagation) e.stopPropagation();  // DOM Level 2
    //else e.cancelBubble = true;                  // IE

}


// =========================================================================================================
// =========================================================================================================
// =========================================================================================================

// FoldierPopup Constructor function
// -------------------------------------------------------------------------------------------------------
function FoldierPopup(htmlDiv, htmlSDiv, deltaX, deltaY, width, minHeight, blockBackground) {
    this.m_mainDiv = htmlDiv;
    this.m_shadDiv = htmlSDiv;
    this.m_deltaX = deltaX;
    this.m_deltaY = deltaY;
    this.m_cancelShow = false;
    this.m_width = width;
    this.m_minHeight = minHeight;
    if (blockBackground) {
        this.m_screenBackground = $('popupScreenBackground');
    }
    else
        this.m_screenBackground = null;

}

// -------------------------------------------------------------------------------------------------------
FoldierPopup.prototype.SetX = function(xPos) {
    this.m_mainDiv.style.left = (xPos + this.m_deltaX) + 'px';
    this.m_shadDiv.style.left = (xPos + this.m_deltaX - 8) + 'px';
}
// -------------------------------------------------------------------------------------------------------
FoldierPopup.prototype.SetY = function(yPos) {
    this.m_mainDiv.style.top = (yPos + this.m_deltaY) + 'px';
    this.m_shadDiv.style.top = (yPos + this.m_deltaY - 8) + 'px';
}


/********************************************************************************************
* StartDrag(): start dragging this popup .
*
* This module defines a single drag() function that is designed to be called
* from an onmousedown event handler.  Subsequent mousemove events will
* move the specified element. A mouseup event will terminate the drag.
* If the element is dragged off the screen, the window does not scroll.
* This implementation works with both the DOM Level 2 event model and the
* IE event model.
* 
* Arguments:
*
*   this.m_mainDiv:  the element that received the mousedown event or
*     some containing element. It must be absolutely positioned.  Its 
*     style.left and style.top values will be changed based on the user's
*     drag.
*
*   event: the Event object for the mousedown event.
*
*********************************************************************************************/
FoldierPopup.prototype.StartDrag = function(event) {
    // The mouse position (in window coordinates)
    // at which the drag begins 
    var startX = event.clientX, startY = event.clientY;

    // The original position (in document coordinates) of the
    // element that is going to be dragged.  Since elementToDrag is 
    // absolutely positioned, we assume that its offsetParent is the
    // document body.
    var origX = g_currentMainPopup.offsetLeft, origY = g_currentMainPopup.offsetTop;
    var s_origX = g_currentShadPopup.offsetLeft, s_origY = g_currentShadPopup.offsetTop;

    // Even though the coordinates are computed in different 
    // coordinate systems, we can still compute the difference between them
    // and use it in the moveHandler() function.  This works because
    // the scrollbar position never changes during the drag.
    var deltaX = startX - origX, deltaY = startY - origY;
    var s_deltaX = startX - s_origX, s_deltaY = startY - s_origY;

    // Register the event handlers that will respond to the mousemove events
    // and the mouseup event that follow this mousedown event.  
    if (document.addEventListener) {  // DOM Level 2 event model
        // Register capturing event handlers
        document.addEventListener("mousemove", moveHandler, true);
        document.addEventListener("mouseup", upHandler, true);
    }
    else if (document.attachEvent) {  // IE 5+ Event Model
        // In the IE event model, we capture events by calling
        // setCapture() on the element to capture them.
        g_currentMainPopup.setCapture();
        g_currentMainPopup.attachEvent("onmousemove", moveHandler);
        g_currentMainPopup.attachEvent("onmouseup", upHandler);
        // Treat loss of mouse capture as a mouseup event
        g_currentMainPopup.attachEvent("onlosecapture", upHandler);
    }
    else {  // IE 4 Event Model
        // In IE 4 we can't use attachEvent() or setCapture(), so we set
        // event handlers directly on the document object and hope that the
        // mouse events we need will bubble up.  
        var oldmovehandler = document.onmousemove; // used by upHandler() 
        var olduphandler = document.onmouseup;
        document.onmousemove = moveHandler;
        document.onmouseup = upHandler;
    }

    // We've handled this event. Don't let anybody else see it.  
    if (event.stopPropagation) event.stopPropagation();  // DOM Level 2
    else event.cancelBubble = true;                      // IE

    // Now prevent any default action.
    if (event.preventDefault) event.preventDefault();   // DOM Level 2
    else event.returnValue = false;                     // IE

    /**
    * This is the handler that captures mousemove events when an element
    * is being dragged. It is responsible for moving the element and its shadow.
    **/
    function moveHandler(e) {
        if (!e) e = window.event;  // IE Event Model

        // Move the element to the current mouse position, adjusted as
        // necessary by the offset of the initial mouse-click.
        g_currentMainPopup.style.left = (e.clientX - deltaX) + "px";
        g_currentMainPopup.style.top = (e.clientY - deltaY) + "px";
        g_currentShadPopup.style.left = (e.clientX - s_deltaX) + "px";
        g_currentShadPopup.style.top = (e.clientY - s_deltaY) + "px";

        // And don't let anyone else see this event.
        if (e.stopPropagation) e.stopPropagation();  // DOM Level 2
        else e.cancelBubble = true;                  // IE

    }

    /**
    * This is the handler that captures the final mouseup event that
    * occurs at the end of a drag.
    **/
    function upHandler(e) {
        if (!e) e = window.event;  // IE Event Model

        // Unregister the capturing event handlers.
        if (document.removeEventListener) {  // DOM event model
            document.removeEventListener("mouseup", upHandler, true);
            document.removeEventListener("mousemove", moveHandler, true);
        }
        else if (document.detachEvent) {  // IE 5+ Event Model
            g_currentMainPopup.detachEvent("onlosecapture", upHandler);
            g_currentMainPopup.detachEvent("onmouseup", upHandler);
            g_currentMainPopup.detachEvent("onmousemove", moveHandler);
            g_currentMainPopup.releaseCapture();
        }
        else {  // IE 4 Event Model
            // Restore the original handlers, if any
            document.onmouseup = olduphandler;
            document.onmousemove = oldmovehandler;
        }

        // And don't let the event propagate any further.
        if (e.stopPropagation) e.stopPropagation();  // DOM Level 2
        else e.cancelBubble = true;                  // IE

    }
}

// Enable dragging for this Popup window. The parameter is an element (within the window) that
// is used as handler for the dragging action.
// -------------------------------------------------------------------------------------------------------
FoldierPopup.prototype.EnableDrag = function(dragElement) {
    if (dragElement == null) {
        //alert('Dragging element is null!!');
        return;
    }

    var self = this;

    if (document.addEventListener)   // DOM Level 2 event model
    {

        dragElement.addEventListener("mousedown", self.StartDrag, false);
        dragElement.addEventListener("click", stopPropagation, false);
    }
    else if (document.attachEvent)   // IE 5+ Event Model
    {
        dragElement.attachEvent("onmousedown", self.StartDrag);
        dragElement.attachEvent("onclick", stopPropagation);
    }
    else // IE 4 Event Model
    {
        // Not supported!!
    }
}




// Position the popup Y relatively to the given element and centered in X
// -------------------------------------------------------------------------------------------------------
FoldierPopup.prototype.SetPositionByElementY = function(elementName, offY) {
    var x = 0, y = 0;
    var e = document.getElementById(elementName);

    while (e.offsetParent) {
        y += e.offsetTop;
        e = e.offsetParent;
    }

    x = (getWinWidth() - this.m_width) / 2;
    y += e.offsetTop + offY;

    //alert(' X = '+x+' window width = '+thisw );
    this.m_mainDiv.style.left = (x) + 'px';
    this.m_mainDiv.style.top = (y) + 'px';
    this.m_shadDiv.style.left = (x - 8) + 'px';
    this.m_shadDiv.style.top = (y - 8) + 'px';


}

// Position the popup Y relatively to the given element and centered in X
// -------------------------------------------------------------------------------------------------------
FoldierPopup.prototype.SetPositionByElementWithScreenAdjust = function(elementName, offX, offY, popupWidth) {
    var x = 0, y = 0;
    var screenWidth = getWinWidth()-100;

    var e = document.getElementById(elementName);
    while (e.offsetParent) {
        x += e.offsetLeft;
        y += e.offsetTop;
        e = e.offsetParent;
    }

    x += e.offsetLeft + offX;
    y += e.offsetTop + offY;

    if (x + popupWidth > screenWidth) {
        x = screenWidth - popupWidth;
    }

    this.m_mainDiv.style.left = (x) + 'px';
    this.m_mainDiv.style.top = (y) + 'px';
    this.m_shadDiv.style.left = (x - 8) + 'px';
    this.m_shadDiv.style.top = (y - 8) + 'px';

}


// -------------------------------------------------------------------------------------------------------
FoldierPopup.prototype.SetPositionByElement = function(elementName, offX, offY) {
    var x = 0, y = 0;
    var e = document.getElementById(elementName);
    while (e.offsetParent) {
        x += e.offsetLeft;
        y += e.offsetTop;
        e = e.offsetParent;
    }

    x += e.offsetLeft;
    y += e.offsetTop;

    this.m_mainDiv.style.left = (x + offX) + 'px';
    this.m_mainDiv.style.top = (y + offY) + 'px';
    this.m_shadDiv.style.left = (x + offX - 8) + 'px';
    this.m_shadDiv.style.top = (y + offY - 8) + 'px';
    //alert( "XPos = "+x + "    Offset = "+ offX );

}

// -------------------------------------------------------------------------------------------------------
FoldierPopup.prototype.SetPositionCenterdOnElement = function(elementName, offY) {
    var x = 0, y = 0;
    var e = document.getElementById(elementName);

    var h = 0;
    var w = 0;
    var thisw = 0;
    var thish = 0;
    if (this.m_mainDiv.offsetHeight) {
        h = e.offsetHeight;
        w = e.offsetWidth;
        thisw = this.m_mainDiv.offsetWidth;
        thish = this.m_mainDiv.offsetHeight;
    }
    else if (this.m_mainDiv.style.pixelHeight) {
        h = e.style.pixelHeight;
        w = e.style.pixelWidth;
        thisw = this.m_mainDiv.pixelWidth
        thish = this.m_mainDiv.pixelHeight;
    }

    // alert('Parent Element Width: '+ w + ' -  This Width: ' + thisw);

    while (e.offsetParent) {
        x += e.offsetLeft;
        y += e.offsetTop;
        e = e.offsetParent;
    }

    x += e.offsetLeft;
    y += e.offsetTop;

    //alert(' w = '+w);
    //alert(' x = '+x);

    var popupX = x;
    if (this.m_mainDiv.style.PixelWidth)
        popupX += (w - this.m_mainDiv.style.PixelWidth) / 2;
    else
        popupX += (w - this.m_mainDiv.offsetWidth) / 2;

    //alert('popupX = '+popupX);

    this.m_mainDiv.style.left = (popupX) + 'px';
    this.m_mainDiv.style.top = (y + offY) + 'px';
    this.m_shadDiv.style.left = (popupX - 8) + 'px';
    this.m_shadDiv.style.top = (y + offY - 8) + 'px';
    //alert( "XPos = "+x + "    Offset = "+ offX );

}

// ------------------------------------------------------------------------------------------
FoldierPopup.prototype.Hide = function()
{
    if (g_currentMainPopup == null)
        return;
    g_currentMainPopup.style.display = 'none';
    g_currentShadPopup.style.display = 'none';
    g_currentMainPopup = null;
    g_currentShadPopup = null;
    if (this.m_screenBackground != null)
        this.m_screenBackground.style.display = 'none';

    if ($('popupScreenBackground') != null)
    {
        $('popupScreenBackground').style.display = 'none';
    }


    showAllFlash();
}

// ------------------------------------------------------------------------------------------
FoldierPopup.prototype.Show = function()
{
    if (this.m_cancelShow == true || g_currentDragDrop != null)
    {
        this.m_cancelShow = false;
        return false;
    }

    hideAllFlash();

    //if (!e) e = window.event;

    // alert( "call show on "+ this.m_mainDiv.id + " on event = "+ e );

    if (g_currentSFPopup != null)
        g_currentSFPopup.Hide();

    if (g_currentMainPopup != null && this.m_mainDiv == g_currentMainPopup) {
        this.Hide();
        return false;
    }

    // Determine the position of the click
    //var x = e.clientX; 
    //var y = e.clientY; 
    //if ( window.pageXOffset )
    //{
    //   x += window.pageXOffset; 
    //   y += window.pageYOffset; 
    //}
    //else
    //{
    //    x += document.documentElement.scrollLeft; 
    //    y += document.documentElement.scrollTop ; 
    //}

    this.Hide();
    //this.SetX( x+10 );
    //this.SetY( y );

    g_currentMainPopup = this.m_mainDiv;
    g_currentShadPopup = this.m_shadDiv;

    g_currentMainPopup.style.display = 'block';
    g_currentShadPopup.style.display = 'block';
    g_currentMainPopup.style.visibility = 'visible';

    this.m_popupWidth = 0;
    this.m_popupHeight = 0;
    if (this.m_mainDiv.offsetHeight) {
        this.m_popupWidth = this.m_mainDiv.offsetWidth;
        this.m_popupHeight = this.m_mainDiv.offsetHeight;
    }
    else if (this.m_mainDiv.style.pixelHeight) {
        this.m_popupWidth = this.m_mainDiv.pixelWidth
        this.m_popupHeight = this.m_mainDiv.pixelHeight;
    }

    this.m_shadDiv.style.width = (this.m_popupWidth + 16) + 'px';
    this.m_shadDiv.style.height = (this.m_popupHeight + 16) + 'px';


    if (g_currentMenu != null)
        g_currentMenu.style.display = 'none';

    if (this.m_screenBackground != null) {
        startFadeIn(this.m_screenBackground, 15, 30);
        this.m_screenBackground.style.display = 'block';
    }

    return true;
}

FoldierPopup.prototype.ResizeShadow = function() {
    this.m_popupWidth = 0;
    this.m_popupHeight = 0;
    if (this.m_mainDiv.offsetHeight) {
        this.m_popupWidth = this.m_mainDiv.offsetWidth;
        this.m_popupHeight = this.m_mainDiv.offsetHeight;
    }
    else if (this.m_mainDiv.style.pixelHeight) {
        this.m_popupWidth = this.m_mainDiv.pixelWidth
        this.m_popupHeight = this.m_mainDiv.pixelHeight;
    }

    this.m_shadDiv.style.width = (this.m_popupWidth + 16) + 'px';
    this.m_shadDiv.style.height = (this.m_popupHeight + 16) + 'px';
}

FoldierPopup.prototype.Mouseout = function(target) {
    this.m_cancelShow = true;
    window.clearTimeout();
    if (target.removeEventListener)
        target.removeEventListener("mouseout", this.Mouseout, false);
    else if (target.detachEvent)
        target.detachEvent("onmouseout", this.Mouseout);
    else
        target.onmouseout = null;

}

FoldierPopup.prototype.ShowIfMouseStill = function(target, e, delay, showCallback) 
{
    this.m_cancelShow = false;
    // Determine the position of the mouse
    /*
    var x = e.clientX; 
    var y = e.clientY; 
    if ( window.pageXOffset )
    {
    x += window.pageXOffset; 
    y += window.pageYOffset; 
    }
    else
    {
    x += document.documentElement.scrollLeft; 
    y += document.documentElement.scrollTop ; 
    }*/

    var self = this;
    if (showCallback != null)
        window.setTimeout(function() { if (self.Show()) showCallback(); }, delay);
    else
        window.setTimeout(function() { self.Show(); }, delay);

    var targetElement = target;

    // also register a onmouseout handler to hide or cancel the pending display
    if (targetElement.addEventListener)
        targetElement.addEventListener("mouseout", function() { self.Mouseout(targetElement); }, false);
    else if (targetElement.attachEvent)
        targetElement.attachEvent("onmouseout", function() { self.Mouseout(targetElement); });
    else
        targetElement.onmouseout = function() { self.Mouseout(targetElement); };

}

function hideCurrentPopup(e) {
    // window.status='Hiding Current Popup' + eventObject ;
    if (!e) e = window.event;  // IE Event Model

    if (g_toolTipWin != null)
        g_toolTipWin.style.display = 'none';

    if (e) {
        var code;
        if (e.keyCode) code = e.keyCode;
        else if (e.which) code = e.which;

        if (code == 3) // Right Click do not hide the popup!
            return;
    }
    if (g_currentMenuButton != null) {
        g_currentMenuButton.className = "smallButtonMenu";
        g_currentMenuButton = null;
    }
    if (g_currentMenu != null) {
        g_currentMenu.style.display = 'none';
        g_currentMenu = null;
    }
    if (g_currentMainPopup == null)
        return;
    g_currentMainPopup.style.display = 'none';
    g_currentShadPopup.style.display = 'none';
    g_currentMainPopup = null;
    g_currentShadPopup = null;



}



// ===============================================================================================
// ITEM POPUP UTILITIES
// ===============================================================================================

function displayDescInItemPopup(itemPopupPos, targetDiv, oExecutor, oEventArgs) {
    if (oExecutor.get_responseAvailable()) {
        targetDiv.innerHTML = oExecutor.get_responseData();
        window.setTimeout(function() { g_itemPopups[itemPopupPos].ResizeShadow(); }, 500); // Give time to main win to resize
        ;
    }
}

function itemPopupShowing(itemPopupPos, itemID, targetDiv)
{
    if ( g_itemPopups[itemPopupPos]!=null )
        actionItemGetDesc(itemID, function(oExecutor, oEventArgs) { displayDescInItemPopup(itemPopupPos, targetDiv, oExecutor, oEventArgs); });
}

function itemPopupOnMouseOver(itemPopupPos, itemID, winDiv, shaDiv, titleDiv, descDiv, e, referenceDivName, mouseOverObject, distX, distY, itemDescriptor) 
{
    if (!e) e = window.event;
    var tdNewLabel = null;

    var tileOwner = $('itemTile_' + itemPopupPos);
    if (tileOwner == null) 
    {
        tileOwner = $('itemBox_' + itemPopupPos);
        tdNewLabel = $('tdItemBox_' + itemPopupPos);
    }
    var itemDesc = itemDescriptor;
    g_itemPopups[itemPopupPos] = new FoldierPopup(winDiv, shaDiv, 2, 2, 700, 100);
    g_itemPopups[itemPopupPos].EnableDrag(titleDiv);
    if (distX != null && distY != null) 
    {
        //g_itemPopups[itemPopupPos].SetPositionByElement(referenceDivName, distX, distY);
        g_itemPopups[itemPopupPos].SetPositionByElementWithScreenAdjust(referenceDivName, distX, distY, 700);
    }
    else if (distX != null) {   // This will center the popup on screen horizonatally
        g_itemPopups[itemPopupPos].SetPositionByElementY(referenceDivName, distY);
    }
    else {   // This will center the popup on screen horizonatally
        g_itemPopups[itemPopupPos].SetPositionByElementY(referenceDivName, 80);
    }
    if (tileOwner != null)
    {
        //alert(tileOwner.className);
        if (tileOwner.className == "itemTile" || tileOwner.className == "itemTileNew")
            g_itemPopups[itemPopupPos].ShowIfMouseStill(mouseOverObject, e, 1000, function() { itemPopupShowing(itemPopupPos, itemID, descDiv); tileOwner.className = 'itemTile'; itemDesc.isNew = false; });
        else if (tileOwner.className.indexOf("ItemBoxNew")>=0)
        {
            g_itemPopups[itemPopupPos].ShowIfMouseStill(mouseOverObject, e, 1000, function() { itemPopupShowing(itemPopupPos, itemID, descDiv); tileOwner.className = 'ItemBox'; tdNewLabel.className = "smartfolderContent"; itemDesc.isNew = false; });
        }
        else if (tileOwner.className.indexOf("ItemBoxSelected") >= 0)
        {
            g_itemPopups[itemPopupPos].ShowIfMouseStill(mouseOverObject, e, 1000, function() { itemPopupShowing(itemPopupPos, itemID, descDiv); tileOwner.className = 'ItemBoxSelected'; tdNewLabel.className = "smartfolderContent"; itemDesc.isNew = false; });
        }
        else
            g_itemPopups[itemPopupPos].ShowIfMouseStill(mouseOverObject, e, 1000, function() { itemPopupShowing(itemPopupPos, itemID, descDiv); /*tdNewLabel.className = "smartfolderContent";*/itemDesc.isNew = false; });
    }
    else
        g_itemPopups[itemPopupPos].ShowIfMouseStill(mouseOverObject, e, 1000, function() { itemPopupShowing(itemPopupPos, itemID, descDiv); });
    return g_itemPopups[itemPopupPos];
}

//CRI Nov 2009: method to refresh the home page when closing popup
function refreshHomeAtClose() {

    //check if the cookie containing the request for a refresh exists:
    var refreshCookie = readCookie("refreshHome");
    if ((refreshCookie != null) && (refreshCookie == 'true')) { 
        eraseCookie("refreshHome");
        parent.location.href="home.aspx";
        return true;
    }
}
