/*
	These are global variables that define
	how many items to view at once (itemsPerPage),
	and our current position (curPos).

	"itemsPerPage" is an accurate descriptor only
	within the context of the application, since
	we are not actually browsing "pages".
*/

itemsPerPage = 2;
curPos = 0;
priceFilter = 0;
/*
	The next code block is the jQuery implementation
	of the XMLHttpRequest object. The outer function
	specifies that when the page loads we want to
	execute a function:

	$(function() {});

	Now within that function, make a request for an
	XML file, and upon success, execute another
	function. The data we get back is assigned to
	"xmlData". This is only available within the
	scope of this function. By assigning it to
	"xmlDataSet", we can now use the XML data
	throughout the application. Now we call the
	"browseXML()" function, which will manage
	our browse functionality.

	NOTE: In a production environment, you may want
	to account for any instance when the request
	for the XML fails. Please see the jQuery
	documentation on the $.ajax() object.
*/

$(function() {

	//var catalog = $.getURLParam("catalog");
	var category = $.getURLParam("category");

	//var node = $.getURLParam("node");

	if(priceFilter)
		pricetext = "&filter=" + priceFilter;
	else
		pricetext = "";
		
	
	if (category != null) {
		var datastring = "category=" + category + "&node=" + node + "&xmlfile=" + catalog + pricetext; 
		//var datastring = "category=" + category + "&node=advertisercategory&xmlfile=Starbucks_Store-Product_Catalog.xml";
	}
	else
		//var datastring = "xmlfile=Starbucks_Store-Product_Catalog.xml";
		var datastring = "xmlfile=" + catalog + pricetext;
	
	//alert("datastring is " + datastring);
	//alert("priceFilter is " + priceFilter);
	$.ajax({
		type: "GET",
		//url: "books.xml",
		url: "get_cj_category.php",
		//data: "category=Coffee and Tea>Accessories>>&xmlfile=Kitchen_Collection-LGC_Product_Catalog.xml",
		//data: "xmlfile=Starbucks_Store-Product_Catalog.xml",		
		data: datastring,
		dataType: "xml",
		success: function(xmlData)
		{
			xmlDataSet = xmlData;
			
			browseXML();
		}
	});
});


function dobrowseXML()
{

	if(priceFilter)
		pricetext = "&filter=priceFilter";
	else
		pricetext = "";
	
	var datastring = "xmlfile=" + catalog + pricetext;

	itemsPerPage = 2;
	curPos = 0;
	
	//alert("Filtering psss riceFilter is " + datastring);
		$.ajax({
		type: "GET",
		//url: "books.xml",
		url: "get_cj_category.php",
		//data: "category=Coffee and Tea>Accessories>>&xmlfile=Kitchen_Collection-LGC_Product_Catalog.xml",
		//data: "xmlfile=Starbucks_Store-Product_Catalog.xml",		
		data: datastring,
		dataType: "xml",
		success: function(xmlData)
		{
			xmlDataSet = xmlData;
			
			browseXML();
		}
	});

}

function dobrowseXML2(min,max)
{

	if(priceFilter)
		pricetext = "&filter=price > '" + min + "' and price < '" + max + "'";
	else
		pricetext = "";
	
	var datastring = "xmlfile=" + catalog + pricetext;

	itemsPerPage = 2;
	curPos = 0;
	
	//alert("Filtering psss riceFilter is " + datastring);
		$.ajax({
		type: "GET",
		//url: "books.xml",
		url: "get_cj_category.php",
		//data: "category=Coffee and Tea>Accessories>>&xmlfile=Kitchen_Collection-LGC_Product_Catalog.xml",
		//data: "xmlfile=Starbucks_Store-Product_Catalog.xml",		
		data: datastring,
		dataType: "xml",
		success: function(xmlData)
		{
			xmlDataSet = xmlData;
			
			browseXML();
		}
	});

}


