
var eventData = [

        {
                name: "Wokingham Half-Marathon ",
                intro: "A well-established and well-organised event celebrating its 25th anniversary in 2010",
                location: "Cantley Park, Wokingham",
                date: '2010-02-21',
                link: '/en/48_9849.htm'
        },{
                name: "From Bristol with Love",
                intro: "Ujima Radio are hosting a night of entertainment to benefit relief work in Haiti",
                location: "BS6 5RR",
                date: '2010-03-03',
                link: '/en/48_10557.htm'
        },{
                name: "Charity Chuckle",
                intro: "A night of brilliant stand-up comedy for the benefit of the Haiti Earthquake Appeal",
                location: "",
                date: '2010-03-12',
                link: '/en/48_10804.htm'
        },{
                name: "Inverness Half-Marathon",
                intro: "Run this scenic half-marathon",
                location: "",
                date: '2010-03-14',
                link: '/en/48_9852.htm'
        },{
                name: "Kilomathon England",
                intro: "The perfect race distance - 16.3 miles",
                location: "Nottingham",
                date: '2010-03-14',
                link: '/en/48_10286.htm'
        },{
                name: "Adidas Silverstone Half Marathon 2010",
                intro: "Run a half marathon at the historic Silverstone motor racing circuit.",
                location: "",
                date: '2010-03-14',
                link: '/en/48_8136.htm'
        },{
                name: "Reading Half Marathon",
                intro: "Join Team Save the Children",
                location: "",
                date: '2010-03-21',
                link: '/en/48_9854.htm'
        },{
                name: "ASICS Fleet Pre-London Half Marathon",
                intro: "One of spring's racing highlights, with top-notch organisation and a fast course.",
                location: "",
                date: '2010-03-21',
                link: '/en/48_9853.htm'
        },{
                name: "Minehead Walk for Children",
                intro: "Join us in a fun walk for family and friends of all ages",
                location: "",
                date: '2010-04-10',
                link: '/en/48_10664.htm'
        },{
                name: "BUPA Great Ireland Run &#8212; 10K",
                intro: "Ireland's premier 10k running event returns to the beautiful Phoenix Park next April. ",
                location: "",
                date: '2010-04-18',
                link: '/en/48_9855.htm'
        },{
                name: "The Brighton Marathon 2010",
                intro: "The first ever Brighton Marathon around the city and along the coast.",
                location: "Brighton",
                date: '2010-04-18',
                link: '/en/48_9087.htm'
        },{
                name: "Boston Marathon 2010",
                intro: "The Boston Marathon is one of the world's oldest and most prestigious annual marathons.",
                location: "",
                date: '2010-04-19',
                link: '/en/48_9856.htm'
        },{
                name: "Sheffield Half-Marathon ",
                intro: "This course has a stadium start and finishes in Sheffield city centre with great crowd support.",
                location: "",
                date: '2010-04-25',
                link: '/en/48_9857.htm'
        },{
                name: "London Marathon",
                intro: "Running the marathon is one of the most rewarding challenges you can participate in, especially when you're raising money to help children around the world.",
                location: "",
                date: '2010-04-25',
                link: '/en/48_2985.htm'
        },{
                name: " Wessex Walks in Wells",
                intro: "Every mile helps a child. ",
                location: "Wells",
                date: '2010-04-25',
                link: '/en/48_10498.htm'
        },{
                name: "The Great West Run 2010",
                intro: "The Great West Run is a half marathon consisting of two laps starting from Exeter Arena in the Exeter city centre.",
                location: "",
                date: '2010-05-02',
                link: '/en/48_9859.htm'
        },{
                name: "BUPA Great Edinburgh Run &#8212; 10K",
                intro: "Run through the historic city of Edinburgh for Save the Children",
                location: "",
                date: '2010-05-02',
                link: '/en/48_9858.htm'
        },{
                name: "Minehead Lunch & Laughter",
                intro: "Join us for our popular Lunch & Laughter event",
                location: "",
                date: '2010-05-05',
                link: '/en/48_10666.htm'
        },{
                name: "Minehead Quiz Night",
                intro: "Enter a team in our annual Quiz Night",
                location: "",
                date: '2010-05-07',
                link: '/en/48_10667.htm'
        },{
                name: "Wessex Walks Salisbury",
                intro: "Every mile helps a child",
                location: "Salisbury",
                date: '2010-05-09',
                link: '/en/48_10499.htm'
        },{
                name: "Tewkesbury Half Marathon",
                intro: "Between the Cotswolds and the Malvern Hills the Tewkesbury Half Marathon and 5 mile run will take place",
                location: "",
                date: '2010-05-09',
                link: '/en/48_10810.htm'
        },{
                name: "BUPA Great Manchester Run &#8212; 10K",
                intro: "The BUPA Great Manchester Run is one of the world's fastest growing running events and Britain's premier 10K. ",
                location: "",
                date: '2010-05-16',
                link: '/en/48_9860.htm'
        },{
                name: "Trek the Inca Trail",
                intro: "Not to be missed: a trek through the Inca ruins and breathtaking ecological sites of Peru, finishing in Machu Picchu, the spectacular Lost City of the Incas.",
                location: "",
                date: '2010-05-22',
                link: '/en/48_705.htm'
        },{
                name: "The Edinburgh Marathon, Hairy Haggis Team Relay and Scottish Half Marathon",
                intro: "With around 25,000 people expected to compete in these events, it will now be one of the biggest running festivals in the UK.",
                location: "Edinburgh, Scotland",
                date: '2010-05-23',
                link: '/en/48_10117.htm'
        },{
                name: "Nightrider",
                intro: "A fantastic cycle challenge taking in all the key sights of London by night.",
                location: "",
                date: '2010-06-19',
                link: '/en/48_9668.htm'
        },{
                name: "Great London Swim",
                intro: "Join the UK&#8217;s biggest one mile open water swims in spectacular settings",
                location: "",
                date: '2010-07-03',
                link: '/en/48_9955.htm'
        },{
                name: "Stirling Big Fun Run",
                intro: "The Big Fun Runs are a growing series of 21 Fun Runs being staged across the UK.",
                location: "University of Stirling, Stirling, FK9 4, UK",
                date: '2010-07-10',
                link: '/en/48_10225.htm'
        },{
                name: "Asics British 10K London Run ",
                intro: "Join Team Save the Children for this summer London run",
                location: "London",
                date: '2010-07-11',
                link: '/en/48_9861.htm'
        },{
                name: "Aberdeen Big Fun Run",
                intro: "The Big Fun Runs are a growing series of 21 Fun Runs being staged across the UK.",
                location: "Hazelhead Park, Aberdeen",
                date: '2010-07-11',
                link: '/en/48_10227.htm'
        },{
                name: "Dundee Big Fun Run",
                intro: "The Big Fun Runs are a growing series of 21 Fun Runs being staged across the UK.",
                location: "Camperdown Park, Dundee, Angus, DD24TF",
                date: '2010-07-17',
                link: '/en/48_10228.htm'
        },{
                name: "Perth Big Fun Run",
                intro: "The Big Fun Runs are a growing series of 21 Fun Runs being staged across the UK.",
                location: "North Inch, Perth, PH1 5HT, UK",
                date: '2010-07-18',
                link: '/en/48_10229.htm'
        },{
                name: "Glasgow Big Fun Run",
                intro: "5km of beautiful scenery!",
                location: "Bellahouston Park, 16 Dumbreck Road, Glasgow, Lanarkshire, G41 5BW",
                date: '2010-07-24',
                link: '/en/48_10230.htm'
        },{
                name: "Edinburgh Big Fun Run",
                intro: "The Edinburgh Big Fun Run is 5km long",
                location: "Holyrood Park, Edinburgh, Midlothian, EH16 5BT",
                date: '2010-07-25',
                link: '/en/48_10232.htm'
        },{
                name: "Middlesbrough Big Fun Run",
                intro: "This is a 5km run through the heart of Middlesbrough",
                location: "Albert Park, Middlesbrough, Cleveland, TS1 3LB, UK",
                date: '2010-07-31',
                link: '/en/48_10233.htm'
        },{
                name: "Mazda London Triathlon",
                intro: "Join Team Save the Children for one the UK&#8217;s fastest growing sports",
                location: "",
                date: '2010-08-07',
                link: '/en/48_9837.htm'
        },{
                name: "Liverpool Big Fun Run",
                intro: "The Liverpool Big Fun Run is 5km long",
                location: "Sefton Park, Liverpool, Merseyside",
                date: '2010-08-07',
                link: '/en/48_10246.htm'
        },{
                name: "Sheffield Big Fun Run",
                intro: "The Manchester Big Fun Run is 5km long",
                location: "Heaton Park, Bury, Greater Manchester",
                date: '2010-08-08',
                link: '/en/48_10247.htm'
        },{
                name: "Leeds Big Fun Run",
                intro: "The Leeds Big Fun Run is 5km long",
                location: "Rother Valley Country Park, Mansfield Road, Wales Bar, Sheffield, South Yorkshire, S26 5PQ",
                date: '2010-08-14',
                link: '/en/48_10248.htm'
        },{
                name: "Derby Big Fun Run",
                intro: "The Derby Big Fun Run is 5km long",
                location: "Whitlingham Country Park, Whitlingham Lane, Norwich, Norfolk, NR14 8TZ",
                date: '2010-08-21',
                link: '/en/48_10249.htm'
        },{
                name: "Norwich Big Fun Run",
                intro: "The Norwich Big Fun Run is 5km long",
                location: "Darley Park, Old Chester Rd, Derby, DE1 3SA",
                date: '2010-08-22',
                link: '/en/48_10250.htm'
        },{
                name: "Great North Swim",
                intro: "Join the UK&#8217;s biggest one mile open water swims in spectacular settings. ",
                location: "",
                date: '2010-09-04',
                link: '/en/48_9956.htm'
        },{
                name: "Cardiff Big Fun Run",
                intro: "The Cardiff Big Fun Run is 5km long",
                location: "Bute Park, Castle St, Cardiff, CF10 3RB‎",
                date: '2010-09-04',
                link: '/en/48_10251.htm'
        },{
                name: "Leicester Big Fun Run",
                intro: "Leicester Big Fun Run is 5km long",
                location: "2 Abbey Park Lodge, Leicester, Leicestershire, LE4 5AQ",
                date: '2010-09-05',
                link: '/en/48_10270.htm'
        },{
                name: "BUPA Great Yorkshire Run &#8212; 10K",
                intro: "Take part in the BUPA Great Yorkshire Run and join 10,000 runners of all abilities aged 15 and over. ",
                location: "Sheffield",
                date: '2010-09-05',
                link: '/en/48_10055.htm'
        },{
                name: "First Monster 120km team duathlon",
                intro: "First Monster is a 120km 4-person team bike and run duathlon round the stunning shores of Loch Ness.",
                location: "Loch Ness, Inverness, Scotland",
                date: '2010-09-11',
                link: '/en/48_10477.htm'
        },{
                name: "London Big Fun Run",
                intro: "This Big Fun Run in the capital takes place in Crystal Palace",
                location: "",
                date: '2010-09-11',
                link: '/en/48_10692.htm'
        },{
                name: "BUPA Great North Run 2010",
                intro: "The world's biggest half marathon!",
                location: "Newcastle",
                date: '2010-09-17',
                link: '/en/48_10056.htm'
        },{
                name: "Coventry Big Fun Run",
                intro: "The Coventry Big Fun Run is 5km long",
                location: "Memorial Park, Kenilworth Road, Coventry, West Midlands, CV3 6PT",
                date: '2010-09-18',
                link: '/en/48_10271.htm'
        },{
                name: "London Big Fun Run",
                intro: "This London Big Fun Run is being held in Victoria Park",
                location: "",
                date: '2010-09-25',
                link: '/en/48_10693.htm'
        },{
                name: "Run to the Beat Half Marathon",
                intro: "Be inspired and spurred on by specially chosen live bands and DJs at this unique half marathon.",
                location: "",
                date: '2010-09-26',
                link: '/en/48_10754.htm'
        },{
                name: "Real Berlin Marathon",
                intro: "This route takes you on a big sightseeing loop through one of the most exciting cities in the world.",
                location: "",
                date: '2010-09-26',
                link: '/en/48_10747.htm'
        },{
                name: "Milton Keynes Big Fun Run",
                intro: "The Milton Keynes Big Fun Run is 5km long",
                location: "Victoria Park, London E9, UK",
                date: '2010-09-26',
                link: '/en/48_10273.htm'
        },{
                name: "Watford Big Fun Run",
                intro: "Watford Big Fun Run is a 5k run",
                location: "Cassiobury Park, Watford, Hertfordshire WD18 7HY, UK",
                date: '2010-10-02',
                link: '/en/48_10274.htm'
        },{
                name: "Cheltenham Big Fun Run",
                intro: "The Cheltenham Big Fun Run is a 5km family orientated run",
                location: "",
                date: '2010-10-03',
                link: '/en/48_10694.htm'
        },{
                name: "Baxter&#8217;s Loch Ness Marathon and Festival of Running",
                intro: "Recognised as one of the UK&#8217;s leading marathons.",
                location: "Loch Ness, Scotland",
                date: '2010-10-03',
                link: '/en/48_10118.htm'
        },{
                name: "Kilomathon Scotland",
                intro: "The perfect race distance - 16.3 miles",
                location: "Royal Highland Centre, Ingliston, Newbridge, Midlothian, EH28 8NB",
                date: '2010-10-03',
                link: '/en/48_10287.htm'
        },{
                name: "Trek the Great Wall of China",
                intro: "Trek along the Great Wall of China, experience Chinese culture and explore Tiananmen Square",
                location: "China",
                date: '2010-10-08',
                link: '/en/48_5667.htm'
        },{
                name: "Southampton Big Fun Run",
                intro: "The Southampton Big Fun Run is 5km long",
                location: "Southampton Common, The Avenue, Southampton, SO17",
                date: '2010-10-09',
                link: '/en/48_10276.htm'
        },{
                name: "Royal Parks Half Marathon",
                intro: "Places are not yet on sale but please register your interest with us.",
                location: "",
                date: '2010-10-10',
                link: '/en/48_10753.htm'
        },{
                name: "Maidstone Big Fun Run",
                intro: "The Maidstone Big Fun Run is a 5km run",
                location: "Mote Park, Willow Way, Maidstone, Kent, ME15 7RN",
                date: '2010-10-10',
                link: '/en/48_10277.htm'
        },{
                name: "The Cardiff Half Marathon ",
                intro: "More than 11,000 runners are expected to turn out in the Welsh capital for this tour of its most famous sights. ",
                location: "Cardiff, Wales",
                date: '2010-10-17',
                link: '/en/48_10062.htm'
        },{
                name: "BUPA Great South Run &#8212; 10 miles",
                intro: "The BUPA Great South Run is Britain's leading 10 mile running event. ",
                location: "Southsea, Portsmouth",
                date: '2010-10-24',
                link: '/en/48_10065.htm'
        },{
                name: "Venice Marathon",
                intro: "Venice is celebrating 25 years of the Marathon, and has been granted permission to deviate the finish to run directly through St Marks Square making it a truly spectacular experience",
                location: "",
                date: '2010-10-24',
                link: '/en/48_10806.htm'
        },{
                name: "Athens Marathon",
                intro: "Athens Marathon is celebrating their 2500th anniversary this year, so it is set to be a momentous occasion!!",
                location: "",
                date: '2010-10-31',
                link: '/en/48_10807.htm'
        },{
                name: "ING New York City Marathon",
                intro: "The application for the ING New York City Marathon 2010 closes on March 15.",
                location: "",
                date: '2010-11-07',
                link: '/en/48_10749.htm'
        },{
                name: "Climb Kilimanjaro",
                intro: "Take part in the ultimate challenge by climbing the highest mountain in&nbsp;Africa",
                location: "Tanzania",
                date: '2011-01-20',
                link: '/en/48_5663.htm'
        },

        {}
];


