//*************************************************
//***** COPYRIGHT 2005, PRACTICAL DATA, INC. ******
//***** - UNAUTHORIZED USE IS PROHIBITED **********
//*************************************************
if(isBlank(imageUrl)) var imageUrl = "http://www.melange4women.com/";
var lastSection = "";
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 == "topSeller") productArray[i].topSeller = subChild.title;
						if(subChild.id == "costPerServing") {
							productArray[i].costPerServing = subChild.title;
							if(isBlank(productArray[i].costPerServing)) productArray[i].costPerServing = 9999;
							productArray[i].costPerServing = productArray[i].costPerServing -0;
						}
						
					}
					subChild = subChild.nextSibling;
				}
				
				//*********************************
				//**** BUILD PRODUCT ID STRING ****
				//*********************************
				if(productArray[i].productId) productIdString += productArray[i].productId + ",";
				
			 	i++;
			 }
			  oChild = oChild.nextSibling;
		   }
		}
	}
	
	//*******************************
	//**** 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 rowItemCount = 1;
	var isOpen = false;
	var productIdList = "";
	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"><tr>\r\n';
				isOpen = true;
			}
			tempHtml += '<td valign="top"><div id="' + productArray[item].id + '" class="displayDiv">';

			if(sortByField == "costPerServing") {
				tempHtml += String(productArray[item].html).replace("costPerServingDisplay", "costPerServingDisplayOn");
			} else {
				tempHtml += String(productArray[item].html).replace("costPerServingDisplayOn", "costPerServingDisplay");
			}
			
			tempHtml += '</div></td>';
			if(rowItemCount != itemsPerRow){tempHtml += '<td><img src="' + imageUrl + '/spacer.gif" width=26></td>';}
			rowItemCount++;
			
			if(rowItemCount > itemsPerRow) {
				tempHtml += '</tr>';
				tempHtml += '<tr><td colspan="' + (itemsPerRow + 1) + '"><br></td></tr>';
				tempHtml += '<tr><td colspan="' + (itemsPerRow + 2) + '" background="' + imageUrl + '/itemRowBg.gif"><img src="' + imageUrl + '/spacer.gif" height=19></td></tr>';
				tempHtml += '<tr><td colspan="' + (itemsPerRow + 1) + '"><br></td></tr>';
				tempHtml += '</table>';
				rowItemCount = 1;
				isOpen = false;
			}
		}
		
		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 sortProductArrayByTopSeller(productArray) {
	productArray = productArray.sort(compareId);
	productArray = productArray.sort(compareTopSeller);
	sortBy = "topSeller";
	return productArray;
} 

