// jQuery + MagicEdit image gallery v0.1
// Copyright 7 May 2010
// http://www.magicedit.com
//
// You are free to use this for commercial or non-commercial projects and to modify
// the code with no restrictions whatsoever.

$(document).ready(function() {
    
    var imageNumber = "1";
    var numSlides = 0;
    var showControls = false;
    
    // Handle image switching
    function switchImage(i) {
      
      $(".me_galleryThumbs a.me_selected").removeClass("me_selected");
      $("#galleryThumb_" + i + " a").addClass("me_selected");

      function showImage(j) {
	
	$("#galleryImage_" + j).fadeIn("fast", function() {

	    // Show caption and controls as appropriate
	    if (j == numSlides) {
	      $(".me_next").hide();
	    }
	    else if (j == 1) {
	      $(".me_prev").hide();
	    }
	    
	    // Position caption
	    $("#galleryImage_" + i + " img").each(function() {
		var caption = $(this).nextAll(".me_galleryCaption");
		caption.css("width",this.width); // Do first to help with caption height calculation
		caption
		  .css({
		      "margin-left":this.offsetLeft-1 + "px",
			"margin-top":"-" + caption.height() + "px"
			});
		// Show caption and controls on hover
		if (j == numSlides) {
		  $(".me_next").hide().removeClass("me_hoverElement");
		  $(".me_prev").addClass("me_hoverElement");
		}
		else if (j == 1) {
		  $(".me_prev").hide().removeClass("me_hoverElement");
		  $(".me_next").addClass("me_hoverElement");
		}
		else $(".me_galleryControls").addClass("me_hoverElement");

		if (showControls) $(".me_hoverElement").fadeTo(400,0.6);
	      });
	  });
      }

      if ($(".me_galleryPanel li:visible").length) {
	$(".me_galleryPanel li:visible").fadeOut(30, function(){
	    showImage(i);
	  });
      }
      else {
	showImage(i);
      }
  
    }

    // Hide the Previous and Next buttons, and handle hover states
    $(".me_galleryControls, .me_galleryPanel li")
      .hover(function() {
	  $(".me_hoverElement").stop().fadeTo(400,0.6);
	  showControls = true;
	},
	function() {
	  $(".me_hoverElement").stop().fadeTo(800,0);
	  showControls = false;
	});
    
    // Remove MagicEdit "edit" and "delete" buttons from thumbnails (when logged in)
    $(".me_galleryThumbs .me_editButton, .me_galleryThumbs .me_deleteButton, .me_galleryThumbs .me_newButton, .me_galleryThumbs .me_upButton, .me_galleryThumbs .me_downButton").remove();
    
    // Move MagicEdit "new" button to a more convenient location
    $(".me_galleryPanel .me_newButton")
      .insertAfter(".me_panelWrap")
      .css({
	  "left":"12px",
	    "top":"8px"
	    });

    // Switch image on click
    $(".me_thumbLink").click(function() {
	imageNumber = $(this).parent("li").attr("id").split("_")[1];
	
	switchImage(imageNumber);
	
	return false;
      });

    // Switch image on Next click
    $(".me_galleryControls").click(function() {
	if ($(this).hasClass("me_next") && (imageNumber < numSlides)) {
	  imageNumber++;
	  switchImage(imageNumber);
	}
	else if ($(this).hasClass("me_prev") && (imageNumber > 1)) {
	  imageNumber--;
	  switchImage(imageNumber);
	}

	return false;
      });

    // Set the ALT attribute on all images to be the caption text
    $(".me_galleryPanel li").each(function(i) {
	i++;
	numSlides++;
	// Remove the caption if it's just the default text
	var captionText = $("#galleryImage_" + i + " .me_galleryCaption p").html();
	if (captionText == "Image caption") {
	  $("#galleryImage_" + i + " .me_galleryCaption").remove();
	}
	else {
	  captionText = captionText.split("<")[0];
	  $("#galleryThumb_" + i + " img").attr("alt",captionText);
	  $("#galleryImage_" + i + " img").attr("alt",captionText);
	}
	  
	//	$(this).css("padding-top",($(this).parent().height()-$(this).height())/2 + "px"); // Center image vertically
      });

    // Remove thumbnails and controls if there is only one image
    if (numSlides == 1) {
      $(".me_galleryThumbs").remove();
      $(".me_galleryControls").remove();

      // Remove the gallery if manager isn't logged in and no images have been uploaded
      if (!($(".me_bar").length) && ($("#galleryImage_1 img").attr("src").indexOf("placeholder") >= 0))
	$(".slideShow").remove();
    }
    
    // Show the first image
    switchImage(imageNumber);

  });

