var today = new Date;
var txt = new Array;
var img = new Array;

//array to hold globally required html objects
var obj = new Array;

//array to hold globally required values
var val = new Array
	val['window_status'] = 'Welcome to the Avon Valley Railway Visitors Website';
	val['lhr_timeout'];
	val['next_season_txt'] = today.getMonth() +1 == 1?(today.getFullYear()):(today.getFullYear() + 1);
	val['this_season_txt'] = today.getMonth() +1 == 1?(today.getFullYear() - 1):(today.getFullYear());
//declare array of month names
var mmtxts = new Array;
mmtxts[1] = 'january';
mmtxts[2] = 'february';
mmtxts[3] = 'march';
mmtxts[4] = 'april';
mmtxts[5] = 'may';
mmtxts[6] = 'june';
mmtxts[7] = 'july';
mmtxts[8] = 'august';
mmtxts[9] = 'september';
mmtxts[10] = 'october';
mmtxts[11] = 'november';
mmtxts[12] = 'december';

//declare array of day names
var ddtxts = new Array();
ddtxts[0] = 'sunday';
ddtxts[1] = 'monday';
ddtxts[2] = 'tuesday';
ddtxts[3] = 'wednesday';
ddtxts[4] = 'thursday';
ddtxts[5] = 'friday';
ddtxts[6] = 'saturday';

//work out when the next steam diesel day is
txt['nsd'] = null;
img['nsd'] = null;

if (sched){
	for (date in sched){
		var sched_day = new Date(date.substr(0,4),(date.substr(5,2))-1,date.substr(8,2),18,00,00);
		if (sched_day >= today && txt['nsd'] == null){
			txt['nsd'] = '~ our next steam or diesel day: ' + getDdMmYyyyTxt(sched_day,3,3) + ' ~';
			img['nsd'] = '/img/' + sched[date] + '.gif';
		}
	}
}

//all the stuff that needs to be done at the start
onload:doOnload();
function doOnload(){
	window.status = val['window_status'];
}

function date_as_mysql_string(date){
	var month = ((date.getMonth() + 1) < 10)?'0' + (date.getMonth() + 1):(date.getMonth() + 1);
	var dom = (date.getDate() < 10)?'0' + String(date.getDate()):date.getDate();
	return date.getFullYear() + '-' + month + '-' + dom;
}

function date_from_mysql_string(str){
	return new Date(str.substr(0,4),(str.substr(5,2))-1,str.substr(8,2));
}

function date_append(dt){
	if (dt == 1 || dt == 21 || dt == 31){
		dt = dt + 'st';
	}else if (dt == 2 || dt == 22){
		dt = dt + 'nd';
	}else if (dt == 3 || dt == 23){
		dt = dt + 'rd'
	}else{
		dt = dt + 'th';
	}
	return dt;
}

function getDdMmYyyyTxt(date,dlen,mlen){
	return ddtxts[date.getDay()].substr(0,dlen) + ' '
		+ date_append(date.getDate()) + ' '
		+ mmtxts[date.getMonth() + 1].substr(0,mlen) + ' '
		+ date.getFullYear();
}

function getDdMmYyTxt(date){
	return date.getDate() + '/'
		+ (date.getMonth()+1) + '/'
		+ String(date.getFullYear()).substr(2,2);
}

function getMmYyTxt(date,mlen){
	return mmtxts[date.getMonth() + 1].substr(0,mlen) + ' \''
		+ String(date.getFullYear()).substr(2,2);
}

function isInThePast(date){
	var date2 = new Date();
	date2.setFullYear(date.getFullYear());
	date2.setMonth(date.getMonth());
	date2.setDate(date.getDate());
	date2.setHours(23);
	date2.setMinutes(59);
	date2.setSeconds(59);
	if (date2 < today){
		return true;
	}else{
		return false;
	}
}

function isThisSeason(date){
	var ret = false;
	if (today.getMonth() == 0){
		if ((date.getMonth() == 0 && date.getYear() == today.getYear()) || (date.getMonth() > 0 && date.getYear() == ( today.getYear() - 1))) ret = true;
	}else{
		if ((date.getMonth() == 0 && date.getYear() == (today.getYear() + 1)) || (date.getMonth() > 0 && date.getYear() == today.getYear())) ret = true;
	}
	return ret;
}

