var defaultSort, defaultContentsFormat, contentsFormat, tableWidth, numColumns, numPerPage, numPages, contentsElements, showDefaultSort,
    detailedSortInfo, showPagingModes, viewAll, sortBy, sortHighLow, pageLinkPrefix, rowpad, columnpad, storename, columnSeparatorColor,
    rowSeparatorColor, listModeBG1, listModeBG2, sortLinkSeparator, modeLinkSeparator, pageLinkSeparator, globalPageNum, globalViewallthistime,
    currentItem, currentItemImage, numArrowDivs, showListMode, addToCartImage, showBrandSort, strikePrice, wishListURL;
var pageItems = new Array();
var oldSortBy = "";



function pagingItem(id, code, name, img, price, saleprice, options, abstr, orderable, brand, theme, mappricing) {
	if (!window.items) window.items = new Array();
	this.id=id.toLowerCase();
	this.code=code;
	this.name=name;
	this.img=img;
	this.price=round_decimals(price,2);
	this.saleprice=round_decimals(saleprice,2);
	if(saleprice) this.useprice=round_decimals(saleprice,2); else this.useprice=round_decimals(price,2);
	this.options=options;
	this.abstr=abstr;
	this.orderable=orderable;
	this.brand=brand;
	this.theme=theme;
	this.mappricing=mappricing;
	this.sortby="name";
	this.defaultsort=window.items.length;
	window.items[window.items.length] = this;
}

function round_decimals(original_number, decimals) {
    var result1 = original_number * Math.pow(10, decimals)
    var result2 = Math.round(result1)
    var result3 = result2 / Math.pow(10, decimals)
    return pad_with_zeros(result3, decimals)
}

function pad_with_zeros(rounded_value, decimal_places) {
    var value_string = rounded_value.toString()
    var decimal_location = value_string.indexOf(".")
    if (decimal_location == -1) {
        decimal_part_length = 0
        value_string += decimal_places > 0 ? "." : ""
    }
    else {
        decimal_part_length = value_string.length - decimal_location - 1
    }
    var pad_total = decimal_places - decimal_part_length
    if (pad_total > 0) {
        for (var counter = 1; counter <= pad_total; counter++)
            value_string += "0"
        }
    return value_string
}

function FIND(item) {
	if (document.getElementById) return(document.getElementById(item));
	if (document.all) return(document.all[item]);
	return(false);
};

function pagingSegment(theArray,pageNum) {
	var newArray = new Array();
	for(i=(pageNum-1)*numPerPage; i<pageNum*numPerPage; i++) {
		newArray[i - (pageNum-1)*numPerPage] = theArray[i];
	}
	return newArray;
};

function sortArray(theArray, loBound, hiBound){
	var pivot, loSwap, hiSwap, temp;

	// Two items to sort
	if (hiBound - loBound == 1)
	{
		if (theArray[loBound].sortby > theArray[hiBound].sortby)
		{
			temp = theArray[loBound];
			theArray[loBound] = theArray[hiBound];
			theArray[hiBound] = temp;
		}
		return;
	}

	// Three or more items to sort
	pivot = theArray[parseInt((loBound + hiBound) / 2)];
	theArray[parseInt((loBound + hiBound) / 2)] = theArray[loBound];
	theArray[loBound] = pivot;
	loSwap = loBound + 1;
	hiSwap = hiBound;

	do {
		while (loSwap <= hiSwap && theArray[loSwap].sortby <= pivot.sortby)
			loSwap++;
		while (theArray[hiSwap].sortby > pivot.sortby)
			hiSwap--;
		if (loSwap < hiSwap)
		{
			temp = theArray[loSwap];
			theArray[loSwap] = theArray[hiSwap];
			theArray[hiSwap] = temp;
		}
	} while (loSwap < hiSwap);

	theArray[loBound] = theArray[hiSwap];
	theArray[hiSwap] = pivot;

	// 2 or more items in first section
	if (loBound < hiSwap - 1)
		sortArray(theArray, loBound, hiSwap - 1);

	// 2 or more items in second section
	if (hiSwap + 1 < hiBound)
		sortArray(theArray, hiSwap + 1, hiBound);
};

function changeClass(which,theclass) {
	which.className = theclass;
}