//-----------------------------------------------------------------------------
// Remove each element from an array depending on result of callback function, func.
//-----------------------------------------------------------------------------
function filter(arr, func) {
    for (i = arr.length; i >= 0; i--) {
        if (func(arr[i])) { arr.splice(i, 1); }
    }
}


//-----------------------------------------------------------------------------
// Display event calendar
//-----------------------------------------------------------------------------
var today = toISODate(new Date());

var eventsDates = [];

eventData.pop();

for (e in eventData)
{
    eventsDates.push(eventData[e].date);
}

filter(eventsDates, beforeToday);

eventsDates.sort();


//-------------------------------------------------------------------------
// Determine if given date (ISO short format e.g. 2010-01-29) is in the
// past.
//-------------------------------------------------------------------------
function beforeToday(d) {
    return d < today;
}

$(function() {
    $('.hide-by-js').hide();

    if (eventsDates.length == 0)
    {
        return;
    }

    $('#eventcalendar').datepicker({
        minDate: fromISODate(eventsDates[0]),
        maxDate: fromISODate(eventsDates[eventsDates.length - 1]),
    
        beforeShowDay: selectableDates,
        dateFormat: 'yy-mm-dd',

        onSelect: function(dateText, inst)  {
            // redirect to the appropriate month#day page
            var year   = dateText.substr(0, 4);
            var month  = dateText.substr(5, 2);
            var day    = dateText.substr(8, 2);

            var base = 'http://www.savethechildren.org.uk/events/';

            window.location = base + year + '/' + month + '/' + day;
        }
    });
});

