//*************************************************
//***** COPYRIGHT 2005, PRACTICAL DATA, INC. ******
//***** - UNAUTHORIZED USE IS PROHIBITED **********
//*************************************************
if(isBlank(imageUrl)) var imageUrl = "http://site.treasuredlocks.com/images/";
var lastSection = "";
var refreshId = "";

function parseProductArray(itemsPerPage) {

	//***********************************************
	//***** BUILD JS ARRAY OF PRODUCT ELEMENTS ******
	//***********************************************
	var i = 0;
	var productArray = new Array();
	
	//*******************************************
	//*** ONLY LOAD IF PRODUCT WRAPPER EXISTS ***
	//*******************************************
	var productIdString = "";
	var productWrapper = document.getElementById("productWrapper");
	if(!isBlank(productWrapper)) {
	
		if(productWrapper.firstChild) { // check for children
			var oChild = productWrapper.firstChild;
			while(oChild) {
			 if(oChild.nodeType==1 && String(oChild.id).indexOf("productDisplay") > -1) { 
				productArray[i] = new Object();
				productArray[i].id = oChild.id;
				productArray[i].html = document.getElementById(oChild.id).innerHTML;
	
				//**************************************************
				//**** PARSE PRODUCT ATTRIBUTES (2 levels deep) ****
				//**************************************************
				var subChild = oChild.firstChild;
				while(subChild) {
					if(subChild.nodeType==1) {
						if(subChild.id == "name") productArray[i].title = subChild.title;
						if(subChild.id == "id") productArray[i].productId = subChild.title;
						if(subChild.id == "price") productArray[i].price = subChild.title - 0;
						//if(subChild.id == "rating") productArray[i].rating = subChild.title - 0;
						//if(subChild.id == "bestseller") productArray[i].bestseller = subChild.title;
						
					}
					subChild = subChild.nextSibling;
				}
				
				//***********************************************************
				//**** BUILD PRODUCT ID STRING ******************************
				//**** - LIMIT TO 200 TO PREVENT OVERFLOWING COOKIE LIMIT ***
				//***********************************************************
				if(productArray[i].id && i < 200) productIdString += productArray[i].id + ",";
				
			 	i++;
			 }
			  oChild = oChild.nextSibling;
		   }
		}
	} else {
		productArray = false;
	}
	
	//*******************************
	//**** SAVE productIdString *****
	//*******************************
	if(!isBlank(productIdString)) {
		productIdString = String(productIdString).substring(0, productIdString.length -1);	//*** TRIM TRAILING COMMA
		pdSetCookie("pd_currentProductList", productIdString, "", "", "");
	}

	//****************
	//**** RETURN ****
	//****************
	return productArray;	
}

function renderProductArray(productArray, currentPageNumber, itemsPerPage, sortByField) {

	var html = "";
	var pageNumber = 1;
	var i = 1;
	var item = new Object();
	var tempHtml = "";
	var tempProductHtml = "";
	var rowItemCount = 1;
	var isOpen = false;
	var productIdList = "";
	var shade = true;
	
	for(item in productArray) {
		productIdList += productArray[item].id;
		tempHtml = "";
		
		if(pageNumber == currentPageNumber || currentPageNumber == "all") {
			
			if(rowItemCount == 1) {
				tempHtml += '<table border="0" cellpadding="0" cellspacing="0" width="100%"><tr>\r\n';
				isOpen = true;
			}
			tempHtml += '<td valign="top"><div id="' + productArray[item].id + '" class="displayDiv">';

			if(!shade) {
				tempProductHtml = String(productArray[item].html).replace(/pagedItemTableOff/g, "pagedItemTableOn");
			} else {
				tempProductHtml = String(productArray[item].html).replace(/pagedItemTableOn/g, "pagedItemTableOff");
			}
			
			tempHtml += tempProductHtml;
			
			tempHtml += '</div></td>';
			rowItemCount++;
			
			if(rowItemCount > itemsPerRow) {
				tempHtml += '</tr></table>';
				rowItemCount = 1;
				isOpen = false;
			}
			
			shade = !shade;
		}
		
		i++;
		pageNumber = Math.ceil(i / itemsPerPage);
		html += tempHtml;
	}
	
	if(isOpen) html += '\r\n</tr></table>\r\n';
	return html;
}

function sortProductArrayByPrice(productArray) {
	productArray = productArray.sort(compareId);
	productArray = productArray.sort(comparePrice);
	sortBy = "price";	
	return productArray;
}