function writePageLinks( viewallThisTime, pageNum) {
	if(numPages > 1) {
		var pageLinks = "<span class=pagelinks>" + pageLinkPrefix;
		for(i=1; i<=numPages; i++) {
			if(i == pageNum && !viewallThisTime)
				pageLinks += "<span class=chosen>" + i + "</span>";
			else
				pageLinks += "<a href=#top onclick=javascript:writePage(" + i + ",false)>" + i + "</a>";
			if(i % 18 == 0)
				pageLinks += "<br>";
			else
		        	if(i < numPages)
		        		pageLinks += "<span class=sep>" + pageLinkSeparator + "</span>";
		}
		if(viewAll) {
			pageLinks += "<span class=sep>" + pageLinkSeparator + "</span>";
			if(viewallThisTime)
				pageLinks += "<span class=chosen>View All</span>";
			else
				pageLinks += "<a href=#top onclick=javascript:changeSortAndPage(sortBy,false,0,true)>View All</a>";
		}
		pageLinks += "</span>";

		var thePageDiv = FIND("pagelinks");
		if(thePageDiv) thePageDiv.innerHTML = pageLinks;
		thePageDiv = FIND("pagelinks2");
		if(thePageDiv) thePageDiv.innerHTML = pageLinks;
	}
};

function writeItemXofX(viewallthistime,pageNum) {
	var currentItemLow = ((pageNum - 1) * numPerPage + 1);
	var currentItemHigh = pageNum * numPerPage;
	var numItems = window.items.length;

	if((currentItemLow <= numItems) && (numItems <= currentItemHigh)) {
		currentItemHigh = numItems;
	}
	var theString = "<span class=itemof>";
	if(currentItemLow == currentItemHigh) {
		theString += "Showing Product " + currentItemLow + " of " + numItems ;
	} else if(viewallthistime) {
		theString += "Showing 1-" + numItems + " of " + numItems ;
	} else {
		theString += "Showing " + currentItemLow + "-" + currentItemHigh + " of " + numItems ;
	}
	theString += "</span>";

	var theItemDiv = FIND("itemof");
	if(theItemDiv) theItemDiv.innerHTML = theString;
	theItemDiv = FIND("itemof2");
	if(theItemDiv) theItemDiv.innerHTML = theString;
};

function writePageXofX(viewallthistime,pageNum) {
	var localContent = "<span class=pageof>";
	if(viewallthistime)
		localContent += "Viewing All Items";
	else
		localContent += "Page " + pageNum + " of " + numPages;

	//debug
	if(detailedSortInfo) {
		if(sortBy == "default")
			localContent += " (Newest First)";
		else {
			localContent += " <span class=detailedsortinfo>(Sorted by ";
			switch(sortBy) {
				case "name": localContent += "Name"; break
				case "price": localContent += "Price"; break
			}
			if(sortHighLow) {
				if(sortBy == "price")
					localContent += " High to Low)";
				else
					localContent += " Z-A)</span>";
			} else {
				if(sortBy == "price")
					localContent += " Low to High)";
				else
					localContent += " A-Z)</span>";
			}
		}
	}

	localContent += "</span>";

	var pageofDiv = FIND("pageof");
	if(pageofDiv) pageofDiv.innerHTML = localContent;
	pageofDiv = FIND("pageof2");
	if(pageofDiv) pageofDiv.innerHTML = localContent;
};

function writeUpDownArrow(divname) {
	if(divname) {
		localContent = "";
		if(sortHighLow) localContent += "&nbsp;" + upArrow; else localContent += "&nbsp;" + downArrow;
		divname.innerHTML = localContent;
	}
}

