/*=========================================================================

JS document:
Magellan
© Copyright Ninesides Inc
Site design & development by ninesides.com
Note: Always practice clean coding =}

##    ## #### ##    ## ########  ######  #### ########  ########  ######  
###   ##  ##  ###   ## ##       ##    ##  ##  ##     ## ##       ##    ## 
####  ##  ##  ####  ## ##       ##        ##  ##     ## ##       ##       
## ## ##  ##  ## ## ## ######    ######   ##  ##     ## ######    ######  
##  ####  ##  ##  #### ##             ##  ##  ##     ## ##             ## 
##   ###  ##  ##   ### ##       ##    ##  ##  ##     ## ##       ##    ## 
##    ## #### ##    ## ########  ######  #### ########  ########  ######  .com

===========================================================================*/

/*****************************************
 * Create arrays
 *****************************************/

//Create array of month names
var months = [ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" ];
 
 //Create array of events for display in calendar. Heading and body must be separated by a ":"
 var events = [
	//2011
	[
		{}, //Jan
		{}, //Feb
		{}, //Mar
		{}, //Apr
		{}, //May
		{}, //Jun
		{ //Jul
			6: "BRIDEX: Brunei Darussalam<br />Stand Number E-39<br />6 - 9 July 2011",
			7: "BRIDEX: Brunei Darussalam<br />Stand Number E-39<br />6 - 9 July 2011",
			8: "BRIDEX: Brunei Darussalam<br />Stand Number E-39<br />6 - 9 July 2011",
			9: "BRIDEX: Brunei Darussalam<br />Stand Number E-39<br />6 - 9 July 2011"
		},
		{}, //Aug
		{ //Sep
			13: "DSEI: London, United Kingdom<br />13 - 16 September 2011",
			14: "DSEI: London, United Kingdom<br />13 - 16 September 2011",
			15: "DSEI: London, United Kingdom<br />13 - 16 September 2011",
			16: "DSEI: London, United Kingdom<br />13 - 16 September 2011",
			28: "MRO Europe: London, United Kingdom<br />28 - 30 September 2011",
			29: "MRO Europe: London, United Kingdom<br />28 - 30 September 2011",
			30: "MRO Europe: London, United Kingdom<br />28 - 30 September 2011"
		}, 
		{}, //Oct
		{ //Nov
			2: "AIAC Canadian Aerospace Summit: Ottawa Convention Centre, Ottawa, Ontario<br />2 - 3 November 2011",
			3: "AIAC Canadian Aerospace Summit: Ottawa Convention Centre, Ottawa, Ontario<br />2 - 3 November 2011"
			
		}, 
		{ //Dec
			6: "PowerGen International: Las Vegas, Nevada, United States<br />6 - 8 December 2011",
			7: "PowerGen International: Las Vegas, Nevada, United States<br />6 - 8 December 2011",
			8: "PowerGen International: Las Vegas, Nevada, United States<br />6 - 8 December 2011"
		}
	],
	//2012
	[
		{}, //Jan
		{ //Feb
			11: "Heli-Expo 2012: Dallas, TX<br />Visit us at Exhibit 1311<br />February 11 - 14, 2012",
			12: "Heli-Expo 2012: Dallas, TX<br />Visit us at Exhibit 1311<br />February 11 - 14, 2012",
			13: "Heli-Expo 2012: Dallas, TX<br />Visit us at Exhibit 1311<br />February 11 - 14, 2012",
			14: "Heli-Expo 2012: Dallas, TX<br />Visit us at Exhibit 1311<br />February 11 - 14, 2012<br /><br /><h3>Singapore Air Show</h3>Singapore<br />Visit us at Exhibit T107<br />February 14 - 19, 2012",
			15: "Singapore Air Show: Singapore<br />Visit us at Exhibit T107<br />February 14 - 19, 2012",
			16: "Singapore Air Show: Singapore<br />Visit us at Exhibit T107<br />February 14 - 19, 2012",
			17: "Singapore Air Show: Singapore<br />Visit us at Exhibit T107<br />February 14 - 19, 2012",
			18: "Singapore Air Show: Singapore<br />Visit us at Exhibit T107<br />February 14 - 19, 2012",
			19: "Singapore Air Show: Singapore<br />Visit us at Exhibit T107<br />February 14 - 19, 2012"
		},
		{ //Mar
			12: "Aerospace and Defense Supplier Summit: Seattle, WA<br />March 12 - 15, 2012",
			13: "Aerospace and Defense Supplier Summit: Seattle, WA<br />March 12 - 15, 2012",
			14: "Aerospace and Defense Supplier Summit: Seattle, WA<br />March 12 - 15, 2012",
			15: "Aerospace and Defense Supplier Summit: Seattle, WA<br />March 12 - 15, 2012"
		},
		{ //Apr
			3: "MRO Americas: Dallas, TX<br />Visit us at Exhibit 641<br />April 3 - 5, 2012",
			4: "MRO Americas: Dallas, TX<br />Visit us at Exhibit 641<br />April 3 - 5, 2012",
			5: "MRO Americas: Dallas, TX<br />Visit us at Exhibit 641<br />April 3 - 5, 2012",
			16: "National Space Symposium: Colorado Springs, CO<br />Visit us at Exhibit 1403<br />April 16 - 19, 2012",
			17: "National Space Symposium: Colorado Springs, CO<br />Visit us at Exhibit 1403<br />April 16 - 19, 2012",
			18: "National Space Symposium: Colorado Springs, CO<br />Visit us at Exhibit 1403<br />April 16 - 19, 2012",
			19: "National Space Symposium: Colorado Springs, CO<br />Visit us at Exhibit 1403<br />April 16 - 19, 2012"
		}, 
		{ //May
			30: "CANSEC 2012: Ottawa, ON<br />May 30 - 31, 2012",
			31: "CANSEC 2012: Ottawa, ON<br />May 30 - 31, 2012"
		},
		{}, //Jun
		{
			9: "Farnborough International Air Show: Farnborough, UK<br />July 9 - 15, 2012",
			10: "Farnborough International Air Show: Farnborough, UK<br />July 9 - 15, 2012",
			11: "Farnborough International Air Show: Farnborough, UK<br />July 9 - 15, 2012",
			12: "Farnborough International Air Show: Farnborough, UK<br />July 9 - 15, 2012",
			13: "Farnborough International Air Show: Farnborough, UK<br />July 9 - 15, 2012",
			14: "Farnborough International Air Show: Farnborough, UK<br />July 9 - 15, 2012",
			15: "Farnborough International Air Show: Farnborough, UK<br />July 9 - 15, 2012"
		}, //Jul
		{}, //Aug
		{}, //Sep
		{
			1: "International Astronautical Congress: Naples, Italy<br />October 1 - 5, 2012",
			2: "International Astronautical Congress: Naples, Italy<br />October 1 - 5, 2012",
			3: "International Astronautical Congress: Naples, Italy<br />October 1 - 5, 2012",
			4: "International Astronautical Congress: Naples, Italy<br />October 1 - 5, 2012",
			5: "International Astronautical Congress: Naples, Italy<br />October 1 - 5, 2012"
		}, //Oct
		{}, //Nov
		{ //Dec
			11: "PowerGen International: Orlando, FL<br />December 11 - 13, 2012",
			12: "PowerGen International: Orlando, FL<br />December 11 - 13, 2012",
			13: "PowerGen International: Orlando, FL<br />December 11 - 13, 2012"
		}
	]
 ];

//Create array of events - for events list (No Duplicates). Heading and body must be separated by a ":"
 var eventsForList = [
	//2011
	[
		{}, //Jan
		{}, //Feb
		{}, //Mar
		{}, //Apr
		{}, //May
		{}, //Jun
		{ //Jul
			6: "BRIDEX: Brunei Darussalam<br />Stand Number E-39<br />6 - 9 July 2011"
		},
		{}, //Aug
		{ //Sep
			13: "DSEI: London, United Kingdom<br />13 - 16 September 2011",
			28: "MRO Europe: London, United Kingdom<br />28 - 30 September 2011"
		}, 
		{}, //Oct
		{ //Nov
			2: "AIAC Canadian Aerospace Summit: Ottawa Convention Centre, Ottawa, Ontario<br />2 - 3 November 2011"
			
		}, 
		{ //Dec
			6: "PowerGen International: Las Vegas, Nevada, United States<br />6 - 8 December 2011"
		}
	],
	//2012
	[
		{ //Jan
			1: "None"
		}, 
		{ //Feb
			11: "Heli-Expo 2012: Dallas, TX<br />Visit us at Exhibit 1311<br />February 11 - 14, 2012",
			15: "Singapore Air Show: Singapore<br />Visit us at Exhibit T107<br />February 14 - 19, 2012"
		},
		{ //Mar
			12: "Aerospace and Defense Supplier Summit: Seattle, WA<br />March 12 - 15, 2012"
		},
		{ //Apr
			3: "MRO Americas: Dallas, TX<br />April 3 - 5, 2012",
			16: "National Space Symposium: Colorado Springs, CO<br />Visit us at Exhibit 1403<br />April 16 - 19, 2012"
		}, 
		{ //May
			30: "CANSEC 2012: Ottawa, ON<br />May 30 - 31, 2012"
		},
		{}, //Jun
		{
			9: "Farnborough International Air Show: Farnborough, UK<br />July 9 - 15, 2012"
		}, //Jul
		{ //Aug
			1: "SmallSat Conference: Logan, UT<br />August 2012"
		},
		{ //Sep
			1: "Proven Aircraft Review: Ogden, UT<br />September 2012"
		},
		{
			1: "International Astronautical Congress: Naples, Italy<br />October 1 - 5, 2012"
		}, //Oct
		{}, //Nov
		{ //Dec
			11: "PowerGen International: Orlando, FL<br />December 11 - 13, 2012"
		}
	]
 ];

/*******************************************************************************
 * Events List
 *******************************************************************************/
 
// Create events list
function createEventsList(currentYear, currentMonth){
	// Generate and display events for 3 months
	var list = [];
	var i=currentMonth;
	var j=0;
	
	while(j<3){
		if(i==12){ i=0; currentYear=currentYear+1; }
		if(getHTML(i,currentYear)) {
			list[j] = getHTML(i,currentYear);
			j++
		}
		i++;
	}
	
	$('#eventsList').html("<div id='events_content'>" + list[0] + list[1] + list[2] + "</div>");
}
 
// Return HTML for month,year (used for events list)
function getHTML(month,year){
	var arr = [];
	if(year==2011){ arr = eventsForList[0][month]; }
	else{ arr = eventsForList[1][month]; }
	
	var list = "";
	var level = 0;
	
	for(var item in arr) { if(arr[item]=="None"){ return false; }
		var value = arr[item];
		var separator = 0;
		separator = value.indexOf(':');
		
		var header = "";
		var content = "";
		
		if(separator == -1){
			heading = value;
		}
		else{
			heading = value.substring(0,separator);
			content = value.substring(separator+1);
		}
		
		list += "<h3>" + heading + "</h3>" + "<p>" + content + "</p>";
	}
	
	return "<h2 class=''><a href='#'>" + months[month] + "</a></h2><div>" + list + "</div>";
}

/*******************************************************************************
 * Calendar
 *******************************************************************************/

// Create events for calendar
function getMonthEvents(year, month, calendarId){
	if(year==2011){ return events[0][month-1]; }
	else{ return events[1][month-1]; }
}

// Add tooltips to events and highlight days
function addTooltips(year, month, calendarId){
	var theEvents = getMonthEvents(year, month, calendarId);
	var theDateLinks = $('#' + calendarId + ' .ui-datepicker-calendar a');
	for(eventDay in theEvents){
		theDateLinks.eq(eventDay-1)
			.attr('title', theEvents[eventDay])
			.tooltip({
				track: true, 
				delay: 0, 
				showURL: false, 
				showBody: ": "
			})
			.parent().addClass('event');
	}
}

// Update datepicker on month change
var _updateDatepicker_o = $.datepicker._updateDatepicker;
$.datepicker._updateDatepicker = function(inst){ 
   _updateDatepicker_o.apply(this, [inst]); 
   addTooltips(inst.drawYear, inst.drawMonth+1, inst.id);
};
