//  Momentum Workshop Photo Bar...

(function($){
  
  $.mw = $.extend($.mw?$.mw:{} , 
  {
    'photobar': {
      'pb': null,
      preloadId: 0,
      currentPhotoId: 0,
      currentPhotos: [],
      // captionStatic: '<span class="instructions">Use your left and right arrow keys to switch images.  Click on an image to get the portfolio page, click on its category to get the services page.</span>',
      // getCaption: function(id) {
      //   if (!this.currentPhotos[id].name) this.currentPhotos[id].name = 'Portfolio Image';
      //   if (!this.currentPhotos[id].longdesc) this.currentPhotos[id].longdesc = '<p>Long Description</p>';
      //   var caption = "<h1>"+this.currentPhotos[id].name+"</h1>";
      //   var links = [];
      //   $.each(this.currentPhotos[id].galleries, function(i, gallery) {
      //     links.push("<a href='"+gallery.href+"'>"+gallery.name+"</a>");
      //   });
      //   caption = caption + this.currentPhotos[id].longdesc;
      //   if (links.length > 0) caption = caption + '<p><strong>Service'+(links.length>1?'s':'')+':</strong> '+links.join(', ')+'</p>';
      //   caption = caption +this.captionStatic;
      //   return caption;
      // },
      gotoPhoto: function(id, animate) {
        if (!this.currentPhotos || (this.currentPhotos.length == 0)) return;
        if (this.loopTimeout) clearTimeout(this.loopTimeout);
        this.currentPhotoId = id = ((id==-1)?this.currentPhotos.length-1:(id % this.currentPhotos.length));
        if (!animate) {
          $("img",this.imgdiv).attr('src', this.currentPhotos[id].src);
          // $.mw.photobar.caption.html(this.getCaption(id));
        } else {
          var imgdiv = this.imgdiv;
          var newdiv = imgdiv.clone(true);
          newdiv.insertBefore(this.imgdiv);
          $("img", newdiv).attr('src', this.currentPhotos[id].src);
          imgdiv.fadeOut(2000, (function(id) { return function() {
            // $.mw.photobar.caption.html($.mw.photobar.getCaption(id));
            $.mw.photobar.setupLoops();
            $(this).remove();
          };})(id));  
          this.imgdiv = newdiv;
        }
      },
      loopTimeout: undefined,
      loops: 0,
      setupLoops: function() {
        if (this.loops > 500) return; // stop looping at some point...
        if (this.loopTimeout) clearTimeout(this.loopTimeout);
        if (!this.currentPhotos || (this.currentPhotos.length < 2) || this.over)
        {
          return;
        }
        this.loopTimeout = setTimeout(function() {$.mw.photobar.next(); $.mw.photobar.loops++; }, 5000);
      },
      mouseOver: function() {
//        this.caption.stop(true,true).slideDown('slow');
//        this.isOver = true;
      },
      mouseOut: function() {
//        this.caption.stop(true,true).slideUp('slow');
//        this.isOver = false;
//        this.setupLoops();
      },
      click: function() {
        var photo = this.currentPhotos[this.currentPhotoId];
        // if (this.isOver) {
          if (photo && photo.href) window.location = photo.href;
        //   else this.mouseOut(); 
        // } else {
        //   this.mouseOver();
        // }
      },
      next: function(force,speed) {
        if (this.isOver && !force) return;
        this.gotoPhoto(this.currentPhotoId+1, 1);
      },
      prev: function(force,speed) {
        if (this.isOver && !force) return;
        this.gotoPhoto(this.currentPhotoId-1, 1);
      },
      
      init: function(pbDom) {
        if (this.currentPhotos.length == 0) return;
        this.pb = $(pbDom);
        this.pb.click(function() {$.mw.photobar.click();}).css('cursor','pointer');
        this.pb.wrap("<div id='photobar_wrapper' />");
        // not sure why it needs to be done this way...
        this.pbwrap = this.pb.parent();
        this.pbwrap.hover(function() {$.mw.photobar.mouseOver();}, function() {$.mw.photobar.mouseOut();});
        // this.caption = $("<div class='captionbar' />").hide();
        // this.caption.insertAfter(this.pb);
        this.imgdiv = $("div:first", pbDom);
        this.gotoPhoto(0,true);
        $(document).bind("keydown", function(e) {
          if (e.keyCode == 37) {
            $.mw.photobar.prev(true,0);
          }
          if (e.keyCode == 39) {
            $.mw.photobar.next(true,0);
          }
        });
        this.preloadPhoto();
      },
      preloadPhoto: function() {
        if (this.preloadId >= this.currentPhotos.length) return;
        this.currentPhotos[this.preloadId].dom = jQuery("<img>").attr("src", this.currentPhotos[this.preloadId].src).bind("load", function() { $.mw.photobar.preloadPhoto(); });
        this.preloadId++;
      }
    }
  });
  
  $.fn.photobar = function(opts) {
    return this.each(function() {
      if (!opts) {
        if ($.mw.photobar.pb) return;
        $.mw.photobar.init(this);        
      } else {
        // tie me to a url
        
        
      }
      
    });
  };
  
  $(function() {
    if ($('.photobar').photobar().length)
    {
      
    }
    
  });
  
})(jQuery);