﻿function getCount(count) {
    $(hidEventCount).val(count);
    setPrevNext();
}

function pastEventPageActions() {
    $(".lnkYear").click(function() {
        if (typeof (hidVenue) != "undefined") {
            var venueId = parseInt($(hidVenue).val());
        }

        if (typeof (hidYear) != "undefined") {
            $(hidYear).val($(this).attr("href").replace("#", ""));
            var year = parseInt($(hidYear).val());
        }
        else {
            var year = $(this).attr("href").replace("#", "");
        }
        $("#yearContent").html($(this).html());

        var service = new EventService();

        service.GetPastEvents(venueId, year, populateEvents);
        var seln = $('#yearSelection');
        seln.slideToggle();
    });

    $(".lnkVenuePastEvent").click(function() {
        $(hidVenue).val($(this).attr("href").replace("#", ""));
        var venueId = parseInt($(hidVenue).val());
        var year = parseInt($(hidYear).val());
        $("#venueContentPastEvent").html($(this).html());

        var service = new EventService();

        service.GetPastEvents(venueId, year, populateEvents);
        var seln = $('#venueSelectionPastEvent');
        seln.slideToggle();
    });
}

function setPrevNext() {
    var count = parseInt($(hidEventCount).val());
    var first = parseInt($(hidCurrentItemNo).val());
    if (first <= 0) {
        first = 0;
        $("#lnkPrevious").hide();
    }
    else {
        $("#lnkPrevious").show();
    }
    if (first + 15 >= count + 0) {
        $("#lnkNext").hide();
    } else {
        $("#lnkNext").show();
    }
}

function getEvents() {
    var service = new EventService();
    var categoryId = parseInt($(hidCategory).val());
    var first = $(hidCurrentItemNo).val();
    var count = parseInt($(hidEventCount).val());
    if (first == '' || first < 0) {
        first = 0;
    }
    first = parseInt(first);
    if (first + 0 >= count + 0) {
        $("#lnkNext").toggle();
    }
    service.GetEvents(venueId, categoryId, first, 15, fillWhatOnTable);
}

function populateEvents(dtos) {
    $(".rowEvent").remove();

    for (var i = 0; i < dtos.length; i++) {
        var dto = dtos[i];
        var content = "<td>" + dto.EventTime + "</td><td>" + dto.Title + "</td><td>" + dto.Result + "</td><td>" + dto.Attendance + "</td>";
        $("#tblPastEvent").append("<tr  class='rowEvent'>" + content + "</tr>");
    }

    if (dtos.length == 0) {
        $("#tblPastEvent").append("<tr  class='rowEvent'><td colspan=4>No Events</td></tr>");
    }
    if ($.browser.msie) {
        var height = $("div.body").height();
        var tableHeight = $("#tblPastEvent").height();
        if (tableHeight > height) {
            height = tableHeight + 60;
        }
        if (height < 250) {
            height = 250;
        }
        $("#content").height(height + 30).children("div:first").height(height + 22);
    }
}

function fillWhatOnTable(dtos) {
    $(".rowWhatOn").remove();

    for (var i = 0; i < dtos.length; i++) {
        var dto = dtos[i];
        var content = "<img class=\"imgWhatOn\" alt=\"" + dto.Title + "\" src='" + dto.ImageURL + "' />" +
        "<h2>" + dto.Title
        + "</h2><p><span class=\"datetime\">" + dto.DateTime +
        "</span></p><p>" + dto.Description +
        "&nbsp;<span class=\"more\"><a href='/What-s-On/Event.aspx?EventId=" + dto.Id + "'>More info</a> &gt;</span></p>"

        $("#tblWhatOnArea").append("<li  class='rowWhatOn'>" + content + "</li>");
    }

    if (dtos.length == 0) {
        var catName = $("#categoryContent").html();
        if (catName == "All") {
            $("#tblWhatOnArea").append("<li  class='rowWhatOn'>No events are scheduled</li>");
        } else {
            $("#tblWhatOnArea").append("<li  class='rowWhatOn'>No events of " + catName + " are scheduled</li>");
        }
    }
    if ($.browser.msie) {
        var height = $("div.main").height();

        if (height == null) {
            height = $("div.body").height();
        }
        if (height < 350) {
            height = 350;
        }
        $("#content").height(height + 30).children("div:first").height(height + 22);
    }
}