function writeArrows(viewallthistime,pageNum) {
	var localContent;
	var leftArrowString = "", rightArrowString = "";
	var upDownArrowDiv  = FIND(sortBy + "arrowdiv");
	var upDownArrowDiv2 = FIND(sortBy + "arrowdiv2");
	if(viewallthistime) {
		writeUpDownArrow(upDownArrowDiv);
		writeUpDownArrow(upDownArrowDiv2);
		leftArrowString += leftArrowG;
		rightArrowString += rightArrowG;
		for(i=1; i<=numArrowDivs; i++) {
			localDiv = FIND("rightArrowDiv" + i);
			if(localDiv) localDiv.innerHTML = rightArrowString;
			localDiv = FIND("leftArrowDiv" + i);
			if(localDiv) localDiv.innerHTML = leftArrowString;
		}
	} else {
		writeUpDownArrow(upDownArrowDiv);
		writeUpDownArrow(upDownArrowDiv2);
		if(numPages == 1) {
			leftArrowString += leftArrowG;
			rightArrowString += rightArrowG;
		} else if(pageNum == 1) {
			leftArrowString += leftArrowG;
			rightArrowString += "<a href=javascript:writePage(2)>" + rightArrow + "</a>";
		} else if (pageNum == numPages) {
			leftArrowString += "<a href=javascript:writePage(" + (numPages - 1) + ")>" + leftArrow + "</a>";
			rightArrowString += rightArrowG;
		} else {
			leftArrowString += "<a href=javascript:writePage(" + (pageNum - 1) + ")>" + leftArrow + "</a>"
			rightArrowString += "<a href=javascript:writePage(" + (pageNum + 1) + ")>" + rightArrow + "</a>";
		}
		for(i=1; i<=numArrowDivs; i++) {
			localDiv = FIND("rightArrowDiv" + i);
			if(localDiv) localDiv.innerHTML = rightArrowString;
			localDiv = FIND("leftArrowDiv" + i);
			if(localDiv) localDiv.innerHTML = leftArrowString;
		}
	}
};

function pagingPrice(localItem) {
	var onsale = (localItem.saleprice && localItem.saleprice != 0);
	var usemap = localItem.mappricing;
	var localContent = "";
	if(localItem.price != 0) {
	    if(usemap && !onsale)
	    	localContent += mapPricing(localItem);
	    else {
	            localContent += " " + regularPriceText;
	            if(onsale && strikePrice) localContent += "<span class=listprice>";
	            if(!onsale) localContent += "<span class=regularprice>";
	            localContent += "$" + localItem.price;
	            if(onsale && strikePrice) localContent += "</span>";
	            if(!onsale) localContent += "</span>";
	            localContent += " &nbsp;";
	    }
	}
	if(onsale && usemap) localContent += mapPricing(localItem);
	if(onsale && !usemap) localContent += "<span class=salepricewords>" + salePriceText + " </span><span class=saleprice>$" + localItem.saleprice + "</b></span><br>";
	return localContent;
}

function pagingAddToCart(localItem,breakit) {
	var localContent = "";
	if(localItem.useprice != 0 && !localItem.mappricing)
		if(localItem.orderable) {
			var newid = localItem.id.replace(/-/g,"");
			if(useNewCheckout)
				localContent += "<form onSubmit='if (this.vwquantity.value == 0) this.vwquantity.value = 1;' method=post action=http://order.store.yahoo.net/cgi-bin/wg-order?" + storename + " name=single_" + newid + " id=" + newid + " style='display:inline;margin:0px;'>";
			else
				localContent += "<form onSubmit='if (this.vwquantity.value == 0) this.vwquantity.value = 1;' method=post action=http://order.store.yahoo.com/cgi-bin/wg-order?" + storename + " name=single_" + newid + " id=" + newid + " style='display:inline;margin:0px;'>";
			localContent += "<input type=hidden name=vwcatalog value=" + storename + ">";
			localContent += "<input type=hidden name=vwitem value=" + localItem.id + ">";
			localContent += "<div><span class=qty>Quantity: </span><input type=text size=1 class=qtybox name=vwquantity value=0></div>";
			if(breakit) localContent += ""; else localContent += "";
			if(addToCartImage != "")
				localContent += addToCartImage;
			else
				localContent += "<input type=submit id=submit value='Add to Cart'>"
			localContent += "</form>";
		} else
			localContent += "<span class=outofstock>Out of Stock</span>";
	return localContent;
}