function sortProductArrayByTitle(productArray) {
	productArray = productArray.sort(compareId);
	productArray = productArray.sort(compareTitle);
	sortBy = "title";
	return productArray;
}

function sortProductArrayByRating(productArray) {
	productArray = productArray.sort(compareId);
	productArray = productArray.sort(compareRating);
	sortBy = "rating";
	return productArray;
}

function sortProductArrayByBestseller(productArray) {
	productArray = productArray.sort(compareId);
	productArray = productArray.sort(compareBestseller);
	sortBy = "bestseller";
	return productArray;
} 


function compareId(item1, item2) {
	if(item1.id > item2.id) return 1;
	else if(item1.id < item2.id) return -1;
	else return 0;
}

function compareTitle(item1, item2) {
	if(item1.title > item2.title) return 1;
	else if(item1.title < item2.title) return -1;
	else return 0;
}

function comparePrice(item1, item2) {
	if(item1.price > item2.price) return 1;
	else if(item1.price < item2.price) return -1;
	else return 0;
}

function compareBestseller(item1, item2) {
	if(item1.bestseller > item2.bestseller) return -1;
	else if(item1.bestseller < item2.bestseller) return 1;
	else return 0;
}

function compareRating(item1, item2) {
	if(item1.rating > item2.rating) return -1;
	else if(item1.rating < item2.rating) return 1;
	else return 0;
}


function displayProductArray(productArray, currentPageNumber, itemsPerPageRequest, sortByField) {
	
	//*************************************
	//**** TEST FOR NOTHING TO DISPLAY ****
	//*************************************
	if(!productArray) return false;
	
	if(productArray.length == 0) {
		document.getElementById("productWrapper").innerHTML = '<div style="text-align:center"><br><br><b>No Products Found.</b><br><br><br><br><br></div>';
		return false;
	}
		
	//***********************************
	//**** REMEMBER GLOBAL SETTINGS *****
	//***********************************
	if(!isBlank(sortByField)) sortBy = sortByField;
	itemsPerPage = itemsPerPageRequest;
	//pdSetCookie("pd_currentPageNumber", currentPageNumber);
	pdSetCookie("pd_pagingSortBy", sortBy);
	
	//********************
	//*** PERFORM SORT ***
	//********************
	if(sortBy == "title") productArray = sortProductArrayByTitle(productArray);
	else if(sortBy == "price") productArray = sortProductArrayByPrice(productArray);
	else if(sortBy == "rating") productArray = sortProductArrayByRating(productArray);
	else if(sortBy == "bestseller") productArray = sortProductArrayByBestseller(productArray);
		
	if(isBlank(currentPageNumber)) currentPageNumber = 1;
	document.getElementById("productWrapper").innerHTML = renderProductArray(productArray, currentPageNumber, itemsPerPage, sortByField);
	document.getElementById("pageInfo").innerHTML = renderPageInfo(productArray.length, currentPageNumber, itemsPerPage);
	document.getElementById("pageInfoBottom").innerHTML = renderPageInfo(productArray.length, currentPageNumber, itemsPerPage);
	//document.getElementById("sortLinks").innerHTML = renderSortLinks(currentPageNumber, sortBy);
	//document.getElementById("sortLinksMenu").innerHTML = renderSortLinksMenu(currentPageNumber, sortBy);
	//document.getElementById("sortLinksMenuBottom").innerHTML = renderSortLinksMenu(currentPageNumber, sortBy);
	document.getElementById("sortByNameLink").innerHTML = renderSortByNameLink(currentPageNumber, sortBy);
	document.getElementById("sortByPriceLink").innerHTML = renderSortByPriceLink(currentPageNumber, sortBy);
	document.getElementById("pageNumberLinks").innerHTML = renderPageNumberLinks(productArray.length, currentPageNumber, itemsPerPage);
	document.getElementById("pageNumberLinksBottom").innerHTML = renderPageNumberLinks(productArray.length, currentPageNumber, itemsPerPage);
	document.getElementById("viewAllButton").innerHTML = renderViewAllButton(productArray.length, currentPageNumber, itemsPerPage);
	document.getElementById("viewAllButtonBottom").innerHTML = renderViewAllButton(productArray.length, currentPageNumber, itemsPerPage);

	//**************
	//*** RETURN ***
	//**************
	return false;
}

function getCurrentPageNumber() {
	var pageNumber = 1;
	var currentSection = pdGetCookie("pd_stickyBreadcrumbSection");
	if(lastSection == currentSection) {
		pageNumber = pdGetCookie("pd_currentPageNumber");
	}
	pdSetCookie("pd_lastPageNumber", pageNumber);
	if(isBlank(pageNumber)) pageNumber = 1;
	return pageNumber;	
}

