/**
 * Crop image scripts
 *
 * @copyright (c) 2004-2008, SWsoft
 */
function InitSbCrop() {
	self.sbCropObject = new SbCrop();
}

var SbCrop = Class.create();
SbCrop.prototype = {

	/**
	 * Current cropper object
	 *
	 * @var Cropper
	 * @access private
	 */
	_cropper: null,

	/**
	 * Crop left control
	 *
	 * @var obj
	 * @access private
	 */
	_cropLeft: null,

	/**
	 * Crop top control
	 *
	 * @var obj
	 * @access private
	 */
	_cropTop: null,

	/**
	 * Crop width control
	 *
	 * @var obj
	 * @access private
	 */
	_cropWidth: null,

	/**
	 * Crop height control
	 *
	 * @var obj
	 * @access private
	 */
	_cropHeight: null,

	/**
	 * Lock aspect ratio control
	 *
	 * @var obj
	 * @access private
	 */
	_lockAspectRatio: null,

	/**
	 * Image control
	 *
	 * @var obj
	 * @access private
	 */
	_image: null,

	/**
	 * Current ratio object
	 *
	 * @var obj
	 * @access private
	 */
	_currentRatio: null,

	/**
	 * Initialises the class
	 *
	 * @access public
	 * @return void
	 */
	initialize: function() {
		this._cropper = null;
		this._cropLeft = $('cropLeft');
		this._cropTop = $('cropTop');
		this._cropWidth = $('cropWidth');
		this._cropHeight = $('cropHeight');
		this._lockAspectRatio = $('lockAspectRatio');
		this._image = $('sbCropImage');
		this._currentRatio = null;

		this._cropLeft.focus();
		if (
			'' != this._cropWidth.value &&
			'' != this._cropHeight.value
		) {
			this._currentRatio = {
				x: this._cropWidth.value,
				y: this._cropHeight.value
			}
		} else {
			var imageDimensions = Element.getDimensions(this._image);
			this._currentRatio = {
				x: imageDimensions.width,
				y: imageDimensions.height
			};
		}

		var manager = this;
		Event.observe(self, 'load', function() { manager.initCropForm(); });

		var updateMethod = function() { manager.onCropFormParamsChange(); };
		Event.observe(this._lockAspectRatio, 'change', updateMethod);
		Event.observe(this._cropLeft, 'keyup', updateMethod);
		Event.observe(this._cropTop, 'keyup', updateMethod);
		Event.observe(this._cropWidth, 'keyup', updateMethod);
		Event.observe(this._cropHeight, 'keyup', updateMethod);
	},

	/**
	 * Inits crop form defaults
	 *
	 * @access public
	 * @return void
	 */
	initCropForm: function() {
		if ('' == this._cropLeft.value) {
			this._cropLeft.value = '0';
		}
		if ('' == this._cropTop.value) {
			this._cropTop.value = '0';
		}
		if ('' == this._cropWidth.value || '' == this._cropHeight.value) {
			var imageDimensions = Element.getDimensions(this._image);
			if ('' == this._cropWidth.value) {
				this._cropWidth.value = imageDimensions.width;
			}
			if ('' == this._cropHeight.value) {
				this._cropHeight.value = imageDimensions.height;
			}
		}
		this.initCropper();
	},

	/**
	 * Inits cropper
	 *
	 * @access public
	 * @return void
	 */
	initCropper: function() {
		var manager = this;
		var options = Object.extend(
			{
				captureKeys: false,

				onEndCrop: function (coords, dimensions) { return manager.onEndCrop(coords, dimensions); }
			}
		);
		if (this._lockAspectRatio.checked) {
			options.ratioDim = this._currentRatio;
		}
		options.displayOnInit = true;
		options.onloadCoords = {
			x1: parseInt(this._cropLeft.value),
			y1: parseInt(this._cropTop.value)
		};
		if (isNaN(options.onloadCoords.x1) || (options.onloadCoords.x1 < 0)) {
			options.onloadCoords.x1 = 0;
		}
		if (isNaN(options.onloadCoords.y1) || (options.onloadCoords.y1 < 0)) {
			options.onloadCoords.y1 = 0;
		}
		options.onloadCoords.x2 = options.onloadCoords.x1 + parseInt(this._cropWidth.value);
		if (isNaN(options.onloadCoords.x2) || (options.onloadCoords.x2 < 0)) {
			options.onloadCoords.x2 = 0;
		}
		options.onloadCoords.y2 = options.onloadCoords.y1 + parseInt(this._cropHeight.value);
		if (isNaN(options.onloadCoords.y2) || (options.onloadCoords.y2 < 0)) {
			options.onloadCoords.y2 = 0;
		}
		var imageDimensions = Element.getDimensions(this._image);
		if (options.onloadCoords.x1 > imageDimensions.width) {
			options.onloadCoords.x1 = imageDimensions.width;
		}
		if (options.onloadCoords.y1 > imageDimensions.height) {
			options.onloadCoords.y1 = imageDimensions.height;
		}

		var startPoint = new Array();
		startPoint[0] = options.onloadCoords.x1;
		startPoint[1] = options.onloadCoords.y1;
		this._cropper = new Cropper.Img(
			this._image,
			options
		);
		this._cropper.moveArea(startPoint);
	},

	/**
	 * onEndCrop event
	 *
	 * @access public
	 * @param object coords of top left point
	 * @param object dimensions of cropped image
	 * @return void
	 */
	onEndCrop: function(coords, dimensions) {
		var value = Math.abs(coords.x1);
		if (value != this._cropLeft.value) {
			this._cropLeft.value = value;
		}
		value = Math.abs(coords.y1);
		if (value != this._cropTop.value) {
			this._cropTop.value = value;
		}
		value = Math.abs(dimensions.width);
		if (value != this._cropWidth.value) {
			this._cropWidth.value = value;
		}
		value = Math.abs(dimensions.height);
		if (value != this._cropHeight.value) {
			this._cropHeight.value = value;
		}
		if (!this._lockAspectRatio.checked && (dimensions.width > 0) && (dimensions.height > 0)) {
			this._currentRatio = {
				x: Math.abs(dimensions.width),
				y: Math.abs(dimensions.height)
			}
		}
	},

	/**
	 * Executes if crop form controls have been changed state
	 *
	 * @access public
	 * @return void
	 */
	onCropFormParamsChange: function() {
		this._cropper.remove();
		this.initCropper();
	},

	/**
	 * Saves all user data and closes form dialog
	 *
	 * @access public
	 * @return void
	 */
	ok: function() {
		var helper = self.parent.getSbImageHelperInstance();

		var formParameters = new Object();
		var src = helper.getAttribute(this._image, 'sbsavedsrc', '');
		if (src.length == 0) {
			src = helper.getAttribute(this._image, 'src', this._image.src);
		}
		formParameters.src = helper.getRelativeImageUrl(src);

		var imageDimensions = Element.getDimensions(this._image);
		formParameters.width = imageDimensions.width;
		formParameters.height = imageDimensions.height;

		formParameters.cropLeft = this._cropLeft.value;
		formParameters.cropTop = this._cropTop.value;
		formParameters.cropWidth = this._cropWidth.value;
		formParameters.cropHeight = this._cropHeight.value;

		var manager = this;
		var request = new SbAjaxRequest.JsonAction(
			self.parent.baseUrl + '/Ajax/Wizard/Edit/Wysiwyg/CropImage',
			formParameters,
			{
				onActionSuccess: function(requestResult) {
					var imageInfo = helper.getSelectedImageInfo();
					if (imageInfo && imageInfo.image) {
						var fckObject = helper.getFckObject();
						fckObject.FCKUndo.SaveUndoStep();

						helper.setImageSrc(imageInfo.image, requestResult.src);
						helper.setAttribute(imageInfo.image, 'width', requestResult.width);
						imageInfo.image.style.width = '';
						helper.setAttribute(imageInfo.image, 'height', requestResult.height);
						imageInfo.image.style.height = '';

						fckObject.FCK.Commands.GetCommand('SbTranslator').Execute();
					}
					manager.close();
				}
			}
		);
	},

	/**
	 * Closes form dialog without saving
	 *
	 * @access public
	 * @return void
	 */
	close: function() {
		self.parent.sbCropFormObject.close();
	}
}