function writeVerticalInfo(currentRow, numRows) {
          var localContent = "";
          localContent += "<tr valign=top>";
          //VERTICAL - LOOP CELLS
          for(currentCol=0; currentCol<numColumns; currentCol++) {
                    localContent += "<td width=" + 100 / numColumns + "% height=100% align=center";
                    if(pageSegment[currentItem]) localContent += " onmouseover=changeClass(this,'vcell-on'); onmouseout=changeClass(this,'vcell'); class=vcell id=vcell" + currentItem;
                    localContent += ">";
                    if(pageSegment[currentItem]) {
                              localItem = pageSegment[currentItem];
                              var elementArray = contentsElements.split("/");
                              for(i=0;i<elementArray.length;i++) {
                                        switch(elementArray[i]) {
                                                  case "name":
                                                            if(localItem.name != "") localContent += "<a href=" + localItem.id + ".html class=paginglink>" + localItem.name + "</a><br>";
                                                  break
                                                  case "code":
                                                            if(localItem.code != "") localContent += "<span class=code>" + localItem.code + "</span><br>";
                                                  break
                                                  case "price":
                                                            localContent += pagingPrice(localItem);
                                                  break
                                                  case "abstract":
                                                            if(localItem.abstr != "") localContent += "<span class=abstract>" + localItem.abstr + "</span><br>";
                                                  break
                                                  case "order":
                                                            if(localItem.useprice != 0) localContent += pagingAddToCart(localItem,true);
                                                  break
                                                  case "image": break
                                        }
                              }
                              currentItem++;
                    } else {
                              localContent += "&nbsp;";
                    }
                    localContent += "</form></td>";
                    if(currentCol<numColumns-1 && columnpad > 0)
                              localContent += writeCellPadding(currentItem);
          }
          localContent += "</tr>";
          if((currentRow < (numRows - 1)) && (rowpad > 0))
                    localContent += writeRowPadding((numColumns + (3 * (numColumns - 1))));
          return localContent;
};

function writeVerticalImage(currentRow, numRows) {
          var localContent = "";
          localContent += "<tr valign=bottom>";
          for(currentCol=0; currentCol<numColumns; currentCol++) {
                    localContent += "<td width=" + 100 / numColumns + "% height=100% align=center";
                    if(pageSegment[currentItemImage]) localContent += "  onmouseover=changeClass(FIND('vcell" + currentItemImage + "'),'vcell-on'); onmouseout=changeClass(FIND('vcell" + currentItemImage + "'),'vcell');";
                    localContent += ">";
                    if(pageSegment[currentItemImage]) {
                              localItem = pageSegment[currentItemImage];
                              localContent += "<table cellspacing=0 cellpadding=0><tr><td><a href=" + localItem.id + ".html>" + localItem.img + "</a></td></tr></table>";
                              currentItemImage++;
                    } else {
                              localContent += "&nbsp;";
                    }
                    localContent += "</td>";
                    if(currentCol<numColumns-1 && columnpad > 0)
                              localContent += writeCellPadding(currentItemImage);
          }
          localContent += "</tr>";
          return localContent;
};

function writeVertical(pageNum, viewallthistime) {
          var localContent = "";
          var numRows = Math.ceil(pageSegment.length / numColumns);
          currentItem=0; currentItemImage = 0;
          for(currentRow=0; currentRow<numRows; currentRow++) {
                    if(pageSegment[currentItem]) {
                              if(contentsElements.indexOf("image") != -1)
                                        localContent += writeVerticalImage(currentRow, numRows);
                              localContent += writeVerticalInfo(currentRow, numRows);
                    }
          }
          return localContent;
};

function writeEllInfoCell(currentRow, numRows) {
	var localContent = "";
	localContent += "<td width=" + 100 / numColumns + "% height=100% align=left onmouseover=changeClass(this,'ecell-on'); onmouseout=changeClass(this,'ecell'); class=ecell id=ecell" + currentItem + ">"
	if(pageSegment[currentItem]) {
		localItem = pageSegment[currentItem];
		var elementArray = contentsElements.split("/");
		for(i=0;i<elementArray.length;i++) {
			switch(elementArray[i]) {
				case "name":
					if(localItem.name != "") localContent += "<a href=" + localItem.id + ".html class=paginglink>" + localItem.name + "</a><br>";
				break
				case "code":
					if(localItem.code != "") localContent += "<span class=code>" + localItem.code + "</span><br>";
				break
				case "price":
					localContent += pagingPrice(localItem);
				break
				case "abstract":
					if(localItem.abstr != "") localContent += "<span class=abstract>" + localItem.abstr + "</span><br>";
			  	
				break
				case "order":
	                                localContent += pagingAddToCart(localItem);
				break
				case "image": break
			}
		}
		currentItem++;
	} else {
		localContent += "&nbsp;";
	}
	localContent += "</td>";
	return localContent;
};

