/*
 * Facebox_Gallery (for jQuery)
 * version: 0.1 (24/01/2009)
 * @requires jQuery v1.2 or later and facebox
 *
 * Dual Licensed under the MIT and GPL:
 *   http://www.opensource.org/licenses/mit-license.php
 *
 * Copyright 2009 Renato Formato <renatoformato@viriglio.it>
 */
(function($){
  var cur_el;
  var images = jQuery([]);
  var gal_ui = jQuery([]);
  var timer = null;
  
  var updateInfo = function(cur_el,index) {
    $("div.info").text("Image "+index+" of "+images.length);
    cur_el.trigger("update_facebox_gallery",[index]);
  }
  
  var loadImage = function(image) {
    var di = $('#facebox div.image');
    di.height(di.height());
    di.width(di.width());
    di.empty();
    di.prepend('<div class="loading"><img src="'+$.facebox.settings.loadingImage+'"/></div>');
    var img = new Image;
    img.src = image.attr("href");
    $(img).load(function(){
      $("#facebox div.loading").remove();
      di.html("<img style=\"display:none\" src=\""+image.attr("href")+"\">").children().fadeIn(1000);
      di.animate({width:img.width,height:img.height},1000);
      $('#facebox').animate({'left': $(window).width() / 2 - ((img.width+40) / 2)+"px"},1000);
    });
    
  }
  
  $.fn.facebox_gallery = function(speed){
    speed = speed || 6000;
    //memorizzo elemento corrente da cui si è caricato il facebox
    this.click(function(){
      cur_el = this;
      var gal = cur_el.rel.match(/gallery\d+(\s|$)/);
      if(gal) {
        gal = gal[0];
        images = $("a[rel*="+gal+"]").filter(function(){
          var rg = new RegExp(gal+"(\s|$)");
          return rg.test(this.rel);
        });
        var index = images.index(this);
        if(!gal_ui.length) {
          gal_ui = $("div.info,div.navigation","#facebox");
          var offset = $('#facebox').offset();
          $('#facebox').css({
            top:	offset.top-43+"px"
          })
        }
        gal_ui.show();
        //eventi navigazione
        $("#facebox img.play").click(function(){
          if(this.src.indexOf($.facebox.settings.playImage)!=-1) {
            this.src = $.facebox.settings.pauseImage;
            timer = window.setInterval(function(){
              index++;
              if(index>=images.length)
                index = 0;
              
              loadImage(images.eq(index));
              updateInfo(images.eq(index),index+1);
              
            },speed);
          } else {
            this.src = $.facebox.settings.playImage;
            window.clearInterval(timer);
            timer = null;
          }
        });
        $("#facebox img.prev").click(function(){
          if(timer) {
            window.clearInterval(timer);
            timer = null;            
          }
          index--;
          if(index<0)
            index = images.length-1;
          loadImage(images.eq(index));
          updateInfo(images.eq(index),index+1);
        });
        $("#facebox img.next").click(function(){
          if(timer) {
            window.clearInterval(timer);
            timer = null;            
          }
          index++;
          if(index>=images.length)
            index = 0;
          loadImage(images.eq(index));
          updateInfo(images.eq(index),index+1);
        });

        
        updateInfo($(cur_el),index+1);
      }
      
    });
    
    return this;
  };

  //evento chiusura facebox
  //nascondo ui gallery
  $(document).bind("close.facebox",function(){
    gal_ui.hide();
    $("img.prev,img.play,img.next","#facebox").unbind("click");
  });

  //inserisco HTML controllo gallery
  var fbhtml = $("<div>").append($.facebox.settings.faceboxHtml);
  $("div.footer",fbhtml).prepend(
  "<div class=\"info\" style=\"display: none;\">Image 2 of 5</div>"+
  "<div class=\"navigation\" style=\"display: none;\">"+
  "<img src=\""+$.facebox.settings.prevImage+"\" class=\"prev\" style=\"cursor: pointer;\"/>"+
  "<img src=\""+$.facebox.settings.playImage+"\" class=\"play\" style=\"cursor: pointer;\"/>"+
  "<img src=\""+$.facebox.settings.nextImage+"\" class=\"next\" style=\"cursor: pointer;\"/>"+
  "</div>");
  $.facebox.settings.faceboxHtml = fbhtml.html();
  
  
})(jQuery)
