var greenRow=false;
var currentCat;
var currentSect;
var currentPage;
var totalPages = 1;
var maxDisplay = 14;
var pageTitle;
var pageCount="";
var totalPages;

var catList= new Array();
catList[0] = new Array("Books", "books");
catList[1] = new Array("Gift Items", "gift");
catList[2] = new Array("Audio CD's", "cd");
catList[3] = new Array("About Stan's Books", "about");
function loadCatalog()
{
	
	var query = window.location.search.substring(1);
	var vars = query.split("&");
	var tempCat = vars[0].split("=");
	currentCat = tempCat[1];
	var tempSect = vars[1].split("=");
	currentSect = tempSect[1];
	var tempPage =  vars[2].split("=");
	currentPage = parseInt(tempPage[1]);
	
	document.write('<script language=\"JavaScript\" src=\"bookstore_catalog_'+ currentCat +'.js\"></script>');
}

function displayPage()
{
	var tempString = "<div style=\"text-align:center\"><p>";
	for (var i = 0; i<catList.length; ++i)
	{
		if (catList[i][1]==currentCat)
		{
			tempString += "<span id=\"selectedPage\">"+ catList[i][0] +"</span>&#160;&#160;&#149;&#160;&#160;";
			
		}
		else
		{
			if (i < catList.length-1)
			{
				tempString += "<a href=\"catalog_display.htm?cat=" + catList[i][1] + "&amp;sect=all&amp;page=" + 1 + "\">" +catList[i][0]+"</a>&#160;&#160;&#149;&#160;&#160;";
			}
			else
			{
				tempString += "<a href=\"\aboutBooks.htm\">"+catList[i][0]+"</a>";
			}
		}
	}
	tempString += "</p></div>";
	document.write(tempString);
	
	if(catalog.length>2)
	{
		var tempString = "<div style=\"text-align:center; margin-bottom:10px\"><p><form name=\"form1\"><select name=\"menu1\"  style=\"width:160\" onChange=\"changeView(this)\">";
		var temp;
		if(currentSect=="all")
		{
			 temp =1;
		}
		else
		{
			temp = currentSect;
		}
		var tempSect = parseInt(temp);
		for (var i=1; i<catalog.length; ++i)
		{
			if(i==tempSect)
			{
				if(i==1){
					tempString += "<option value=\"all\" selected>All " + catalog[0] +"</option>";
				}
				else
				{
					tempString += "<option value="+i+" selected>"+ catalog[i][0][1]+"</option>";;
				}
			}

			else if(i!=tempSect)
			{
				if(i==1){
					tempString += "<option value=\"all\">All " + catalog[0] +"</option>";
				}
				else
				{
					tempString += "<option value="+i+">"+ catalog[i][0][1]+"</option>";
				}
			}
		}
		tempString +="</select></form></p></div>";
		document.write(tempString);
	}
	
	if(currentSect=="all"){
		pageTitle = "All " + catalog[0];
	}
	else{
	pageTitle = catalog[currentSect][0][0];
	}
	countPages();
}

function countPages()
{
	var secLength = 1;
	if(currentSect=="all"){
		for(var i =1; i<=catalog.length-1; ++i)
		{
			secLength += catalog[i].length-1;
		}
	}
	else{
		secLength += catalog[currentSect].length-1;
	}
	totalPages = Math.ceil(secLength/maxDisplay);
	displayPageCount();
}

function displayPageCount()
{
	//alert(totalPages);
	if(totalPages>1){
		var tempString="<p style=\"text-align:center; margin:15px;\">";
		for (var i=1; i<=totalPages; ++i)
		{
			if(i==currentPage)
			{
				tempString += "<span id=\"selectedPage\">"+currentPage+"</span>";
			}
			else
			{
				tempString += "<a href=\"catalog_display.htm?cat="+currentCat+"&amp;sect="+currentSect +"&amp;page="+i+"\">"+i+"</a>";
			}
			if(i<totalPages)
			{
				tempString += "&#160;&#160;&#149;&#160;&#160;"
			}
		}
		tempString += "</p>";
		pageCount = tempString;
		document.write(pageCount);
	}
	else if(totalPages==1){
		var tempString="<p>&nbsp;</p>";
		document.write(tempString);
	}
	displayCatalog();
}

function displayCatalog()
{
	var greenRow = false;
	var tempSection = currentSect;
	var tempString = "<table class=\"bookTable\" cellspacing=\"0\" cellpadding=\"5\">";
	var maxCount = currentPage * maxDisplay;
	var displayCount = maxCount - maxDisplay;
	var tempCount = 0;
	if (currentSect=="all"){
		tempString += "<tr><td colspan = \"3\" class=\"columnTableHeaderTD\">"+pageTitle+"</td></tr>";
		for (var i =1; i<catalog.length; ++i){
			for (var j= 1; j< catalog[i].length; ++j){
				var title=catalog[i][j][0];
				var graphic="<img src=books/" + catalog[i][j][1] + ">";
				var linkText = "http://www.adventurepublications.net/tek9.asp?pg=products&specific=" + catalog[i][j][2];
				if(tempCount<maxCount){
					if(tempCount>=displayCount){
						if(greenRow==true){
							tempString +="<tr bgColor=\"#D7E3D7\"><td style=\"text-align:center; width:130px;\">"+graphic+"</td><td><h3>"+title +"</h3></td><td width=\"100\"><a href="+linkText+" target=\"_blank\">Click Here to<br>Buy This Item at<br>Adventure<br>Publications</td></tr>";
							greenRow=false;
						}
						else{
							tempString +="<tr><td style=\"text-align:center;  width:130px;\">"+graphic+"</td><td><h3>"+title +"</h3></td><td width=\"100\"><a href="+linkText+" target=\"_blank\">Click Here to<br>Buy This Item at<br>Adventure<br>Publications</td></tr>";
							greenRow=true;
						}
					}
					++tempCount;
				}
				else{
					break;
				}
			}			
		}
	}
	else{
		tempString += "<tr><td colspan = \"3\" class=\"columnTableHeaderTD\">"+pageTitle+"</td></tr>";
		for (var j= 1; j<= catalog[currentSect].length-1; ++j){
			var title=catalog[currentSect][j][0];
			var graphic="<img src=books/" + catalog[currentSect][j][1] + ">";
			
			var linkText = "http://www.adventurepublications.net/tek9.asp?pg=products&specific=" + catalog[currentSect][j][2];
			if(tempCount<maxCount){
				if(tempCount>=displayCount){
					if(greenRow==true){
						tempString +="<tr bgColor=\"#D7E3D7\"><td style=\"text-align:center\">"+graphic+"</td><td><h3>"+title +"</h3></td><td width=\"100\"><a href="+linkText+" target=\"_blank\">Click Here to<br>Buy This Item at<br>Adventure<br>Publications</td></tr>";
						greenRow=false;
					}
					else{
						tempString +="<tr><td style=\"text-align:center\">"+graphic+"</td><td><h3>"+title +"</h3></td><td width=\"100\"><a href="+linkText+" target=\"_blank\">Click Here to<br>Buy This Item at<br>Adventure<br>Publications</td></tr>";
						greenRow=true;
					}
				}
				++tempCount;
			}
			else{
				break;
			}
				
		}			
	}
	tempString += "</table><br />";
	//alert(tempString);
	document.write(tempString);
	if(totalPages >1){
		document.write(pageCount);
	}
}


function changeView (which)
{
	var tempLink= which.value;
	var temp = "catalog_display.htm?cat="+ currentCat +"&amp;sect=" + tempLink + "&amp;page=1";
	//alert(which.value);
	
	window.location=temp;
}
loadCatalog();
