﻿function sendToLogin() {
    alert("Session has expired, please login again");
    parent.window.location.href = "http://www.locallinkup.com";
}

var cropTool;
var resize = false;
var origImageWidth;
var origImageHeight;
$(document).ready(function() {
    cropTool = $.Jcrop('#cropbox', { bgOpacity: .4, onSelect: setResizeLoc, onChange: showCoords });

    // Disable crop tool
    cropTool.disable();

    // Get size of original image
    origImageWidth = document.getElementById('originWidth').value;
    origImageHeight = document.getElementById('originHeight').value;
    // Hide preview
    hideControl('preview');
});

// Enable image cropping
function CropButtonClick() {
    // Enable crop tool
    cropTool.enable();
    resize = false;
    // Allow selection to be moved
    cropTool.setOptions({ allowMove: true });
    // Animate and display crop zone
    cropTool.animateTo([origImageWidth / 4, origImageHeight / 4, (origImageWidth / 4) * 3, (origImageHeight / 4) * 3]);
    // Maintain the apsect ratio
    cropTool.setOptions({ aspectRatio: 4 / 3 });
    // Show preview
    document.getElementById('preview').src = document.getElementById('imgLink').value;
    // Set the edit type to crop
    document.getElementById('EditType').value = "crop";
    // Show preview
    showControl('preview');
}

// Enable image resizing
function ResizeButtonClick() {
    // Enable crop tool
    cropTool.enable();
    resize = true;
    // Do not allow selection to be moved and maintain the apsect ratio
    cropTool.setOptions({ allowMove: false, aspectRatio: 4 / 3 });
    // Animate and display resize zone
    cropTool.animateTo([0, 0, origImageWidth, origImageHeight]);
    // Show preview
    document.getElementById('preview').src = document.getElementById('imgLink').value;
    // Set the edit type to resize
    document.getElementById('EditType').value = "resize";
    // Hide preview
    hideControl('preview');
}

// Cancel button
function CancelButtonClick() {
    cropTool.release();
    document.getElementById('hfReload').value = "reload";
    __doPostBack('hfReload', '');
    // Hide preview
    hideControl('preview');
}

function Close() {
    parent.hideModalWindow('imageEditorModal');
}

function closeReturn() {
    parent.hideModalWindow('imageEditorModal');
    parent.setPlinkImage(document.getElementById('hfRetVal').value);
}

function setResizeLoc(im) {
    if (resize) {
        $get("<%=btnSaveNewImage.ClientID%>").click();
    }
}

function showCoords(c) {
    var rx = 100 / c.w;
    var ry = 100 / c.h;
    document.getElementById('XPos').value = c.x;
    document.getElementById('YPos').value = c.y;
    document.getElementById('Width').value = c.w;
    document.getElementById('Height').value = c.h;

    $('#preview').css({
        width: Math.round(rx * origImageWidth) + 'px',
        height: Math.round(ry * origImageHeight) + 'px',
        marginLeft: '-' + Math.round(rx * c.x) + 'px',
        marginTop: '-' + Math.round(ry * c.y) + 'px'
    });
}

// Hide control
function hideControl(controlId) {
    var control = document.getElementById(controlId);
    control.style.visibility = "hidden";
}

// Show control
function showControl(controlId) {
    var control = document.getElementById(controlId);
    control.style.visibility = "visible";
}
