/*
 * ACSI tip class
 * 
 * @author 	Robert Fokken
 * @uses   	Mootools native Tips (http://mootools.net/docs/more/Interface/Tips)
 * @created 05-02-2010
 * 
 * For examples see wiki
 * 
 */
var Acsi_Tips = new Class({
    
	Extends: Tips,
	options :
	{
		sTitleTextSeperator: '',
		sTextContent: ''
	},
	
	/**
	 *  This function is called for the title and the href of a link element
	 *  so it can be called twice in some cases
	 * 
	 *  title => title of tooltip  
	 *  href  => content of tooltip
	 * 
	 *  HTML OUTPUT  
	 *  
     *	<div class="options.className"> //the className you pass in options will be assigned here.
	 *	  <div class="tip-top"></div> //useful for styling 
	 *    <div class="tip"> 
	 *     <div class="tip-title"></div> 
	 *     <div class="tip-text"></div> 
	 *    </div> 
	 *    <div class="tip-bottom"></div> //useful for styling
	 *	</div>
	 *   
	 */
    fill: function(tipElement, contents)
    {
		// check if we need to extract contents from a DOM element
		// Expecting something like title="DOM:ElementID" 
		// Then there should be a hidden element to provide HTML content
		if (contents.test('^DOM:', 'i'))
		{ 
			sReference = contents.split(':')[1].trim();			
			this.fnRenderOutput( $(sReference).get('html'), tipElement);
		}
		// JSON support to get tooltip content
		// Expected in title or href something like title="JSON:dataservice.php?iMessageID=999&iLanguageID=1"
		else if (contents.test('^JSON:', 'i')) 
		{
			sService   = contents.split(':')[1].trim();
			aURIParts  = sService.split('?').trim();
			sURI 	   = aURIParts[0];
			
			var aDataServiceParms = new Array();
			if (aURIParts[1])
			{
				sURIParams = aURIParts[1].trim();
				if (sURIParams != '')
				{
					aDataServiceParms = sURIParams.split('&'); 
				}
			}

			var jSonRequest = new Request.JSON(
			{		
				url: sURI,
				async: true,
				onSuccess: function(oJson)
				{
			 		if (oJson.sData)
					{
			 			this.fnRenderOutput(oJson.sData, tipElement);
					}
				}.bind(this)
			}).send("data="+JSON.encode(aDataServiceParms) );
		}
		// AJAX support to get tooltip content
		// Expected in title or href something like title="AJAX:dataservice.php?iMessageID=999&iLanguageID=1"
		else if (contents.test('^AJAX:', 'i'))
		{
			var sURI = contents.replace(/AJAX:/i,'');			
			var oAJAXRequest = new Request (
			{
				url : sURI,
				async: false,
				onComplete: function (responseText, responseXML)
				{					
					this.fnRenderOutput(responseText, tipElement);
				}.bind(this),
				method: 'get'
			}).send();
		}		
		// Normal behavior, take content from title tag		
		else
		{			
			this.fnRenderOutput(contents, tipElement);			
		}
    },
    
    fnRenderOutput: function(sContent, tipElement)
    {	
    	
    	if (this.options.sTitleTextSeperator != '' && sContent.contains( this.options.sTitleTextSeperator )  )
    	{
    		var aContentParts = sContent.split(this.options.sTitleTextSeperator);			
    		tipElement.set('html', aContentParts[0].trim() );
    		this.sTextContent = aContentParts[1];
    	}
    	else
    	{
    		    		
    		// Fixed when only title tag is filled and href is not or filled with # 
    		if ( (sContent == '' || sContent == '#') && this.sTextContent && this.sTextContent != '' && tipElement.get('class') == 'tip-text' )
    		{
    			sContent = this.sTextContent;
    			this.sTextContent = '';
    		}
    		
    		// uitzondering om bij href (tip-text) met http en https eruit te halen voor tonen
    		if ( tipElement.get('class') == 'tip-text' && ( sContent.contains('http') || sContent.contains('\/') ) )
    		{
    			this.sTextContent = '';
    			tipElement.set('html', '' );
    			tipElement.setStyle('display', 'none');
    		}
    		else if ( (sContent != '' && sContent != '#') )
    		{
    			tipElement.set('html', sContent.trim() );
    		}
    	}
    }
});
