/*	Script to construct Upcoming show section on index page
	Author: Stephen Grabner
	Date: 17 Jan 2010
	Version: 2b1

	NOTES
	This version aims to overcome bugs in the logic of the display module (listed below) - the logic was never that well developed in v1. To do so, some assumptions have been made about the data structure. These need to listed in comments for anyone updating details.
	This logic can be used for a php version later that can be updated using an initial file, and then using a web-based interface.
	BUGS
	1. Main Gallery available is not displaying "Available" title (tag is displaying).
	2. Maintenance title not showing.
	
*/

// Setup variables

// current date, and day,month,year
var now = new Date();
// Testing - change today's date to test how script logic acts.
//now = new Date("jan 27, 2010");

// counters 
var cc = 0;
var flag = false;

// Construct date of format dd/mm/yyyy
function formatDate(oldDate) {
	var date = oldDate.getDate();
	var month = oldDate.getMonth() + 1;
	var year = oldDate.getFullYear();
	newDate = date + "/" + month + "/" + year;
	return newDate;
}

// month names
months = new Array("JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC");


// Setup calendar constructor function
// All exhibitions go into mgArtist/Title1. If there is a shared exhibition (ie 2 SEPARATE exhibitions, not group shows), add the second in mgArtist/Title2.
function Calendar(start, end, link, mgArtist1, mgTitle1, mgArtist2, mgTitle2,  swArtist, swTitle) {
	this.start = new Date(start);
	this.end = new Date(end);
	this.link = link;
	this.mgArtist1 = mgArtist1;
	this.mgTitle1 = mgTitle1;
	this.mgArtist2 = mgArtist2;
	this.mgTitle2 = mgTitle2;
	this.swArtist = swArtist;
	this.swTitle = swTitle;
}

// setup cal array - holds that year's info
cal = new Array();

// Add object data for that year
// nb: this will need to be updated for each year, or if details change thru current year.
// Format:	DATES:  Start, End, Link
//			Main 1: Artist, Main 1 Title
//			Main 2: Artist, Main 2 Title
//			Small:  Artist, Small Title
//
// Rules
// 1. Only add information when it exists. If there is no title, then don't add anything. The script will take care of it.
// 2. If a spot is not yet filled, add "Available" to its artist.
// 3. Main 2 is used for when Artists are sharing the Main space as 2 separate exhibitions. Don't add anything unless for this purpose. Group shows are placed in Main 1. If there is no Main 2 exhibition, leave empty (ie "").
// 4. For members shows, make sure the word "Member" occurs somewhere in the Artist1 field. Usually, it will be "Project Members".
// 5. If there are too many artists exhibiting, just use "Various" or "Group show", or put nothing in.

cal[0] = new Calendar(
	/* Dates  */	"1/13/2010", "1/24/2010",	"exhib1001.html", 
	/* Main 1 */	"Project Members", "First In, Best Dressed",
	/* Main 2 */	"", "", 
	/* Small  */	"", "");
	
cal[1] = new Calendar(
	/* Dates  */	"1/27/2010", "2/7/2010",	"exhib1002.html",
	/* Main 1 */	"M.Chilby", "Syd City VS Steel City",
	/* Main 2 */	"", "",
	/* Small  */	"Kathy Moon", "Kathy Moon");
	
cal[2] = new Calendar(
	/* Dates  */	"2/10/2010", "2/21/2010",	"exhib1003.html",
	/* Main 1 */	"B.Cairncross, J.Gatt, A.Clement & L.McCartney", "In & Around",
	/* Main 2 */	"", "",
	/* Small  */	"", "");
	
cal[3] = new Calendar(
	/* Dates  */	"2/24/2010", "3/7/2010", 	"exhib1004.html",
	/* Main 1 */	"Cur: S.Bessell", "Women @ Work",
	/* Main 2 */	"", "",
	/* Small  */	"", "");

cal[4] = new Calendar(
	/* Dates  */	"3/10/2010", "3/21/2010", "exhib1005.html", 
	/* Main 1 */	"S.Dale & K.Johnston", "Roadside",
	/* Main 2 */	"", "",
	/* Small  */	"Greer Taylor", "Grid");

cal[5] = new Calendar(
	/* Dates  */	"3/24/2010", "4/4/2010",	"exhib1006.html", 
	/* Main 1 */	"IDAC", "",
	/* Main 2 */	"", "",
	/* Small  */	"", "");

