/*
//What follows is an example of how to create and use a new list manager.
var lm = new linkManager(
	new Array(
		new Array(
				  'http://www.dumblittleman.com/2007/06/39-ways-to-live-and-not-merely-exist.html',
				  'Live life,Don\'t just exist',
				  '39 Ways to Live, and Not Merely Exist'),
		new Array(
				 'http://www.lifehack.org/articles/productivity/quantity-aids-creativity.html',
				 'Creativity,Brainstorming',
				 'Generate many potential (and ridiculous / impossible) solutions before winnowing them down. There is almost always more than one right answer and just because a right answer has been found does not mean it is the best answer.')
	)
);

listAll.innerHTML += '<hr /><br /><br /><br /><h2>Display 10 random</h2>';
for(var i=0; i<10; i++){
	listAll.innerHTML += '<p>'+lm.getRand()+'</p>';
}
listAll.innerHTML += '<hr /><br /><br /><br /><h2>Display Cloud</h2>'+lm.getCloud();

listAll.innerHTML += '<hr /><br /><br /><br /><h2>Display List of All Once</h2>'+lm.getAllOnce();

listAll.innerHTML += '<hr /><br /><br /><br /><h2>Link of the day</h2>'+lm.duJour();

*/


//Constructor
/*Pre: 
	-link_array is a 2-d array of url's, csv of phrases, a description.
	-randomNum.js has been included.
*/
function linkManager(link_array){
	this.links = link_array;
	
	this.randgen = new RandomNumberGenerator();
	
	this.getRand = lmGetRandom; //returns text. get a single random link with description.
	this.getPhrases = lmGetAllPhrases; //returns an array of all linked phrases without description.
	this.getCloud = lmGetPhraseCloud; //returns text. get cloud of phrases
	this.getAllOnce = lmGetAllOnce; //returns text. list everything once with descriptions
	this.getSpecificFull = lmGetSpecificFull; //returns text. Specific link.
	this.duJour = lmDuJour; //returns text. Returns link(s) of the day.
}

//Return special items for this particular day of the month
function lmDuJour(){
	//Get number of links to display per day such that none are missed in a month.
	var linkCount = this.links.length;
	var perDay = roundup(linkCount / 30);
	var d = new Date();
	var dom = d.getDate(); //dom = day of month
//	alert('perday '+perDay+"\ntotal links "+this.links.length+"\nfirst link index "+(dom%linkCount));

	var toReturn =this.getSpecificFull(dom%linkCount)+'<br /><br /><br />';
	var index=0;
	for(var i=2; i<=perDay; i++)
	{
		index = rounddown(linkCount / i)+(dom%linkCount);
		
		if(index>=linkCount){ index= index%linkCount; }
		
//		alert(index);
		toReturn+= this.getSpecificFull(index)+'<br /><br /><br />';
	}
	return toReturn;
}

/* Return the html for the specified link with the full description. Simply use the first phrase. */
function lmGetSpecificFull(index){
	var phrases = this.getPhrases(index);
	
	var toReturn = '';
	if(phrases.length != 0){ toReturn +=phrases[0]; }
	
	//Return link to the URL on phrase, followed by the description, if there is one.
	if(this.links[index][2] != ''){
		toReturn +=' - '+this.links[index][2];
	}
	return toReturn;
}

//returns an array of all phrases to display, but no description.
//Does not return blank phrases with no link.
function lmGetAllPhrases(index){
	var temp = this.links[index];
	//temp now contains a row from the link array.
	
	var phrases = temp[1].split(','); //get the csv of phrases into an array.
	var phrase = new Array();
	
	for(var i=0; i<phrases.length; i++){
		if(phrases[i] != ''){
			phrase.push('<a target="_blank" href="'+temp[0]+'">'+phrases[i]+'</a>');
		}
	}

	return phrase;
}

//Display a random link with description.
function lmGetRandom()
{
	var index = this.randgen.nextMinMax(0,this.links.length-1);
	var phrases = this.getPhrases(index);
	
	var toReturn = '';
	if(phrases.length != 0){
		toReturn +=phrases[this.randgen.nextMinMax(0,phrases.length-1)];
	}

	if(this.links[index][2] != ''){ toReturn+=' - '+this.links[index][2]; }

	//Return link to the URL on phrase, followed by the description.
	return toReturn;
}

//list all the links once with description. Just use first phrase to link on.
function lmGetAllOnce(){
	var toReturn='';
	for(var i=0; i<this.links.length; i++)
	{
		toReturn+= this.getSpecificFull(i)+'<br /><br />';
	}
	return toReturn;
}

//list all the links. Links with multiple phrases will be listed multiple times. Don't list descriptions.
function lmGetPhraseCloud(){
	var phrases;
	var toReturn='';
	for(var i=0; i<this.links.length; i++)
	{
		phrases = this.getPhrases(i);
		for(var j=0; j<phrases.length; j++)
		{
			toReturn+=phrases[j]+', ';
		}
	}
	return removeLastChar(removeLastChar(toReturn));
}

//////////////////////////// GENERIC JAVASCRIPT FUNCTIONS ///////////////////////////////////////
//remove last character. Good for remove comma at the end of a string.
//http://www.ozzu.com/programming-forum/remove-last-character-t46875.html
function removeLastChar(tempString){
	return tempString.slice(0, -1);
}

//Round up to the nearest whole number.
function roundup(number){
	if(number>Math.round(number)){ return Math.round(number)+1; }
	else{ return Math.round(number); }
}

//Round down to the nearest whole number.
function rounddown(number){
	if(number<Math.round(number)){ return Math.round(number)-1; }
	else{ return Math.round(number); }
}
