var glrImages;
var debug = false;

/* Scroller and gallery */
$(document).ready(function () {

    glrImages = new Array();

    try {
    
    $(".prev-page").hover(function() {
        if (!$(this).hasClass("scroller-disabled"))
            $(this).css("background-position", "0 -103px");
    }, function(){
        $(this).css("background-position", "0 0");
    });
    
    $(".next-page").hover(function() {
        if (!$(this).hasClass("disabled"))
            $(this).css("background-position", "-10px -103px");
    }, function(){
        $(this).css("background-position", "-10px 0");
    });
       
    $(".glr-images").each(function() {
      
      var id = parseInt($(this).attr("id").split("-")[2]);
      
      if (debug) alert("0");
               
      var images = $(this).scrollable({
        disabledClass: "box-disabled",
        navi: "#box-nav-" + id.toString(),
        prev: "#box-prev-page-" + id.toString(),
        next: "#box-next-page-" + id.toString(),
        size: 1,
        api: true
      });
           
      images.onSeek(function() { 
          var i = this.getIndex(); 
          this.getItems().slice(Math.max(i - 2, 0), i + 2).each(function() { 
              if (debug) alert("1.2");
              var el = $(this); 
              if (!el.find("img").length) { 
                  var img = $("<img/>").attr("src", el.attr("title")); 
                  el.prepend(img).removeAttr("title");
                  img.tooltip({
                      tip: "#tooltip-" + el.attr("id"),
                      position: ["original", "original"], 
                      opacity: 0.8,
                      offset: [200, 0],
                      effect: "fade"
                  });
              } 
          }); 
      });
      
      if (debug) alert("2");
      
      glrImages[id] = images;
      
    });
    
    
    $(".glr-thumbnails").each(function() {
    
      var glr = $(this);
      var id = parseInt(glr.attr("id").split("-")[2]);
          
      glr.find(".glr-pic").each(function(index) { 

        $(this).overlay({ 
            target: "#glr-box-" + id, 
            expose: { maskId: "mask" }, 
            onLoad: function() {
              glrImages[id].seekTo(index, 0); 
            } 
        });
      });
    
    });

    } catch(err) {
      alert(err);
    }

});

