/**
 * Fotolia module scripts
 *
 * @copyright (c) 2004-2008, SWsoft
 */
function initSearchForm() {
	var searchFormObject = new FotoliaSearchForm();
	if (self.fotoliaSearchFormObject) {
		searchFormObject.setPageNum(self.fotoliaSearchFormObject.getPageNum());
		searchFormObject.setPageSize(self.fotoliaSearchFormObject.getPageSize());
	}
	self.fotoliaSearchFormObject = searchFormObject;
	self.parent.fotoliaSearchFormManager.searchForm = searchFormObject;
}

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

	/**
	 * Search string input
	 *
	 * @var object
	 * @access private
	 */
	_searchStringInput: null,

	/**
	 * Current page number
	 *
	 * @var int
	 * @access private
	 */
	_pageNum: null,

	/**
	 * Current page size
	 *
	 * @var int
	 * @access private
	 */
	_pageSize: null,

	/**
	 * Current category
	 *
	 * @var FotoliaCategory
	 * @access private
	 */
	_currentCategory: null,

	/**
	 * Initialises object
	 *
	 * @access public
	 * @return void
	 */
	initialize: function() {
		this._searchStringInput = $('searchString');
		this._searchStringInput.isDefault = false;

		var searchForm = this;
		if (this._searchStringInput.hasClassName('sb-text-light')) {
			this._searchStringInput.isDefault = true;
			this.removeDefaultSearchEvent = this.removeDefaultSearch.bindAsEventListener(this);
			Event.observe(
				this._searchStringInput,
				'focus',
				searchForm.removeDefaultSearchEvent
			);
		}
		Event.observe(
			this._searchStringInput,
			'keypress',
			function (event) { return searchForm.onSearchStringInputKeyPress(event); }
		);
		this._pageSize = null;
		this._currentCategory = null;
	},

	/**
	 * Removes default search
	 *
	 * @access public
	 * @return bool
	 */
	removeDefaultSearch: function(event) {
		this._searchStringInput.value = '';
		this._searchStringInput.removeClassName('sb-text-light');
		this._searchStringInput.isDefault = false;
		Event.stopObserving(this._searchStringInput, 'focus', this.removeDefaultSearchEvent);
	},

	/**
	 * Checks that form is empty
	 *
	 * @access public
	 * @return bool
	 */
	isEmpty: function() {
		return (
			((this._searchStringInput.isDefault) || ('' == this._searchStringInput.value)) &&
			((null == this._currentCategory) || ('' == this._currentCategory.getId()))
		);
	},

	/**
	 * Returns request params to the self
	 *
	 * @access public
	 * @return object
	 */
	getRequestParams: function() {
		var result = new Object();
		result.searchString = (this._searchStringInput.isDefault)
			? ''
			: this._searchStringInput.value;
		if (null != this._currentCategory) {
			result.categoryType = this._currentCategory.getType();
			result.categoryPath = this._currentCategory.getPath();
		}
		if (null != this._pageNum) {
			result.pageNum = this._pageNum;
		}
		if (null != this._pageSize) {
			result.pageSize = this._pageSize;
		}
		return result;
	},

	/**
	 * Returns request params to the self
	 *
	 * @access public
	 * @param event object
	 * @return object
	 */
	onSearchStringInputKeyPress: function(event) {
		if (document.all) event = self.event;
		if (event.keyCode == 13) {
			stopEvent(event);
			this.setPageNum(1);
			self.parent.fotoliaSearchFormManager.search();
			return false;
		}
		return true;
	},

	/**
	 * Returns page number in image list
	 *
	 * @access public
	 * @return int
	 */
	getPageNum: function() {
		return this._pageNum;
	},

	/**
	 * Sets page number in image list
	 *
	 * @access public
	 * @params int
	 * @return void
	 */
	setPageNum: function(pageNum) {
		this._pageNum = pageNum;
	},

	/**
	 * Returns page size in image list
	 *
	 * @access public
	 * @return int
	 */
	getPageSize: function() {
		return this._pageSize;
	},

	/**
	 * Sets page size in image list
	 *
	 * @access public
	 * @params int
	 * @return void
	 */
	setPageSize: function(pageSize) {
		this.setPageNum(1);
		this._pageSize = pageSize;
	},

	/**
	 * Sets current category
	 *
	 * @access public
	 * @params FotoliaCategory
	 * @return void
	 */
	setCurrentCategory: function(categoryObject) {
		this._currentCategory = categoryObject;
	}
}