function isNextSeason(date){
	var ret = false;
	if (today.getMonth() == 0){
		if ((date.getMonth() == 0 && date.getYear() == today.getYear() + 1) || (date.getMonth() > 0 && date.getYear() == ( today.getYear()))) ret = true;
	}else{
		if ((date.getMonth() == 0 && date.getYear() == (today.getYear() + 2)) || (date.getMonth() > 0 && date.getYear() == today.getYear() + 1)) ret = true;
	}
	return ret;
}

function getHhMmSs(date){
	var ret = date.getHours() < 10?'0'+date.getHours()+':':date.getHours()+':';
	ret = ret + (date.getMinutes() < 10?'0'+date.getMinutes()+':':date.getMinutes()+':');
	ret = ret + (date.getSeconds() < 10?'0'+date.getSeconds():date.getSeconds());
	return ret;
}

// #### event listings functionality ####

function evl_write_listing(evtype_tid){
	var next_season_reached = false
	var ret = '<p style="margin-left:10px;">dates for season ' + val['this_season_txt'] + ':</p><ul>';
	var last_date = '0000-00-00';
	var forced_next_season = false;
	for (e in events){
		//only do event processing if it is the requested evtype
		if (events[e]['evtype_tid'] == evtype_tid){
			//get Date object of event date
			obj['evl_event_date'] = new Date(events[e]['date'].substr(0,4),events[e]['date'].substr(5,2) - 1,events[e]['date'].substr(8,2));
			//if the event is the first one of next season set the 'next_season_reached indicator'
			if (isNextSeason(obj['evl_event_date']) && !next_season_reached){
				next_season_reached = true;
				if (ret == '<p style="margin-left:10px;">dates for season ' + val['this_season_txt'] + ':</p><ul>' ||
					last_date < date_as_mysql_string(today)
				){
					ret = '<p style="margin-left:10px;">dates for season ' + val['next_season_txt'] + ':</p><ul>';
					forced_next_season = true;
				}else{
					ret = ret + '</ul><p style="margin-left:10px;">dates for season ' + val['next_season_txt'] + ':</p><ul>';
				}
			}
			//create a li if event date is in this or next season
			if (isThisSeason(obj['evl_event_date']) || isNextSeason(obj['evl_event_date'])){
				//deduce the li class according to event this/next season & event in past or not, start the li code
				var li_class = next_season_reached?'event_dates_fut':isInThePast(obj['evl_event_date'])?'event_dates_exp':'event_dates';
				if (forced_next_season && next_season_reached) li_class = 'event_dates';
				var li = '<li class="' + li_class + '">';
				
				//only append a variant if one exists
				if (events[e]['evvar_name']) li = li + events[e]['evvar_name'] + ': ';
				
				//append the date string as a href for more info about the day
				li = li + getDdMmYyyyTxt(obj['evl_event_date'],3,3);
				
				//the event availability status
				if (events[e]['evstat_name'] == 'not bookable at present' && events[e]['book_date'] && !isInThePast(obj['evl_event_date'])){
					//if status is 'not yet bookable' and, the event has a book date and event date is not in the past
					obj['evl_book_date'] = new Date(events[e]['book_date'].substr(0,4),events[e]['book_date'].substr(5,2) - 1,events[e]['book_date'].substr(8,2));
					if (isInThePast(obj['evl_book_date'])){
						//if bookable date is in the past just return the evstat name
						li = li + ' (<span style="color:' + events[e]['colour'] + '">' + events[e]['evstat_name'] + '</span>)';
					}else{
						//otherwise say when the event can be booked
						li = li + ' (<span style="color:' + events[e]['colour'] + '">bookable from ' + getDdMmYyyyTxt(obj['evl_book_date'],3,3) + '</span>)';
					}
				}else if (!isInThePast(obj['evl_event_date'])){
					//if above is false and event is not in the past
					li = li + ' (<span style="color:' + events[e]['colour'] + '">' + events[e]['evstat_name'] + '</span>)';
				}
				//end the li and append to ret
				li = li + '</li>';
				ret = ret + li;
				last_date = date_as_mysql_string(obj['evl_event_date']);
			}
		}
	}
	//end the list and print to document
	ret = ret + '</ul>';
	document.write(ret);
}


