//
// LES - Link Exchange System
// An OO interface to easily display LES links/banners on a page.
//

// --------------------------------------------------------------------------------
// Constants
// --------------------------------------------------------------------------------

/*
	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!
*/
var LES_DOMAIN		= document.domain; 
var LES_PORT		= window.location.port;
var LES_URI			= '/shop/controls/les_adapter/v2.0/les_link.php';



// --------------------------------------------------------------------------------
// Class LESLink
// ----------------------------------------

// ----------------------------------------
// Private section
// ----------------------------------------

LESLink.prototype._buildLinkToSvc = function ( /* string */lesId, /* string */type)
{
	//alert("LESLink::_buildLinkToSvc( " + lesId + ", " + type + " )");

	var qrystr = "";

	if (lesId != "")
	{
		qrystr = "?id=" + lesId;
	}

	if (type != "")
	{
		if (qrystr == "")
		{
			qrystr = "?";
		}
		else
		{
			qrystr = qrystr + "&";
		}
		qrystr = qrystr + "type=" + type;
	}
	
	var reqUri = "http://" + LES_DOMAIN + ":" + LES_PORT + LES_URI + qrystr;

	//alert( reqURI );
	return reqUri;
}

// ----------------------------------------
// Constructor
// ----------------------------------------

function LESLink()
{
	//alert("LESLink::LESLink()");

	this.name = "LESLink";
	
	// http://net.tutsplus.com/tutorials/javascript-ajax/5-ways-to-make-ajax-calls-with-jquery/
	// It should be noted that Internet Explorer always caches AJAX calls, while other browsers 
	// behave differently. So we’d better tell the browser explicitly whether or not AJAX should 
	// be cached. With jQuery, we can accomplish this simply by typing:
	$.ajaxSetup(
		{  
			cache: false  
		}
	);	
}

// ----------------------------------------
// Public section
// ----------------------------------------

LESLink.prototype.getText = function ( /* string */boxId, /* int */lesId)
{
	//alert("LESLink::getText( " + boxId + ", " + lesId + " )");

	var boxId = "#" + boxId;
	var reqUri = this._buildLinkToSvc(lesId, /* type text */"t");
	$(boxId).load(reqUri);
}

LESLink.prototype.getBanner = function ( /* string */boxId, /* int */ lesId)
{
	//alert("LESLink::getBanner( " + boxId + ", " + lesId + " )");

	var boxId = "#" + boxId;
	var reqUri = this._buildLinkToSvc(lesId, /* type text */"b");
	$(boxId).load(reqUri);
}

LESLink.prototype.getRandomPartner = function ( /* string */ boxId)
{
	//alert("LESLink::getRandomPartner( " + boxId + " )");

	var boxId = "#" + boxId;
	var reqUri = this._buildLinkToSvc( /* lesId */"", /* type */"");
	$(boxId).load(reqUri);
}

LESLink.prototype.rotatePartners = function ( /* string */boxId)
{
	//alert("LESLink::rotatePartners( " + boxId + " )");

	this.getRandomPartner( boxId );
	
	var boxId = "#" + boxId;
	var reqUri = this._buildLinkToSvc( /* lesId */"", /* type */"");
	//alert(reqUri);

	// Call the periodical update jQuery plugin
	$.periodic(
		{
			period	: 10000 /* 10 sec. */
		},
		function()
		{
			$.ajax( 
				{
					url		: reqUri,
					success	: function( data ) 
					{  
						$(boxId).html( data );  
					}
				}
			);
		}
	);	
}

// ----------------------------------------
// End of Class
// --------------------------------------------------------------------------------

/* EOF */

