var Listing = {
  init : function(){
    $('.image').lazyloader(); 
  }
};

var MikeGallery = {
  index_of_last_clicked: 0,
  animating: false,
  center_x: 450,
  down_arrow: "Hide Info",
  up_arrow: "Show Info",

  init: function(){
    var gallery_assets = $('#assets.gallery');
    if (gallery_assets) {
      this.init_gallery_page(gallery_assets);
    }

    // var title = $('#title');
    // if (title) {
    //   this.init_title(title);
    // }
  
    // disable dragging of images
    $('body').delegate('img', 'dragstart', function() { return false });
  },
  
  last_clicked: null,
  
  // not currently used
  // index_of_pagination_element: function(el) {
  //   var tokens = el.attr('href').split("#");
  //   return tokens[tokens.length-1];
  // },

  init_gallery_page: function(assets) {
    if (assets) {
      // important that these binds come before slides() to hook clicks before
      // animation starts

      // thumb click
      $('.pagination li a').bind('click', function() {
        if (MikeGallery.animating) {
          return;
        }

        // slides.js automatically adds anchor links to thumbs in the pagination
        // element, ie <a href="#3"><img src="thumb.jpg"></a>
        var tokens = $(this).attr('href').split("#");
        MikeGallery.index_of_last_clicked = parseInt(tokens[tokens.length-1]);
      });

      // "previous" click
      $('a.prev').bind('click', function(){
        if (MikeGallery.animating) {
          return;
        }

        var index = MikeGallery.index_of_last_clicked;
        var new_index;

        if (index > 0) {
          new_index = index - 1;
        } else {
          new_index = $('.pagination li').length - 1;
        }
        MikeGallery.index_of_last_clicked = new_index;
      });

      // "next" click
      $('a.next').bind('click', function(){
        if (MikeGallery.animating) {
          return;
        }

        var index = MikeGallery.index_of_last_clicked;
        var new_index;

        if (index == ($('.pagination li').length - 1)) {
          new_index = 0
        } else {
          new_index = index + 1;
        }
        MikeGallery.index_of_last_clicked = new_index;
      });

      // select the start image if requested in the URL
      // slides.js has a start: attribute but it doesn't seem to work
      // properly (any index other than 1 doesn't show the image initially)
      var index_requested;
      if (location.hash.match(/^#[0-9]+/)) {
        index_requested = location.hash.slice(1);
      }

      var start = parseInt(index_requested || 0);
      assets.slides({
        preload: true,
        start: (start+1),
        container: 'assets_container',
        preloadImage: PathHelper.asset("images/loading.gif"),
        generateNextPrev: false,
        generatePagination: false,
        effect: _4ORMAT_DATA.theme.transition_effect, 
        animationStart: function(current) {
          MikeGallery.animating = true;
          var index = MikeGallery.index_of_last_clicked;
          MikeGallery.scroll_pagination_to(index);
          MikeGallery.set_url_index(index);
        },
        animationComplete: function(current) {
          MikeGallery.animating = false;
          MikeGallery.init_current_caption_scrollpane();
        },
        slidesLoaded: function() {
          MikeGallery.set_width_of_pagination();
          $('.pagination').jScrollPane();
          MikeGallery.pagination_scrollpane = $('.pagination').data('jsp');
          MikeGallery.scroll_pagination_to(start);
          MikeGallery.index_of_last_clicked = start;
          MikeGallery.init_current_caption_scrollpane();
        }
      });
      MikeGallery.init_current_caption_scrollpane();
      $(window).load(function(){
        // reset the pagination when all images are loaded
        MikeGallery.set_width_of_pagination();
      });
    }
  },

  init_title: function(title) {
    title.delegate('.arrow.down', 'click', function() {
      MikeGallery.show_title();
      return false;
    });
    title.delegate('.arrow.up, .hide', 'click', function() {
      MikeGallery.hide_title();
      return false;
    });
    $('#title .content').jScrollPane();
  },
  


  set_url_index: function(index) {
    location.hash = "#" + index;
  },

  scroll_pagination_to: function(index) {
    index = parseInt(index);
    
    var width_of_target = MikeGallery.width_of_thumbs(index, index);
    var width_of_all_previous;

    if (index < 1) {
      width_of_all_previous = 0;
    } else {
      width_of_all_previous = MikeGallery.width_of_thumbs(0, index-1);
    }

    var new_position = (width_of_all_previous + Math.floor(width_of_target / 2));

    // console.log("width of target: " + width_of_target);
    // console.log("width of prev: " + width_of_all_previous);
    // console.log("new position: " + new_position);

    if (new_position >= MikeGallery.center_x) {
      MikeGallery.pagination_scrollpane.
          scrollToX(new_position-MikeGallery.center_x, true);
    } else {
      MikeGallery.pagination_scrollpane.scrollToX(0, true);
    }
  },

  set_width_of_pagination: function() {
    var sum = MikeGallery.width_of_thumbs();
    // console.log("setting with of pagination to " + sum);
    $('.pagination ul.thumb_container').width(sum);
    if (MikeGallery.pagination_scrollpane) {
      MikeGallery.pagination_scrollpane.reinitialise();
    }
  },

  // returns the sum of the outer width of the pagination thumbnails from
  // start_index to end_index inclusive
  width_of_thumbs: function(start_index, end_index, selector) {
    var sum = 0;
    var selector = selector || ".pagination ul.thumb_container li.thumb_item";
    var parsed_start_index = parseInt(start_index);
    var parsed_end_index = parseInt(end_index);

    if (end_index != undefined) {
      selector = selector + ':lt(' + (parsed_end_index+1) + ')';
    }

    if ((start_index != undefined) && parsed_start_index > 0) {
      selector = selector + ':gt(' + (parsed_start_index-1) + ')';
    }

    // console.log(selector);

    $(selector).each(function(i,li){
      sum = sum + $(li).outerWidth(true);
    });

    return sum;
  },

  init_current_caption_scrollpane: function() {
    $('.caption .copy').jScrollPane();
  }
}


$(document).ready(function(){
  if(_4ORMAT_DATA.page.type == 'gallery'){
    MikeGallery.init.apply(MikeGallery);
  }
  if(_4ORMAT_DATA.page.type == 'listing'){
    Listing.init();
  }
});


