var widget_month = 0;

$(function(){

    var month_view = $('#month_view').val() == 'y';
    var past_view = $('#past_view').val() == 'y';
    var months_count = $('#months_count').val();

    var cal_index=0;
    $('input[name=cal-i]').each(function(){
        $(this).attr('name', 'cal-'+cal_index);
        cal_index++;
    });
    
    var cal_width = $('.cal-scroll-item:first').width();

    function set_month_label()
    {
        var month_name_selector = 'input[name^=cal-'+widget_month+']';
        var month_name = $(month_name_selector).val();
        $('#cal-month-name').html(month_name);
        if (!month_view) {
            check_buttons();
        }
    }

    function check_buttons()
    {
        if (widget_month >= months_count-1) {
            $('a.nextMonth').addClass('disabled');
        } else {
            $('a.nextMonth').removeClass('disabled');
        }

        if (widget_month == 0) {
            $('a.prevMonth').addClass('disabled');
        } else {
            $('a.prevMonth').removeClass('disabled');
        }
    }        

    if (!month_view)
    {
        check_buttons();
        
        $('.nextMonth').click(function(e){
                
            if (widget_month >= months_count-1) return false;
    
            widget_month++;
            var x = -widget_month*cal_width + 1;
            $(".cal-scroll-inner").animate({left:x+'.px'},
                                           {duration:1000,
                                           easing:'swing',
                                           queue:false} );
            set_month_label();
    
            return false;
        });
    
        $('.prevMonth').click(function(e){
                
            if (widget_month == 0) return false;
    
            widget_month--;
            var x = -widget_month*cal_width + 1;
            $(".cal-scroll-inner").animate({left:x+'.px'},
                                           {duration:1000,
                                           easing:'swing',
                                           queue:false} );
    
            set_month_label();
    
            return false;
        });
    }
    
    if (past_view) {
        widget_month = months_count-1;
        var x = -widget_month*cal_width + 1;
        $(".cal-scroll-inner").css('left', x+'px');
        set_month_label();
    }

    $('h3.daytitle').click(function(e){

        var $this = $(this);
        var day_key = $this.attr('name').split('_')[1];
        var day_selector = 'ul[name=day'+day_key+']';
        var $day = $(day_selector);
        if ($this.hasClass('collapsed'))
        {
            $this.removeClass('collapsed');
            $day.slideDown('fast');
        }
        else
        {
            $this.addClass('collapsed');
            $day.slideUp('fast');
        }

    });

    $("select[name=filterEvent]").change( function() {
        var $this = $(this);
        var new_url = $this.val();
        window.location = new_url;
    });

});

var currentAnchor = null;



$(function() {
    $('li.eventday').addClass('active');

    $('td.upcomingEvent a,td.hasEvent a').click(function(e) {
        // get the name of the anchor:
        var anchorName = $(this).attr('href').split('#')[1];

        if (!$('li.'+anchorName+':first').length) {
            return true;
        }
        e.preventDefault();

        var $anchor = $('a[name='+anchorName+']:first');
        currentAnchor = $anchor;

        var $event_days = $('li.eventday:visible');
        var $active = $('li.eventday a[name=' + anchorName + ']:first').parent();

        $event_days.removeClass('current');
        $active.addClass('active current');

        var window_height = $(window).height();
        var scroll_pos = $('html,body').scrollTop();

        var scroll_to = Math.min(currentAnchor.offset().top, $(document).height()-window_height);

        var distance = Math.abs(scroll_to - scroll_pos);
        var duration = Math.min(3000, distance);

        $('html,body').animate({scrollTop: scroll_to},
                                duration, 'swing',
                                function(){$active.find('ul:first').slideDown('normal');});

        return true;
    });
    
    var $previous_events_show = $('.showPreviousEvents');
    var $previous_events = $('.previousEvents');
    $previous_events.hide();
    $previous_events_show.click(function(e){
        if ($previous_events.is(':visible')) {
            $previous_events.slideUp();
            $previous_events_show.addClass('active');
        } else {
            $previous_events_show.removeClass('active');
            $previous_events.slideDown();
        }
    });
});

function scrollWindowIntoView() {
    $('html,body').animate({scrollTop: currentAnchor.offset().top}, {duration:1000, easing:'swing'});
}