//-------------------------------------------------------------------------
// Callback function for calendar to determine which dates can be selected.
//-------------------------------------------------------------------------
function selectableDates(theDate) {
    var iDate = toISODate(theDate);

    for (d in eventsDates) {
        if (eventsDates[d] == iDate) {
            return [ true ];
        }
    }

    return [ false ];
}


//-----------------------------------------------------------------------------
// Convert a (short) ISO date string into a javascript Date object.
//-----------------------------------------------------------------------------
function fromISODate(date)
{
    var year   = date.substr(0, 4);
    var month  = parseInt(unpad(date.substr(5, 2))) - 1;
    var day    = date.substr(8, 2);

    return new Date(year, month, day);
}


//-----------------------------------------------------------------------------
// Convert a javascript Date object into a (short) ISO date string
//-----------------------------------------------------------------------------
function toISODate(date) {
    return date.getFullYear() + '-' + pad(date.getMonth() + 1) + '-' + pad(date.getDate());
}


//-----------------------------------------------------------------------------
// Remove leading zero, if present
//-----------------------------------------------------------------------------
function unpad(n) { return n[0] === '0' ? n.substr(1) : n; }


//-----------------------------------------------------------------------------
// Add leading zero to numbers < 10
//-----------------------------------------------------------------------------
function pad(n) { return n < 10 ? '0' + n : n; }


//-----------------------------------------------------------------------------
// Log debug to firebug console
//-----------------------------------------------------------------------------
function clog(str)
{
    if (window.console)
    {
        console.log(str);
    }
}
