/*	================================================================================
	Collections
	Author: 	Paul Cole
				
	Date:		
	Description:Legacy code used in collection database
				Code should be altered to use framework
				
				If there are more tham 3 thumbnail this code is used to allow users
				to dynamically look through them with out having to reload the page.
	================================================================================ */
	
/* jQuery extension method taken from:
*  http://www.learningjquery.com/2008/02/simple-effects-plugins
*/
jQuery.fn.slideFadeToggle = function(speed, easing, callback) {
  return this.animate({opacity: 'toggle', height: 'toggle'}, speed, easing, callback); 
};


var curPage = 1;
var totalPages = 0;
var animating = false;

$(document).ready(function() {

    $.BM.useComponents('fx,fx.slide', function() {
        runAllViews();
    });


    function runAllViews() {

        // Hide the extra images on the opening image page
        $("ul#page" + curPage + " .hideImage").hide();

        // Expand or Contract the see all views
        $("p.seeAllViews a").click(function() {
            // If the extra images are hidden on the current image page...
            if ($("p.seeAllViews a").hasClass("closeAllViews") == false) {
                // Add the up arrow to the see all views link
                $("p.seeAllViews a").addClass("closeAllViews");

                $("p.seeAllViews a").text($("p.seeAllViews a").text().replace("See", "Hide"));

                // Show the extra set of images
                $("ul#page" + curPage + " .hideImage:hidden").slideFadeToggle('slow', 'linear', function() {
                    if (totalPages > 1) {
                        $("#page" + curPage).addClass("moreViewsPage");
                    }
                });

                // If we have more than one page and the current page does not have more than 3 images,
                // we need to add the moreViewsPage class so that it is the same height as all the other
                // pages
                if (totalPages > 1 && $("ul#page" + curPage + " li").hasClass("hideImage") == false) {
                    $("#page" + curPage).addClass("moreViewsPage");
                }

                // Show or hide the previous/next links
                if (totalPages > 1) {
                    if (curPage < totalPages) {
                        $("#nextImage").show("fast", function() { $(".seeAllViewLinks").fadeIn("slow"); });
                    }
                    if (curPage > 1) {
                        $("#prevImage").show("fast", function() { $(".seeAllViewLinks:hidden").fadeIn("slow"); });
                    }
                }
            } else {
                // Remove the up arrow class which will then show the down arrow
                $("p.seeAllViews a").removeClass("closeAllViews");
                $("p.seeAllViews a").text($("p.seeAllViews a").text().replace("Hide", "See"));

                // Hide the extra images
                $("#page" + curPage).removeClass("moreViewsPage");
                $("ul#page" + curPage + " .hideImage:visible").slideFadeToggle('slow', 'linear');

                // Hide the previous/next links
                $("#prevImage:visible").fadeOut("slow");
                $("#nextImage:visible").fadeOut("slow", function() { $('.seeAllViewLinks').hide(); });
            }

            return false;
        });

        // Next thumbnail page
        $("#nextImage").click(function() {
            if (animating) { return false; }
            if (curPage < totalPages) {
                animating = true;
                $("#page" + curPage).hide('slide', { direction: 'left', callback: function() {
                    curPage++;
                    $("#page" + curPage).show('slide', { direction: 'right', callback: function() { animating = false; } });
                    if (curPage == totalPages) {
                        $("#nextImage:visible").fadeOut("slow");
                    }
                    $("#prevImage:hidden").fadeIn("slow");
                }
                });
            }

            return false;
        });

        // Previous thumbnail page
        $("#prevImage").click(function() {
            if (animating) { return false; }
            if (curPage > 1) {
                animating = true;
                $("#page" + curPage).hide('slide', { direction: 'right', callback: function() {
                    curPage--;
                    $("#page" + curPage).show('slide', { direction: 'left', callback: function() { animating = false; } });
                    if (curPage == 1) {
                        $("#prevImage:visible").fadeOut("slow");
                    }
                    $("#nextImage:hidden").fadeIn("slow");
                }
                });
            }

            return false;
        });
    }
});