function sortProductArrayByCostPerServing(productArray) {
	productArray = productArray.sort(compareId);
	productArray = productArray.sort(compareCostPerServing);
	sortBy = "costPerServing";
	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 compareTopSeller(item1, item2) {
	if(item1.topSeller > item2.topSeller) return -1;
	else if(item1.topSeller < item2.topSeller) 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 compareCostPerServing(item1, item2) {
	if(item1.costPerServing > item2.costPerServing) return 1;
	else if(item1.costPerServing < item2.costPerServing) return -1;
	else return 0;
}

function displayProductArray(productArray, currentPageNumber, itemsPerPageRequest, sortByField) {
	
	//*************************************
	//**** TEST FOR NOTHING TO DISPLAY ****
	//*************************************
	if(productArray.length == 0) 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 == "topSeller") productArray = sortProductArrayByTopSeller(productArray);
	else if(sortBy == "costPerServing") productArray = sortProductArrayByCostPerServing(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("previousButton").innerHTML = renderPreviousButton(productArray.length, currentPageNumber, itemsPerPage);
	document.getElementById("previousButtonBottom").innerHTML = renderPreviousButton(productArray.length, currentPageNumber, itemsPerPage);
	document.getElementById("pageNumberLinks").innerHTML = renderPageNumberLinks(productArray.length, currentPageNumber, itemsPerPage);
	document.getElementById("pageNumberLinksBottom").innerHTML = renderPageNumberLinks(productArray.length, currentPageNumber, itemsPerPage);
	document.getElementById("nextButton").innerHTML = renderNextButton(productArray.length, currentPageNumber, itemsPerPage);
	document.getElementById("nextButtonBottom").innerHTML = renderNextButton(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 = "";
	if(currentPageNumber != "all") {
		for(var i=1;i<pageCount + 1;i++) {
			if(currentPageNumber == i) {
				html += '<font class="pageNumOn">' + i + '</font>&nbsp;<font class="pageNum"> | </font>&nbsp;';
			} else {
				html += '<a href="#" class="pageNum" onClick="return(displayProductArray(productArray, ' + i + ', ' +  itemsPerPage + '))">' + i + '</a>&nbsp;<font class="pageNum"> | </font>&nbsp;';
			}
		}
		html = html.substring(0, html.length - 15);	//**** TRIM TRAILING SEPARATOR
	}
	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">Page ' + currentPageNumber + ' of ' + pageCount + '</font>';
	}
	
	return html;
}

function renderPreviousButton(productCount, currentPageNumber, itemsPerPage) {
	var html = "";
	if(currentPageNumber != "all") {	
		if(currentPageNumber > 1) {
			var previousPage = currentPageNumber - 1;
			html += '<a href="#" onClick="return(displayProductArray(productArray, ' + previousPage + ', ' +  itemsPerPage + '))"><img src="' + imageUrl + '/pageArrowPrevious.gif" border="0"></a>';
		} else {
			html += '';
		}
	}
	return html;
}

function renderNextButton(productCount, currentPageNumber, itemsPerPage) {
	var pageCount = Math.ceil(productCount/itemsPerPage);
	var html = "";
	if(currentPageNumber != "all") {	
		var nextPage = currentPageNumber + 1;
		if(nextPage <= pageCount) {
			html += '<a href="#" onClick="return(displayProductArray(productArray, ' + nextPage + ', ' +  itemsPerPage + '))"><img src="' + imageUrl + '/pageArrowNext.gif" border="0"></a>';
		} else {
			html += '';
		}
	}
	return html;
}

function renderViewAllButton(productCount, currentPageNumber, itemsPerPage) {
	var pageCount = Math.ceil(productCount/itemsPerPage);
	if(pageCount == 1) return "";
	var html = "";
	if(currentPageNumber == "all") {
		html += '<a href="#" onClick="return(displayProductArray(productArray, 1, itemsPerPage))" class="viewAllLink">Return to Paged View</a>';
	} else {
		html += '<a href="#" onClick="return(displayProductArray(productArray, \'all\', itemsPerPage))" class="viewAllLink">View All Items</a>';
	}
	return html;
}

function renderSortLinks(currentPageNumber, sortBy) {
	if(currentPageNumber != "all" && currentPageNumber > 1) currentPageNumber = 1;
	var html = "";
	if(sortBy == "title") {
		html += '<font class="sortLinkOn">Sorted By Designer</font>';
	} else {
		html += '<img src="' + imageUrl + '/sortArrows.gif">&nbsp;<a href="#" class="sortLink" onClick="return(displayProductArray(productArray, \'' + currentPageNumber + '\',itemsPerPage, \'title\'))">Sort By Designer</a>';
	}
	html += '&nbsp; &nbsp; &nbsp; &nbsp;';
	
	if(sortBy == "price") {
		html += '<font class="sortLinkOn">Sorted By Price</font>';
	} else {
		html += '<img src="' + imageUrl + '/sortArrows.gif">&nbsp;<a href="#" class="sortLink" onClick="return(displayProductArray(productArray, \'' +  currentPageNumber + '\',itemsPerPage, \'price\'))">Sort By Price</a>';
	}
	/*
	html += '&nbsp; &nbsp;';
	
	if(sortBy == "rating") {
		html += '<font class="sortLinkOn">By Top Rated</font>';
	} else {
		html += '<img src="' + imageUrl + '/sortArrows.gif">&nbsp;<a href="#" class="sortLink" onClick="return(displayProductArray(productArray, \'' + currentPageNumber + '\',itemsPerPage, \'rating\'))">By Top Rated</a>';
	}
	html += '&nbsp; &nbsp;';
	
	if(sortBy == "topSeller") {
		html += '<font class="sortLinkOn">By Top Sellers</font>';
	} else {
		html += '<img src="' + imageUrl + '/sortArrows.gif">&nbsp;<a href="#" class="sortLink" onClick="return(displayProductArray(productArray, \'' +  currentPageNumber + '\',itemsPerPage, \'topSeller\'))">By Top Sellers</a>';
	}
	html += '&nbsp; &nbsp;';
	*/
	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\'))"><img src="' + imageUrl + '/previousItemButton.gif" border="0"></a>';
			}
			if(i+1 < productArray.length) {
				html += '<a href="#" onclick="return(jumpToUrl(\'' + productArray[i+1] + '.html\'))"><img src="' + imageUrl + '/nextItemButton.gif" border="0"></a>';
			}
		}
	}
	
	document.getElementById("previousNextProductButtons").innerHTML = html;
	return false;
}

