/**
 * Fotolia purchase image form scripts
 *
 * @copyright (c) 2004-2008, SWsoft
 */
var FotoliaPurchaseImageFormManager = Class.create();
FotoliaPurchaseImageFormManager.prototype = {

	/**
	 * Fotolia image id
	 *
	 * @var string
	 * @access private
	 */
	_imageId: null,

	/**
	 * Form object
	 *
	 * @var SbWysiwygForm
	 * @access private
	 */
	_formObject: null,

	/**
	 * Form content iframe
	 *
	 * @var obj
	 * @access private
	 */
	_contentIframe: null,

	/**
	 * Selected license
	 *
	 * @var string
	 * @access private
	 */
	_license: null,

	/**
	 * Object constructor
	 *
	 * @access public
	 * @param string
	 * @return void
	 */
	initialize: function(imageId) {
		this._imageId = imageId;
		this._formObject = self.parent.fotoliaPurchaseImageFormObject;
		this._contentIframe = self.parent.$('fotoliaPurchaseImageFormContent');

		var manager = this;
		this._formObject.resetEventsListeners();
		this._formObject.observeOnAfterClose(
			function() {
				manager.clearContent();
			}
		);
		this.openSelectLicenseStep();
		this._formObject.open();
	},

	/**
	 * Cleares loaded content
	 *
	 * @access public
	 * @return void
	 */
	clearContent: function() {
		this._contentIframe.src = '/blank.html';
		this._formObject.changeSize(800, 600);
	},

	/**
	 * Changes modal form size
	 *
	 * @access public
	 * @param int
	 * @param int
	 * @return void
	 */
	changeFormSize: function(width, height) {
		this._formObject.changeSize(width, height);
	},

	/**
	 * Returns max form dimensions
	 *
	 * @access public
	 * @return object
	 */
	getMaxDimensions: function() {
		return this._formObject.getMaxDimensions();
	},

	/**
	 * Sets new license
	 *
	 * @access public
	 * @param FotoliaLicense
	 * @return void
	 */
	setLicense: function(license) {
		this._license = license;
	},

	/**
	 * Returns URL to the current view
	 *
	 * @access public
	 * @return void
	 */
	getCurrentView: function() {
		if (this._license) {
			return this._getConfirmationStepUrl();
		}
		return this._getSelectLicenseStepUrl();
	},

	/**
	 * Loads select license step
	 *
	 * @access public
	 * @return void
	 */
	openSelectLicenseStep: function() {
		this._license = null;

		this._contentIframe.src = this._getSelectLicenseStepUrl();
	},

	/**
	 * Loads confirmation step
	 *
	 * @access public
	 * @return void
	 */
	openConfirmationStep: function() {
		if (this._license) {
			this._contentIframe.src = this._getConfirmationStepUrl();
		}
	},

	/**
	 * Executes purchase process
	 *
	 * @access public
	 * @param FotoliaConfirmationStepManager
	 * @return void
	 */
	processPurchase: function(confirmationStepManager) {
		confirmationStepManager.startPurchaseProgress();

		var requestParams = new Object();
		requestParams.imageId = this._imageId;
		requestParams.licenseName = this._license.name;

		var resultMethod = function () {
			confirmationStepManager.showError('Unexpected Sitebuilder error');
		};
		var manager = this;
		var request = new SbAjaxRequest.JsonAction(
			self.parent.baseUrl + '/Ajax/Wizard/Edit/Modules/Fotolia/PurchaseImage',
			requestParams,
			{
				addOverlay: false,

				asynchronous: true,

				onActionSuccess: function(requestResult) {
					if (requestResult.replaceImages && Object.isArray(requestResult.replaceImages)) {
						resultMethod = function () {
							manager.replaceImages(requestResult.replaceImages);
							confirmationStepManager.showSuccessDialog();
						};
					}
				},

				onActionError: function(errorText) {
					resultMethod = function () {
						confirmationStepManager.showError(errorText);
					};
				},

				onJsonException: function(html) {
					resultMethod = function () {
						confirmationStepManager.fatalError(html);
					};
				},

				onComplete: function() {
					resultMethod();
				}
			}
		);
	},

	/**
	 * Replaces required images after purchase process
	 *
	 * @access public
	 * @param Array
	 * @return void
	 */
	replaceImages: function(imagesList) {
		var helper = self.parent.getSbImageHelperInstance();
		var fckObject = helper.getFckObject();
		var wysiwygImagesList = fckObject.FCK.EditorDocument.getElementsByTagName('IMG');

		for(var i = 0; i < imagesList.length; i++) {
			var fromSrc = imagesList[i].from;
			var toSrc = imagesList[i].to;
			self.parent.sbImageObject.replaceImage(fromSrc, toSrc);

			if (wysiwygImagesList && wysiwygImagesList.length) {
				for(var j = 0; j < wysiwygImagesList.length; j++) {
					if (fromSrc == helper.getImageSrc(wysiwygImagesList[j])) {
						helper.setImageSrc(wysiwygImagesList[j], toSrc);
					}
				}
			}
		}
	},

	/**
	 * Returns select license step URL
	 *
	 * @access private
	 * @return string
	 */
	_getSelectLicenseStepUrl: function() {
		var requestUrl = self.parent.baseUrl + '/Wizard/Edit/Modules/Fotolia/SelectLicense';
		var requestParams = new Object();
		requestParams.imageId = this._imageId;
		requestUrl += '?' + Object.toQueryString(requestParams);
		return requestUrl;
	},

	/**
	 * Returns confirmation step URL
	 *
	 * @access private
	 * @return string
	 */
	_getConfirmationStepUrl: function() {
		var requestUrl = self.parent.baseUrl + '/Wizard/Edit/Modules/Fotolia/ConfirmationStep';
		var requestParams = new Object();
		requestParams.imageId = this._imageId;
		requestParams.licenseName = this._license.name;
		requestUrl += '?' + Object.toQueryString(requestParams);
		return requestUrl;
	}
}