function writeEllImageCell() {
	var localContent = "";
	localContent += "<td align=center onmouseover=changeClass(FIND('ecell" + currentItem + "'),'ecell-on'); onmouseout=changeClass(FIND('ecell" + currentItem + "'),'ecell');>"
	if(pageSegment[currentItem]) {
		localItem = pageSegment[currentItem];
		localContent += "<a href=" + localItem.id + ".html>" + localItem.img + "</a>";
	} else {
		localContent += "&nbsp;";
	}
	localContent += "</td>";
	return localContent;
};

function writeEll(pageNum, viewallthistime) {
	var localContent = "";
	var numRows = Math.ceil(pageSegment.length / numColumns);
	currentItem=0; currentItemImage = 0;
	for(currentRow=0; currentRow<numRows; currentRow++) {
		if(pageSegment[currentItem]) {
			localContent += "<tr valign=top>";
			for(currentCol=0; currentCol<numColumns; currentCol++) {
				if(pageSegment[currentItem]) {
					if(contentsElements.indexOf("image") != -1)
						localContent += writeEllImageCell();
					localContent += writeEllInfoCell();
				} else {
					if(contentsElements.indexOf("image") != -1)
						localContent += "<td>&nbsp;</td>";
					localContent += "<td>&nbsp;</td>";
				}
				if(currentCol<numColumns-1 && columnpad > 0)
					localContent += writeCellPadding();
			}
			localContent += "</tr>";
			if((currentRow < (numRows - 1)) && (rowpad > 0))
				localContent += writeRowPadding((numColumns * 2 + (3 * (numColumns - 1))));
		}
	}
	return localContent;
};

function writeWrapCell(currentRow, numRows) {
	var localContent = "";
	localContent += "<td width=" + 100 / numColumns + "% height=100% align=left onmouseover=changeClass(this,'wcell-on'); onmouseout=changeClass(this,'wcell'); class=wcell>"
	if(pageSegment[currentItem]) {
		localItem = pageSegment[currentItem];
		var elementArray = contentsElements.split("/");
		for(i=0;i<elementArray.length;i++) {
			switch(elementArray[i]) {
				case "image":
					localContent += "<a href=" + localItem.id + ".html>" + localItem.img + "</a>";
				break
				case "name":
					if(localItem.name != "") localContent += "<a href=" + localItem.id + ".html class=paginglink>" + localItem.name + "</a><br>";
				break
				case "code":
					if(localItem.code != "") localContent += "<span class=code>" + localItem.code + "</span><br>";
				break
				case "price":
					localContent += pagingPrice(localItem);
				break
				case "abstract":
					if(localItem.abstr != "") localContent += "<span class=abstract>" + localItem.abstr + "</span><br>";
				break
				case "brand":
					if(localItem.brand != "") localContent += "<span class=brand>" + localItem.brand + "</span><br>";
				break
	                        case "order":
	                                localContent += pagingAddToCart(localItem);
	                        break
			}
		}
		currentItem++;
	} else {
		localContent += "&nbsp;";
	}
	localContent += "</td>";
	return localContent;
};

function writeWrap(pageNum, viewallthistime) {
	var localContent = "";
	var numRows = Math.ceil(pageSegment.length / numColumns);
	currentItem=0; currentItemImage = 0;
	for(currentRow=0; currentRow<numRows; currentRow++) {
		if(pageSegment[currentItem]) {
			localContent += "<tr valign=top>";
			for(currentCol=0; currentCol<numColumns; currentCol++) {
				if(pageSegment[currentItem])
					localContent += writeWrapCell();
				else
					localContent += "<td>&nbsp;</td>";
				if(currentCol<numColumns-1 && columnpad > 0)
					localContent += writeCellPadding();
			}
			localContent += "</tr>";
			if((currentRow < (numRows - 1)) && (rowpad > 0))
				localContent += writeRowPadding((numColumns * 2 + (3 * (numColumns - 1))));
		}
	}
	return localContent;
};

function writeCellPadding(num) {
	if(num) localnum = num; else localnum = currentItem;
	if(pageSegment[localnum]) {
		var localContent = "<th><img src=http://www.scrapbook.com/images/spacer.gif height=1 width="
		                 +  round_decimals(columnpad / 2,0) + "></th>"
		                 +  "<th width=1 bgcolor=" + columnSeparatorColor + "><img src=http://www.scrapbook.com/images/spacer.gif"
		                 +  " height=1 width=1></th>"
		                 +  "<th><img src=http://www.scrapbook.com/images/spacer.gif height=1 width="
		                 +  round_decimals(columnpad / 2,0) + "></th>";
		return localContent;
	} else {
		var localContent = "<th><img src=http://www.scrapbook.com/images/spacer.gif height=1 width="
		                 +  round_decimals(columnpad / 2,0) + "></th>"
		                 +  "<th width=1><img src=http://www.scrapbook.com/images/spacer.gif"
		                 +  " height=1 width=1></th>"
		                 +  "<th><img src=http://www.scrapbook.com/images/spacer.gif height=1 width="
		                 +  round_decimals(columnpad / 2,0) + "></th>";
		return localContent;
	}
}

