/**************************************************
 * CRandomText, version 0.10
 * Copyright 2007 Ivengi BV. Benelux
 **************************************************/
 /*
CONSTRUCTOR
new CRandomImage(DivID,NumberOfTexts,aText);
DivID										-> ID of the DIV that is going to contain the text
NumberOfTexts 					-> Number Of texts appearing in the div
ImgClass		  					-> Class that all the images should have
aText									  -> Array Of text

FUNCTIONS
Start();								-> Get new set of random text

###########
# EXAMPLE #
###########

<script type="text/javascript" src="CRandomText.js"></script>
<script type="text/javascript" src="imanagerexample.js"></script>  // LINK TO aImage array
<script type="text/javascript">
	var RandomText = new CRandomText("textbar",1,aText);
</script>
	
<div id="textbar"></div>	


%%%%%%%%% imanagerexample.js (file to be created in imanager OR a non-dynamic file on the server) %%%%%%%%%%

var aText = new Array();
aText.push("1:dit is text 1"); 
aText.push("2:dit is text 2"); 
aText.push("3:dit is text 3"); 


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

*/
 

function CRandomText(DivID,NumberOfTexts,aText){
		this.aSelectedText 		= new Array();
		this.aUnSelectedText 	= new Array();
		this.DivID							= DivID;
		this.NumberOfTexts			= NumberOfTexts;
		
		this.aText							= aText;
 		var _this = this;
	 	this.addLoadHandler(function(){ _this.Start(); });
}

CRandomText.prototype.Start = function() {
	this.aUnSelectedText.splice(0,this.aUnSelectedText.length); 
	this.aUnSelectedText = this.aUnSelectedText.concat(this.aText);
	this.aSelectedText.splice(0,this.aSelectedText.length); 
	
	for(var i = 0; i < this.NumberOfTexts; i++) {
		this.GetRandom();
	}
	
	var TargetDiv = document.getElementById(this.DivID);
	
	TargetDiv.innerHTML = "";
	for(var j = 0; j < this.aSelectedText.length; j++) {
		TargetDiv.innerHTML += this.aSelectedText[j];
	}
}

CRandomText.prototype.GetRandom = function() {
	if(this.aUnSelectedText.length > 0) {
		var RandomId = Math.floor((this.aUnSelectedText.length)*Math.random());
		this.aSelectedText = this.aSelectedText.concat(this.aUnSelectedText[RandomId]);
		this.aUnSelectedText.splice(RandomId,1);
	}
}

CRandomText.prototype.addLoadHandler = function(handler)
{
	if(window.addEventListener)
	{
		window.addEventListener("load",handler,false);
	}
	else if(window.attachEvent)
	{
		window.attachEvent("onload",handler);
	}
	else if(window.onload)
	{
		var oldHandler = window.onload;
		window.onload = function piggyback()
		{
			oldHandler();
			handler();
		};
	}
	else
	{
		window.onload = handler;
	}
}
