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

	/**
	 * Current image id
	 *
	 * @var string
	 * @access public
	 */
	imageId: null,

	/**
	 * Current images list
	 *
	 * @var FotoliaSearchFormImageList
	 * @access private
	 */
	_imageList: null,

	/**
	 * Image container
	 *
	 * @var object
	 * @access private
	 */
	_container: null,

	/**
	 * Image source
	 *
	 * @var object
	 * @access private
	 */
	_source: null,

	/**
	 * Image preview container
	 *
	 * @var object
	 * @access private
	 */
	_previewContainer: null,

	/**
	 * Object constructor
	 *
	 * @access public
	 * @param FotoliaSearchFormImageList
	 * @param string
	 * @return void
	 */
	initialize: function(imageList, imageId) {
		this._imageList = imageList;
		this.imageId = imageId;
		this._container = $('image_' + imageId + '_container');
		this._source = $('image_' + imageId + '_source');
		this._previewContainer = this._createPreviewContainer();
		var imageObject = this;
		addEvent(
			this._container,
			'click',
			function (event) {return imageObject._imageList.select(imageObject.imageId);}
		);

		addEvent(
			this._source,
			'mouseover',
			function (event) {return imageObject.onMouseOver(event);}
		);
	},

	/**
	 * Selects image
	 *
	 * @access public
	 * @return void
	 */
	select: function() {
		this._container.style.backgroundColor = '#FF0000';
	},

	/**
	 * Removes selection from image
	 *
	 * @access public
	 * @return void
	 */
	unSelect: function() {
		this._container.style.backgroundColor = '#FFFFFF';
	},

	/**
	 * Creates preview container
	 *
	 * @access private
	 * @return void
	 */
	_createPreviewContainer: function() {
		var previewContainer = document.createElement('div');
		$('fotoliaImagesList').appendChild(previewContainer);

		previewContainer.tooltip = new SbTooltip(
			this._source, previewContainer,
			{ min_distance_x: 15, min_distance_y: 15 }
		);
		previewContainer.isLoaded = false;
		previewContainer.isLoading = false;

		return previewContainer;
	},

	/**
	 * onMouseOver event
	 *
	 * @access public
	 * @return void
	 */
	onMouseOver: function(event) {
		if (this._previewContainer.isLoading || this._previewContainer.isLoaded) {
			return true;
		}
		this._previewContainer.isLoading = true;
		var imageManager = this;

		this._previewContainer.style.backgroundColor = '#FFFFFF';
		Element.update(
			this._previewContainer,
			self.parent.fotoliaSearchFormManager.getWaitMessageHtml()
		);
		var requestParameters = new Object();
		requestParameters.imageId = this.imageId;
		var request = new SbAjaxRequest.ContainerUpdater(
			self.parent.baseUrl + '/Ajax/Wizard/Edit/Modules/Fotolia/ImagePreview',
			requestParameters,
			imageManager._previewContainer,
			{
				addOverlay: false,

				onComplete: function() {
					imageManager._previewContainer.isLoading = false;
					imageManager._previewContainer.isLoaded = true;
					imageManager._previewContainer.tooltip.recalculatePosition();
				}
			}
		);
	}
}