(function($, window) {

$(document).ready(function(){
    var $galleryContent = $("#galleryContent"),
        $galleryLeftBtn = $("#galleryLeftBtn"),
        $galleryRightBtn = $("#galleryRightBtn");

    function getCSSLeftPositionForGallery()
    {
      return parseInt($galleryContent.css("left").replace("px", ""));
    }

    function canGalleryScrollLeft(scrollAmount)
    {
      var leftPosition = getCSSLeftPositionForGallery();
      return leftPosition < 0;
    }

    function canGalleryScrollRight()
    {
      var leftPosition = getCSSLeftPositionForGallery(),
            galleryParentWidth = $galleryContent.parent().innerWidth(),
            galleryWidth = $galleryContent.outerWidth();
      return (galleryParentWidth + Math.abs(leftPosition)) < (galleryWidth);
    }

    function updateGalleryButtonDisabledStates()
    {
        if (canGalleryScrollLeft())
        {
            if($galleryLeftBtn.hasClass("galleryLeftBtnDisabled"))
                $galleryLeftBtn.removeClass("galleryLeftBtnDisabled");
        }
        else
        {
            if(!$galleryLeftBtn.hasClass("galleryLeftBtnDisabled"))
                $galleryLeftBtn.addClass("galleryLeftBtnDisabled");
        }

        if (canGalleryScrollRight())
        {
            if($galleryRightBtn.hasClass("galleryRightBtnDisabled"))
                $galleryRightBtn.removeClass("galleryRightBtnDisabled");
        }
        else
        {
            if(!$galleryRightBtn.hasClass("galleryRightBtnDisabled"))
                $galleryRightBtn.addClass("galleryRightBtnDisabled");
        }
    }

   if ($galleryContent.length > 0) {
       var scrollAmount = 800;
       var galleryIsAnimating = false;
       
       $galleryLeftBtn.click(function(event){
            var position = getCSSLeftPositionForGallery();
            if (canGalleryScrollLeft() && !galleryIsAnimating) {
                galleryIsAnimating = true;

                position = position + scrollAmount;
                if (position > 0)
                {
                    position = 0;
                }
                $galleryContent.animate({ left: position }, 1000, null, function() 
                        {
                            galleryIsAnimating = false;
                            updateGalleryButtonDisabledStates();
                        });
            }
       });

       $galleryRightBtn.click(function(event){

          var galleryParentWidth = $galleryContent.parent().innerWidth(),
                galleryWidth = $galleryContent.outerWidth(),
                position = getCSSLeftPositionForGallery();

            if (position <= 0)
            {
                if (canGalleryScrollRight() && !galleryIsAnimating) {
                    galleryIsAnimating = true;
                    var position = position - scrollAmount;
                    var maxPosition = galleryWidth - galleryParentWidth;

                    if (maxPosition > 0 && Math.abs(position) > maxPosition)
                    {
                        position = -maxPosition;
                    }
                    $galleryContent.animate({ left: position }, 1000, null, function() 
                        {
                            galleryIsAnimating = false;
                            updateGalleryButtonDisabledStates();
                        });
                }
            }

            return false;
       });

       updateGalleryButtonDisabledStates();

    }
 });
} (jQuery, window));