function getCurrentSortBy() {
	var sortBy = "";
	var currentSection = pdGetCookie("pd_stickyBreadcrumbSection");
	if(lastSection == currentSection) {
		sortBy = pdGetCookie("pd_pagingSortBy");
	}
	if(isBlank(sortBy)) sortBy = "";
	return sortBy;	
}

//*******************************************************
//****** DISPLAY PAGE ELEMENTS RELATED TO SORTING *******
//*******************************************************
function renderPageNumberLinks(productCount, currentPageNumber, itemsPerPage) {
	var pageCount = Math.ceil(productCount/itemsPerPage);
	if(pageCount == 1) return "";	
	var html = "";
	
	html += '<table border="0" cellpadding="0" cellspacing="2"><tr valign="middle">';
		
		if(currentPageNumber != "all") {	
		if(currentPageNumber > 1) {
			var previousPage = currentPageNumber - 1;
			html += '<td><a href="#" onClick="return(displayProductArray(productArray, ' + previousPage + ', ' +  itemsPerPage + '))" class="pageArrow"><img src="http://site.thegiftboutique.com/pd_pagingEngine/images/arrowLeftBig.gif" border="0" hspace="3"></a></td>';
		} else {
			html += '';
		}
	}

	if(currentPageNumber != "all") {
		for(var i=1;i<pageCount + 1;i++) {
			if(currentPageNumber == i) {
				html += '<td class="pageNumBgOn"><font class="pageNumOn">' + i + '</font></td>';
			} else {
				html += '<td class="pageNumBg"><a href="#" class="pageNum" onClick="return(displayProductArray(productArray, ' + i + ', ' +  itemsPerPage + '))">' + i + '</a></td>';
			}
		}
	}
	
		if(currentPageNumber != "all") {	
		var nextPage = currentPageNumber + 1;
		if(nextPage <= pageCount) {
			html += '<td><a href="#" onClick="return(displayProductArray(productArray, ' + nextPage + ', ' +  itemsPerPage + '))" class="pageArrow"><img src="http://site.thegiftboutique.com/pd_pagingEngine/images/arrowRightBig.gif" border="0" hspace="3"></a></td>';
		} else {
			html += '';
		}
	}
	html += '</tr></table>';
	
	return html;
}

function renderPageInfo(productCount, currentPageNumber, itemsPerPage) {
	var pageCount = Math.ceil(productCount/itemsPerPage);
	if(pageCount == 1) return "";
	var html = "";
	
	if(currentPageNumber != "all") {
		html += '<font class="pageInfo"><nobr>&nbsp; Page ' + currentPageNumber + ' of ' + pageCount + '</nobr></font>';
	}
	
	return html;
}

function renderViewAllButton(productCount, currentPageNumber, itemsPerPage) {
	var pageCount = Math.ceil(productCount/itemsPerPage);
	if(pageCount == 1) return "";
	var html = "";
	if(currentPageNumber == "all") {
		html += '<nobr>&nbsp;&nbsp;<a href="#" onClick="return(displayProductArray(productArray, 1, itemsPerPage))" class="viewAllLink">Return to Paged View</a>&nbsp;&nbsp;</nobr>';
	} else {
		html += '<nobr>&nbsp;&nbsp;<a href="#" onClick="return(displayProductArray(productArray, \'all\', itemsPerPage))" class="viewAllLink">View All Items</a>&nbsp;&nbsp;</nobr>';
	}
	return html;
}

function renderSortLinks(currentPageNumber, sortBy) {
	if(currentPageNumber != "all" && currentPageNumber > 1) currentPageNumber = 1;
	var html = "";
	html += '<table border="0" cellpadding="0" cellspacing="0"><tr>';
		
	if(sortBy == "price") {
		html += '<td>Sort By Price</td>';
	} else {
		html += '<td><a href="#" class="sortLink" onClick="return(displayProductArray(productArray, \'' +  currentPageNumber + '\',itemsPerPage, \'price\'))">Sort By Price</a></td>';
	}
	
	if(sortBy == "title") {
		html += '<td>Sort By Name</td>';
	} else {
		html += '<td><a href="#" class="sortLink" onClick="return(displayProductArray(productArray, \'' + currentPageNumber + '\',itemsPerPage, \'title\'))">Sort By Name</a></td>';
	}

	if(sortBy == "rating") {
		html += '<td>Sort By Rating</td>';
	} else {
		html += '<td><a href="#" class="sortLink" onClick="return(displayProductArray(productArray, \'' + currentPageNumber + '\',itemsPerPage, \'rating\'))">Sort By Rating</a></td>';
	}
	
	if(sortBy == "bestseller") {
		html += '<td>Sort By Bestseller</td>';
	} else {
		html += '<td><a href="#" class="sortLink" onClick="return(displayProductArray(productArray, \'' +  currentPageNumber + '\',itemsPerPage, \'bestseller\'))">Sort By Bestseller</a></td>';
	}
	
	html += '</tr></table>';
	
	return html;
}

