/**
 * This class manages the behaviour of the sponsors.
 * It allows to generate the sponsors' logos in 
 * function of a location's reference.
 * 
 * This is a Singleton class. You can access to its instance throught 
 * the method "Sponsor.getInstance()".
 *
 * This class is dependent of:
 * - YAHOO.util.Dom
 * - WebUtility
 * 
 * @since 11th June 2007
 * @copyright Copyright (c) 2007 Complinet UK Ltd. (http://www.complinet.com)
 * @author Alexandre Arica <alexandre.arica@complinet.com>
 */
function Sponsor(){

	this._sDomElIdMainContainer = '';
	this._sUrlAjaxScriptSponsor = '';
	this._sLocationRef = '';
	this._aSponsorsLogos = new Array();
	this._oIntervalle = null;
}


/**
 * Contains an unique instance of the class 'Sponsor'
 * @var Sponsor
 */
Sponsor.oInstance = null;


/**
 * Singleton pattern.
 * Get a unique instance of this class.
 * @access public static
 * @param void
 * @return Sponsor
 */
Sponsor.getInstance = function(){
	if(!Sponsor.oInstance){
		Sponsor.oInstance = new Sponsor();
	}
	return Sponsor.oInstance;
}


/**
 * Set the HTML element which will 
 * contain the sponsors' logos.
 * @access public static
 * @param string sDomElIdLocation
 * @return void
 */
Sponsor.setDomElIdMainContainer = function(sDomElIdMainContainer){


	if(!YAHOO.util.Dom.get(sDomElIdMainContainer)){
		alert('From "Sponsor::setDomElIdMainContainer()" : The HTML element "'+sDomElIdMainContainer+'" does not exist!');
		return false;
	}
	Sponsor.getInstance()._sDomElIdMainContainer = sDomElIdMainContainer;
}


/**
 * Set the url of the script which 
 * supplies the XML doc with the sponsors's info.
 * @access public static
 * @param string sUrlAjaxScriptSponsor
 * @return void
 */
Sponsor.setUrlAjaxScriptSponsor = function(sUrlAjaxScriptSponsor){

	Sponsor.getInstance()._sUrlAjaxScriptSponsor = sUrlAjaxScriptSponsor;
}


/**
 * Set a location's reference.
 * @access public static
 * @param string sLocationRef
 * @return void
 */
Sponsor.setLocationRef = function(sLocationRef){

	Sponsor.getInstance()._sLocationRef = sLocationRef;
}


/**
 * Refresh the sponsors' logos
 * @access public static
 * @param void
 * @return void
 */
Sponsor.refresh = function(){
    
	// We add the location's reference to the url of the ajax script
	var sUriServerSideScript = Sponsor.getInstance()._sUrlAjaxScriptSponsor;
	
	var oCallback = {
					  timeout: 10000, 
					  scope: Sponsor.getInstance(),
					  success: Sponsor.getInstance()._generate, 
					  failure: function(o){ Sponsor.refresh(); } 
					};

	// We send an AJAX request in order to get the new list of domains
	WebUtility.sendRequest (sUriServerSideScript, oCallback);
}


/**
 * Generate, from an AJAX XML response, the sponsors' logos.
 * @access private
 * @param object o The connection object returned by asyncRequest.
 * @return void
 */
Sponsor.prototype._generate = function(o){
	// We check if the document is well formed
	if(o.responseXML.getElementsByTagName('sponsors').length == 0){
		alert('From "Sponsor::_generate()" : The XML is not well formed. It could be an encoding issue.');
		return false;
	}

	// Inits
	var sSponsorRef = '', sSponsorName = '', sSponsorLogoImgTag = '';

	// We get the sponsors from the received xml document
	var aSponsors = o.responseXML.getElementsByTagName('sponsor');

	// We generate the HTML contents
	this._aSponsorsLogos = new Array();
	for(iSponsor=0; iSponsor < aSponsors.length; iSponsor++){
		sSponsorRef = aSponsors[iSponsor].childNodes[0].childNodes[0].nodeValue;
		sSponsorName = aSponsors[iSponsor].childNodes[1].childNodes[0].nodeValue;
		sSponsorLogoImgTag = aSponsors[iSponsor].childNodes[2].childNodes[0].nodeValue;
		this._aSponsorsLogos[iSponsor] = '<a target="_blank" href="/recruitment/default/index/AdvertiserMiniSite/advertiser/'+sSponsorRef+'" title="'+sSponsorName+'">'+sSponsorLogoImgTag+'</a>';
	}

	if(this._aSponsorsLogos.length > 0){
		
		// We display the list of the sponsors' logos randomly
		this._displayRandomly();
	
		// We will refresh the list of the sponsors' logos every 10 secondes
		if(this._oIntervalle){
			clearInterval(this._oIntervalle);
		}
		if(this._aSponsorsLogos.length > 5){
			this._oIntervalle = setInterval(function() { Sponsor.getInstance()._displayRandomly(); }, 10000);
		}
		
	}

}


/**
 * Display the list of the sponsors' logos randomly.
 * @access private
 * @param void
 * @return void
 */
Sponsor.prototype._displayRandomly = function(){
	// Inits
	
	var iSponsorsLogosLength = this._aSponsorsLogos.length;
	var aCloneSponsorsLogos = new Array();
	var iSponsorsDone = 0, iRandomSponsor = 0;
	var sHtmlSponsors = '';

	// We create a clone of the array which contains the sponsors' logos
	for(iSponsor=0; iSponsor < iSponsorsLogosLength; iSponsor++){
		
		aCloneSponsorsLogos[iSponsor] = this._aSponsorsLogos[iSponsor];
	}

	// We construct randomly the HTML contents
	while(iSponsorsDone < iSponsorsLogosLength){		
		iRandomSponsor = Math.round(Math.random()*(iSponsorsLogosLength-1));
		if(iRandomSponsor in aCloneSponsorsLogos){
			sHtmlSponsors += aCloneSponsorsLogos[iRandomSponsor]+'<br /><br />';
			delete aCloneSponsorsLogos[iRandomSponsor];
			iSponsorsDone++;
		}
	}

	// We append to the main container the list of sponsors' logos	
	WebUtility.innerHtml(this._sDomElIdMainContainer, sHtmlSponsors);
}
