/**
 * A little manager that allows us to swap the image dynamically
 *
 */

var myX1;
var myX2;
var myY1;
var myY2;

var CropImageManager = {
    /**
     * Holds the current Cropper.Img object
     * @var obj
     */
    curCrop: null,

    /**
     * Initialises the cropImageManager
     *
     * @access public
     * @return void
     */
//    init: function() {
//        this.attachCropper();
//    },

    /**
     * Handles the changing of the select to change the image, the option value
     * is a pipe seperated list of imgSrc|width|height
     *
     * @access public
     * @param obj event
     * @return void
     */
    onChange: function( e ) {
        var vals = $F( Event.element( e ) ).split('|');
        this.setImage( vals[0], vals[1], vals[2] );
    },

    /**
     * Sets the image within the element & attaches/resets the image cropper
     *
     * @access private
     * @param string Source path of new image
     * @param int Width of new image in pixels
     * @param int Height of new image in pixels
     * @return void
     */
    setImage: function(element, imgSrc, w, h ) {
        $(element).src = imgSrc;
        $(element).width = w;
        $(element).height = h;
//        this.attachCropper(element);
    },

    /**
     * Attaches/resets the image cropper
     *
     * @access private
     * @return void
     */
    attachCropper: function(mainImage, livePreview, ratiox, ratioy, minx, miny, initialX1, initialY1, initialX2, initialY2) {
        if( this.curCrop == null ) this.curCrop = new Cropper.ImgWithPreview( mainImage, {
            ratioDim: { x: ratiox, y: ratioy },
            displayOnInit: true,
            onloadCoords: { x1: initialX1, y1: initialY1, x2: initialX2, y2: initialY2 },
            minWidth: minx,
			minHeight: miny,
            onEndCrop: onEndCrop,
            previewWrap: livePreview
        } );
        else this.curCrop.reset();
    },

    /**
     * Removes the cropper
     *
     * @access public
     * @return void
     */
    removeCropper: function() {
        if( this.curCrop != null ) {
            this.curCrop.remove();
            this.curCrop = null; //needed this to get the aspect to change...
        }
    },

    getX1: function() {
        return myX1;
    },

    getY1: function() {
        return myY1;
    },

    getX2: function() {
        return myX2;
    },

    getY2: function() {
        return myY2;
    }
//    ,

    /**
     * Resets the cropper, either re-setting or re-applying
     *
     * @access public
     * @return void
     */
//    resetCropper: function() {
//        this.attachCropper();
//    }
};


// setup the callback function
function onEndCrop( coords, dimensions ) {

    myX1 = coords.x1;
    myX2 = coords.x2;
    myY2 = coords.y2;
    myY1 = coords.y1;

//    $( 'htmlX1' ).value = coords.x1;
//    $( 'htmlY1' ).value = coords.y1;
//    $( 'htmlX2' ).value = coords.x2;
//    $( 'htmlY2' ).value = coords.y2;
//    $( 'width' ).value = dimensions.width;
//    $( 'height' ).value = dimensions.height;
}

//// basic example
//Event.observe(
//    window,
//    'load',
//    function() {
////        CropImageManager.init();
////        Event.observe( $('removeCropper'), 'click', CropImageManager.removeCropper.bindAsEventListener( CropImageManager ), false );
////        Event.observe( $('resetCropper'), 'click', CropImageManager.resetCropper.bindAsEventListener( CropImageManager ), false );
////        Event.observe( $('imageChoice'), 'change', CropImageManager.onChange.bindAsEventListener( CropImageManager ), false );
//    }
//);


//CropImageManager.initializeCrop = function() {
//    Class.create().init();
//}

//CropImageManager.InitializeCrop = function(element) {
//    Class.create().init(element);
//}


//CropImageManager.InitializeCrop = Class.create();
//Object.extend(CropImageManager.InitializeCrop.prototype){
//      this.element = $(element);
//      this.init(this.element);
//};



/*
if( typeof(dump) != 'function' ) {
    Debug.init(true, '/');

    function dump( msg ) {
        Debug.raise( msg );
    };
} else dump( '---------------------------------------\n' );
*/
