﻿GimiWhatsOn = function () {
    return {
        Initialise: function () {
            var eventsId = document.getElementById('gimi-whats-on-events');
            if (eventsId) {
                iCM.RPC.GimiWhatsOn.GetLatestEventsList(
					function (target, response) {
					    if (response) {
					        var e = document.getElementById(target);
					        if (e) {
					            if (gimiWhatsOnScriptReplace) {
					                e.innerHTML = '';
					                var odd = false;
					                for (i = 0; i < response.result.length; i++) {
					                    var evt = response.result[i];
					                    // List item
					                    var li = document.createElement('li');
					                    li.setAttribute('class', (odd ? 'odd' : 'even'));

					                    // Anchor
					                    var a = document.createElement('a');
					                    var loc = evt.location ? evt.location : '';
					                    a.innerHTML = '<strong>' + evt.heading + '</strong>, ' + evt.start + ', ' + loc;
					                    a.setAttribute('href', evt.link);
					                    li.appendChild(a);
					                    e.appendChild(li);

					                    odd = !odd;
					                }
					            }
					        }
					    }
					},
					function (error) {

					},
					'gimi-whats-on-events'
				);
            }

            var top5 = document.getElementById('gimi-whats-on-top5');
            if (top5) {
                iCM.RPC.GimiWhatsOn.GetTopFiveList(
					function (target, response) {
					    if (response) {
					        var e = document.getElementById(target);
					        if (e) {
					            if (gimiWhatsOnScriptReplace) {
					                e.innerHTML = '';
					                var odd = false;
					                for (i = 0; i < response.result.length; i++) {
					                    var art = response.result[i];
					                    // List item
					                    var li = document.createElement('li');
					                    li.setAttribute('class', (odd ? 'odd' : 'even'));

					                    // Anchor
					                    var a = document.createElement('a');
					                    a.innerHTML = art.heading;
					                    a.setAttribute('href', art.link);
					                    li.appendChild(a);
					                    e.appendChild(li);

					                    odd = !odd;
					                }
					            }
					        }
					    }
					},
					function (error) {

					},
					'gimi-whats-on-top5'
				);
            }
        }
    }
} ();

iCM.GimiWhatsOn = GimiWhatsOn;
