﻿$(document).ready(function(){
    if($('#dates').length > 0 && $('#dates').find('li > span.year').length > 1)
    {
        var _hiddenElements = $('#dates').find('li:not(.active) > span.year');
        if($('#dates > li.active').length < 1)
        {
            _hiddenElements = $('#dates').find('li:not(:first) > span.year');
        }
        
        $('#dates').find('li > span.year').css({cursor: "pointer"}).next('ul').wrap("<div class=\"months\"></div>");
        
        $('#dates').find('li.active > span.year').append("<img src=\"/images/active-arrow.gif\" alt=\"\" />");
        $('#dates').find('li:not(.active) > span.year').append("<img src=\"/images/inactive-arrow.gif\" alt=\"\" />");
        
        _hiddenElements.next('div.months').hide();
        
        var _selectedYear = $('#dates div.months:visible').prev('span.year').get(0);
        
        $('#dates').find('li > span.year').click(function() {    
            var _this = $(this);
            var months = _this.next("div.months");
            
            if (_selectedYear && (_this.get(0) == _selectedYear)) {
                _this.children("img").attr("src", "/images/inactive-arrow.gif");
                //animate (_this) backwards
                _selectedYear = null;                
            }
            else {
                if (_selectedYear != null)
                {
                    $(_selectedYear).children("img").attr("src", "/images/inactive-arrow.gif");
                    //animate (_selectedYear) backwards
                }
                _selectedYear = _this.get(0);
                //animate (_this) forwards
                _this.children("img").attr("src", "/images/active-arrow.gif");
            }
                        
            $("#dates div.months:visible").not(months).slideUp("slow");
            months.slideToggle("slow");
        });
    }
});