// JavaScript Document

function initVals(dif, lineCount, itemsPerPage)
{
	var page = 1;
	var pageCount = (lineCount / itemsPerPage);
	if(parseInt(pageCount) < pageCount) pageCount = (parseInt(pageCount) + 1);
	document.getElementById("page" + dif).alt = page;
	document.getElementById("pageCount" + dif).alt = pageCount;
	document.getElementById("itemsPerPage" + dif).alt = itemsPerPage;
}


function setArrows(dif)
{
	var pageCount = document.getElementById("pageCount" + dif).alt;
	var page = document.getElementById("page" + dif).alt;
	
	if(page == 1)
	{
		document.getElementById("leftArrow" + dif).src="/i/if/spacer.gif";
	}else{
		document.getElementById("leftArrow" + dif).src="/i/search/left_mod_arrow.gif";
	}

	if(page == pageCount)
	{
		document.getElementById("rightArrow" + dif).src="/i/if/spacer.gif";
	}else{
		document.getElementById("rightArrow" + dif).src="/i/search/right_mod_arrow.gif";
	}
	
}

function getStarString(rating, maxRating)
{
	var i;
	var starString = "<table border='0' cellspacing='0' cellpadding='0' height='8'><tr>";
	if(rating != 0)
	{	
		for(i=0; i < rating; i++)
		{
			starString = starString + "<td><img src='/i/swap/darkStar.gif' width='8' height='8'></td>";
		}
		for(i=rating; i < maxRating; i++)
		{
			starString = starString + "<td><img src='/i/swap/lightStar.gif' width='8' height='8'></td>";
		}
		starString = starString + "</tr></table>";
	}else
	{
		starString = "&nbsp;";
	}
	
	
	return starString;
}

function seeStars(rating, maxRating)
{
	document.write(getStarString(rating, maxRating));
}

function album(image, artist, title, year, rating, artistURL, albumURL, isMuze)
{
	this.image = image;
	this.artist = artist;
	this.title = title;
	this.year = year;
	this.rating = rating;
	this.artistURL = artistURL;
	this.albumURL = albumURL;
	this.isMuze = isMuze;
}

