var calendars = new Array();

function Calendar(element)
{
	this.element = element;
    this.highlighted = undefined;

    this.hideCalendar = function()
    {
        $(this.element).stop();
        $(this.element).fadeTo("slow", 0);
    }

    this.showCalendar = function()
    {
        $(this.element).stop();
        $(this.element).fadeTo("slow", 1);
    }

    this.previousMonth = function(ev)
    {
        ev.data.hideCalendar();

        newYear = ev.data.year;
        newMonth = ev.data.month - 1;

        ev.data.switchMonth(newYear, newMonth);
    }

    this.nextMonth = function(ev)
    {
        ev.data.hideCalendar();

        newYear = ev.data.year;
        newMonth = ev.data.month + 1;

        ev.data.switchMonth(newYear, newMonth);
    }

    this.rebindButtons = function()
    {
        this.year = Number($(".date .year", element).text());
        this.month = Number($(".date .month", element).text());
        this.previousMonthButton = $("a.previousmonth", this.element);
        this.nextMonthButton = $("a.nextmonth", this.element);
        this.previousMonthButton.bind('click', this, this.previousMonth);
        this.nextMonthButton.bind('click', this, this.nextMonth);

        $("td a", this.element).each(
                function (i, domElement)
                {
                    var elem = $(domElement);
                    elem.click(
                            function()
                            {
                                loadComic("/comic/" + elem.attr('href').substr(1) + "/");
                            });
                });
    }

    this.updateCurrent = function(current)
    {
        if(this.highlighted)
        {
            this.highlighted.removeClass('currentcomic');
        }

        this.highlighted = $('.comicid_' + current);
        this.highlighted.addClass('currentcomic');
    }

    this.switchMonth = function(newYear, newMonth)
    {
        if(newMonth < 1)
        {
            newMonth += 12;
            newYear -= 1;
        }

        if(newMonth > 12)
        {
            newMonth -= 12;
            newYear += 1;
        }

        if(newYear != this.year || newMonth != this.month)
        {
            thisthis = this;
            $(this.element).load("/calendar/" + newYear + "/" + newMonth + " .calendar>*", "",
                    function()
                    {
                        thisthis.showCalendar();
                        thisthis.rebindButtons();

                        thisthis.updateCurrent(comicId);
                    });
        }
    }

    this.rebindButtons();
    this.updateCurrent(comicId);
} 


/* Page Load */
$(document).ready(function calendarPageLoad()
{
	$(".calendar").each(
			function (i, domElement)
			{
                calendars.push(new Calendar(domElement));
			});

    hotspots = $("div.hotspot");
    sideHotspots = $("div.hotspot.leftside, div.hotspot.rightside");

    imagediv.css("width", "25%");
    imagediv.css("height", "25%");

	image.click(
			function()
			{
				showAll(true);
			});

    $("a#firstpage").click(
			function()
			{
				loadComic("/comic/first/");
			});
});


