var isLoaded = false;

//global var of products
var prodList = new Array()

var item = null;
var lastItemName = '';

function addItem() {

	if (item.on_sale == null || item.on_sale.toLowerCase() == 'false') {
		item.on_sale = false;
	} else {
		item.on_sale = true;	
	}

	if (item.is_new == null || item.is_new.toLowerCase() == 'false') {
		item.is_new = false;
	} else {
		item.is_new = true;
	}
	
	if (item.soldOut == null || item.soldOut.toLowerCase() == 'false') {
		item.soldOut = false;
	} else {
		item.soldOut = true;
	}

	prodList[prodList.length] = new Object();

	prodList[prodList.length-1] = item;
}

function newItem() {

	item = new Object();
	item.common_name = null;
	item.sci_name = null;
	item.category = null;
	item.price = null;
	item.description = null;
	item.image = null;
	item.on_sale = null;
	item.is_new = null;
	item.originalID = null;
	item.availPotted = null;

}

var allLets = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';

function writeProductList(whichCat, byWhich) {

	var listML = '';

	var lastL = '';

	var usedLets = '';

	if (byWhich == null) {
		byWhich = 'common';
	}

	var cnt = -1;

	for (var i=0;i<prodList.length ;i++ ) {
		if (whichCat == 'all' || prodList[i].category == whichCat) {

			cnt++;

			var thisL = '';

			if (byWhich != 'sci') {
				thisL = prodList[i].common_name.charAt(0).toUpperCase();
			} else {
				thisL = prodList[i].sci_name.charAt(0).toUpperCase();
			}

			if (thisL != lastL) {

				listML += '<div title="The plants in this section begin with ' + thisL + '" style="position:relative;clear:both;margin-top:' + (cnt==0?cnt:30) + 'px;height:43px;" id="qN_' + thisL + '"><div style="position:relative;float:left;border:1px solid #B2F7B2;"><div style="height:36px;width:36px;background-image:url(\'/img/alphabet.gif\');background-position:-5px -' + (allLets.indexOf(thisL)*36 + 2) + 'px;"></div></div><div style="position:relative;float:left;padding-left:120px;padding-top:10px;"><a title="click to jump to the top of the page" href="#bodyTop" style="color:#B2F7B2;font-size:11px;font-family:trebuchet ms;">^ top</a></div></div>';
				usedLets += lastL;
				lastL = thisL;
			}

			var name1 = prodList[i].common_name;
			var name2 = prodList[i].sci_name;

			if (byWhich == 'sci') {
				name1 = prodList[i].sci_name;
				name2 = prodList[i].common_name;
			}			

			if (prodList[i].image == null || prodList[i].image == '') {
				prodList[i].image = 'blank.gif';
			}

			listML += '<div style="position:relative;clear:both;border:1px solid #B2F7B2;margin-top:6px;" id="item_' + prodList[i].originalID + '">' +
					  '<div style="position:relative;height:150px;">' +
					  '<div style="position:relative;float:left;background-color:#B2F7B2;padding:3px;font-weight:bold;">' + name1 + '&nbsp; &nbsp; (' + name2 + ')</div>' +
					  '<a onMouseOver="showDiv(\'click_' + i + '\');" onMouseOut="hideDiv(\'click_' + i + '\');" href="javascript:showImage(' + i + ',\'/img/' + prodList[i].image + '\', document.getElementById(\'item_' + cnt + '_img\'), \'' + replace(name1, '\'', '\\\'') + '\', \'' + replace(name2, '\'', '\\\'') + '\', ' + i + ');" id="item_' + cnt + '_img"><img title="click to enlarge this image of ' + name1 + '" style="position:relative;float:right;" src="/img/' + replace(prodList[i].image, '.jpg', '_T.jpg') + '"></a><br>' +		
					  '<div class="click2Enlarge" onMouseOver="showDiv(\'click_' + i + '\');" onMouseOut="hideDiv(\'click_' + i + '\');" id="click_' + i + '" onClick="showImage(' + i + ',\'/img/' + prodList[i].image + '\', document.getElementById(\'item_' + cnt + '_img\'), \'' + replace(name1, '\'', '\\\'') + '\', \'' + replace(name2, '\'', '\\\'') + '\', ' + i + ');">click&nbsp;to&nbsp;enlarge</div>' +
					  '<div style="position:relative;float:left;clear:left;color:#ffffff;font-family:trebuchet ms;font-size:13px;margin-top:5px;margin-bottom:5px;	padding-left:5px;width:370px;">' + replace(prodList[i].description, '\'', '\\\'') + '</div>' +					  
					  '</div>';
				
					  
			if (prodList[i].soldOut == null || prodList[i].soldOut == false) {			

				if (prodList[i].availPotted == 'true') {
					
					listML += '<div style="position:absolute;top:125px;left:30px;width:215px;text-align:left;color:#ffffff;font-size:12px;font-family:trebuchet ms;line-height:18px;" title="click to add 10 ' + name1 + ' to your cart"><div style="position:relative;float:left;padding-top:3px;">bare root: ' + formatDollars(getUnitPrice(prodList[i])) + '&nbsp;</div>' + getButtonML('add', 'addToCart(\'' + replace(name1, '\'', '\\\'') + '\', ' + prodList[i].originalID + ', document.getElementById(\'item_' + prodList[i].originalID + '\'))', 'l') + '</div>';
					listML += '<div style="position:absolute;top:125px;left:190px;width:135px;text-align:left;color:#ffffff;font-size:12px;font-family:trebuchet ms;line-height:18px;" title="click to add 10 ' + name1 + ' to your cart"><div style="position:relative;float:left;padding-top:3px;">potted: ' + formatDollars(getPottedUnitPrice(prodList[i])) + '&nbsp;</div>' + getButtonML('add', 'addToCart(\'' + replace(name1, '\'', '\\\'') + '\', ' + prodList[i].originalID + ', document.getElementById(\'item_' + prodList[i].originalID + '\'), true)', 'l') + '</div>';

				} else {
					listML += '<div style="position:absolute;top:125px;left:30px;width:215px;text-align:left;color:#ffffff;font-size:12px;font-family:trebuchet ms;line-height:18px;" title="click to add 10 ' + name1 + ' to your cart"><div style="position:relative;float:left;padding-top:3px;">bare root: ' + formatDollars(getUnitPrice(prodList[i])) + '&nbsp;</div>' + getButtonML('add', 'addToCart(\'' + replace(name1, '\'', '\\\'') + '\', ' + prodList[i].originalID + ', document.getElementById(\'item_' + prodList[i].originalID + '\'))', 'l') + '</div>';
				}
			
			} else {
				listML += '<div style="position:absolute;top:125px;left:30px;width:215px;text-align:left;color:#ffffff;font-size:12px;font-family:trebuchet ms;line-height:18px;font-weight:bold;color:#C77979;">SOLD OUT until next season</div>';
			}

			listML += '</div>';

		}

	}

	if (usedLets.indexOf(lastL == -1)) {
		usedLets += lastL;
	}

	listML += '<br><br>';

	listML = '<div style="position:relative;float:left;border:1px solid #B2F7B2;border-bottom:0px;font-family:trebuchet ms;font-size:12px;">' +
			 '<span style="color:#B2F7B2;padding-left:3px;">Sort by: </span>' +			 
			 '<a title="' + (byWhich=='common'?'this list is sorted by common name':'sort this list by common name') + '" class="sBA' + (byWhich=='common'?'_on':'') + '" id="common_sort_option" style="position:relative;padding-left:5px;padding-right:5px;" href="javascript:if(sortProducts(\'common\') || (document.getElementById(\'prodListDiv\').innerHTML = writeProductList(\'' + whichCat + '\', \'common\'))){void(0);}">Common name</a>' +
			 '<a title="' + (byWhich=='sci'?'this list is sorted by latin name':'sort this list by latin name') + '" class="sBA' + (byWhich=='sci'?'_on':'') + '" id="sci_sort_option" style="position:relative;border-left:1px solid #B2F7B2;padding-left:5px;padding-right:5px;" href="javascript:if(sortProducts(\'sci\') || (document.getElementById(\'prodListDiv\').innerHTML = writeProductList(\'' + whichCat + '\', \'sci\'))){void(0);}">Latin name</a>' +
			 '</div>' + 
			 '<div style="position:relative;float:left;font-family:trebuchet ms;font-size:12px;color:#B2F7B2;line-height:17px;">&nbsp;&nbsp;<strong style="font-size:13px;">' + (cnt + 1) + '</strong> plant' + ((cnt+1)>1?'s':'') + ' displaying</div>' +
			 '<div style="position:relative;border:1px solid #B2F7B2;float:left;width:496px;margin-bottom:30px;"><div style="padding:2px;">' + getQuickNav(usedLets) + 
			 '</div></div>' + listML;

	return(listML);

}