function loadAlbums(dif, albumArray)
{
	var page = document.getElementById("page" + dif).alt;
	var itemsPerPage = document.getElementById("itemsPerPage" + dif).alt;
	setArrows(dif);
	setPageIcons(dif);
	var pageIndex = itemsPerPage;
	var indexOne;
	var indexTwo;
	var indexThree;
	var indexFour;
	
	var albumOne;
	var albumTwo;
	var albumThree;
	var albumFour;
	
	if(itemsPerPage == 4)
	{
		indexOne = (page * pageIndex) - 4;
		indexTwo = (page * pageIndex) - 3;
		indexThree = (page * pageIndex) - 2;
		indexFour = (page * pageIndex) - 1;
	
		albumOne = albumArray[indexOne];
		albumTwo = albumArray[indexTwo];
		albumThree = albumArray[indexThree];
		albumFour = albumArray[indexFour];
	}else
	{
		indexOne = (page * pageIndex) - 2;
		indexTwo = (page * pageIndex) - 1
		albumOne = albumArray[indexOne];
		albumTwo = albumArray[indexTwo];
	}
	
		
	//Set album one info
	if(albumOne.isMuze)
	{
		document.getElementById("artistName1" + dif).innerHTML="<a href=\"" + albumOne.artistURL + "\">" + albumOne.artist + "</a>";
	}else
	{
		document.getElementById("artistName1" + dif).innerHTML=albumOne.artist;
	}
	
	document.getElementById("albumTitle1" + dif).innerHTML=albumOne.title;	
	document.getElementById("img1" + dif).src=albumOne.image;
	document.getElementById("year_1" + dif).innerHTML=albumOne.year;
	document.getElementById("rating_1" + dif).innerHTML=getStarString(albumOne.rating, 5);
	document.getElementById("albumTitle1" + dif).href=albumOne.albumURL;
	document.getElementById("img1link" + dif).href=albumOne.albumURL;
	
	
	if(albumTwo != null)
	{
		//Set album two info
		if(albumTwo.isMuze)
		{
			document.getElementById("artistName2" + dif).innerHTML="<a href=\"" + albumTwo.artistURL + "\">" + albumTwo.artist + "</a>";
		}else
		{
			document.getElementById("artistName2" + dif).innerHTML=albumTwo.artist;
		}
		document.getElementById("albumTitle2" + dif).innerHTML=albumTwo.title;
		document.getElementById("img2" + dif).src=albumTwo.image;
		document.getElementById("year_2" + dif).innerHTML=albumTwo.year;
		document.getElementById("rating_2" + dif).innerHTML=getStarString(albumTwo.rating, 5);
		document.getElementById("albumTitle2" + dif).href=albumTwo.albumURL;
		document.getElementById("img2link" + dif).href=albumTwo.albumURL;
	}else
	{
		//Set album two info to blank
		document.getElementById("albumTitle2" + dif).innerHTML='<img src="/i/if/spacer.gif" border="0" width="1" height="1" />';
		document.getElementById("artistName2" + dif).innerHTML='&nbsp;';
		document.getElementById("img2" + dif).src="/i/if/spacer.gif";
		document.getElementById("year_2" + dif).innerHTML='<img src="/i/if/spacer.gif" border="0" width="1" height="1" />';
		document.getElementById("rating_2" + dif).innerHTML='<img src="/i/if/spacer.gif" border="0" width="1" height="1" />';
		document.getElementById("albumTitle2" + dif).href="#";
		document.getElementById("img2link" + dif).href="#";
	}
	
if(itemsPerPage == 4)
{
		if(albumThree != null)
	{
		//Set album three info
		if(albumThree.isMuze)
		{
			document.getElementById("artistName3" + dif).innerHTML="<a href=\"" + albumThree.artistURL + "\">" + albumThree.artist + "</a>";
		}else
		{
			document.getElementById("artistName3" + dif).innerHTML=albumThree.artist;
		}
		document.getElementById("albumTitle3" + dif).innerHTML=albumThree.title;
		document.getElementById("img3" + dif).src=albumThree.image;
		document.getElementById("year_3" + dif).innerHTML=albumThree.year;
		document.getElementById("rating_3" + dif).innerHTML=getStarString(albumThree.rating, 5);
		document.getElementById("albumTitle3" + dif).href=albumThree.albumURL;
		document.getElementById("img3link" + dif).href=albumThree.albumURL;
	}else
	{
		//Set album two info to blank
		document.getElementById("albumTitle3" + dif).innerHTML='<img src="/i/if/spacer.gif" border="0" width="1" height="1" />';
		document.getElementById("artistName3" + dif).innerHTML='&nbsp;';
		document.getElementById("img3" + dif).src="/i/if/spacer.gif";
		document.getElementById("year_3" + dif).innerHTML='<img src="/i/if/spacer.gif" border="0" width="1" height="1" />';
		document.getElementById("rating_3" + dif).innerHTML='<img src="/i/if/spacer.gif" border="0" width="1" height="1" />';
		document.getElementById("albumTitle3" + dif).href="#";
		document.getElementById("img3link" + dif).href="#";
	}

	if(albumFour != null)
	{
		//Set album three info
		if(albumFour.isMuze)
		{
			document.getElementById("artistName4" + dif).innerHTML="<a href=\"" + albumFour.artistURL + "\">" + albumFour.artist + "</a>";
		}else
		{
			document.getElementById("artistName4" + dif).innerHTML=albumFour.artist;
		}
		document.getElementById("albumTitle4" + dif).innerHTML=albumFour.title;
		document.getElementById("img4" + dif).src=albumFour.image;
		document.getElementById("year_4" + dif).innerHTML=albumFour.year;
		document.getElementById("rating_4" + dif).innerHTML=getStarString(albumFour.rating, 5);
		document.getElementById("albumTitle4" + dif).href=albumFour.albumURL;
		document.getElementById("img4link" + dif).href=albumFour.albumURL;
	}else
	{
		//Set album two info to blank
		document.getElementById("albumTitle4" + dif).innerHTML='<img src="/i/if/spacer.gif" border="0" width="1" height="1" />';
		document.getElementById("artistName4" + dif).innerHTML='&nbsp;';
		document.getElementById("img4" + dif).src="/i/if/spacer.gif";
		document.getElementById("year_4" + dif).innerHTML='<img src="/i/if/spacer.gif" border="0" width="1" height="1" />';
		document.getElementById("rating_4" + dif).innerHTML='<img src="/i/if/spacer.gif" border="0" width="1" height="1" />';
		document.getElementById("albumTitle4" + dif).href="#";
		document.getElementById("img4link" + dif).href="#";
	}

}
}

function next(dif)
{
	var pageCount = document.getElementById("pageCount" + dif).alt;
	var page = document.getElementById("page" + dif).alt;
	if(page != pageCount)
	{
		page++;
		document.getElementById("page" + dif).alt = page;
		eval("loadAlbums(dif,albums" + dif + ");");
	}
}

function prev(dif)
{
	var page = document.getElementById("page" + dif).alt;
	if(page != 1)
	{
		page--;
		document.getElementById("page" + dif).alt = page;
		eval("loadAlbums(dif,albums" + dif + ");");
	}
}

function setPage(page,dif)
{
	document.getElementById("page" + dif).alt = page;
}