function browseXML()
{
/*
	In jQuery, you can choose an XML data source, and
	then select the name of the node elements you want
	to grab. The results can be assigned to a variable,
	which can further be refined if needed. This is
	demonstrated with the "resultSetLength" variable.

	"strToAppend" will be the sum of our HTML that
	will eventually (re)populate our "widget" DIV when
	a user browses.
*/
	//alert("priceFilter is " + priceFilter);
	
	resultSetLength = $("product",xmlDataSet).length;
	strToAppend = "<p>";
	
	//alert("items returned " + resultSetLength);
	
/*
	Determine what set of matched results the user is
	currently browsing. Because we started "curPos" at 0,
	we must add 1 to get the actual current position.
*/
	if (curPos + itemsPerPage > resultSetLength)
	{
		showingThrough = resultSetLength;
	}
	else
	{
		showingThrough = parseInt(curPos + itemsPerPage);
	}
	strToAppend += "Showing <b>" + parseInt(curPos + 1) + "</b> through <b>" + showingThrough + "</b> of <b>" + resultSetLength + "</b>";
	//strToAppend += " books from the New York Times&reg; Best Sellers List"
	strToAppend += "</p>";
	
	var category = $.getURLParam("category");
	if (category != null) 
		strToAppend += "Category: " + category;
	
//	strToAppend += "<a href=\"" + baseurl + "\">Home</a>";
	strToAppend += "<p>Show Me:&nbsp; ";
/*
	Let's give the user a preference for how many results
	they want to view per page. When the user clicks the link,
	we modify the value of "itemsPerPage" to accomplish this task.
	Then we send the user back to the beginning of the result set,
	and call the "browseXML()" function again.
*/
	//strToAppend += "<a href='#' onclick='itemsPerPage = 2;curPos = 0;browseXML();return false;'>2 at a time</a> &nbsp;|&nbsp; ";
	//strToAppend += "<a href='#' onclick='itemsPerPage = 3;curPos = 0;browseXML();return false;'>3 at a time</a> &nbsp;|&nbsp; ";
	//strToAppend += "<a href='#' onclick='itemsPerPage = 4;curPos = 0;browseXML();return false;'>4 at a time</a> &nbsp;|&nbsp; ";
	strToAppend += "<a href='#' onclick='itemsPerPage = " + resultSetLength + ";curPos = 0;browseXML();return false;'>All</a></p>";
	strToAppend += "<p>";
/*
	If the user wants to view all the results, then we
	don't want to give them the capability to browse.
	Show all the results, or display the navigation
	for browsing depending on where the user is at
	within the result set.
*/
	if (itemsPerPage != resultSetLength)
	{
		if (curPos == 0) // First page. Go forward only.
		{
			strToAppend += "<font color=\"grey\">&laquo; Previous Items</font>";
			strToAppend += " &nbsp;|&nbsp; ";			
			strToAppend += "<a href='#' onclick='curPos += " + itemsPerPage + ";browseXML();return false;'>Next Items &raquo;</a>";
		}
		if (curPos > 0 && parseInt(curPos + itemsPerPage) < resultSetLength) // Somewhere inbetween.
		{
			strToAppend += "<a href='#' onclick='curPos -= " + itemsPerPage + ";browseXML();return false;'>&laquo; Previous Items</a>";
			strToAppend += " &nbsp;|&nbsp; ";
			strToAppend += "<a href='#' onclick='curPos += " + itemsPerPage + ";browseXML();return false;'>Next Items &raquo;</a>";
		}
		if (parseInt(curPos + itemsPerPage) >= resultSetLength) // Last page. Go back only.
		{
			strToAppend += "<a href='#' onclick='curPos -= " + itemsPerPage + ";browseXML();return false;'>&laquo; Previous Items</a>";
		}
	}

	strToAppend += "</p>";
	//strToAppend += "<p> - - - </p>";
/*
	This is the real meat of the jQuery functionality.
	We are performing 3 major tasks:

	1.) Grab each "title" node from our XML data source that
		falls below a certain index. The ":lt" is the expression
		that performs this task.
	2.) Now of those title nodes, filter out those that are
	    greater than a certain index. This will give us our
	    new result set, which is displayed to the user with
		"Showing X through Y of Z". The ":gt" expression is
		used in conjunction with the "filter()" method to
		accomplish this task.
	3.) For each set of results we refined, we now want to
	    execute a function. We know we need the "author",
		"publisher", and the "ISBN" for our book, but we
		only searched for "title". With the "each()" method
		we have access to the index (i), just like in a JavaScript
		"for loop". So we can get each one of those nodes
		that fall at the same index as our "title" nodes.
		However, we always start our results array at 0,
		so we want to add our current position to that
		in order to get the proper node. We use the ":eq"
		expression to accomplish this task.
*/
	//if(priceFilter> 0)
	//	strToAppend += "Filter by Price" + priceFilter + "<br>";
	
	strToAppend += "<ul id=\"listproduct_narrow\">";
	$("product:lt(" + parseInt(curPos + itemsPerPage) + ")",xmlDataSet).filter(":gt(" + parseInt(curPos - 1) + ")").each(function(i) {
//		strToAppend += "<img src=\"" + $("imageurl:eq(" + parseInt(curPos + i) + ")",xmlDataSet).text() + "\"></p>";	
//		strToAppend += "Name: " + $("name:eq(" + parseInt(curPos + i) + ")",xmlDataSet).text() + "</p>";
//		strToAppend += "Description:" + $("description:eq(" + parseInt(curPos + i) + ")",xmlDataSet).text() + "</p>";
//		strToAppend += "Price: " + $("price:eq(" + parseInt(curPos + i) + ")",xmlDataSet).text() + "</p>";
//		strToAppend += "Category: <a href=\"" + baseurl + "?category=" + $("advertisercategory:eq(" + parseInt(curPos + i) + ")",xmlDataSet).text() + "\">" + $("advertisercategory:eq(" + parseInt(curPos + i) + ")",xmlDataSet).text() + "</a></p>";
//		strToAppend += "</p>";
		
			//intPrice = parseInt($("price:eq(" + parseInt(curPos + i) + ")",xmlDataSet).text());
			
			//if(intPrice < priceFilter || priceFilter == 0 )
			//{
			
			strDescription = $("description:eq(" + parseInt(curPos + i) + ")",xmlDataSet).text();
			
			if(strDescription.length>160)
				strDescription = strDescription.substr(0,100) + '...';
			
			strToAppend +=  "<li class=\"best_seller\">";
			strToAppend +=	"<a href=\"" +  $("buyurl:eq(" + parseInt(curPos + i) + ")",xmlDataSet).text() + "\">";
			strToAppend += "<img height=100 src=\"" + $("imageurl:eq(" + parseInt(curPos + i) + ")",xmlDataSet).text() + "\">";
			strToAppend +=	"</a>" + "<br>";
			strToAppend +=	"<a href=\"" +  $("buyurl:eq(" + parseInt(curPos + i) + ")",xmlDataSet).text() + "\">" + $("name:eq(" + parseInt(curPos + i) + ")",xmlDataSet).text()  + "</a>" + "<br>";
			//strToAppend +=	"<span class=\"news-text\">" + $("description:eq(" + parseInt(curPos + i) + ")",xmlDataSet).text();
			
			strToAppend +=	"<span class=\"news-text\">" + strDescription;
			

			strToAppend +=	"<br><b>Price:</b> $" + $("price:eq(" + parseInt(curPos + i) + ")",xmlDataSet).text(); 

			if( $("price:eq(" + parseInt(curPos + i) + ")",xmlDataSet).text() != $("price:eq(" + parseInt(curPos + i) + ")",xmlDataSet).text())
				strToAppend +=	"<br><b>Sales Price:</b> $" + $("price:eq(" + parseInt(curPos + i) + ")",xmlDataSet).text() + "<br>";

			//strToAppend +=	"Ships and sold by <br>";
			strToAppend +=	"<img src=\"" +  $("impressionurl:eq(" + parseInt(curPos + i) + ")",xmlDataSet).text() + "\">";				
			
			strToAppend +=	"<a href=\"" +  $("buyurl:eq(" + parseInt(curPos + i) + ")",xmlDataSet).text() + "\">";
			strToAppend += "<img  src=\"http://www.abt.com/images/prod_cart_ico.gif\">";
			strToAppend +=	"</a>" + "<br>";
			
			strToAppend +=	"<a href=\"" +  $("buyurl:eq(" + parseInt(curPos + i) + ")",xmlDataSet).text() + "\">";
			strToAppend += "Read more";
			strToAppend +=	"</a>" + "<br>";

			
		//	strToAppend +=	"<br>keywords:" +  $("advertisercategory:eq(" + parseInt(curPos + i) + ")",xmlDataSet).text()   + "</span>";	  	 
			strToAppend +=	"</span>";	  	 
/*			if(node=="advertisercategory")
				strToAppend += "Category: <a href=\"" + baseurl + "?category=" + $("advertisercategory:eq(" + parseInt(curPos + i) + ")",xmlDataSet).text() + "\">" + $("advertisercategory:eq(" + parseInt(curPos + i) + ")",xmlDataSet).text() + "</a>";
			else
				strToAppend += "Category: <a href=\"" + baseurl + "?category=" + $("thirdpartycategory:eq(" + parseInt(curPos + i) + ")",xmlDataSet).text() + "\">" + $("thirdpartycategory:eq(" + parseInt(curPos + i) + ")",xmlDataSet).text() + "</a>";
*/
			strToAppend += "</p>";	
			strToAppend += "</li>";		
			//}
	});
	
	strToAppend += "</ul>";

	//strToAppend += "<p> - - - </p>";
/*
	Populate our DIV with the HTML we have constructed.
*/
	$("#widget").html(strToAppend);
}
/*
	Obfuscated, and without comments, this script would
	probably only be about 2 KB.
*/