function renderSortByNameLink(currentPageNumber, sortBy) {
	if(currentPageNumber != "all" && currentPageNumber > 1) currentPageNumber = 1;
	var html = "";
		
	if(sortBy == "title") {
		html += '<span class="sortLinkOn"><nobr><img src="http://site.thegiftboutique.com/pd_pagingEngine/images/arrowDown.gif">Sort By Name</nobr></span>';
	} else {
		html += '<nobr><img src="http://site.thegiftboutique.com/pd_pagingEngine/images/arrowSmall.gif"><a href="#" class="sortLink" onClick="return(displayProductArray(productArray, \'' + currentPageNumber + '\',itemsPerPage, \'title\'))">Sort By Name</a></nobr>';
	}

	return html;
}

function renderSortByPriceLink(currentPageNumber, sortBy) {
	if(currentPageNumber != "all" && currentPageNumber > 1) currentPageNumber = 1;
	var html = "";
		
	if(sortBy == "price") {
		html += '<span class="sortLinkOn"><nobr><img src="http://site.thegiftboutique.com/pd_pagingEngine/images/arrowDown.gif">Sort By Price</nobr></span>';
	} else {
		html += '<nobr><img src="http://site.thegiftboutique.com/pd_pagingEngine/images/arrowSmall.gif"><a href="#" class="sortLink" onClick="return(displayProductArray(productArray, \'' +  currentPageNumber + '\',itemsPerPage, \'price\'))">Sort By Price</a></nobr>';
	}
	
	return html;
}

function renderSortLinksMenu(currentPageNumber, sortBy) {
	if(currentPageNumber != "all" && currentPageNumber > 1) currentPageNumber = 1;
	var html = "";
	html += '<select onChange="eval(this.options[this.selectedIndex].value)" class="sortDDMenu" style="COLOR: #6699FF; font-family: arial, verdana, sans-serif; font-size: 10px; font-weight:700; width:110px;">';
	html += '<option>Sort Items:</option>';
		
	if(sortBy == "title") {
		html += '<option selected>By Name</option>';
	} else {
		html += '<option value="displayProductArray(productArray, \'' +  currentPageNumber + '\',itemsPerPage, \'title\')">By Name</option>';
	}

	if(sortBy == "price") {
	html += '<option selected>By Price</option>';
	} else {
		html += '<option value="displayProductArray(productArray, \'' +  currentPageNumber + '\',itemsPerPage, \'price\')">By Price</option>';
	}

	if(sortBy == "bestseller") {
		html += '<option>By Bestseller</option>';
	} else {
		html += '<option value="displayProductArray(productArray, \'' +  currentPageNumber + '\',itemsPerPage, \'bestseller\')">By Bestseller</option>';
	}

	if(sortBy == "brand") {
		html += '<option selected>By Brand</option>';
	} else {
		html += '<option value="displayProductArray(productArray, \'' +  currentPageNumber + '\',itemsPerPage, \'brand\')">By Brand</option>';
	}

	html += '</select>';
	
	return html;
}

function renderPreviousNextProductButtons(currentProductId) {
	var html = "";
	var productListString = pdGetCookie("pd_currentProductList");
	if(!isBlank(productListString)) {
		if(String(productListString).indexOf(currentProductId) > -1) {
			var productArray = String(productListString).split(",");
			var item = new Object();
			var i = 0;
			for(item in productArray) {
				if(currentProductId == productArray[item]) break;
				i++;
			}
			if(i > 0) {
				html += '<a href="#" onclick="return(jumpToUrl(\'' + productArray[i-1] + '.html\'))">PREVIOUS</a>';
			}
			if(i+1 < productArray.length) {
				html += '<a href="#" onclick="return(jumpToUrl(\'' + productArray[i+1] + '.html\'))">NEXT</a>';
			}
		}
	}
	
	document.getElementById("previousNextProductButtons").innerHTML = html;
	return false;
}
