/*
 *	Upcoming events listing
 *	-----------------------
 *	Copyright (c) 2011 Wabbit Web Works
 *	All Rights Reserved
 *
 *	This programme is free software: you can redistribute it and/or modify
 *	it under the terms of the GNU General Public Licence as published by
 *	the Free Software Foundation, either version 3 of the Licence, or
 *	(at your option) any later version.
 *
 *	This programme is distributed in the hope that it will be useful,
 *	but WITHOUT ANY WARRANTY; without even the implied warranty of
 *	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *	GNU General Public Licence for more details.
 *
 *	You should have received a copy of the GNU General Public Licence
 *	along with this programme.  If not, see <http://www.gnu.org/licenses/>.
 */

/*
 *	upcoming_events()
 *	------------------
 *	Fill the "upcoming_events" elemet(s) with text to "block" to show
 *	the element.  There should only be one element with this class name
 *	really.  I originally used an ID instead of a class, but it didn't
 *	play well with the info slider on the welcome page.
 *
 *	Fudge the date back four hours so that a current event continues to
 *	display util 4:00am.
 */
function upcoming_events(el,events)
{
    var tmp = new Date();
    var d = new Date(tmp - (3600000 * 4));

    var day = d.getDate();
    if (day < 10)
	day = '0' + day;

    var month = d.getMonth() + 1;
    if (month < 10)
	month = '0' + month;

    var date = d.getFullYear().toString() + month.toString() + day.toString();
    var count = 0;

    var buf = '<h2>Upcoming Candygirls Parties</h2>';

    for (var i = 0; i < events.length; i++) {
	if (count >= 3)
	    break;

	if (events[i].date < date)
	    continue;

	buf += events[i].longdate;
	if (events[i].note) {
	    var note = events[i].note;
	    if (events[i].uri)
		note = '<a href="' + events[i].uri + '">' + note + '</a>';
	    buf += ' <span class="desc">(' + note + ')</span>';
	}
	buf += '<br>';
	count++;
    }

    if (count) {
	buf += '<div class="strapline">See our <a href="/events.html">events page</a> for more dates</div>';
    }
    else {
	buf += '<div class="strapline">See our <a href="/events.html">events page</a> for the dates</div>';
    }
    el.innerHTML = buf;
}

/*
 *	load the upcoming eventswhen the page has loaded
 */
add_load_event(function() {
    var el = document.getElementById('upcoming_events');
    if (!el) {
	alert("No 'upcoming_events' ID defined");
	return;
    }

    xmlrpc_get_data('/data/upcoming_events.json',function(json) {
	try {
	    upcoming_events(el,eval(json));
	}
	catch (err) {
	    alert('eval: ' + err);
	}
    });
});