function writeRowPadding(colspan) {
	if(pageSegment[currentItem]) {
		var localContent = "<tr><th><img src=http://www.scrapbook.com/images/spacer.gif width=1 height="
		                 +  round_decimals(rowpad / 2,0) + "></th></tr>"
		                 +  "<tr><th bgcolor=" + rowSeparatorColor +  " colspan=" + colspan + "><img src=http://www.scrapbook.com/images/spacer.gif"
		                 +  " width=1 height=1></th></tr>"
		                 +  "<tr><th><img src=http://www.scrapbook.com/images/spacer.gif width=1 height="
		                 +  round_decimals(rowpad / 2,0) + "></th></tr>";
		return localContent;
	} else
		return "";
}

function writePage(pageNum, viewallthistime) {
	var localContent = "";
	if((numPerPage > -1) && !(viewallthistime)) pageSegment = pagingSegment(window.items,pageNum); else pageSegment = window.items;
	writePageLinks(viewallthistime,pageNum);
	writeItemXofX(viewallthistime,pageNum);
	writePageXofX(viewallthistime,pageNum);
	writeSortLinks("",viewallthistime);
	writeSortLinks("2",viewallthistime);
	writePagingModesLinks();
	writeArrows(viewallthistime,pageNum);

	var numRows = Math.ceil(pageSegment.length / numColumns);
	//localContent += "<hr>";
	switch(contentsFormat) {
		case "LIST":
			localContent += writeAsList(viewallthistime,pageNum);
		break
		case "VERTICAL":
			localContent += "<table cellspacing=0 cellpadding=0 width=" + tableWidth + " border=0>";
			localContent += writeVertical(pageNum, viewallthistime);
			localContent += "</table>";
		break
		case "ELL":
			localContent += "<table cellspacing=0 cellpadding=0 width=" + tableWidth + " border=0>";
			localContent += writeEll(pageNum, viewallthistime);
			localContent += "</table>";
		break
		case "WRAP":
			localContent += "<table cellspacing=0 cellpadding=0 width=" + tableWidth + " border=0>";
			localContent += writeWrap(pageNum, viewallthistime);
			localContent += "</table>";
		break
		default:
			writeAsList(viewallthistime,pageNum);
	}
	//localContent += "<hr>";

	var theDiv = FIND("contents");
	theDiv.innerHTML = localContent;

	temp = document.location.href.split(".");
	temp2 = temp[temp.length - 2].split("/");
	var currentPageHTML = temp2[temp2.length - 1] + ".html";

	SetCookie('thePage', pageNum, exp);
	SetCookie('theSort', sortBy, exp);
	SetCookie('thePageName', currentPageHTML, exp);
	if(sortHighLow) SetCookie('highlow', "1", exp); else SetCookie('highlow', "0", exp);
	SetCookie('contentsformat', contentsFormat, exp);
	if(viewallthistime) SetCookie('viewall', "1", exp); else SetCookie('viewall', "0", exp);

	globalPageNum = pageNum;
	globalViewallthistime = viewallthistime;

	if (window.quicklookSetup) {
		quicklookSetup();
	}
};

function changeSortAndPage(newSortBy, newHighLow, pageNum, viewallthistime) {
	sortBy = newSortBy;
	sortHighLow = newHighLow;

	updateSortBy(window.items, sortBy);
	sortArray(window.items,0,window.items.length-1);
	if(sortHighLow) window.items.reverse();

	if(viewallthistime)
		writePage(pageNum, true);
	else
		writePage(pageNum, false);
};