function sortProducts(byWhich) {

	var lowestInd = null;
	var thisOne = null; 
	
	var sortedProds = new Array();

	for (var i=0;i<prodList.length ;i++ ) {
		
		if (prodList[i]!=null) {

			thisOne = prodList[i];

			for (var j=0;j<prodList.length ;j++ ) {

				if (prodList[j]!=null) {
	
					var currIName = '';
					var currJName = ''

					if (byWhich != 'sci') {
							
						currIName = thisOne.common_name;
						currJName = prodList[j].common_name;
						
					} else {
						
						currIName = thisOne.sci_name;
						currJName = prodList[j].sci_name;
					}

					if (currJName < currIName) {
						thisOne = prodList[j];
						lowestInd = j;
					}
				}


			}

			if (thisOne != prodList[i]) {				
				i=0;
				if (thisOne.originalID == null) {
					thisOne.originalID = sortedProds.length;
				}				
				sortedProds[sortedProds.length] = thisOne;
				prodList[lowestInd] = null;
			}			
		}

		
	}

	//and get the last one left

	for (var i=0;i<prodList.length ;i++ ) {
		if (prodList[i] != null) {
			if (prodList[i].originalID == null) {
				prodList[i].originalID = sortedProds.length;
			}
			sortedProds[sortedProds.length] = prodList[i];
		}
	}
	prodList = sortedProds;

}

