//	Friends of ImaginEERIEing
//	This code loads up a local CSV file containing
//	information about haunts that use our software and ideas
//	to display to visitors.

//	First, some definitions.

//	Column abstractions

	var HAUNT_ID = 0;
	var HAUNT_NAME_ID = 1;
	var HAUNT_GORDO = 2;
	var HAUNT_MIRROR = 3;
	var HAUNT_CRYPT = 4;
	var HAUNT_BLACK_LIGHT = 5;
	var HAUNT_OTHER_IDEA = 6;
	var HAUNT_YORICK = 7;
	var HAUNT_FROSTY = 8;
	var URL_ID = 9;
	var IMAGE_URL_ID = 10;
	var HAUNT_REVIEW_ID = 11;

//	Default settings
	//	friends_filter can be set to
	//	HAUNT_YORICK, HAUNT_MIRROR, HAUNT_CRYPT, 
	//	HAUNT_BLACK_LIGHT, or HAUNT_OTHER_IDEA
	//	to only show items of that type.
	var friends_filter = -1;

	//	friends_header
	//	This string is shown if at least one
	//	item matches.
	var friends_header = '';

	//	set friends_debug to TRUE to see a CSV listing
	var friends_debug = false;

	//	number of friends found
	var friends_found = 0;


//	This function loads the friends from the CSV file.
//	It then renders them nicely and puts them in a div
//	with an ID of "gallery."

function load_friends( the_filter )
{
	friends_filter = the_filter;
	var myAjax = new Ajax.Request(
		'http://www.imagineerieing.com/friends.csv',
		{ method: 'get', parameters: '', onComplete: parseFriendsCSV });
}

function parseFriendsCSV( originalRequest )
{
	var contents = originalRequest.responseText;
	var rows = contents.split( "\n" );
	var c = '<table border="1">';
	var d = '';
	for( var i=0; i<rows.length; i++ )
	{
		//	rows[i] = '",' + rows[i] + ',"';
		rows[i] = rows[i].slice(1, rows[i].length - 1);
		var row = rows[i].split('","');
		if (row.length > 1)
		{
			c = c + '<tr valign="top">';
			for( var j=0; j<row.length; j++)
				c = c + '<td>' + row[j] + '</td>';
			c = c + '</tr>';
			if (i > 0) d = makeHauntBlock( row ) + d;
		}
	}
	c = c + '</table>';
	d = '<div class="friends-display">' + d + '</div>';
	if (friends_debug) d = d + c;
	if (friends_found > 0) d = friends_header + d;
	$('gallery').innerHTML = d;
}

function makeHauntBlock( row )
{
	//	Respect the filter
	if (friends_filter > 0 && row[friends_filter] == '') return '';

	//	Add the friend
	friends_found = friends_found + 1;
	var hb = '<div class="haunt-info">';
	hb = hb + '<div class="haunt-name"><a href="' + row[URL_ID] + '">' + row[HAUNT_NAME_ID] + '</a></div>';
	hb = hb + '<div class="haunt-thumb-box">';
	hb = hb + '<a href="' + row[URL_ID] + '">';
	hb = hb + '<img src="' + row[IMAGE_URL_ID] + '" alt="' + row[HAUNT_NAME_ID] + '" class="haunt-thumb" />';
	hb = hb + '</a></div><div class="haunt-review">' + row[HAUNT_REVIEW_ID] + '</div></div>';
	return hb;
}