function initPaging() {
	var pageNum, viewallthistime = false, highlowthistime = false;

	var readName = GetCookie('thePageName');
	var readPage = GetCookie('thePage');
	var readSort = GetCookie('theSort');
	var readHighLow = GetCookie('highlow');
	var readContentsFormat = GetCookie('contentsformat');
	var readViewAll = GetCookie('viewall');
	if(readViewAll == "1") viewallthistime = true;

	if(readName || readSort || readContentsFormat) {
		temp = document.location.href.split(".");
		temp2 = temp[temp.length - 2].split("/");
		currentPageHTML = temp2[temp2.length - 1] + ".html";
		if(readName == currentPageHTML)
			(readPage == 0) ? pageNum = 1 : pageNum = parseInt(readPage);
		else
			pageNum = 1;
		readSort ? sortBy = readSort : sortBy = defaultSort;
		(readHighLow == 1 && readSort) ? highlowthistime = true : highlowthistime = false;
		(readContentsFormat == null) ? contentsFormat = defaultContentsFormat : contentsFormat = readContentsFormat;
	} else {
		pageNum = 1;
		sortBy = defaultSort;
		contentsFormat = defaultContentsFormat;
	}
	if(sortBy == "default")
		writePage(pageNum,viewallthistime);
	else
		changeSortAndPage(sortBy,highlowthistime,pageNum,viewallthistime);
};

function writeAsList(viewallthistime,pageNum) {
	var content = "";
	if(viewallthistime)
		var currentItemLow = 1;
	else
		var currentItemLow = ((pageNum - 1) * numPerPage + 1);
	content += "<table width=" + tableWidth + " cellspacing=1>";
	content += "<tr><td class=listheader>#</td><td class=listheader>Image</td><td class=listheader>Name</td>";
	content += "<td class=listheader align=right>Price</td></tr>";
	for(i=0; i<pageSegment.length; i++) {
		itemnow = pageSegment[i];
		if(itemnow) {
			content += "<tr valign=top bgcolor=";
			(i % 2 == 0) ? content += listModeBG1 : content += listModeBG2;
			content += ">";
			content += "	<td align=right valign=middle class=listnumber>";
			content += currentItemLow + i + ".";
			content += "	</td>";
			content += "	<td>";
			content += "<a href=" + itemnow.id + ".html>" + itemnow.img + "</a>";
			content += "	</td>";
			content += "	<td width=\"100%\">";
			content += "		<a href=" + itemnow.id + ".html>" + itemnow.name + "</a>";
			if(itemnow.brand != "") content += "<div class=brand>Brand: " + itemnow.brand + "</div>";
			if(itemnow.theme != "") content += "<div class=theme>Theme: " + itemnow.theme + "</div>";
			if(itemnow.useprice != 0 && itemnow.useprice != "") content += pagingAddToCart(itemnow,false) + "&nbsp;";
			content += "	</td>";
			content += "	<td align=right><nobr><span class=list"
			content += itemnow.saleprice == 0 ? "" : "regularprice";
			content += ">";
			content += itemnow.useprice == 0 ? "" : "$" + itemnow.useprice;
			content += "</span></nobr>";
			content += "	</td>";
			content += "</tr>";
		}
	}
	content += "</table>";
	return content;
};

function updateSortBy(array, sortby) {
	/* find what value to sort the array by and store it to a variable local to each item */
	for(i=0; i<array.length; i++) {
		switch(sortby) {
			case "default" :
				array[i].sortby = array[i].defaultsort;
			break
			case "name" :
				array[i].sortby = array[i].name;
			break
			case "price" :
				array[i].sortby = array[i].useprice-0;
			break

			case "brand" :
				array[i].sortby = array[i].brand;
			break
			case "theme" :
				array[i].sortby = array[i].theme;
			break
			default : array[i].sortby = array[i].useprice-0;
		}
	}
};

function writeOneSortLink(linkid,linkname,sep,chosen,bot,viewallthistime) {
	var localContent = "";
	if(chosen) {
		localContent += "<a class=chosen href=#top onclick=javascript:changeSortAndPage(\"" + linkid + "\",";
		if(sortHighLow) localContent += "false"; else localContent += "true";
		localContent += ",1," + viewallthistime + ");>";
		localContent += linkname;
		localContent += "</a>";
	} else {
		localContent += "<a href=#top onclick=javascript:changeSortAndPage(\"" + linkid + "\",false,1," + viewallthistime + ");>";
		localContent += linkname;
		localContent += "</a>";
	}
	localContent += "<span id=" + linkid + "arrowdiv" + bot + "></span>";
	if(sep) localContent += sortLinkSeparator;
	return localContent;
}

