/**
 * Constructor.
 * @param path The path to the PngObjectServlet.
 */
PngObject = function(path) {
	this.path = path;
	this.params = new Object();
}

/**
 * Get the path to the PngObjectServlet.
 * @return The path to the PngObjectServlet.
 */
PngObject.prototype.getPath = function() {
	return this.path;
}

/**
 * Set the path to the PngObjectServlet.
 * @param path The path to the PngObjectServlet. 
 */
PngObject.prototype.setPath = function(path) {
	this.path = path;
}

/**
 * Add a parameter.
 * @param paramName The name of the parameter.
 * @param paramValue The value of the paremter.
 */
PngObject.prototype.addParam = function(paramName, paramValue) {
	this.params[paramName] = paramValue;
}

/**
 * Get a parameter.
 * @param paramName The name of the parameter.
 * @return The value of the parameter.
 */
PngObject.prototype.getParam = function(paramName) {
	return this.params[paramName];
}

/**
 * Get all parameters.
 * @return The parameters.
 */
PngObject.prototype.getParams = function() {
	return this.params;
}

/**
 * Replace the content of the element.
 * @param element The element to replace to content of.
 */
PngObject.prototype.replaceElement = function (element) {
	this.addParam('title', element.innerHTML);
	
	// Different behavior depending if this image contains a hover component.
	if(this.getParam('hasHover') != null && this.getParam('hasHover') == 'true') {
		// Set the generated image as the background url.
		element.style.backgroundImage = 'url(' + this.getImgPath() + ')';
		
		// Set the innerHTML to hidden.
		element.innerHTML = '<span style="visibility: hidden;">' + element.innerHTML + '</span>';
	} else {
		// Replace the innerHTML with the generated image.
		element.innerHTML = this.getImgTag();
	}
}

/**
 * Replace the content of an element with a specific id.
 * @param elementId The id of the element.
 */
PngObject.prototype.replaceElementById = function(elementId) {
	var element = document.getElementById(elementId);
	this.replaceElement(element);
}

/**
 * Replace the content of all elements of a specific tag.
 * @param tagName The name of the tag to replace all content of.
 */
PngObject.prototype.replaceElementsByName = function(name) {
	var elements = document.getElementsByTagName(name);
	for(var i = 0;i < elements.length;i++) {
		var element = elements[i];
		this.replaceElement(element);
	}
}

/**
 * Get the generated image path.
 */
PngObject.prototype.getImgPath = function() {
	// Start with the base path.
	var imgPath = this.getPath();
	
	// Add the parameters
	var paramSeparator = '?'
	for(paramName in this.getParams()) {
		var paramValue = this.getParam(paramName);
		
		imgPath += paramSeparator + paramName + '=' + escape(paramValue);
		
		paramSeparator = '&';
	}
	
	// Add this to support pngbehavior
	// imgPath += '&.png'
	
	return imgPath;
}

/**
 * Get the img tag representing this PngObject.
 * @return The img tag.
 */
PngObject.prototype.getImgTag = function() {
	return '<img src="' + this.getImgPath() + '" alt="' + this.getParam('title') + '"/>';
}