/*
 * Define a Javascript Class for the theme manager
 */

function ThemeManager(applicationUrl) 
{
	/**
	 * Application URL
	 */
	this.applicationUrl = applicationUrl;

	/**
	 * Default tooltip options
	 */	
	this.tooltipOptions = {duration: 0.1, 
                           delay:0, 
                           effect:'appear', 
                           viewport: false, 
                           className: 'tooltips'};
	
	// Object initialization
	var callingObject = this;
	Event.observe(window, 'load', function() {
		callingObject.renderTooltips();
	});	

	/**
	 * Shows a transparent spinner over the target layer
	 * @param HTMLElement targetElement
	 */
	this.showSpinner = function(targetElement, position)
	{
		if (!targetElement)
			return;
		
		var dashletDiv = $(document.createElement('DIV'));
		
		// Manage the postion
		if (!position || (position != 'left' && position != 'right'))
		dashletDiv.addClassName('DashletSpinner');
		else
		{
			if (position == 'right')
				dashletDiv.addClassName('DashletSpinnerRight');
			else
				dashletDiv.addClassName('DashletSpinnerLeft');
		}
		
		dashletDiv.style.height   = targetElement.getHeight()+'px';
		dashletDiv.style.width    = targetElement.getWidth()+'px';
		dashletDiv.style.display  = 'block';
		dashletDiv.style.position = 'absolute';
		dashletDiv.style.zIndex = 9999;
				
		targetElement.insert({top: dashletDiv});
	}
	
	/**
	 * Remove all the transparent spinners over the target layer
	 * @param HTMLElement targetElement
	 */	
	this.hideSpinner = function(targetElement) 
	{
		if (!targetElement)
			return;
		
		targetElement.select('DIV.DashletSpinner').each(function (e) {
			e.remove();
		});
	}	
	
	/**
	 * Make a scriptaculous tooltip over the given element
	 * @param HTMLElement targetElement
	 * @param string      content
	 */	
	this.makeTooltip = function(targetElement, content)
	{	
		if (!targetElement)
			return;

		new Tip(targetElement, content, this.tooltipOptions);
	}
	
	/**
	 * Make a scriptaculous tooltip over any element having a title attribute.
	 * The title attribute is cleared after the creation to prevent the browser to 
	 * show its own tooltip
	 * @param HTMLElement targetElement
	 */	
	this.renderTooltips = function(targetElement)
	{	
		var callingObject = this;
		if (!targetElement)
			$$('[title]').each(function (e) {				
				callingObject.makeTooltip(e, e.title);
				e.title = '';
			});	
		else
			targetElement.select('[title]').each(function (e) {
				callingObject.makeTooltip(e, e.title);
				e.title = '';
			});		
	}
	

}