/** layer.js	by Scott Honey

	This set of javascript functions ouputs the raw html needed to do dynmaic/on the fly updates of the Singing Sylbarry web page.  These functions are very narrow in focus, and probably do not have much value for other programs.  The information that people will probably want to change is very accessable through SEVERAL customizable variables.

I have split the functions into three major parts:

	Part 1 are low level functions that do small, specific tasks, usually outputting a small html string.

	function linkedImage(hlink,image,height,width,alt,beforetext,aftertext)
	function jsImage(hlink,image,height,width,alt,beforetext,aftertext)
	function makeSpaces(sound)
	function ordinalString(number)

	Part 2 are mid level functions that are passed an argument, and then return the html string for whatever the function is for, usually for making an HTML table's cell.  They rely heavily on part 1 functions.  There are two different sets of Part 2s, one is the normal set, and one is the "empty" set.  The empty set outputs empty cells that are approximatly the same size as a non-empty cells with similar names.  These "empty cells" fill up space and are approximatly the size of a normal cell.  They can be eliminated and replaced with blank cells if they are causing crashes or something with minimal effect on the layout.

	function makeMinusTable(dataArray,removeFunction, index)
	function makePlayAllTable(sound)
	function makeToneTable(sound,toneNumber)
	function makeEmptyMinusTable()
	function makeEmptyPlayAllTable()
	function makeEmptyToneTable()
	function makeSimpleTable(sound)
	function makeEmptySimpleTable()

	Part 3 are the high level functions that call part 2 functions to compose full tables.  

	function redrawFavoritesarray()
	function redrawGamesarray()


	There is a set of variables that control the color and other tags for the cells and frames that is located in the ./js/setup.js file.  If there needs to be any color modification, these variables can be easily changed.  Everything can be controlled from there, I wouldn't really recomment mucking around with the code, it can be a pain.  I will comment the variables where they are.
*/


///////////part 1///////////

/* linkedImage(hlink,image,height,width,alt,beforetext,aftertext){
	This is a helper function that one can pass an image, its hyperlink, and  tag info to, and then it will return the proper string to display it in HTML. */
function linkedImage(hlink,image,height,width,alt,beforetext,aftertext){
	if (alt==null) {alt="";}
	if (beforetext==null) {beforetext="";}
	if (aftertext==null)  {aftertext="";}
	outS  = '<A HREF="'+ hlink +'\">' +beforetext;
	outS += '<IMAGE SRC="'+Imagepath+image+Imageextension+'" '; 
	outS += 'border=0 height = '+height+' width = '+width+' alt = "'+alt+'" >'+aftertext+'</a>';
	return outS;
} 


/*This is a helper function that one can pass an image, its hyperlink, and its tags into, with the addition of any data that should go before the image, and then it will return the proper string. */
function jsImage(hlink,image,height,width,alt,beforetext,aftertext){
	hlink="javascript: " +hlink;
	return linkedImage(hlink,image,height,width,alt,beforetext,aftertext);
}


/* This function is passed a string and returns enough spaces so that the if it were concated to the string it's length would equal makeSpacesNumberofSpaces.*/
function makeSpaces(sound){
  var spaceS="";
  for (i = sound.length; i < makeSpacesNumberofSpaces; i++)
    {spaceS += space;}
  return spaceS;
}

/* returns an ordinal string for numbers 1 to 20*/
function ordinalString(number) {
	if (number <0) return "";
	if (number==1)  return "1st";
	if (number==2)  return "2nd";
	if (number<21) return number+"th";
	return "";
}

////////////part2///////////

function makeMinusTable(dataArray,removeFunction, index) {
  minusS = layerMinusCellStartTag+layerFontTag+space;
  minusS+= jsImage(removeFunction+'('+index+');', layerMinusCellGraphic,12,12,'Remove '+dataArray[index]+' from this list');
  minusS+= layerCellEndTag;
  return minusS;
}

function makePlayAllTable(sound) {
	playAllS  = layerPlayAllStartTag + layerFontTag + space;
	playAllS += '<A HREF="javascript: parent.playAllSounds(\''+sound+'\');\">'+sound+'</a>';
	playAllS += makeSpaces(sound) +layerCellEndTag;
	return playAllS;
}

