﻿// Wire events to TranslationPopup class
TranslationPopup.prototype.Initialize = TranslationPopup_Initialize;
TranslationPopup.prototype.BeginMove = TranslationPopup_BeginMove;
TranslationPopup.prototype.BeginResize = TranslationPopup_BeginResize;
TranslationPopup.prototype.CloseWindow = TranslationPopup_CloseWindow;
TranslationPopup.prototype.DisplayWindow = TranslationPopup_DisplayWindow;
TranslationPopup.prototype.CheckSelection = TranslationPopup_CheckSelection;
TranslationPopup.prototype.SelectWindow = TranslationPopup_SelectWindow;
TranslationPopup.prototype.DeselectWindow = TranslationPopup_DeselectWindow;
TranslationPopup.prototype.DeselectAll = TranslationPopup_DeselectAll;
TranslationPopup.prototype.MouseMoved = TranslationPopup_MouseMoved;
TranslationPopup.prototype.UpdateVerticalScrollbar = TranslationPopup_UpdateVerticalScrollbar;
TranslationPopup.prototype.UpdateWindowOffset = TranslationPopup_UpdateWindowOffset;
TranslationPopup.prototype.UpdateWindowPosition = TranslationPopup_UpdateWindowPosition;
TranslationPopup.prototype.ShowDebug = TranslationPopup_ShowDebug;
TranslationPopup.prototype.UpdateWindowSize = TranslationPopup_UpdateWindowSize;
TranslationPopup.prototype.InitializeWindow = TranslationPopup_InitializeWindow;
TranslationPopup.prototype.UpdateWindow = TranslationPopup_UpdateWindow;
TranslationPopup.prototype.CancelPopup = TranslationPopup_CancelPopup;
TranslationPopup.prototype.AcceptPopup = TranslationPopup_AcceptPopup;

function TranslationPopup()
{
}

function TranslationPopup_Initialize()
{
    isDebug = false;             // Places the control in debug mode
    windowX = 48;               // Window x co-ordinate (relative to the viewport)
    windowY = 48;               // Window y co-ordinate (relative to the viewport)
    windowWidth = 259;          // Window width
    windowHeight = 185;         // Window height
    windowPadding = 0;          // Window padding
    offset = 0;                 // Window offset
    offsetSpeed = 0;            // Speed of change in window offset
    scrollbarY = 0;             // Vertical scrollbar position
    mouseX = 0;                 // Mouse x co-ordinate (relative to the viewport)
    mouseY = 0;                 // Mouse y co-ordinate (relative to the viewport)
    windowVisible = false;      // Whether the window is currently visible
    windowSelected = true;      // Whether the window is currently selected
    beingMoved = false;         // Whether the window is currently being moved
    moveMouseX = 0;             // Mouse x co-ordinate (relative to left of window) when move began
    moveMouseY = 0;             // Mouse y co-ordinate (relative to top of window) when move began
    beingResized = false;       // Whether the window is currently being resized
    resizeMouseX = 0;           // Mouse x co-ordinate (relative to right of window) when resize began
    resizeMouseY = 0;           // Mouse y co-ordinate (relative to bottom of window) when resize began
    
    // Interval to update the window
    window.setInterval(this.UpdateWindow, 5);
    
    translationPopup.contentHolder = "";
    translationPopup.content = "";
    
    // Delegate to call when work is done
    delegate = "";

    // Wire events to page elements
    document.body.onmousedown=this.CheckSelection;
    document.body.onmouseup=this.DeselectAll;
    document.body.onmousemove=this.MouseMoved;
    document.getElementById("popupTopLeft").onmousedown=this.BeginMove;
    document.getElementById("popupTop").onmousedown=this.BeginMove;
    document.getElementById("popupTopRight").onmousedown=this.BeginMove;
    document.getElementById("popupBottomResize").onmousedown=this.BeginResize;
    document.getElementById("translationCancelButton").onclick=this.CancelPopup;
    document.getElementById("translationAcceptButton").onclick=this.AcceptPopup;
    
    translationPopup.UpdateWindowSize();
}