cal[6] = new Calendar(
	/* Dates  */	"4/7/2010", "4/18/2010",	"exhib1007.html", 
	/* Main 1 */	"L.Dargham", "",
	/* Main 2 */	"", "",
	/* Small  */	"Judy Bourke", "Judy Bourke");

cal[7] = new Calendar(
	/* Dates  */	"4/21/2010", "5/2/2010",	"exhib1008.html", 
	/* Main 1 */	"5x5x5", "",
	/* Main 2 */	"", "",
	/* Small  */	"Isil Gungor", "Isil Gungor");

cal[8] = new Calendar(
	/* Dates  */	"5/5/2010", "5/16/2010",	"exhib1009.html", 
	/* Main 1 */	"Agnieszka Golda & Martin Johnson", "Frenzy Episode",
	/* Main 2 */	"Brogan Bunt", "Dumb Living",
	/* Small  */	"", "");

cal[9] = new Calendar(
	/* Dates  */	"5/19/2010", "5/30/2010", "exhib1010.html", 
	/* Main 1 */	"Various", "Pattern Over Time",
	/* Main 2 */	"", "",
	/* Small  */	"", "");

cal[10] = new Calendar(
	/* Dates  */	"6/2/2010", "6/13/2010", "exhib1011.html", 
	/* Main 1 */	"Anthony Scerri", "Finding Motherland",
	/* Main 2 */	"Freya Jobbins", "Paraphernalia",
	/* Small  */	"", "");

cal[11] = new Calendar(
	/* Dates  */	"6/16/2010", "6/27/2010", "exhib1012.html", 
	/* Main 1 */	"George Todorovski", "multicultural II",
	/* Main 2 */	"", "",
	/* Small  */	"Jeremy Westblade", "Taking Flight");

cal[12] = new Calendar(
	/* Dates  */	"6/30/2010", "7/11/2010", "exhib1013.html",
	/* Main 1 */	"J.Aubourg", "Looking For Blue",
	/* Main 2 */	"", "",
	/* Small  */	"", "");

cal[13] = new Calendar(
	/* Dates  */	"7/14/2010", "7/25/2010", "exhib1014.html",
	/* Main 1 */	"Project Members", "Project Members Show",
	/* Main 2 */	"", "",
	/* Small  */	"", "");

cal[14] = new Calendar(
	/* Dates  */	"7/26/2010", "8/1/2010", "../maintenance.html",
	/* Main 1 */	"Maintenance", "",
	/* Main 2 */	"", "",
	/* Small  */	"", "");

cal[15] = new Calendar(
	/* Dates  */	"8/4/2010", "8/15/2010", "exhib1015.html", 
	/* Main 1 */	"A.Hulmes & P.Livingstone", "Undercurrents",
	/* Main 2 */	"", "",
	/* Small  */	"", "");

cal[16] = new Calendar(
	/* Dates  */	"8/18/2010", "8/29/2010", "exhib1016.html", 
	/* Main 1 */	"Arts & Media Staff", "TAFE Staff Show",
	/* Main 2 */	"", "",
	/* Small  */	"", "");

cal[17] = new Calendar(
	/* Dates  */	"9/1/2010", "9/12/2010", "exhib1017.html", 
	/* Main 1 */	"Wollongong Pictorial Mafia", "Shooting Gallery",
	/* Main 2 */	"", "",
	/* Small  */	"", "");

cal[18] = new Calendar(
	/* Dates  */	"9/15/2010", "9/26/2010", "exhib1018.html", 
	/* Main 1 */	"Various", "Bunkershed",
	/* Main 2 */	"", "",
	/* Small  */	"", "");

cal[19] = new Calendar(
	/* Dates  */	"9/29/2010", "10/10/2010", "exhib1019.html",
	/* Main 1 */	"Various", "Spontaneous Construction",
	/* Main 2 */	"", "",
	/* Small  */	"", "");

cal[20] = new Calendar(
	/* Dates  */	"10/13/2010", "10/24/2010", "exhib1020.html",
	/* Main 1 */	"Available", "",
	/* Main 2 */	"", "",
	/* Small  */	"", "");