function writeSortLinks(bot,viewallthistime) {
	var sortLinksDiv = FIND("sortlinks" + bot);
	if(sortLinksDiv) {
		var localContent = "<nobr><span class=sortlinks><b>Sort:</b>&nbsp;&nbsp;";
		switch(sortBy) {
			case "name":
					if(showDefaultSort) localContent += writeOneSortLink("default","Newest",true,false,bot,viewallthistime);
					localContent += writeOneSortLink("name","Name",true,true,bot,viewallthistime);
					localContent += writeOneSortLink("price","Price",true,false,bot,viewallthistime);

			break
			case "price":
					if(showDefaultSort) localContent += writeOneSortLink("default","Newest",true,false,bot,viewallthistime);
					localContent += writeOneSortLink("name","Name",true,false,bot,viewallthistime);
					localContent += writeOneSortLink("price","Price",true,true,bot,viewallthistime);


			break
			default:
					if(showDefaultSort) {
						localContent += "<span class=chosen>";
						localContent += "Newest";
						localContent += "</span>";
						localContent += sortLinkSeparator;
					}
					localContent += writeOneSortLink("name","Name",true,false,bot,viewallthistime);
					localContent += writeOneSortLink("price","Price",true,false,bot,viewallthistime);

		}
		localContent += "</span></nobr>";
		sortLinksDiv.innerHTML = localContent;
	}
};

function changeFormat(newFormat) {
	contentsFormat = newFormat;
	writePage(globalPageNum, globalViewallthistime);
}

function mapPricing(useItem) {
	var mapstring = "<span class=map>";
	mapstring += "<form method=post action=http://order.store.yahoo.com/cgi-bin/wg-order?" + storename + ">"
	mapstring += '<input alt="Click for instant price quote" type=IMAGE src="http://lib1.store.vip.sc5.yahoo.com/lib/tigergps/instant-price-quote.gif" height="16" width="155" border=0>';
	mapstring += '<br><b>In your shopping cart </b><a href="javascript:makeMAPWin(300,350);">(Why?)</a><br>';
	mapstring += "<input type=hidden name=vwcatalog value=" + storename + ">"
	mapstring += "<input type=hidden name=vwitem value=" + localItem.id + ">";
	if(addToCartImage != "")
		mapstring += addToCartImage;
	else
		mapstring += "<input type=submit id=submit value='Add to Cart'>"
	mapstring += "</form>";
	mapstring += "</span>";
	return mapstring;
}

function writePagingModesLinks() {
	if(showPagingModes) {
		var localContent  = "<div id=modes>";
		(contentsFormat != "VERTICAL") ? localContent += "<a href=#top onclick=\"javascript:changeFormat('VERTICAL');\">" : localContent += "<span class=chosen>";
		localContent += "Grid";
		(contentsFormat != "VERTICAL") ? localContent += "</a>" : localContent += "</span>";
		localContent += "<span class=sep>" + modeLinkSeparator + "</span>";

		(contentsFormat != "ELL") ? localContent += "<a href=#top onclick=\"javascript:changeFormat('ELL');\">" : localContent += "<span class=chosen>";
		localContent += "Wide Grid";
		(contentsFormat != "ELL") ? localContent += "</a>" : localContent += "</span>";
		localContent += "<span class=sep>" + modeLinkSeparator + "</span>";
/*
		(contentsFormat != "WRAP") ? localContent += "<a href=#top onclick=\"javascript:changeFormat('WRAP');\">" : localContent += "<span class=chosen>";
		localContent += "Style 3";
		(contentsFormat != "WRAP") ? localContent += "</a>" : localContent += "</span>";
*/
		if(showListMode) {
//			localContent += "<span class=sep>" + modeLinkSeparator + "</span>";

			(contentsFormat != "LIST") ? localContent += "<a href=#top onclick=\"javascript:changeFormat('LIST');\">" : localContent += "<span class=chosen>";
			localContent += "List";
			(contentsFormat != "LIST") ? localContent += "</a>" : localContent += "</span>";
		}

		localContent += "</div>";

		var listModeDiv = FIND("pagingmodes");
		if(listModeDiv) listModeDiv.innerHTML = localContent;
		listModeDiv = FIND("pagingmodes2");
		if(listModeDiv) listModeDiv.innerHTML = localContent;
	}
}
