/*	--------------------------------------------------------------------------------
	Dependencies:
		prototype/prototype.js
		LES 2.0.0
	--------------------------------------------------------------------------------
*/

// --------------------------------------------------------------------------------
// 
var AjaxLES = Class.create();

/*
	The URI to the LES "service" "running" on this site.
	This "service" connects to the real LES system and retrieves the requested link as HTML.
	We use 
		 "document.domain" and 
		 "window.location.port" 
	to make this script as domain.port independent as possible.
	To put it simple, no configuration, this should work on every client/server!
*/
AjaxLES.DOMAIN	= document.domain;
AjaxLES.PORT	= window.location.port;

AjaxLES.URI_SVC_TEXTLINK	= "http://" + AjaxLES.DOMAIN + ":" + AjaxLES.PORT + "/ropemarks-5.0.0/les/text_link.php";
AjaxLES.URI_SVC_BANNERLINK	= "http://" + AjaxLES.DOMAIN + ":" + AjaxLES.PORT + "/ropemarks-5.0.0/les/banner_link.php";
AjaxLES.URI_SVC_PARTNERLINK	= "http://" + AjaxLES.DOMAIN + ":" + AjaxLES.PORT + "/ropemarks-5.0.0/les/partner_link.php";

AjaxLES.prototype =  
{
	// --------------------------------------------------------------------------------
	// Private section
	// --------------------------------------------------------------------------------
	
	__showXmlHttpRequest: function( /* string */ eventName, /* object */ xmlHttpRequest )
	{
		alert(	eventName + ": "			+ xmlHttpRequest							+ " \n\n " +
				"All response headers: \n"	+ xmlHttpRequest.getAllResponseHeaders()	+ " \n\n " +
				"readyState: "				+ xmlHttpRequest.readyState					+ " \n\n " +
				"responseText: "			+ xmlHttpRequest.responseText				+ " \n\n " +
				"responseXML: " 			+ xmlHttpRequest.responseXML				+ " \n\n " +
				"status: " 					+ xmlHttpRequest.status						+ " \n\n " +
				"statusText: "				+ xmlHttpRequest.statusText					+ " \n\n " 
		);
	},
	
	doLoaded: function( /*object*/xmlHttpRequest )
	{
//		this.__showXmlHttpRequest( "onLoaded", xmlHttpRequest );
	},
	doComplete: function( /*object*/xmlHttpRequest )
	{
//		this.__showXmlHttpRequest( "onComplete", xmlHttpRequest );
	},
	doSuccess: function( /*object*/xmlHttpRequest )
	{
//		this.__showXmlHttpRequest( "onSuccess", xmlHttpRequest );
	},
	doFailure: function( /*object*/xmlHttpRequest )
	{
//		this.__showXmlHttpRequest( "onFailure", xmlHttpRequest );
	},
	

	// --------------------------------------------------------------------------------
	// --------------------------------------------------------------------------------

	__getRequestURI: function( /*string*/uri, /*string*/id )
	{
		var reqURI = uri;
																						
		if ( id != "" )
		{
			reqURI = reqURI + "?id=" + id;
		}
		//alert( reqURI );
		return reqURI;
	},
	
	__getTextLinkRequestURI: function( /*string*/ lesId )
	{
		return this.__getRequestURI( AjaxLES.URI_SVC_TEXTLINK, lesId )
	},

	__getBannerLinkRequestURI: function( /*string*/ lesId )
	{
		return this.__getRequestURI( AjaxLES.URI_SVC_BANNERLINK, lesId )
	},

	__getPartnerLinkRequestURI: function( /* string */ partnerId )
	{
		return this.__getRequestURI( AjaxLES.URI_SVC_PARTNERLINK, partnerId )
	},
	
	// --------------------------------------------------------------------------------
	// Public section
	// --------------------------------------------------------------------------------

	// --------------------------------------------------------------------------------
	// --------------------------------------------------------------------------------
	
	initialize: function()
	{
		this.name = "AjaxLES";
		this.ajaxOptions = 
		{
			asynchronous: true, 
		    method: 'get',
			evalScripts: true,
			onLoaded: this.doLoaded.bind( this ),
			onComplete: this.doComplete.bind( this ),
			onSuccess: this.doSuccess.bind( this ),
			onFailure: this.doFailure.bind( this )
		};		
	}, 

	// --------------------------------------------------------------------------------
	// --------------------------------------------------------------------------------
	
	getText: function( /*string*/boxId, /*int*/lesId )
	{
		var reqURI = this.__getTextLinkRequestURI( lesId );
		ajax = new Ajax.Updater( boxId, reqURI, this.ajaxOptions );
	},
	
	// --------------------------------------------------------------------------------
	// --------------------------------------------------------------------------------
	
	getBanner: function( /*string*/boxId, /*int*/lesId )
	{
		var reqURI = this.__getBannerLinkRequestURI( lesId );
		ajax = new Ajax.Updater( boxId, reqURI, this.ajaxOptions );
	},
	
	// --------------------------------------------------------------------------------
	// --------------------------------------------------------------------------------
	
	getPartner: function( /*string*/boxId, /*int*/partnerId )
	{
		var reqURI = this.__getPartnerLinkRequestURI( partnerId );
		ajax = new Ajax.Updater( boxId, reqURI, this.ajaxOptions );
	}, 

	getFeaturedPartner: function( /*string*/boxId, /*int*/partnerId )
	{
		this.getPartner( boxId, partnerId );
	},
	
	getRandomPartner: function( /*string*/boxId )
	{
		var reqURI = this.__getPartnerLinkRequestURI( "" );
		ajax = new Ajax.Updater ( boxId, reqURI, this.ajaxOptions );		
	}, 
	
	rotatePartners: function( /*string*/ boxId )
	{
		var reqURI = this.__getPartnerLinkRequestURI( "" );
		
		var ajaxOptions = 
		{
			asynchronous: true, 
		    method: 'get',
			frequency : 10, // initial number of seconds interval between calls
			evalScripts: true,
			onLoaded: this.doLoaded.bind( this ),
			onComplete: this.doComplete.bind( this ),
			onSuccess: this.doSuccess.bind( this ),
			onFailure: this.doFailure.bind( this )
		};		
		
		ajax = new Ajax.PeriodicalUpdater( boxId, reqURI, ajaxOptions );
	}//,
	
} // AjaxLES.prototype

/* EOF */