/*
 * 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, forceUseSmallImage)
	{
		if (!targetElement)
			return;
		
		var dashletDiv = $(document.createElement('DIV'));
		
		// Manage the postion
		if (!position || (position != 'left' && position != 'right'))
		{
			if (!forceUseSmallImage && targetElement.getHeight() > 20 && targetElement.getWidth() > 20)
				dashletDiv.addClassName('DashletSpinner');
			else
				dashletDiv.addClassName('DashletSpinnerMini');				
		}
		else
		{
			if (position == 'right')
			{
				if (!forceUseSmallImage && targetElement.getHeight() > 20 && targetElement.getWidth() > 20)
					dashletDiv.addClassName('DashletSpinnerRight');
				else
					dashletDiv.addClassName('DashletSpinnerMiniRight');
			}
			else
			{
				if (!forceUseSmallImage && targetElement.getHeight() > 20 && targetElement.getWidth() > 20)
					dashletDiv.addClassName('DashletSpinnerLeft');
				else
					dashletDiv.addClassName('DashletSpinnerMiniLeft');					
			}
		}
		
		dashletDiv.style.height   = targetElement.getHeight()+'px';
		dashletDiv.style.width    = targetElement.getWidth()+'px';
		dashletDiv.style.display  = 'block';
		dashletDiv.style.position = 'absolute';
		dashletDiv.style.zIndex = 9999;

		if (targetElement.getStyle('position') == 'relative')
		{
			dashletDiv.style.top = 0;
			dashletDiv.style.left = 0;
		}
		else
		{
			dashletDiv.style.top = targetElement.cumulativeOffset().top;
			dashletDiv.style.left = targetElement.cumulativeOffset().left;
		}

		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();
		});
			
		targetElement.select('DIV.DashletSpinnerMini').each(function (e) {
			e.remove();
		});
			
		targetElement.select('DIV.DashletSpinnerLeft').each(function (e) {
			e.remove();
		});
		
		targetElement.select('DIV.DashletSpinnerLMiniLeft').each(function (e) {
			e.remove();
		});
		
		targetElement.select('DIV.DashletSpinnerRight').each(function (e) {
			e.remove();
		});
		
		targetElement.select('DIV.DashletSpinnerMiniRight').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;

		if (!content || content.length == 0)
			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
	 * @param string      elementType  (default a)
	 */	
	this.renderTooltips = function(targetElement, elementType)
	{
		if (elementType === null)
			elementType = 'a';

		var callingObject = this;
		if (!targetElement)
			$$(elementType + '[title]').each(function (e) {
				callingObject.makeTooltip(e, e.title);
				e.title = '';
			});	
		else
			targetElement.select(elementType + '[title]').each(function (e) {
				callingObject.makeTooltip(e, e.title);
				e.title = '';
			});		
	}
	

}
