/**
 * SbImage helper scripts
 *
 * @copyright (c) 2004-2008, SWsoft
 */
function getSbImageHelperInstance() {
	if (!self.sbImageHelperObject) {
		self.sbImageHelperObject = new SbImageHelper(self.parent.sbFckObject);
	}
	return self.sbImageHelperObject;
}

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

	/**
	 * FCK editor objects structure like
	 * {
	 *		FCK: self.FCK,
	 *		FCKUndo: self.FCKUndo,
	 *		FCKSelection: self.FCKSelection,
	 *		FCKLang: self.FCKLang,
	 *		FCKConfig: self.FCKConfig,
	 *		FCKDebug: self.FCKDebug,
	 *		FCKBrowserInfo: self.FCKBrowserInfo
	 * }
	 *
	 * @var obj
	 * @access private
	 */
	_fckObject: null,

	/**
	 * Current FCK selection range
	 *
	 * @var obj
	 * @access private
	 */
	_selectionRange: null,

	/**
	 * Current image
	 *
	 * @var obj
	 * @access private
	 */
	_image: null,

	/**
	 * Current link
	 *
	 * @var obj
	 * @access private
	 */
	_link: null,

	/**
	 * Initialises the class
	 *
	 * @access public
	 * @param structure FCK editor objects
	 * @return void
	 */
	initialize: function(fckObject) {
		this._fckObject = fckObject;
	},

	/**
	 * Returns current
	 *
	 * @access public
	 * @return structure FCK editor objects
	 */
	getFckObject: function() {
		return this._fckObject;
	},

	/**
	 * Saves selection range
	 *
	 * @access public
	 * @return void
	 */
	loadSelection: function() {
		if (null == this._fckObject || null == this._fckObject.FCK) {
			alert ('Internal Sitebuilder error');
			return;
		}
		if (this._fckObject.FCK.EditorDocument.selection) {
			this._selectionRange = this._fckObject.FCK.EditorDocument.selection.createRange();
		} else if (this._fckObject.FCK.EditorWindow.getSelection) {
			var selectionObject = this._fckObject.FCK.EditorWindow.getSelection();
			if (!selectionObject || selectionObject.rangeCount < 1) {
				this._selectionRange = null;
			}
			this._selectionRange = selectionObject.getRangeAt(0);
		}
		var image = this._fckObject.FCK.Selection.GetSelectedElement();
		if (image && image.tagName != 'IMG' && !(image.tagName == 'INPUT' && image.type == 'image')) {
			image = null;
		}
		this._image = image;
		this._link = this._fckObject.FCK.Selection.MoveToAncestorNode('A');
	},

	/**
	 * Hides selection
	 *
	 * @access public
	 * @return void
	 */
	hideSelection: function() {
		if (this._fckObject.FCK.EditorDocument.selection) {
			this._fckObject.FCK.EditorDocument.selection.empty();
		}
	},

	/**
	 * Resotres selection range
	 *
	 * @access public
	 * @return void
	 */
	restoreSelection: function() {
		if (!this._selectionRange) {
			return;
		}
		if (this._selectionRange.select) {
			this._selectionRange.select();
		} else if (this._fckObject.FCK.EditorWindow.getSelection) {
			var selection = this._fckObject.FCK.EditorWindow.getSelection();
			selection.removeAllRanges();
			selection.addRange(this._selectionRange);
		}
		this._selectionRange = null;
		this._image = null;
		this._link = null;
	},

	/**
	 * Loads current FCKeditor selection
	 *
	 * @access public
	 * @return structure
	 */
	getSelectedImageInfo: function() {
		var result = null;
		if (!this._image) {
			return;
		}
		var tempImage = this._image.cloneNode(false);
		var savedUrl = this.getImageSrc(tempImage);

		var iWidth, iHeight;
		var regexSize = /^\s*(\d+)px\s*$/i;
		if (tempImage.style.width) {
			var aMatch = tempImage.style.width.match(regexSize);
			if (aMatch) {
				iWidth = aMatch[1];
				tempImage.style.width = '';
			}
		}

		if (tempImage.style.height) {
			var aMatch  = tempImage.style.height.match(regexSize);
			if (aMatch) {
				iHeight = aMatch[1];
				tempImage.style.height = '';
			}
		}

		result = new Object();
		result.image = this._image;
		result.src = savedUrl;
		result.alt = this.getAttribute(tempImage, 'alt', '');
		result.vspace = this.getAttribute(tempImage, 'vspace', '');
		result.hspace = this.getAttribute(tempImage, 'hspace', '');
		result.border = this.getAttribute(tempImage, 'border', '');
		result.align = this.getAttribute(tempImage, 'align', '');
		result.width = iWidth ? iWidth : this.getAttribute(tempImage, 'width', '');
		result.height = iHeight ? iHeight : this.getAttribute(tempImage, 'height', '');
		result.id = tempImage.id;
		result.dir = tempImage.dir;
		result.lang = tempImage.lang;
		result.title = tempImage.title;
		result.className = tempImage.getAttribute('class', 2) || '';
		result.longDesc = tempImage.longDesc;
		result.style = (document.all)
			? tempImage.style.cssText
			: tempImage.getAttribute('style', 2);

		result.link = this._link;
		if (this._link) {
			var savedUrl = this.getAttribute(this._link, '_fcksavedurl', '');
			if (savedUrl.length == 0) {
				savedUrl = this._link.getAttribute('href', 2);
			}
			result.linkHref = savedUrl;
			result.linkTarget = this._link.target;
		}
		tempImage = null;

		return result;
	},

	/**
	 * Returns WYSIWYG image src
	 *
	 * @access public
	 * @param object
	 * @return string
	 */
	getImageSrc: function(imageObject) {
		var url = this.getAttribute(imageObject, '_fcksavedurl', '');
		if (url.length == 0) {
			url = this.getAttribute(imageObject, 'src', '');
		}
		return this.getRelativeImageUrl(url);
	},

	/**
	 * Updates WYSIWYG image src
	 *
	 * @access public
	 * @param object
	 * @param string
	 * @return void
	 */
	setImageSrc: function(imageObject, src) {
		var url = this.getAbsoluteImageUrl(src);

		imageObject.src = url;
		this.setAttribute(imageObject, '_fcksavedurl', url);
	},

	/**
	 * Returns relative image URL
	 *
	 * @access public
	 * @param string url
	 * @return string
	 */
	getRelativeImageUrl: function(url) {
		return url.startsWith(self.siteFolder)
			? url.substring(self.siteFolder.length, url.length)
			: url;
	},

	/**
	 * Returns absolute image URL
	 *
	 * @access public
	 * @param string url
	 * @return string
	 */
	getAbsoluteImageUrl: function(url) {
		return !url.startsWith('http://') && !url.startsWith('https://') && !url.startsWith(self.siteFolder)
			? self.siteFolder + url
			: url;
	},

	/**
	 * Returns element attribute
	 *
	 * @access public
	 * @param object element
	 * @param string element name
	 * @param string default value
	 * @return string
	 */
	getAttribute: function(element, name, defaultValue) {
		var attribute = element.attributes[name];

		if (attribute == null || !attribute.specified) {
			return defaultValue ? defaultValue : '';
		}

		var value;
		if (!(value = element.getAttribute(name, 2))) {
			value = attribute.nodeValue;
		}
		return (value == null ? defaultValue : value);
	},

	/**
	 * Sets element attribute
	 *
	 * @access public
	 * @param object element
	 * @param string element name
	 * @param string value
	 * @return void
	 */
	setAttribute: function(element, name, value) {
		if (null == value || 0 == value.length) {
			element.removeAttribute(name, 0);
		} else {
			element.setAttribute(name, value, 0);
		}
	}
}