// table to convert units. first index - new unit, second index - old unit
var UNIT_CONVERSION = {
    "inch" :  {"inch" : 1,      "cm" : 1.0 / 2.5402,  "point" : 1.0 / 72.0,     "pica" : 1.0 / 864.0},
    "cm" :    {"inch" : 2.5402, "cm" : 1,             "point" : 1.0 / 28.3465,  "pica" : 1.0 / 340.1307},
    "point" : {"inch" : 72.0,   "cm" : 28.3465,       "point" : 1,              "pica" : 1.0 / 12.0},
    "pica" :  {"inch" : 864.0,  "cm" : 340.1307,      "point" : 12.0,           "pica" : 1}
}

var ASPECT_FIELDS = {
    "width" : "jobsTable_width",
    "height" : "jobsTable_height",
    "unit" : "jobsTable_unit",
    "profile" : "jobsTable_profile",
    "percent" : "jobsTable_percent"
}

var DEFAULT_UNIT = "cm";

function aspectRatioOnSizeChange(evt, id) {
    if (window.event != null) {
        evt = window.event;
    }
    if (evt != null) {
        var control = evt.target ? evt.target : evt.srcElement;
        id = new String(control.id);
    }
    if (id == null) {
        return;
    }
    var idPrefix;
    var field = null;
    for (var afield in ASPECT_FIELDS) {
        if (id.indexOf(ASPECT_FIELDS[afield]) > 0) {
            field = afield;
            idPrefix = id.substring(0, id.lastIndexOf(":"));
            break;
        }
    }
    if (field == null) {
        return;
    }

    var widthControl = document.getElementById(idPrefix + ":jobsTable_width");
    var heightControl = document.getElementById(idPrefix + ":jobsTable_height");
    var percentControlField = document.getElementById(idPrefix + ":jobsTable_percentcomboboxField");
    var percentControlValue = document.getElementById(idPrefix + ":jobsTable_percentcomboboxValue");
    var unitControl = document.getElementById(idPrefix + ":jobsTable_unit");
    var profileControl = document.getElementById(idPrefix + ":jobsTable_profile");
    var assetWidth = widthControl.alt;
    var assetHeight = heightControl.alt;

    var oldUnit = unitControl.oldUnit;
    if (! oldUnit) {
        oldUnit = DEFAULT_UNIT;
    }
    var unit = unitControl.value;
    unitControl.oldUnit = unit;

    var oldDpi = profileControl.oldDpi;
    if (! oldDpi) {
        if (profileControl.oldDpiX) {
            oldDpi = profileControl.oldDpiX;
            oldDpi = oldDpi.substr(oldDpi.indexOf("x") + 1);
        } else {
            oldDpi = 72.0;
        }
    }
    var dpi = profileControl.value;
    dpi = dpi.substr(dpi.indexOf("x") + 1);
    profileControl.oldDpi = dpi;

    var value = widthControl.value;
    var value2 = heightControl.value;
    var ratio;
    switch (field) {
        case "width":
            value2 = widthControl.value * (assetHeight / assetWidth);
            break;
        case "height":
            value = heightControl.value * (assetWidth / assetHeight);
            break;
        case "unit":
            ratio = 1;
            var unit2 = unit;
            var oldUnit2 = oldUnit;
            if (unit == "pixels" || oldUnit == "pixels") {
                // pixels
                if (oldUnit2 == "pixels") {
                    oldUnit2 = "inch";
                    ratio /= dpi;
                }
                if (unit2 == "pixels") {
                    unit2 = "inch";
                    ratio *= dpi;
                }
            }
            ratio *= UNIT_CONVERSION[unit2][oldUnit2];
            value = widthControl.value * ratio;
            value2 = heightControl.value * ratio;
            break;
        case "profile":
            ratio = 1;
            if (unit != "pixels") {
                ratio = oldDpi / dpi;
            }
            value = widthControl.value * ratio;
            value2 = heightControl.value * ratio;
            break;
        case "percent":
            ratio = percentControlField.value / 100;
            if (unit != "pixels") {
                ratio /= dpi;
                ratio *= UNIT_CONVERSION[unit]["inch"];
            }
            value = assetWidth * ratio;
            value2 = assetHeight * ratio;
            break;
    }

    var percentage;
    if (field == "percent") {
        percentage = percentControlField.value;
        if (percentage > 120) {
            percentControlField.style.backgroundColor = "red";
        } else {
            percentControlField.style.backgroundColor = "";
        }
    } else {
        percentage = 0;
        if (unit == "pixels") {
            percentage = value / assetWidth;
        } else {
            var assetWidthInch = assetWidth / dpi;
            var newWidthInch = value * UNIT_CONVERSION["inch"][unit];
            percentage = newWidthInch / assetWidthInch;
        }

        percentage = Math.round(percentage * 10000) / 100;
        percentControlField.value = percentage;
        percentControlValue.value = percentage;
        if (percentage > 120) {
            percentControlField.style.backgroundColor = "red";
        } else {
            percentControlField.style.backgroundColor = "";
        }
    }

    value = Math.round(value * 100) / 100;
    value2 = Math.round(value2 * 100) / 100;
    switch (field) {
        case "width":
            heightControl.value = value2;
            break;
        case "height":
            widthControl.value = value;
            break;
        case "unit":
        case "profile":
        case "percent":
            widthControl.value = value;
            heightControl.value = value2;
            break;
    }
}

function implAspectRatioRegisterListeners(control) {
    control.onclick = aspectRatioOnSizeChange;
    var children = control.childNodes;
    for (var i = 0; i < children.length; i++) {
        var child = children[i];
        if (child.nodeType == 1) {
            implAspectRatioRegisterListeners(child);
        }
    }
}

function aspectRatioRegisterListeners(control) {
    implAspectRatioRegisterListeners(control.parentNode);
}

