$(document).ready(function() {

	//set_cookie("history","",0);

	// All functions used come from cookies.js.
	// First, initialize a special cookie-format array.

	var oldHistoryArray = init_cookie_array();

	// Read the array in the cookie already, if there is one.

	oldHistoryArray = get_cookie_array("history");

	//alert(oldHistoryArray.toString());

	if (typeof historyID != "undefined" && typeof historyName != "undefined" && typeof historyPrice != "undefined") {

		var newHistoryItem = historyID + "|" + historyName + "|" + historyPrice;
		var newHistoryArray = [newHistoryItem];
		newHistoryArray = newHistoryArray.concat($.grep(oldHistoryArray,
			function(val, key) {

				if (val == newHistoryItem) {

					return false;
				}

				else {

					return true;
				}
			})
		);
	}

	else {

		newHistoryArray = oldHistoryArray;
	}

	// Next, populate the history scroller with the history data

	var historyContents = "";
	var historyItemSplit = "";

	$.each(newHistoryArray, function() {

			historyItemSplit = this.toString();
			historyItemSplit = historyItemSplit.split("|");
			historyItemID = historyItemSplit[0];
			historyItemName = historyItemSplit[1];
			historyItemPrice = historyItemSplit[2];

			historyContents += '						<div class="contentClientThumb" id="contentClientThumb' + historyItemID + '">' + "\n" + '<a href="product' + historyItemID.substr(2) + '.html#nav" class="thumbImageLink"><img src="images/history_thumbs/' + historyItemID + '.png" alt="' + historyItemName + '" /></a> <a href="" class="thumbLink">' + historyItemName + " " + historyItemPrice + '</a>' + "\n" + '<a href="#" class="removeThumbLink"><img src="images/blue_circle_x.png" alt="X" /></a>' + "\n" + '						</div>' + "\n\n";
		}

	);

	$("#historyScrollerDiv").html(historyContents);

	// Create the delete-this-item function for each X button

	$("a.removeThumbLink").click(function() {

			// Find the ID of the item we're deleting

			deletedID = $(this).parent().attr("id");
			deletedID = deletedID.substr(18);

			// Delete the item from the history array

			historyIndex = -1;
			arrayIndex = 0;

			$.each(newHistoryArray, function() {

					if (this.toString().substr(0, this.toString().indexOf("|")) == deletedID) {

						historyIndex = arrayIndex;
						return false;
					}

					else {

						arrayIndex++;
					}
				}
			);

			newHistoryArray.splice(historyIndex, 1);

			// Reset the cookie

			set_cookie_array("history", newHistoryArray, 60*60*24*7);

			// Hide the item on the page

			$(this).parent().fadeTo(250,0).hide(300);

			return false;
		}
	);

	// Finally, set the new cookie to last a week

	set_cookie_array("history", newHistoryArray, 60*60*24*7);

	var oldHistoryArray = init_cookie_array();

	get_cookie_array("history", oldHistoryArray);

	// Now build the history scroller functionality

	var historyScrollPage = 0;

	var numThumbs = ($("#historyScrollerDiv > div.contentClientThumb").size());
	var numHistoryPages = Math.floor(numThumbs / 4);

	if ((numThumbs % 4) > 0) {

		numHistoryPages += 1;
	}

	$("#historyScrollLeftButton").click( function() {

			if (historyScrollPage == 0) {

				return false;
			}

			else {

				historyScrollPage--;
				newLeft = (historyScrollPage * -400) + "px";
				$("#historyScrollerDiv").animate({left: newLeft}, "normal");
			}

			return false;
		}
	);

	$("#historyScrollRightButton").click( function() {

			if (historyScrollPage == numHistoryPages - 1) {

				return false;
			}

			else {

				historyScrollPage++;
				newLeft = (historyScrollPage * -400) + "px";
				$("#historyScrollerDiv").animate({left: newLeft}, "normal");
			}

			return false;
		}
	);
});