// Begins the move process, by finding the mouse position
function TranslationPopup_BeginMove()
{
    beingMoved = true;
    moveMouseX = mouseX - windowX;
    moveMouseY = mouseY - windowY;
}

// Begins the resize process, by finding the mouse position
function TranslationPopup_BeginResize()
{
    beingResized = true;
    resizeMouseX = windowX + windowWidth - mouseX;
    resizeMouseY = windowY + windowHeight - mouseY;
}

// Closes the window    
function TranslationPopup_CloseWindow()
{
    document.getElementById("popup").style.visibility = "hidden";
    windowVisible = false;
}

// Displays the window
//function TranslationPopup_DisplayWindow()
//{
//    document.getElementById("popup").style.display = "block";
//    windowVisible = true;
//}

// Displays the window
function TranslationPopup_DisplayWindow(controlID, UpdateDelegate)
{
    contentHolder = controlID;
    content = document.getElementById(contentHolder).innerText;
    
    // store the delegate to call when the popUp is finished
    delegate = UpdateDelegate;
    
    document.getElementById("translationTextArea").innerText = content;
    
    document.getElementById("popup").style.visibility = "visible";
    translationPopup.windowVisible = true;
}
  
// Checks whether the window should be selected or deselected
function TranslationPopup_CheckSelection()
{
    if  ((mouseX >= windowX) && (mouseX < windowX + windowWidth + 12) &&
        (mouseY >= windowY) && (mouseY < windowY + windowHeight + 50))
    {
        if (windowVisible) translationPopup.SelectWindow();
    }
    else
    {
        if (windowVisible) translationPopup.DeselectWindow();
    }
}

// Selects the window
function TranslationPopup_SelectWindow()
{
    windowSelected = true;
    document.getElementById("popupTopLeft").style.background = "url(images/common/popup/popupTopLeft.png)";
    document.getElementById("popupTop").style.background = "url(images/common/popup/popupTop.png)";
    document.getElementById("popupTopRight").style.background = "url(images/common/popup/popupTopRight.png)";
}

// Deselects the window
function TranslationPopup_DeselectWindow()
{
    windowSelected = false;
    document.getElementById("popupTopLeft").style.background = "url(images/common/popup/popupTopLeftDisabled.png)";
    document.getElementById("popupTop").style.background = "url(images/common/popup/popupTopDisabled.png)";
    document.getElementById("popupTopRight").style.background = "url(images/common/popup/popupTopRightDisabled.png)";
}

// Deselects all icons when the mouse button is released
function TranslationPopup_DeselectAll()
{
    beingMoved = false;
    beingResized = false;
}

// Updates the variables storing the mouse co-ordinates
function TranslationPopup_MouseMoved()
{
    if (event.clientX)
    {
        mouseX = event.clientX;
        mouseY = event.clientY;
    }
    else if (event.pageX) // Navigator compatability
    {
        mouseX = event.pageX;
        mouseY = event.pageY - scrollbarY;
    }
}

// Updates the variable storing the vertical scrollbar co-ordinate
function TranslationPopup_UpdateVerticalScrollbar()
{
    if (window.pageYOffset)
    {
        scrollbarY = window.pageYOffset;
    }
    else if (document.documentElement && document.documentElement.scrollTop)
    {
        scrollbarY = document.documentElement.scrollTop;
    }
    else if (document.body)
    {
        scrollbarY = document.body.scrollTop;
    }
}


// Updates the window offset and moves the window accordingly
function TranslationPopup_UpdateWindowOffset()
{
    if (offset > scrollbarY)
    {
        if (offset - scrollbarY <- offsetSpeed * (1 - offsetSpeed) / 2) offsetSpeed++;
        if (offset - scrollbarY > (1 - offsetSpeed) * (2 - offsetSpeed) / 2) offsetSpeed--;
    }
    if (offset < scrollbarY)
    {
        if (scrollbarY - offset < offsetSpeed * (offsetSpeed + 1) / 2) offsetSpeed--;
        if (scrollbarY - offset > (offsetSpeed + 1) * (offsetSpeed + 2) / 2) offsetSpeed++;
    }
    if (offset == scrollbarY) offsetSpeed=0;
    offset += offsetSpeed;
    document.getElementById("popup").style.top = windowY + offset + "px";
    if (isDebug) translationPopup.ShowDebug();
}