function setPageIcons(dif)
{
	var i;
	var pageCount = document.getElementById("pageCount" + dif).alt;
	var page = document.getElementById("page" + dif).alt;
	pageCount++;
	var tableSize = (pageCount * 11) + ((pageCount * 2) - 2);
	var pageIconString = "<table border='0' cellspacing='0' cellpadding='0' height='9' width='" + tableSize + "'><tr>";
	
	for(i = 1; i < pageCount; i++)
	{
		if(i == page)
		{
			pageIconString = pageIconString + "<td width='11'><img src=\"/i/search/page_mod_on.gif\" width=\"11\" height=\"9\" border=\"0\" alt=\"current page\" /></td>";
		}else
		{
			pageIconString = pageIconString + "<td width='11'><img onClick=\"setPage(" + i + "," + dif + "); loadAlbums(" + dif + ",albums" + dif + "); \" src=\"/i/search/page_mod_off.gif\" width=\"11\" height=\"9\" border=\"0\" alt=\"page " + i + "\" /></td>";
		}
		if(i != pageCount) 
		{
			pageIconString = pageIconString + "<td width='2'><img src=\"/i/if/spacer.gif\" width=\"2\" height=\"1\" /></td>";
		}
		
	}
	pageIconString = pageIconString + "</tr></table>";
	document.getElementById("pageIconRow" + dif).innerHTML = pageIconString;
}

// start artist twidler functions

function artist(name, url, hasBio)
{
	this.name = name;
	this.url = url;
	this.hasBio = hasBio;
}

function loadArtists(dif, artistArray)
{
	var page = document.getElementById("page" + dif).alt;
	var itemsPerPage = 5;
	setArrows(dif);
	setArtistPageIcons(dif);
	
	var artist;
	var tdIndex;
	//var stopIndex = (itemsPerPage + 1);
	
	for(i=0; i < itemsPerPage; i++)
	{
		indexOne = ((page * itemsPerPage) - i) -1;
		artist = artistArray[indexOne];
		tdIndex = (itemsPerPage - i);
		if(artist != null)
		{
			document.getElementById("tdBullet" + tdIndex + dif).innerHTML = "<img src=\"/i/if/bullet2.gif\" width=\"4\" height=\"8\" />";
			if(artist.hasBio == "true")
			{
				document.getElementById("tdName" + tdIndex + dif).innerHTML="<a href=\"" + artist.url + "\">" + artist.name + "</a>";
			}else
			{
				document.getElementById("tdName" + tdIndex + dif).innerHTML = artist.name;
			}
		}else
		{
			document.getElementById("tdBullet" + tdIndex + dif).innerHTML = "&nbsp;";
			document.getElementById("tdName" + tdIndex + dif).innerHTML = "&nbsp;";
		}
	}
	
}

function setArtistPageIcons(dif)
{
	var i;
	var pageCount = document.getElementById("pageCount" + dif).alt;
	var page = document.getElementById("page" + dif).alt;
	pageCount++;
	var tableSize = (pageCount * 11) + ((pageCount * 2) - 2);
	var pageIconString = "<table border='0' cellspacing='0' cellpadding='0' height='9' width='" + tableSize + "'><tr>";
	
	for(i = 1; i < pageCount; i++)
	{
		if(i == page)
		{
			pageIconString = pageIconString + "<td><img src=\"/i/search/page_mod_on.gif\" width=\"11\" height=\"9\" border=\"0\" alt=\"current page\" /></td>";
		}else
		{
			pageIconString = pageIconString + "<td><img onClick=\"setPage(" + i + "," + dif + "); loadArtists(" + dif + ",artists" + dif + "); \" src=\"/i/search/page_mod_off.gif\" width=\"11\" height=\"9\" border=\"0\" alt=\"page " + i + "\" /></td>";
		}
		if(i != pageCount) 
		{
			pageIconString = pageIconString + "<td><img src=\"/i/if/spacer.gif\" width=\"2\" height=\"1\" /></td>";
		}
	}
	pageIconString = pageIconString + "</tr></table>";
	document.getElementById("pageIconRow" + dif).innerHTML = pageIconString;
}

function nextArtist(dif)
{
	var pageCount = document.getElementById("pageCount" + dif).alt;
	var page = document.getElementById("page" + dif).alt;
	if(page != pageCount)
	{
		page++;
		document.getElementById("page" + dif).alt = page;
		eval("loadArtists(dif,artists" + dif + ");");
	}
}

function prevArtist(dif)
{
	var page = document.getElementById("page" + dif).alt;
	if(page != 1)
	{
		page--;
		document.getElementById("page" + dif).alt = page;
		eval("loadArtists(dif,artists" + dif + ");");
	}
}


