var  rfdmGalleryContentStack = [];
var  rfdmGalleryScrollStack = [];

//----------------------------------------------------------------------------
//  
//----------------------------------------------------------------------------
function rfdmGalleryThumbSetClick(thumb)
{
    thumb.unbind('click');
    thumb.click(function () {
	$("#pageContent").fadeOut(200, function() {
	    $("#pageContent").load(thumb.find("src"));
	    $("#pageContent").fadeIn(200);
	});
    });
}

//----------------------------------------------------------------------------
//  
//----------------------------------------------------------------------------
function rfdmGalleryFullSizeImageLinkClick() {
    rfdmGalleryContentStack.push($("#pageContent").html());
//    rfdmGalleryScrollStack.push($(window).scrollTop());
    var fs = $("#pageContent").find("img").attr("id").replace("medium/","");
    $('.pageTopLogoFixed').hide();
    $('.fixedRightPageAd').hide();
    $('#pageContent').fadeOut(200, function() {
	$('#pageContent').html("<img class=\"DwmGalleryFullSizeImage\" src=\"" + fs + "\">");
	$('#pageContent').fadeIn(200, function() {
	    $('#pageContent').find("img").unbind('click');
	    $('#pageContent').find("img").click(function () {
		rfdmGalleryCollapseFull();
	    });
	});
    });
}

//----------------------------------------------------------------------------
//  
//----------------------------------------------------------------------------
function rfdmGalleryCollapseFull()
{
    $("#pageContent").fadeOut(200, function() {
	$("#pageContent").html(rfdmGalleryContentStack.pop());
//	$(window).scrollTop(rfdmGalleryScrollStack.pop()); 
	$("#pageContent").find(".DwmGalleryFullSizeImageLink").unbind('click');
	$("#pageContent").find(".DwmGalleryFullSizeImageLink").click(rfdmGalleryFullSizeImageLinkClick);
	$("#pageContent").fadeIn(200, function() {
	    $('.pageTopLogoFixed').show();
	    $('.fixedRightPageAd').show();
	    $("#pageContent").find("img").unbind('click');
	    $("#pageContent").find("img").click(rfdmGalleryCollapseMedium);
	    $('#pageContent').find('.DwmGalleryMediumImage').hover(
		function() {
		    $('#pageContent').find('.DwmGalleryMediumToThumbsHint').html("<br>Back to thumbs");
		    $('#pageContent').find('.DwmGalleryMediumToThumbsHint').fadeIn(200);
		},
		function() {
		    $('#pageContent').find('.DwmGalleryMediumToThumbsHint').fadeOut(200);
		}
	    );
	});
    });
}

//----------------------------------------------------------------------------
//  
//----------------------------------------------------------------------------
function rfdmGalleryCollapseMedium()
{
    $("#pageContent").find('img').parent().fadeOut(200, function() {
	$('#pageContent').html(rfdmGalleryContentStack.pop());
	$('#pageContent').fadeIn(200, function() {
	    $(window).scrollTop(rfdmGalleryScrollStack.pop());
	    $("#pageContent").unbind('click');
	    rfdmGalleryThumbsSetClicks();
	});
    });
}

//----------------------------------------------------------------------------
//  
//----------------------------------------------------------------------------
function rfdmGalleryExpandMedium()
{
    //  

    $("#pageContent").find(".DwmGalleryFullSizeImageLink").unbind('click');
    $("#pageContent").find(".DwmGalleryFullSizeImageLink").click(rfdmGalleryFullSizeImageLinkClick);
    $("#pageContent").find(".DwmGalleryMediumImage").unbind('click');
    $("#pageContent").find(".DwmGalleryMediumImage").click(function () {
	rfdmGalleryCollapseMedium();
    });
    $('#pageContent').find('.DwmGalleryMediumImage').hover(
	function() {
	    $('#pageContent').find('.DwmGalleryMediumToThumbsHint').html("<br>Back to thumbs");
	    $('#pageContent').find('.DwmGalleryMediumToThumbsHint').fadeIn(200);
	},
	function() {
	    $('#pageContent').find('.DwmGalleryMediumToThumbsHint').fadeOut(200);
	}
    );
}

//----------------------------------------------------------------------------
//  
//----------------------------------------------------------------------------
function rfdmGalleryExpandThumb()
{
    var  thumb = $(this);
    rfdmGalleryScrollStack.push($(window).scrollTop());
    rfdmGalleryContentStack.push($("#pageContent").html());
    $("#pageContent").fadeOut(200, function() {
	$("#pageContent").html(
	    "<div>\
               <div>\
                 <div class=\"DwmGalleryFullSizeImageLink\">Full<br>Size</div>\
                 <div class=\"DwmGalleryMediumToThumbsHint\"><br>Click image to return to thumbs</div>\
               </div>\
               <div><img class=\"DwmGalleryMediumImage\" src=\"" + thumb.attr("id") + "\" id=\"" + thumb.attr("id") + "\"></div>\
             </div>");
	$("#pageContent").fadeIn(200, function() {
	    rfdmGalleryExpandMedium();
	});
    });
}

//----------------------------------------------------------------------------
//  
//----------------------------------------------------------------------------
function rfdmGalleryThumbsSetClicks()
{
    $("#thumbs").find(".DwmGalleryThumbLink").unbind('click');
    $("#thumbs").find(".DwmGalleryThumbLink").click(rfdmGalleryExpandThumb);
}

//----------------------------------------------------------------------------
//  Action taken after page is loaded.
//----------------------------------------------------------------------------
$(document).ready(function() {
    var numThumbs = $("#thumbs").find(".DwmGalleryThumbLink").size();
    if (numThumbs > 0) {
	//  there are photos.
	rfdmGalleryThumbsSetClicks();
    }
})

