﻿(function ($) {
    //  $ == jquery, shortened to reduce traffic; function ensures if adopted by a later process, the jquery will still run.

    // execute your scripts when DOM is ready. this is a good habit
    $(function () {

        // make #myelement listen for mousewheel events
        $("#myelement").mousewheel(function (event, delta) {

        });

        // http://server:port/sub/folders/index.htm
        // e.g.  http://server:port/sub/folders/modelling/standards.htm
        $.get(window.dataDirectory + "bgs_events.txt?" + Math.random(), null, function (result, status) {
            //$.get("../_data/bgs_events.txt?" + Math.random(), null, function (result, status) {

            window.eval("var data = " + result);

            var items = $(".scrollable .items");
            if (items.length == 0) {
                return;
            }
            items = items[0];

            data = data.sort(sortEventsByDate);

            for (var i in data) {
                var item = data[i];
                if (item.Date < new Date())
                    continue;

                var div = document.createElement("div");
                div.className = "item";
                var html = "<p><strong>" + item.Subject + "</strong></p>" +
							"<p>" + item.Description + "</p>" +
							"<p>" + item.DisplayDate + "</p>" +
							"<p>" + item.Location + "</p>" +
							"<p>" + item.Web + "</p>";
                div.innerHTML = html;
                items.appendChild(div);
            }

            /* Note: class of 'item' required by scrollable function
            <div class="item">
            <p><strong><a href="#">45. BGS AGM</a></strong></p>
            <p>Annual AGM open to all members</p>
            <p>April 14, 2012</p>
            <p>Didcot Railway Centre</p>
            <p>website</p>
            </div>*/

            // initialize scrollable with mousewheel support
            $(".scrollable").scrollable({
                vertical: true,
                //  easing: swing,
                mousewheel: true,
                circular: true,
                speed: 6000
            }).autoscroll({ autoplay: true, steps: 1, interval: 1500, autoplay: true, autopause: true }); // originally 1000 om site

        }, "text")
		.error(function () {
		    var items = $(".scrollable .items");
		    if (items.length == 0) {
		        return;
		    }
		    items[0].innerHTML = "Unable to load events";
		});
    });
})(jQuery);

function sortEventsByDate(a, b) {
    if (a.Date < b.Date) {
        return -1;
    } else if (a.Date > b.Date) {
        return 1;
    }
    return 0;
}
