﻿$j = jQuery.noConflict();

var coordX = -1;
var coordY = -1;
var giveX = 9;
var giveY = 18;
var lastDiv;
var divCache = [];

function GetCellFromEvent(e) {
    if (jQuery.browser.mozilla) {
        coordX = e.originalEvent.layerX;
        coordY = e.originalEvent.layerY;
    } else {
        coordX = e.originalEvent.offsetX;
        coordY = e.originalEvent.offsetY;
    }

    if (coordX && coordY) {
        return {
            CurrentRow: Math.floor(coordY / 16) - 7,
            CurrentCell: Math.floor(coordX / 16) - 5
        };
    }
        
    return null;
}

function ShowPopup(cellCoords) {
    var popUp = document.getElementById("MessagePopUp");

    popUp.style.visibility = "visible";
    popUp.style.top = ((parseInt(cellCoords.CurrentRow) + 8) * 16 + 10) + "px";
    popUp.style.left = ((parseInt(cellCoords.CurrentCell) + 7) * 16 - 160) +"px";
}

function GetCellText(cellCoords) {
    if (cellCoords) {
        if (textArray[cellCoords.CurrentRow] && textArray[cellCoords.CurrentRow][cellCoords.CurrentCell]) {
            return textArray[cellCoords.CurrentRow][cellCoords.CurrentCell];
        }
    }

    return null;
}

$j(document).ready(function() {
    var container = document.getElementById("HeartContainer");
    var popUp = document.getElementById("MessagePopUp");
    var currentDivIndex = -1;
    var popupLocked = false;

    if (window.textArray) {
        // Initialize the DIV-grid.
        // [MB - CODE:FH]
        var allOverIdx = 0;

        for (var i = -3; i <= 27; i++) {
            for (var y = -5; y < 35; y++) {
                allOverIdx++;
                var myDiv = document.createElement("div");

                if (textArray[i] && textArray[i][y]) {
                    if (textArray[i] && (textArray[i][y] === "~EntryLocked~")) {
                        $j(myDiv).addClass("LockedDiv");
                    }
                    else {
                        $j(myDiv).addClass("UsedDiv");
                    }
                } else {
                    if (textArray[i] && (textArray[i][y] === "")) {
                        $j(myDiv).addClass("LockedDiv");
                    }
                    else {
                        $j(myDiv).addClass("NormalDiv");
                    }
                }

                if (window.blockedCells) {
                    $j(myDiv).html("<span style=\"font-size:8px;\">" + i + "," + y + "</span>");

                    if (window.blockedCells[i] && window.blockedCells[i][y])
                        $j(myDiv).addClass("BlackDiv");
                }

                container.appendChild(myDiv);

                divCache[allOverIdx] = $j(myDiv);
            }
        }

        // Preselect Popup on startup.
        // [MB]
        if (window.preselectRow && window.preselectCell) {
            var cellCoords = { CurrentRow: preselectRow, CurrentCell: preselectCell };
            var cellText = GetCellText(cellCoords);
            if (cellText) {
                popUp.innerHTML = cellText;
                ShowPopup(cellCoords);
            }
            popupLocked = true;
        }

        // Append handlers for hover and clicks.
        // [MB - CODE:FH]
        $j("#HeartOverlay").mousemove(function(e) {
            var cellCoords = GetCellFromEvent(e);

            if (cellCoords) {
                currentDivIndex = cellCoords.CurrentRow * 15 + cellCoords.CurrentCell;

                var cellText = GetCellText(cellCoords);
                if (cellText)
                    popUp.innerHTML = cellText;

                if (cellText) {
                    if (currentDivIndex < divCache.length && textArray[cellCoords.CurrentRow] && textArray[cellCoords.CurrentRow][cellCoords.CurrentCell]) {
                        popupLocked = false;
                        ShowPopup(cellCoords);
                    } else if (!popupLocked) {
                        //popUp.style.visibility = "hidden";
                    }
                }

            }
        });

        $j("#HeartOverlay").click(function(e) {
            var cellCoords = GetCellFromEvent(e);

            if (cellCoords) {
                var cellText = GetCellText(cellCoords);

                /*if (!cellText) {
                    window.location = "/Home.aspx/MessageForm";
                }*/
            }
        });
    }
});



function UpdateRemainingSigns(lbOutputId, callingTextarea, maxNumberOfSigns) {
    var remainingSize = maxNumberOfSigns - callingTextarea.value.length;

    if (remainingSize <= 0) {
        callingTextarea.value = callingTextarea.value.substr(0, maxNumberOfSigns);
        remainingSize = 0;
    }
    
    document.getElementById(lbOutputId).innerHTML = remainingSize;
}