var $activePhoto	= null;
var $activeWeek		= null;

$(document).ready( function () {
    $('<div id="loading">Loading...</div>').prependTo('.potw-div');

	// Set up function for handling the Photo of the Year voting results link
    var $potyLink   = $(".poty-results-link a").click( function(event) {
        $('#potw-collection-date-chooser a').removeClass( 'dc-selected' );
																
        if ( $activeWeek ) {
            $activeWeek.hide();
            $activeWeek     = null;
        }

        $("#poty-results").fadeIn();

        event.preventDefault();
    });

    // Add click handlers for the links to activate a week
	$("#potw-collection-date-chooser a").click( function (event) {
        $('#potw-collection-date-chooser a').removeClass( 'dc-selected' );

	// Find the id of the week to activate and activate it

		activateWeek ( $(this).children(".potw-week-id").text() );
        $(this).addClass( 'dc-selected' );

        event.preventDefault();
    });

    // Add click handlers for the links to activate a photo
	$(".potw-controls a").click( function (event) {
		// Deactivate the link for the currently active photo
		$(".potw-controls a").removeClass( "active" );

		// Find the id of the week to activate and activate it
		activatePhoto ( $(this).children(".potw-photo-id").text(), $(this).attr("href") );
		
		$(this).addClass( "active" );

        event.preventDefault();
   });

	// Activate first week in the thumbnails
	var $weekLinks	= $("#potw-collection-date-chooser a");
	$weekLinks.eq( $weekLinks.size() - 1 ).trigger( "click" );

});

function activateWeek ( weekID ) {
    var $weekDiv        = $("#" + weekID);
	var photoID;

	// Not efficient, but hide POTY Voting Results just in case it's visible
    $("#poty-results").hide();

    // Hide the currently visible week
    if ( $activeWeek ) {
        $activeWeek.hide();
		$activeWeek		= null;
    }

	$weekDiv.show();
	$activeWeek			= $weekDiv;

	// Activate first photo of the week
	$weekDiv.find(".potw-controls a").eq(0).trigger( "click" );
}

function activatePhoto ( photoID, imgSrc ) {
    var $loading    = $("#loading");

    // Hide the currently visible week
    if ( $activePhoto ) {
        $activePhoto.hide();
		$activePhoto	= null;
    }

	$loading.show();

    var $photoDiv        = $("#" + photoID);
    var $photoPhotoDiv   = $photoDiv.children(".photo-div");
    var $photoImg        = $photoPhotoDiv.children("img");
    var photoTitle       = $photoDiv.children(".potw-title").text();

	$activePhoto = $photoDiv;

    var showPhoto = function () {
        $loading.hide();
        $activePhoto.fadeIn( 1000 );
    };

    // If image not already created and loaded, create it and add to the div.
    if ( $photoImg.length == 0 ) {
        $photoImg    = $("<img/>").attr( {alt: photoTitle, title: photoTitle} );
        $photoImg.prependTo($photoPhotoDiv);
        $photoImg.bind("load", showPhoto);
        $photoImg.attr("src", imgSrc );
    } else {
        showPhoto();
    }
}