// Moves the window
function TranslationPopup_UpdateWindowPosition()
{
    windowX = mouseX - moveMouseX;
    windowY = mouseY - moveMouseY;
    document.getElementById("popup").style.left = windowX + "px";
    document.getElementById("popup").style.top = windowY + offset + "px";
    if (isDebug) translationPopup.ShowDebug();
}

// Presents window information in textarea
function TranslationPopup_ShowDebug()
{
    document.getElementById("translationTextArea").value = 
        "Window Position X: " + windowX + "\r" + 
        "Window Position Y: " + windowY + "\r" + 
        "Window Length X: " + windowWidth + "\r" + 
        "Window Length Y: " + windowHeight + "\r";
}

// Resizes the window
function TranslationPopup_UpdateWindowSize()
{
    document.getElementById("popupTop").style.width = (windowWidth - 12) + "px";
    document.getElementById("popupLeft").style.height = (windowHeight - 50) + "px";
    document.getElementById("popupBody").style.width = (windowWidth - 12) + "px";
    document.getElementById("popupBody").style.height = (windowHeight - 50) + "px";
    document.getElementById("popupRight").style.height = (windowHeight - 50) + "px";
    document.getElementById("popupBottom").style.width = (windowWidth - 28) + "px";
    document.getElementById("translationTextArea").style.height = (windowHeight - 82) + "px";
    if (isDebug) translationPopup.ShowDebug();
}

// Initializes the position and size of the window
function TranslationPopup_InitializeWindow()
{
    translationPopup.UpdateWindowSize()
}

// Updates the position and size of the window
function TranslationPopup_UpdateWindow() 
{
    translationPopup.UpdateVerticalScrollbar();
    translationPopup.UpdateWindowOffset();
    if (beingMoved) translationPopup.UpdateWindowPosition();
    if (beingResized) 
    {
        windowWidth = Math.min(668, Math.max(259, mouseX - windowX + resizeMouseX));
        windowHeight = Math.min(450, Math.max(185, mouseY - windowY + resizeMouseY));
        translationPopup.UpdateWindowSize();
    }
}

// Discards the contents of the window
function TranslationPopup_CancelPopup()
{
    document.getElementById("translationTextArea").value = "";
    translationPopup.CloseWindow();
}

// Accepts the contents of the window
//function TranslationPopup_AcceptPopup()
//{
//    alert("Do some AJAX magic");
//    document.getElementById("translationTextArea").value = "";
//    translationPopup.CloseWindow();
//}

// Accepts the contents of the window
function TranslationPopup_AcceptPopup()
{
    content = document.getElementById("translationTextArea").value;
    document.getElementById("translationTextArea").value = "";
    //
    //   Get the language to save
    //
    //var languageSelectName = document.getElementsByName("LanguageDropDownName")[0].innerText;
    //var languageCombo = document.getElementById(languageSelectName);
    //var languageCode = languageCombo.value;
    var languageCode = currentLanguageCode;
    //
    //   Find the DataToken or ContentToken
    //
    var elementObject = document.getElementById(contentHolder);
    var tokenName = "";
    var isContent = true;
    try
    {
        tokenName = elementObject.attributes["ContentToken"].nodeValue;
    }
    catch (exception)
    {
        tokenName = elementObject.attributes["DataToken"].nodeValue;
        isContent = false; 
    } 
    //
    //  Ajax call to save the new content
    //
    if (isContent)
    {  
        SaveTranslatedContent(tokenName, content, languageCode);
    }
   else
   {
        SaveTranslatedData(tokenName, content, languageCode);
   }  
    elementObject.innerText = content; 
    //
    //  End Ajax
    //

    translationPopup.CloseWindow();
    return content;
}