﻿var jcropModes =  { cropping:1, tagging:2, selecting:3 } 
var jcrop_api = null;
var jcrop_mode = 0;
var timeoutJcropDestroy = 0;
var timeoutJcropCreate = 0;

function jcropSelectExistingTag(imgID, imgWidth, imgHeight, area) {    
    if (jcrop_mode == 0 || jcrop_mode == jcropModes.selecting) {
        if (timeoutJcropDestroy) {
            window.clearTimeout(timeoutJcropDestroy);
            timeoutJcropDestroy = 0;
        }
        
        if (!timeoutJcropCreate) {
            timeoutJcropCreate = window.setTimeout(function() {
                if (jcrop_api == null) {
                    jcrop_api = $.Jcrop('#' + imgID, { trueSize: [imgWidth, imgHeight], allowResize: 0, allowSelect: 0 });
                }

                if (jcrop_api != null)
                    jcrop_api.setSelect(area);

                timeoutJcropCreate = 0;
            },
        200);
        }
        
        jcrop_mode = jcropModes.selecting;
    }
}

function jcropRotateSelection() {
    if (jcrop_api) {
        var rect = jcrop_api.tellSelect();
        jcrop_api.setSelect([rect.x, rect.y, rect.x + rect.h, rect.y + rect.w]);

        /*
        var x2 = jcrop_api.tellSelect().x2;
        while(jcrop_api.tellSelect().w < rect.h)
            jcrop_api.setSelect([rect.x, rect.y, ++x2, jcrop_api.tellSelect().y2]);

        var y2 = jcrop_api.tellSelect().y2;
        while (jcrop_api.tellSelect().h < rect.w)
            jcrop_api.setSelect([rect.x, rect.y, jcrop_api.tellSelect().x2, ++y2]);
        */    
        //jcrop_api.setOptions({ aspectRatio: 0, allowResize: 0, allowSelect: 0 });
        //jcrop_api.focus();
    }
}

function jcropReleaseSelection(){    
    if (jcrop_mode == jcropModes.selecting) {
        if (timeoutJcropCreate) {
            window.clearTimeout(timeoutJcropCreate);
            timeoutJcropCreate = 0;
        }
        
        if (!timeoutJcropDestroy) 
            timeoutJcropDestroy = window.setTimeout('if(jcrop_api != null) { jcrop_api.destroy(); jcrop_api = null; jcrop_mode = 0; timeoutJcropDestroy = 0; }', 500);
    }
}

function jcropCropping(imgID, imgWidth, imgHeight, area) {
    if (jcrop_api) 
        jcrop_api.destroy();

    jcrop_api = $.Jcrop('#' + imgID, { trueSize: [imgWidth, imgHeight], onSelect: jcropOnSelect, onChange: jcropOnChange });
    jcrop_mode = jcropModes.cropping;
    jcrop_api.animateTo(area);
}

function jcropStopCropping() {
    if (jcrop_mode == jcropModes.cropping) {
        if (jcrop_api)
            jcrop_api.destroy();
            
        jcrop_api = null;
        jcrop_mode = 0;
    }
    
}

function jcropTagging(imgID, imgWidth, imgHeight) {
    if(jcrop_api) 
        jcrop_api.destroy();

    jcrop_api = $.Jcrop('#' + imgID, { trueSize: [imgWidth, imgHeight], onSelect: jcropOnSelect, onChange: jcropOnChange });
    jcrop_mode = jcropModes.tagging;
}

function jcropStopTagging() {
    if (jcrop_mode == jcropModes.tagging) {
        if (jcrop_api)
            jcrop_api.destroy();

        jcrop_api = null;
        jcrop_mode = 0;
    }
}