cal[21] = new Calendar(
	/* Dates  */	"10/27/2010", "11/7/2010", "exhib1021.html",
	/* Main 1 */	"Various", "Lingua Loca",
	/* Main 2 */	"", "",
	/* Small  */	"", "");

cal[22] = new Calendar("11/10/2010", "11/21/2010", "exhib1022.html",
	/* Main 1 */	"ISWOW", "Snapshots of Life...",
	/* Main 2 */	"", "",
	/* Small  */	"", "");

cal[23] = new Calendar(
	/* Dates  */	"11/24/2010", "12/5/2010", "exhib1023.html",
	/* Main 1 */	"TAFE 3rd Year", "Graduation Show",
	/* Main 2 */	"", "",
	/* Small  */	"", "");

cal[24] = new Calendar(
	/* Dates  */	"12/8/2010", "12/19/2010", "exhib1024.html",
	/* Main 1 */	"ICAN", "",
	/* Main 2 */	"", "",
	/* Small  */	"", "");


// Converts a date to dd MMM format for exhibition date heading
function exDate(when) {
	eDay = when.getDate();
	eMonth = months[when.getMonth()];
	eDate = eDay + " " + eMonth;
	return eDate;
}

// Changes 1 digit number to 2 digit by adding leading 0 (not used)
function check(x) {
	if (x < 10) {
		x = "0" + x;
	}
	return x;
}

// Nicer than tba! (not used)
function noDetails(word) {
	if ((word="tba")||(word="TBA")) {
		return("Details soon");	
	}
}

// Display function - constructs the elements for each exhibition fortnight, given an exhibition index number.
// Uses arrays to store each element (including html fragments) and a loop to construct the relevant line of html.
// 3 lines are constructed: a date heading, Main Gallery details, and Small Wall details.
// IF tests remove the M/S tags if a members show is using both galleries.
 