function getQuickNav(usedLets) {

	var qNavML = '';

	for (var i=0;i<allLets.length;i++) {
		var thisLet = allLets.charAt(i);

		if (usedLets.indexOf(thisLet)==-1) {
			qNavML += '<div title="no entries in this list for ' + thisLet + '" style="font-family: trebuchet ms,arial,helvetica;;position:relative;float:left;width:17px;line-height:18px;background-color:#B2F7B2;text-align:center;font-size:12px;margin-right:' + (thisLet!='Z'?'2':'0') + 'px;color:#538D53;">' + thisLet + '</div>';
		} else {
			qNavML += '<a title="click to jump to the ' + thisLet + 's" class="qNA" style="position:relative;float:left;width:17px;line-height:18px;text-align:center;font-size:12px;margin-right:' + (thisLet!='Z'?'2':'0') + 'px;" href="#qN_' + thisLet + '">' + thisLet + '</a>';
		}

	}

	qNavML  = '<div style="position:relative;float:left;padding-bottom:2px;">' + qNavML + '</div>';

	return(qNavML);

}

function getSecondsParm() {
	var thisTime = new Date();
	return(thisTime.getTime());
}

document.write('<scr' + 'ipt type="text/javascript" src="js/products.js?' + getSecondsParm() + '"><' + '/sc' + 'ript>');