/**
 * Fotolia select image form manager
 *
 * @copyright (c) 2004-2008, SWsoft
 */
function initSearchFormManager() {
	self.sbPreloader.hide();
	self.parent.fotoliaSearchFormManager = new FotoliaSearchFormManager();
}

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

	/**
	 * Search form container
	 *
	 * @var object
	 * @access private
	 */
	_searchFormContainer: null,

	/**
	 * Images list container
	 *
	 * @var object
	 * @access private
	 */
	_imagesListContainer: null,

	/**
	 * Categories list container
	 *
	 * @var object
	 * @access private
	 */
	_categoriesListContainer: null,

	/**
	 * Categories list container
	 *
	 * @var object
	 * @access private
	 */
	_categoriesListContainer: null,

	/**
	 * Selected image id
	 *
	 * @var string
	 * @access private
	 */
	_selectedImageId: null,

	/**
	 * Indicates that representative category is selected
	 *
	 * @var bool
	 * @access private
	 */
	_isRepresentativeCategoriesSelected: true,

	/**
	 * Search form object
	 *
	 * @var FotoliaSearchForm
	 * @access public
	 */
	searchForm: null,

	/**
	 * Images list object
	 *
	 * @var FotoliaSearchFormImageList
	 * @access public
	 */
	imagesList: null,

	/**
	 * Object constructor
	 *
	 * @access public
	 * @return void
	 */
	initialize: function() {
		this._searchFormContainer = $('fotoliaSearchForm');
		this._imagesListContainer = $('fotoliaImagesList');
		this._categoriesListContainer = $('fotoliaCategoriesList');
		this.refreshSearchForm();

		self.sb_panel.registerPageOnloadAction(
			'representativeCategoryTab',
			'self.parent.fotoliaSearchFormManager.showRepresentativeCategoriesContent();'
		);
		self.sb_panel.registerPageOnloadAction(
			'conceptualCategoryTab',
			'self.parent.fotoliaSearchFormManager.showConceptualCategoriesContent();'
		);
	},

	/**
	 * Saves all user data and closes form dialog
	 *
	 * @access public
	 * @return void
	 */
	ok: function() {
		var selectedImageId = null;
		if (null != this.imagesList) {
			selectedImageId = this.imagesList.getSelectedImageId();
		}
		var requestParameters = new Object();
		if (null != selectedImageId) {
			requestParameters.selectedImageId = selectedImageId;
		}
		var manager = this;
		var request = new SbAjaxRequest.JsonAction(
			self.parent.baseUrl + '/Ajax/Wizard/Edit/Modules/Fotolia/SaveImage',
			requestParameters,
			{
				onActionSuccess: function(requestResult) {
					self.parent.sbImageObject.setSrc(requestResult.src);
					manager.close();
				}
			}
		);
	},

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

	/**
	 * Selects image from the list
	 *
	 * @access public
	 * @param string
	 * @return void
	 */
	selectImage: function(selectedImageId) {
		this.setSelectedImageId(selectedImageId);
		if (this.imagesList) {
			this.imagesList.select(selectedImageId);
		}
	},

	/**
	 * Sets selected image id
	 *
	 * @access public
	 * @param string
	 * @return void
	 */
	setSelectedImageId: function(selectedImageId) {
		this._selectedImageId = selectedImageId;
	},

	/**
	 * Returns selected image id
	 *
	 * @access public
	 * @return string
	 */
	getSelectedImageId: function() {
		return this._selectedImageId;
	},

	/**
	 * Shows representative categories content
	 *
	 * @access public
	 * @return void
	 */
	showRepresentativeCategoriesContent: function() {
		this._isRepresentativeCategoriesSelected = true;
		var requestUrl = self.parent.baseUrl + '/Ajax/Wizard/Edit/Modules/Fotolia/CategoriesList/Representative';
		var container = $('fotoliaRepresentativeCategoryContainer');
		if (container._isLoaded) {
			return;
		}
		container._isLoaded = true;
		var requestParameters = new Object();
		this._searchRequest(requestUrl, requestParameters, container);
	},

	/**
	 * Shows conceptual categories content
	 *
	 * @access public
	 * @return void
	 */
	showConceptualCategoriesContent: function() {
		this._isRepresentativeCategoriesSelected = false;
		var requestUrl = self.parent.baseUrl + '/Ajax/Wizard/Edit/Modules/Fotolia/CategoriesList/Conceptual';
		var container = $('fotoliaConceptualCategoryContainer');
		if (container._isLoaded) {
			return;
		}
		container._isLoaded = true;
		var requestParameters = new Object();
		this._searchRequest(requestUrl, requestParameters, container);
	},

	/**
	 * Changes page number in image list
	 *
	 * @access public
	 * @params int
	 * @return void
	 */
	gotoPage: function(pageNum) {
		this.searchForm.setPageNum(pageNum);
		this.search();
	},

	/**
	 * Changes page size in image list
	 *
	 * @access public
	 * @params int
	 * @return void
	 */
	setPageSize: function(pageSize) {
		this.searchForm.setPageSize(pageSize);
		this.search();
	},

	/**
	 * Sets current category
	 *
	 * @access public
	 * @params FotoliaCategory
	 * @return void
	 */
	setCurrentCategory: function(categoryObject) {
		if (this.searchForm) {
			this.searchForm.setCurrentCategory(categoryObject);
		}
		if ('' != categoryObject.getType()) {
			this._isRepresentativeCategoriesSelected = ('representative' == categoryObject.getType());
		}
	},

	/**
	 * Selects category
	 *
	 * @access public
	 * @params FotoliaCategory
	 * @return void
	 */
	selectCategory: function(categoryObject) {
		if (this.searchForm) {
			this.searchForm.setPageNum(1);
			this.searchForm.setCurrentCategory(categoryObject);
			if ('' != categoryObject.getType()) {
				this._isRepresentativeCategoriesSelected = ('representative' == categoryObject.getType());
			}
			this.refreshSearchForm();
		}
	},

	/**
	 * Returns wait message HTML
	 *
	 * @access public
	 * @return string
	 */
	getWaitMessageHtml: function() {
		return '<div style="border: #000000 1px solid; padding: 4px 4px 4px 4px;">' +
					self.parent.htmlSpecialChars(SbGetLocaleByKey('FOTOLIA-WAIT')) + ' ' +
					self.parent.htmlSpecialChars(SbGetLocaleByKey('FOTOLIA-LOADING')) +
				'</div>';
	},

	/**
	 * Executes search
	 *
	 * @access public
	 * @return void
	 */
	search: function() {
		if (!this.searchForm) {
			return;
		}
		if (this.searchForm.isEmpty()) {
			this._imagesListContainer.hide();
			this._categoriesListContainer.show();
			if (this._isRepresentativeCategoriesSelected) {
				self.sb_panel.tooglePage('representativeCategoryTab');
				this.showRepresentativeCategoriesContent();
			} else {
				self.sb_panel.tooglePage('conceptualCategoryTab');
				this.showConceptualCategoriesContent();
			}
			return;
		}
		var requestUrl = self.parent.baseUrl + '/Ajax/Wizard/Edit/Modules/Fotolia/SearchImagesList';
		var requestParameters = (null == this.searchForm)
			? new Object() : this.searchForm.getRequestParams();
		var maxDimensions = self.parent.fotoliaSelectImageFormObject.getMaxDimensions();
		requestParameters.height = maxDimensions.height;
		var container = this._imagesListContainer;

		this._categoriesListContainer.hide();
		this._imagesListContainer.show();

		this._searchRequest(requestUrl, requestParameters, container);
	},

	/**
	 * Refreshes search form
	 *
	 * @access public
	 * @return void
	 */
	refreshSearchForm: function() {
		var requestParameters = (null == this.searchForm)
			? new Object() : this.searchForm.getRequestParams();
		var managerObject = this;

		var overlayElements = (Element.visible(managerObject._imagesListContainer))
			? [this._imagesListContainer, this._searchFormContainer]
			: [this._searchFormContainer];

		var requestOptions = {
			overlayElements: overlayElements,

			onCreate: function() {
				managerObject.searchForm = null;
			},

			onComplete: function() {
				managerObject.search();
			}
		};
		var request = new SbAjaxRequest.ContainerUpdater(
			self.parent.baseUrl + '/Ajax/Wizard/Edit/Modules/Fotolia/SearchForm',
			requestParameters,
			managerObject._searchFormContainer,
			requestOptions
		);
	},

	/**
	 * Executes search request
	 *
	 * @access private
	 * @params string
	 * @params object
	 * @return void
	 */
	_searchRequest: function(requestUrl, requestParameters, container) {
		var request = new SbAjaxRequest.ContainerUpdater(
			requestUrl,
			requestParameters,
			container
		);
	}
}