function TodayInHistoryDialog()
{}

/** The currently viewed history entry */
TodayInHistoryDialog.currentHistoryIndex = 0;

/** The list of today in history entries for today. */
TodayInHistoryDialog.historyEntries = [];

/** 
 * Retrieves the current day in history information and displays the dialog box. 
 */
TodayInHistoryDialog.display = function()
{
	if (TodayInHistoryDialog.historyEntries.length !== 0)
	{
		$('#todayInHistory').dialog('open');			
	}
	else
	{
		var date = new Date();

		$.getJSON("http://www.newmexicohistory.org/include/todayInHistory.php?day=" + date.getDate() + "&month=" + (date.getMonth()+1),
	        function(data)
			{
				TodayInHistoryDialog.historyEntries = data;
				TodayInHistoryDialog.currentHistoryIndex = 0;
				
				if (TodayInHistoryDialog.historyEntries.length === 0)
				{
					$("#todayInHistoryContent").hide();
					$("#noTodayInHistoryContent").show();
				}
				else
				{
					$("#todayInHistoryContent").show();
					$("#noTodayInHistoryContent").hide();
					
					if (TodayInHistoryDialog.historyEntries.length == 1)
					{
						$("#previousDIH").hide();
						$("#nextDIH").hide();
					}
					else
					{
						$("#previousDIH").show();
						$("#nextDIH").show();
					}
			
					TodayInHistoryDialog.setTodayInHistory();
				}
				
				$('#todayInHistory').dialog('open');			
	      	}
		);
	}
};

/**
 * Fills in the fields in the today in history dialog.
 */
TodayInHistoryDialog.setTodayInHistory = function()
{
	var i;
	var resourceString;
	var resource;
	
	if (TodayInHistoryDialog.historyEntries.length === 0) {return;}

	var historyEntry = TodayInHistoryDialog.historyEntries[TodayInHistoryDialog.currentHistoryIndex];

	var historyDate = new Date();
	historyDate.setYear(historyEntry.historyDate.substring(0,4));
	historyDate.setMonth(parseInt(historyEntry.historyDate.substring(5,7), 10)-1);
	historyDate.setDate(historyEntry.historyDate.substring(8,10));

	$("#historyDate").text(historyDate.toDateString());
	$("#historyTitle").text(historyEntry.title);
	$("#historyContent").html(historyEntry.body);
	$("#historySource").text(historyEntry.source);

	$("#historyResources").empty();
	
	// if there are no resources, hide the resources' title div
	if (historyEntry.resources.length === 0)
	{
		$("#historyResourceTitle").hide();
	}
	else
	{
		$("#historyResourceTitle").show();
		
		resourceString = "<ul>";

		for (i = 0; i < historyEntry.resources.length; ++i)
		{
			resource = historyEntry.resources[i];
			resourceString += "<li class='resource'>";
			resourceString += "<span class='resourceAuthor'>" + resource.author + "</span>&nbsp;";
			resourceString += "<span class='resourceTitle'>" + resource.title + "</span>&nbsp;";
			resourceString += "<span class='resourceAttribution'>" + resource.attribution + "</span>&nbsp;";
			if (resource.url.length > 0)
			{
				resourceString += '<span class="resourceAttribution"><a href="' + resource.url + '" onclick="window.open(this.href, \'content\');return false;">' + resource.url + '</a></span>';
			}
			resourceString += "</li>";		
		}
		resourceString += "</ul>";
		$("#historyResources").append(resourceString);
	}
};

/**
 * Gets the next entry in today in history, or the first entry if it is at the end of the list
 */
TodayInHistoryDialog.getNext = function()
{
	$("#nextDIH").blur();

	TodayInHistoryDialog.currentHistoryIndex++;
	if (TodayInHistoryDialog.currentHistoryIndex == TodayInHistoryDialog.historyEntries.length)
	{
		TodayInHistoryDialog.currentHistoryIndex = 0;
	}
	TodayInHistoryDialog.setTodayInHistory();
};

/**
 * Gets the previous entry in today in history, or the last entry if it is at the start of the list
 */
TodayInHistoryDialog.getPrevious = function()
{	
	$("#previousDIH").blur();

	TodayInHistoryDialog.currentHistoryIndex--;
	if (TodayInHistoryDialog.currentHistoryIndex == -1)
	{
		TodayInHistoryDialog.currentHistoryIndex = (TodayInHistoryDialog.historyEntries.length-1);
	}
	TodayInHistoryDialog.setTodayInHistory();
};