function display(index) {
	// Builds fragments to display for each section
	
	// Set initial flag values
	var mainGallerySplit = false; // 2 main gallery exhibitions
	var smallWall = true; // small wall gallery has exhibition?
	var maintenance = false; // maintenance in gallery? 
	var wholeGallery = false; // main exhibition uses small wall as well (member's exhibitions)
	
	// Set exhibition details flags
	var artist1 = cal[index].mgArtist1;
	var title1 = cal[index].mgTitle1;
	var artist2 = cal[index].mgArtist2;
	var title2 = cal[index].mgTitle2;
	var smallArtist = cal[index].swArtist;
	var smallTitle = cal[index].swTitle;
	var year = cal[index].start.getFullYear();
	var href = cal[index].link;
	
	
	// Set initial variables based on artist fields to account for errors in updating details
	if ((artist1 == "") && (title1 == "")) {
		artist1 = "Available"
	}
	
	if ((smallArtist == "") && (smallTitle == "")) {
		smallArtist = "Available";
	}
	
	// Set unique values of flags based on details
	// Cases: artist1 is "Maintenance".
	if ((artist1 == "Maintenance") || (artist1 == "maintenance")) {
		maintenance = true;
		wholeGallery = true;
	}
	
	// Set mainGallerySplit flag
	// Cases: Maintenance isn't happening, then if there is artist2 info
	if ((maintenance == false) && (artist2 != "")) {
		mainGallerySplit = true;
	}
	
	// Set wholeGallery flag (maintenance case set above)
	// Case1: 	Last remaining case: Member's show (checks for word "member" in artist)
	if ((artist1.indexOf("Member") > -1) || (artist1.indexOf("member") > -1)) {
		if ((artist1.indexOf("Project") > -1) || (artist1.indexOf("project"))) {
			wholeGallery = true;
		}
	}
	
	// Set smallWall flag
	if (wholeGallery == true) {
		smallWall = false;
	}

	// Build exhibition dates title
	datesE = new Array();
	datesE[0] = "<h5>";
	datesE[1] = exDate(cal[index].start);
	datesE[2] = " - ";
	datesE[3] = exDate(cal[index].end);
	datesE[4] = " " + cal[index].start.getFullYear();
	datesE[5] = "</h5>";
	
	
	

	
	
	// Build Main Gallery title and artist.
	mgE1 = new Array();
	
	
	
	
	// Add opening anchor tag
	mgE1[0] = "<p><a href=\"" + year + "/" + href + "\" title=\"Click for more information\">"; 
	

	// add M sticker span if wholeGallery is false (ie when no maintenance, or member's show
	if ((wholeGallery == false) && (maintenance == false)) {
		mgE1[1] ="<span>&nbsp;M&nbsp;</span>&nbsp;";
	} else {
		mgE1[1] = "";
	}
	
	// Add title if exists
	if (title1 == "") {
		mgE1[2] = "";
		mgE1[3] = artist1;
	} else {
		mgE1[2] = title1;
		mgE1[3] = "<br />(" + artist1 + ")";
	}
	
	// if show isn't Maintenance, add artist name
	if ((maintenance == false) /* && (artist1 != "Available") */) {
		mgE1[3] = mgE1[3] + "</a></p>";
	} else {
		mgE1[3] = "Closed for Maintenance</a></p>";
	}
	
	// Add second exhbition if split main gallery.
	if (mainGallerySplit == true) {
		var mgE2 = new Array;
		mgE2[0] = "<p><a href=\"" + year + "/" + href + "#smallwall\" title=\"Click for more information\">";
		if ((title2 != "") || (title2 != "TBA") || (title2 != "tba") || title2 != "Tba") {
			mgE2[1] = title2 + "<br />";
			mgE2[2] = "(" + artist2 + ")</a></p>";
		} else {
			mgE2[1] = "";
			mgE2[2] = artist2 + "</a></p>";
		}
 	}

	// Use this section if only wanting to display the artist's name for the SW gallery.
	if (smallWall == true) {
		swE = new Array();
		swE[0] = "<p><a href=\"" + year + "/" + href + "#smallwall\" title=\"Click for more information\">";
		swE[1] = "<span>&nbsp;S&nbsp;</span>&nbsp;";
		if (smallTitle == "") {
			swE[2] = "Available";
		} else {
		swE[2] = smallTitle;
		}
		swE[2] = swE[2] + "</a></p>";
	}
	// Write the fragments to the page.
	// write dates heading
	for (var i = 0; i < 6; i++) {
		document.write(datesE[i]);
	}
	
	// write Main Gallery exhib details
	 
	for (var i = 0; i < 4; i++) {
		document.write(mgE1[i]);
	}
	
	// write Main Gallery 2nd exhibition details (if occurring)
	if (mainGallerySplit == true) {
		for (var i = 0; i < 3; i++) {
		document.write(mgE2[i]);
		}
	}	
	
	
	// Write Small Wall exhib details
	if (smallWall == true) {
		for (var i = 0; i < 3; i++) {
		document.write(swE[i]);
		}
	}
	
	// Print variables - use for testing
	// document.write();
}

// Find the first exhibition after today's date.
// Run forwards from 0 to 25, comparing start date to the current date. The first one greater than today's date is the one to display.
// Commented out docwrites are for testing purposes.


// setup
cc = 0;
flag = false;

// write starting values
//document.write("<p>NOW: " + formatDate(now) + "</p>");
//document.write("<p>Beginning...</p>");

// Main loop for determining exhibition to display
// There are 25 elements in the cal array - cc=0->24 - loop continues until cc=25 (no match), or until the flag is set indicating a match.
// If cc=25 (no match), we are either in the last exhibition period OR past the last exhibition period. A message is displayed based on either of these conditions.
while ((cc <= 24) && (flag == false)) {
	if (cal[cc].start > now) {
		flag = true;
	}
	//document.write("<p>cc: " + cc + " - Date: " + formatDate(cal[cc].start) + "</p>");
	//document.write("<p>Cond:" + ((cc <= 24) && (flag == true)) + " - Flag: " + flag +"</p>");
	cc++;
}

cc = cc - 1;
//document.write("<p>Finished Loop!</p>");
//document.write("<p>cc: " + cc + " - flag: " + flag);

// Display Heading
document.write("<h4>Upcoming Exhibitions</h4>");
// Display exhibition details
if ((cc == 25) && (now <= cal[cc-1].end)) { // in last exhibition period
	document.write("<p>Our last exhibition of the year<br />is now showing!</p>");
} else
if ((cc == 25) && (now > cal[cc-1].end)) { // after last exhibition period
	document.write("<p>Our new year programme<br />will be posted soon!</p>");
} else
display(cc);	
if (cc <= 23) {
	display(cc+1);
}

