// mod_if_gallery.js - jquery javascript for ifoundry gallery module

function generatethumbnails(newfolder){
          //once the thumbnail fade-out animation is finished,
          //remove all the existing thumbnails
          $(this).remove();
          //then kick-off an ajax call to re-generate the thumbnails
          $.ajax({
            type		:	'POST',
            url		:	'modules/mod_if_gallery/thumbnails.php',
            data		:	{folder: newfolder},
            dataType	:	'html',
            timeout	:	5000,
            success	:	function(d,s){
            					$(".mod_if_gallery_thumbnail_container:first").html(d);
            					setupThumbnailHandlers();
            					var img = $(".mod_if_gallery_thumbnail:first").find("img:first").attr("rel");
            					setBigImage(img);
          
            				},
            error		:	function(o,s,e){
	            		      $(".mod_if_gallery_thumbnail_container:first").html('');
            				}	
          });//ajax
        }

function setBigImage(img) {
  var newsrc = "modules/mod_if_gallery/image.php/jewellery.jpg?";
  newsrc += "width=400&height=340&image=";
  newsrc += img;
  oldsrc = $("#mod_if_gallery_bigimage img:first").attr("src");
  if (newsrc != oldsrc) {
    $("#mod_if_gallery_bigimage img:first").load(function(){$(this).fadeIn("slow");}).hide().attr("src",newsrc);
  }
}

function setupThumbnailHandlers() {
  $(".mod_if_gallery_thumbnail").click(
      function() {
        var img = $(this).find("img:first").attr("rel");
        setBigImage(img);
      }
    )//click
    .hover(
        function(){
          $(this).addClass("hand"); 
        	 var img = $(this).find("img:first").attr("rel");
        	 setBigImage(img);
        },
        function(){
          $(this).removeClass("hand");
        });//hover
}//setupThumbnailHandlers


$(document).ready(function() {

  //--------------------------------------------
  //setup the sub-folder selection event handler
  //--------------------------------------------
  $(".mod_if_gallery_folders li").click(
      function() {
        //hide the big image, then the thumbnails
        newfolder = $(this).text();
        $(".mod_if_gallery_bigimage img:first").fadeOut("fast");
        $(".mod_if_gallery_thumbnail").fadeOut("fast",generatethumbnails(newfolder) );
        
      }
    )
    .hover(
        function(){
          $(this).addClass("hand");
        },
        function(){
          $(this).removeClass("hand");
        });//hover
        
    setupThumbnailHandlers();

});//document.ready 

