﻿function selectTerm() {
    if ($('option', $('#term')).length == 0)
        return;
    $('#select-term-form').submit();
}

// Gets the list of events for the selected month and highlighs the dates in the calendar
function populateCalendar(month, year) {

    // Get the events for this store for the selected year and month
    $.getJSON('/School/' + $('#school-slug').val() + '/Events/' + year + '/' + month + "?asJson=True", function (data) {

        // Kill all selection logic in the calendar
        $('.ui-datepicker-calendar td').attr("onclick", "");
        $('.ui-datepicker-calendar td a').attr("href", "javascript:void(0)");

        // Add event handler for this month and year to calendar cells
        $('.ui-datepicker-calendar td a').click(function () { getEvents(year, month, $(this).html()); });

        // Add highlighting and click handlers to the dates with events
        //$('.ui-datepicker-calendar td a:exactly(' + now.getDate() + ')').removeClass('ui-state-highlight');
        var now = new Date();
        if (now.getMonth() == month - 1 && now.getFullYear() == year)
            $('.ui-datepicker-calendar td a:exactly(' + now.getDate() + ')').addClass('ui-state-active').removeClass('ui-state-highlight');

        for (i in data)
            $('.ui-datepicker-calendar td a:exactly(' + data[i] + ')').addClass('ui-state-highlight');
    });
}

// Called when a date is clicked in the calendar
// Loads and displays events in the right pane
function getEvents(year, month, day) {
    $('.ui-datepicker-calendar td a').removeClass('ui-state-active');
    $('.ui-datepicker-calendar td a:exactly(' + day + ')').addClass('ui-state-active');
    var url = "/School/" + $('#school-slug').val() +"/Events/" + year + "/" + month + (day == null ? "" : "/" + day);
    $.ajax({
        url: url,
        complete: function (result, status) {
            $('#events-right-col').fadeOut(500, function () {
                removeCalendarHeight();
                $(this).html(result.responseText);
                addthis.toolbox(".event_addthis_toolbox");
                addthis.counter(".event_addthis_counter");
                $(this).fadeIn(500, function () { equalizeEventColumns(); });
            });
        }
    });
}

function populateCalendarAndGetEvents(month, year) {
    populateCalendar(month, year);
    getEvents(year, month);
}

function removeCalendarHeight() {
    $('#events-left-col').css('height', '');
}

function equalizeEventColumns() {
    $('#events-left-col').height($('#event-section-wrapper').height() - 60);
}

$(function () {

    if ($('#carousel .product-info').length >= 4) {
        $('#carousel').jcarousel({
            wrap: 'circular',
            scroll: 4,
            itemFirstInCallback: function (jcarousel, item) {
                $(item).find('.product-info').css('border', 'none');
            }
        });
    } else {
        $('#carousel').css('height', '225px');
    }


    $('#subscribe-button').colorbox({
        width: 760,
        height: 580,
        opacity: 0.2,
        iframe: true
    });

    // Create calendar
    $('#datepicker').datepicker({
        inline: true,
        onChangeMonthYear: function (year, month, inst) {
            populateCalendarAndGetEvents(month, year);
        }
    });

    // Highlight dates
    var now = new Date();
    populateCalendar(now.getMonth() + 1, now.getFullYear());

    $('#subscribe').watermark('Enter your email', { className: 'water-mark' });
}); 
