﻿
// Constructor function for the EditPanel class.
function PhotoEditTab()
{
    this.tabs = new Array();
    this.service = new IPhotoEditService();
    this.cropper = new Cropper();
    this.editPhotoOptions = new Array();
    this.addEffectOptions = new Array();
}

// Fields and methods for the EditPanel class.
PhotoEditTab.prototype = {

    apertureID: 0,
    mediaID: null,
    mediaReloadCallback: null,
    mediaNotifyCallback: null,
    notifyTabChanged: null,
    notifyCropperStart: null,
    notifyCropperStop: null,
    tabs: null,
    service: null,
    currentTint: 0,
    cropper: null,
    editPhotoOptions: null,
    addEffectOptions: null,
    currentEditOption: -1,
    currentAddEffectOption: -1,
    orientation: 0,
    currentTab: -1,
    cropperRPID: null,
    cropperImageID: null,
    cropperMinWidth: 0,
    cropperMinHeight: 0,
    cropperShortSize: 0,
    cropperLongSize: 0,
    cropperRunning: false,
    resolutionWarningID: null,
    zoomLevel: 0,
    zoomMin: 0,
    zoomMax: 0,
    zoomChangedCallback: null,
    zoomChangedNoApertureCallback: null,
    manager: null,
    frame: 0,
    blackAndWhiteOnly: false,

    setBlackAndWhiteOnly: function(onlyBW) {
        this.blackAndWhiteOnly = onlyBW;
    },

    setManager: function(manager) {
        this.manager = manager;
    },

    getManager: function() {
        return this.manager;
    },

    setMediaID: function(ID) {
        this.mediaID = ID;
        this.stopCropper();
    },

    setApertureID: function(ID) {
        this.apertureID = ID;
    },

    getMediaID: function() {
        return this.mediaID;
    },

    getApertureID: function() {
        return this.apertureID;
    },

    setCropperImageID: function(ID) {
        this.cropperImageID = ID;
    },

    setOrientation: function(landscape) {
        this.orientation = landscape ? 0 : 1;
        this.restartCropper();
    },

    setResolutionWarningID: function(ID) {
        this.resolutionWarningID = ID;
    },

    setMediaReloadCallback: function(func) {
        this.mediaReloadCallback = func;
    },

    setMediaNotifyCallback: function(func) {
        this.mediaNotifyCallback = func;
    },

    setNotifyTabChanged: function(func) {
        this.notifyTabChanged = func;
    },

    setNotifyCropperStart: function(func) {
        this.notifyCropperStart = func;
    },

    setNotifyCropperStop: function(func) {
        this.notifyCropperStop = func;
    },

    setZoomChangedCallback: function(func) {
        this.zoomChangedCallback = func;
    },

    setZoomChangedNoApertureCallback: function(func) {
        this.zoomChangedNoApertureCallback = func;
    },

    getFrame: function() {
        return this.frame;
    },

    incFrame: function() {
        this.frame++;
    },

    getCurrentTint: function() {
        return this.currentTint;
    },

    selectTint: function(num) {
        if (this.currentTint != num) {
            this.currentTint = num;
            this.tint(num);
            //            this.selectTintRadioButton(num);
        }
    },

    selectTintRadioButton: function(num) {
        this.currentTint = num;
        //        document.getElementById('chkTint' + num).checked = true;
    },

    getSelectPhotoPanel: function() {
        return selectPhotos;
    },

    addTab: function(tab) {
        this.tabs.push(tab);
    },

    changeTab: function(num) {
        this.stopCropper();

        if (this.currentTab == num) {
            return;
        }

        for (var i = 0; i < this.tabs.length; i++) {
            var tab = this.tabs[i];

            if (i == num) {
                document.getElementById(tab.DivID).style.display = '';
                document.getElementById(tab.ButtonID).src = tab.ImageOn;
            }
            else {
                document.getElementById(tab.DivID).style.display = 'none';
                document.getElementById(tab.ButtonID).src = tab.ImageOff;
            }
        }

        this.currentTab = num;

        if (this.notifyTabChanged != null) {
            this.notifyTabChanged(num);
        }
    },

    getCurrentTab: function() {
        return this.currentTab;
    },

    restartCropper: function() {
        if (this.cropperRunning) {
            this.startCropper(this.cropperRPID, this.cropperShortSize, this.cropperLongSize);
        }
    },

    startCropper: function(rpID, shortSize, longSize) {
        this.stopCropper();
        this.cropperRPID = rpID;
        this.cropperLongSize = longSize;
        this.cropperShortSize = shortSize;
        this.service.GetMinimumResolution(this.mediaID, rpID, shortSize / longSize, this.onGotMinResolution, null, this);
    },

    onGotMinResolution: function(result, obj, shortSize, longSize) {
        var imgdiv = document.getElementById(obj.cropperImageID);

        obj.cropperMinWidth = Math.round(imgdiv.clientWidth * result[0]);
        obj.cropperMinHeight = Math.round(imgdiv.clientHeight * result[1]);

        var initial = obj.getBestCropFit(obj.cropperLongSize, obj.cropperShortSize, imgdiv.width, imgdiv.height);

        if (!obj.cropper) {
            obj.cropper = new Cropper;
        }

        var proportionWidth = obj.cropperLongSize;
        var proportionHeight = obj.cropperShortSize;

        if (obj.orientation == 1) {
            proportionWidth = obj.cropperShortSize;
            proportionHeight = obj.cropperLongSize;
        }

        obj.cropperRunning = true;
        obj.cropper.baseObject = obj;
        obj.cropper.startCropping(obj.cropperImageID, initial.Width, initial.Height, proportionWidth, proportionHeight, obj.cropperMinWidth, obj.cropperMinHeight, obj.onCropperUpdate);

        if (obj.notifyCropperStart) {
            obj.notifyCropperStart();
        }
    },

    onCropperUpdate: function(cropInfo, obj) {
        var warnRes = ((cropInfo.width <= obj.cropperMinWidth) || (cropInfo.height <= obj.cropperMinHeight))

        if (obj.resolutionWarningID) {
            document.getElementById(obj.resolutionWarningID).style.visibility = warnRes ? 'visible' : 'hidden';
        }
    },

    showResolutionWarning: function(show) {
        if (this.resolutionWarningID) {
            document.getElementById(this.resolutionWarningID).style.visibility = show ? 'visible' : 'hidden';
        }
    },

    applyCrop: function() {
        var cropInfo = this.cropper.getCropInfo();

        this.stopCropper();

        var imgdiv = document.getElementById(this.cropperImageID);

        var l = eval(cropInfo.left) / imgdiv.clientWidth;
        var r = eval((cropInfo.left) + cropInfo.width) / imgdiv.clientWidth;
        var t = eval(cropInfo.top) / imgdiv.clientHeight;
        var b = eval((cropInfo.top) + cropInfo.height) / imgdiv.clientHeight;

        if (l < 0) l = 0;
        if (r > 0.99) r = 1.0;
        if (t < 0) t = 0;
        if (b > 0.99) b = 1.0;

        this.notifyMediaOperation('crop');
        this.service.Crop(this.apertureID, this.mediaID, l, t, r, b, this.mediaReloadCallback, gotError);
    },

    getBestCropFit: function(cropWidth, cropHeight, imgWidth, imgHeight) {
        var factor = 1.5;
        var initialWidth;
        var initialHeight;
        var cropRatio = cropWidth / cropHeight;

        if (this.orientation == 1) {
            cropRatio = cropHeight / cropWidth;
        }

        if (imgWidth >= imgHeight) {
            initialHeight = imgHeight / factor;
            initialWidth = initialHeight * cropRatio;
        }
        else {
            initialWidth = imgWidth / factor;
            initialHeight = initialWidth / cropRatio;
        }
        return { Width: Math.round(initialWidth), Height: Math.round(initialHeight) };
    },

    stopCropper: function() {
        if (this.cropper) {
            this.cropper.stopCropping();
        }

        if (this.cropperRunning) {
            this.cropperRunning = false;

            if (this.notifyCropperStop) {
                this.notifyCropperStop();
            }
        }
    },

    notifyMediaOperation: function(type) {
        this.incFrame();

        if (this.mediaNotifyCallback != null) {
            var result = this.mediaNotifyCallback(type);

            if (typeof (result) == 'undefined') {
                return true;
            }
            else {
                return result;
            }
        }

        return true;
    },

    tint: function(num) {
        if (this.notifyMediaOperation('tint') != false) {
            this.service.Tint(this.apertureID, this.mediaID, num, this.mediaReloadCallback, gotError, 'tint');
        }
    },

    rotate: function(counterClockwise) {
        if (this.notifyMediaOperation('rotate') != false) {
            this.service.Rotate(this.apertureID, this.mediaID, counterClockwise, this.mediaReloadCallback, gotError, 'rotate');
        }
    },

    redEye: function() {
        if (this.notifyMediaOperation('redEye') != false) {
            this.service.RedEye(this.apertureID, this.mediaID, this.mediaReloadCallback, gotError, 'redEye');
        }
    },

    flip: function() {
        if (this.notifyMediaOperation('flip') != false) {
            this.service.Flip(this.apertureID, this.mediaID, this.mediaReloadCallback, gotError, 'flip');
        }
    },

    flipV: function() {
        if (this.notifyMediaOperation('flipV') != false) {
            this.service.FlipV(this.apertureID, this.mediaID, this.mediaReloadCallback, gotError, 'flipV');
        }
    },

    restoreOriginal: function() {
        if (this.notifyMediaOperation('revert') != false) {
            this.service.RestoreOriginal(this.apertureID, this.mediaID, this.mediaReloadCallback, gotError, 'revert');
        }
    },

    clearSessionEdits: function() {
        // no callback here, if you want to get the callbacks use 'restoreOriginal' instead.
        this.service.RestoreOriginal(this.apertureID, this.mediaID, function() { }, gotError);
    },

    saveEdits: function() {
        if (this.notifyMediaOperation('save') != false) {
            this.service.SaveEdits(this.apertureID, this.mediaID, this.mediaReloadCallback, gotError, 'save');
        }
    },

    setZoomLevel: function(level) {
        var zoom = this.zoomLevel;

        if (this.zoomLevel != level) {
            if (this.getMediaID() != null) {
                this.zoomLevel = level;

                if (this.zoomLevel > this.zoomMax) {
                    this.zoomLevel = this.zoomMax;
                }

                if (this.zoomLevel < this.zoomMin) {
                    this.zoomLevel = this.zoomMin;
                }
            }

            if (zoom != this.zoomLevel) {
                if (this.zoomChangedCallback != null) {
                    this.zoomChangedCallback(this.zoomLevel);
                }
                this.updateZoomGraph();
            }
        }
    },

    getZoomLevel: function() {
        return this.zoomLevel;
    },

    setZoomMin: function(min) {
        this.zoomMin = min;
    },

    setZoomMax: function(max) {
        this.zoomMax = max;
    },

    zoomIn: function() {
        if (this.getMediaID() != null) {
            this.setZoomLevel(this.zoomLevel + 1);
        }
        else if (this.zoomChangedNoApertureCallback != null) {
            this.zoomChangedNoApertureCallback();
        }
    },

    zoomOut: function() {
        if (this.getMediaID() != null) {
            this.setZoomLevel(this.zoomLevel - 1);
        }
        else if (this.zoomChangedNoApertureCallback != null) {
            this.zoomChangedNoApertureCallback();
        }
    },

    updateZoomGraph: function() {
        for (var i = this.zoomMin; i <= this.zoomMax; i++) {
            var img = document.getElementById('ctl00_pageContent_photoEditTab_toolbar_zoom_' + i);

            if (i > this.zoomLevel) {
                img.src = img.src.replace(/on/g, 'off');
            }
            else {
                img.src = img.src.replace(/off/g, 'on');
            }
        }
    },

    processRequirements: function() {
        if (this.blackAndWhiteOnly === true) {
            this.selectTint(1);
        }
    },

    addEditPhotoOption: function(option) {
        this.editPhotoOptions.push(option);
    },

    changeEditPhotoOption: function(idx) {

        if (this.currentEditOption == idx) {
            return;
        }

        for (var i = 0; i < this.editPhotoOptions.length; i++) {

            var opt = this.editPhotoOptions[i];

            if (i == idx) {
                document.getElementById(opt.SpanId).className = opt.classOn;
                document.getElementById(opt.ButtonId).src = opt.ImageOn;
            }
            else {
                document.getElementById(opt.SpanId).className = opt.classOff;
                document.getElementById(opt.ButtonId).src = opt.ImageOff;
            }
        }

        this.currentEditOption = idx;
    },

    addAddEffectOption: function(option) {
        this.addEffectOptions.push(option);
    },

    changeAddEffectOption: function(idx) {

        if (this.currentAddEffectOption == idx) {
            return;
        }

        for (var i = 0; i < this.addEffectOptions.length; i++) {

            var opt = this.addEffectOptions[i];

            if (i == idx) {
                document.getElementById(opt.SpanId).className = opt.classOn;
                document.getElementById(opt.ButtonId).src = opt.ImageOn;
            }
            else {
                document.getElementById(opt.SpanId).className = opt.classOff;
                document.getElementById(opt.ButtonId).src = opt.ImageOff;
            }
        }

        this.currentAddEffectOption = idx;
    }

};
