/*	Quote Browser using JQuery
	resused code, modified by jmorgan

	Use a date function to select a quote number
	and quote source file (to reduce traffic load)
*/
var d = new Date();

//curPos = 0 + parseInt(d.getDate()/4)
curPos = 0;

/*
	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.

*/

$(function() {
	$.ajax({
		type: "GET",
		url: "js/aquotes.xml",
		dataType: "xml",
		success: function(xmlData)
		{
			xmlDataSet = xmlData;
            resultSetLength = $("quote", xmlDataSet).length;
           /* Check the length of the xml to the date to see if
             * we can increment the current position to the day
             * of the month
             */
            if(resultSetLength >= d.getDate()){
                curPos = d.getDate();
            } else {
                curPos = 0;
            }
            
			browseXML();
		}
	});
});
/*
,
		error: function (XMLHttpRequest, textStatus, errorThrown) 
		{
			alert("error with xml read " +XMLHttpRequest.statusText+ " and " + XMLHttpRequest.responseXML.toString());
		}
 */


function browseXML()
{
    
	// get the total number of quotes in the file
	//resultSetLength = $("quote", xmlDataSet).length;


	// start building the resulting HTML string for output
	strToAppend = "<p style=\"text-align:center; padding:3px; margin:0px;\">";
/*
	Determine which quote the user is
	currently viewing. 
*/

	if (resultSetLength > 0)
	{

		if (curPos == 0) // First quote. Go forward only.
		{
			strToAppend += "<input type=\"button\" value=\"Previous\" style=\"width:40%;\" onclick=\"\"></input>";
			strToAppend += " &nbsp;&nbsp; ";
			strToAppend += "<input type=\"button\" value=\"Next\" style=\"width:40%;\" onclick=\"curPos += 1;browseXML();return false;\"></input>";

		}
		if (curPos > 0 && parseInt(curPos + 1) < resultSetLength) // Somewhere inbetween.
		{
			strToAppend += "<input type=\"button\" value=\"Previous\" style=\"width:40%;\" onclick=\"curPos -= 1;browseXML();return false;\"></input>";
			strToAppend += " &nbsp;&nbsp; ";
			strToAppend += "<input type=\"button\" value=\"Next\" style=\"width:40%;\" onclick=\"curPos += 1;browseXML();return false;\"></input>";
		}
		if (parseInt(curPos + 1) >= resultSetLength) // Last page. Go back only.
		{
			strToAppend += "<input type=\"button\" value=\"Previous\" style=\"width:40%;\" onclick=\"curPos -= 1;browseXML();return false;\"></input>";
			strToAppend += " &nbsp;&nbsp; ";
			strToAppend += "<input type=\"button\" value=\"Next\" style=\"width:40%;\" onclick=\"\"></input>";
		}
                
	}

	strToAppend += "</p>";
	
/*
	Read in the XML data, filter it and extract quotes

	1.) Grab each "quote" node from the XML data that
		falls below a certain index. The ":lt" is the expression
		that performs this task.
	2.) 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 can get each one of "text" nodes
		that fall at the same index as our "quote" 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.
*/
	$("text:lt(" + parseInt(curPos + 1) + ")",xmlDataSet).filter(":gt(" + parseInt(curPos - 1) + ")").each(function(i) {
        strToAppend += "<div id=\"quote\" style=\"background-color:#FFF; padding:1em;\">"
        strToAppend += "<p>&quot;" + $(this).text() + "&quot;</p>";
		strToAppend += "<p style=\"text-align:right; font-size:9pt;\">&mdash; " + $("attr:eq(" + parseInt(curPos + i) + ")",xmlDataSet).text() + "</p>";
		strToAppend += "</p></div>";
	});
	
/*
	Populate the target DIV with the HTML.
*/
	$("#quotwidget").html(strToAppend);
}

