//
// Create a HTMLDataSet that loads its data from a table with id="productsTable" located in the current file
// The DataSet will use the cells from the first row of data as the headers for the data columns
// Setting the URL parameter to null will automatically hide the internal source element.
//
var dsArtists = new Spry.Data.HTMLDataSet(null, "artistsTable");
var pvArtists = new Spry.Data.PagedView(dsArtists, { pageSize: 20 });

var dsPaintings = new Spry.Data.XMLDataSet("data/artists-paintings.php", "/collection/artist[@name='{dsArtists::Name}']/painting");

// Add an observer on PostLoad event that removes the class 'SpryHiddenRegion' from the div structure used for displaying the data
dsArtists.addObserver({onPostLoad: function(ds, data) {
      Spry.Utils.removeClassName("spry_structure", "AEx2");
      StartFilterTimer();
}});

dsArtists.addObserver({onCurrentRowChanged: function(ds, data) {
	StartPopupsTimer();
}});

function removeIntro() {
	Spry.Utils.addClassName("artistIntro", "AExHide");
	Spry.Utils.removeClassName("artistDetail", "AExHide");
}

function FilterData()
{
	var tf = document.getElementById("namefilter");
	if (!tf.value)
	{
		// If the text field is empty, remove any filter
		// that is set on the data set.

		dsArtists.filter(null);
		StartPopupsTimer();
		// dsArtists.setCurrentRow(pvArtists.curRowID);
		return;
	}

	// Set a filter on the data set that matches any row
	// that begins with the string in the text field.

	var regExpStr = tf.value;
	
	var regExp = new RegExp(regExpStr, "i");
	
	var filterFunc = function(ds, row, rowNumber)
	{
		var str = row["Name"];
		if (str && str.search(regExp) != -1)
			return row;
		return null;
	};

	dsArtists.filter(filterFunc);
	dsArtists.setCurrentRow(pvArtists.curRowID);
	removeIntro();
	StartPopupsTimer();
}


function StartFilterTimer()
{
	if (StartFilterTimer.timerID)
		clearTimeout(StartFilterTimer.timerID);
	StartFilterTimer.timerID = setTimeout(function() { StartFilterTimer.timerID = null; FilterData(); }, 300);
}

function StartPopupsTimer()
{
	if (StartPopupsTimer.timerID)
		clearTimeout(StartPopupsTimer.timerID);
	StartPopupsTimer.timerID = setTimeout(function() { StartPopupsTimer.timerID = null; setupPopups(); }, 2000);
}
