/// <reference Name="MicrosoftAjax.js" />
var POPUP_WIDTH = 220; // Thumbnailer width
var POPUP_HEIGHT = 239; // Thumbnailer height
var object;

function setXY(theX, theY, pxOffset, pyOffset) {
    var iDivScrollLeft = 0;
    var iDivScrollTop = 0;

    var container = $get("divContainer");
    if (container != null) {
        iDivScrollTop = container.scrollTop;
        iDivScrollLeft = container.scrollLeft;

        theX += iDivScrollLeft;
        theY += iDivScrollTop;
    }

    if (((theX + 10) + (POPUP_WIDTH - iDivScrollLeft)) > (document.getElementsByTagName("body")[0].clientWidth - POPUP_WIDTH)) {
        if (theX - (POPUP_WIDTH + pxOffset) < 0)
            theX = 0;
        else
            theX -= (POPUP_WIDTH + pxOffset);
    }

    if (((theY + 10) + (POPUP_HEIGHT - iDivScrollTop)) > (document.getElementsByTagName("body")[0].clientHeight - POPUP_HEIGHT)) {
        if (theY - (POPUP_HEIGHT + pyOffset) < 0) {
            theY = 0;
        }
        else {
            theY -= (POPUP_HEIGHT + pyOffset - 50); //subtract 50 for the to div
        }
    }

    var photoBox = $get("photoBox");
    photoBox.style.left = theX + "px";
    photoBox.style.top = theY + "px";
}

function showItMap(header, serialNo, pText, pxOffset, pyOffset, event) {
    if (!event)
    {
        event = window.event;
    }
    var posX, posY;
    var photoBox = $get("photoBox");
    photoBox.innerHTML = pText;

    if (Sys.Browser.agent === Sys.Browser.Firefox) {
        posX = event.pageX + 5;
        posY = event.pageY + (POPUP_HEIGHT / 2);
    }
    else {
        posX = event.x + document.body.scrollLeft + 5;
        posY = event.y + document.body.scrollTop;
    }

    setXY(posX, posY, pxOffset, pyOffset);

    var fn = function(box) {
        setTimeout(function() {
            box.style.visibility = 'visible';
        }, 400);
    };
    fn(photoBox);
}

function setXYEmail(theX, theY, pBoundWidth, pBoundHeight) {
    if (((theX + 190) > pBoundWidth) && (theX - 195 >= 0))
        theX -= 195;
    if (((theY + 190) > pBoundHeight) && (theY - 195 >= 0) && (theY - 195 >= document.getElementsByTagName("body")[0].scrollTop))
        theY -= 195;

    var photoBox = $get("photoBox");
    photoBox.style.left = theX + 10 + "px";
    photoBox.style.top = theY + 10 + "px";
}

function showItEmail(header, serialNo, pText, pBoundWidth, pBoundHeight, event) {
    if (!event)
    {
        event = window.event;
    }
    var photoBox = $get("photoBox");
    photoBox.innerHTML = pText;

    var posX, posY;
    if (Sys.Browser.agent === Sys.Browser.Firefox) {
        posX = event.pageX;
        posY = event.pageY;
    }
    else {
        posX = event.x + document.body.scrollLeft;
        posY = event.y + document.body.scrollTop;
    }
    setXYEmail(posX, posY);

    var fn = function(box) {
        setTimeout(function() {
            box.style.visibility = 'visible';
        }, 400);
    };
    fn(photoBox);
}

function hideIt() {
    var photoBox = $get("photoBox");
    photoBox.style.visibility = 'hidden';
    photoBox.innerHTML = '';
}