function makeToneTable(sound,toneNumber){
	toneS  = layerToneTableStartTag + layerFontTag + space;
	toneS += jsImage('parent.intoGameslist(\''+sound+toneNumber+'\');',layerToneTableAddGraphic,12,12,'Add '+sound+toneNumber+' to Game List');
	toneS += space + jsImage('parent.playSound(\''+sound+toneNumber+'\');',layerToneTableToneGraphic+toneNumber,12,20,'Play ' + ordinalString(toneNumber)+ ' Tone',toneNumber+space);
	toneS += layerCellEndTag;
	return toneS;
}


function makeEmptyMinusTable(){
  minusS = layerMinusCellStartTag+layerFontTag+space;
  minusS+='<image src="'+Imagepath+layerEmptyMinusCellGraphic+Imageextension+'" height=12 width=12>'
  minusS+= layerCellEndTag;
  return minusS;
}

function makeEmptyPlayAllTable() {
	playAllS  = layerPlayAllStartTag + layerFontTag + space;
	playAllS += makeSpaces("") +space +space + layerCellEndTag;
	return playAllS;
}

function makeEmptyToneTable(){
	toneS  = layerToneTableStartTag + layerFontTag + space;
	toneS += '<image src="'+Imagepath+layerEmptyToneTableAddGraphic+Imageextension+'" height=12 width=12>'
	toneS += makeSpaces("")
	toneS += '<image src="'+Imagepath+layerEmptyToneTableToneGraphic+Imageextension+'" height=12 width=20>'
	toneS += layerCellEndTag;
	return toneS;
}

function makeSimpleTable(sound){
  simpleS = layerSimpleTableStartTag+layerFontTag+'<A href=\"javascript: parent.playSound(\''+sound+'\');\">'+sound+'</a>'+makeSpaces(sound)+layerCellEndTag;
  return simpleS;
}

function makeEmptySimpleTable(){
 simpleS = layerSimpleTableStartTag+layerFontTag+makeSpaces("")+makeSpaces("")+layerCellEndTag;
  return simpleS;
}




function redrawFavoritesarray() {
  FAVORITES.document.clear();
  outS='';
  outS+=layerBodyTag+layerBaseFont+layerTableStartTag;
  for (var lineno=(Favoritesarray.length-1);lineno>-1;lineno--){
 	outS += layerRowStartTag;

 		//making the remove from the array cell
	outS += makeMinusTable(Favoritesarray,"parent.outofFavoriteslist",lineno);

		//making the cell that lets you play all of the sounds
	outS += makePlayAllTable(Favoritesarray[lineno]);
	
	for (var toneNumber = 1; toneNumber <6; toneNumber++){
		outS += makeToneTable(Favoritesarray[lineno], toneNumber);}
	outS += layerRowEndTag;
  }

for (var lineno=(layerDefaultRowstoShow - 1 - Favoritesarray.length);lineno>-1;lineno--){

  outS +=layerRowStartTag;
  outS += makeEmptyMinusTable();
  outS += makeEmptyPlayAllTable()
  for (var toneNumber = 1; toneNumber <6; toneNumber++){
	outS +=makeEmptyToneTable();}
  outS+=layerRowEndTag;
}

  outS+=layerTableEndTag + '<br>';
  FAVORITES.document.writeln(outS);
  FAVORITES.document.close();
}//end redrawFavoritesarray




function redrawGamesarray(){
  GAMESLIST.document.clear();
  outS = '';
  outS+= layerBodyTag+ layerTableStartTag;
  for (var lineno=(Gamesarray.length-1);lineno>-1;lineno--){
      outS += layerRowStartTag + makeMinusTable(Gamesarray,"parent.outofGameslist",lineno);
      outS += makeSimpleTable(Gamesarray[lineno]);
      outS += layerRowEndTag;
  }
for (var i=(layerDefaultRowstoShow - 1 - Gamesarray.length);i>-1;i--){
  outS+=layerRowStartTag+makeEmptyMinusTable()+makeEmptySimpleTable()+layerRowEndTag;
}
  outS+=layerTableEndTag + '<br>';
  GAMESLIST.document.write(outS);
  GAMESLIST.document.close();
}//end redrawGamesarray
