// 
//  jquery.junior.jump.js
//  jumping icon for frontpage junior
//  
//  Created by John van Dijk on 2009-12-18.
//  Copyright 2009 Ruby Libre. All rights reserved.
// 
(function($){
  
    // Declare namespace if not already defined
    if(!$.Junior){
      $.Junior = new Object();
    }
  
    $.Junior.Jump = function(el){
      // To avoid scope issues, use 'base' instead of 'this'
      // to reference this class from internal events and functions.
      var base = this;
      
      // Access to jQuery and DOM versions of element
      base.$el  = $(el);
      base.$link = base.$el.find('> p > a');
      base.$title = base.$link.children('span');
      
      base.init = function() {
        base.$link.css({ right: -60 }).focus(function() { $(this).blur(); });
        base.$title.hide();
        
        // only ie6
        if ($.browser.msie && parseInt($.browser.version.substr(0,1), 10) < 7) base.$el.append(base.$title);
        
        $(window).load(base.onLoad);
        base.$el.mouseenter(base.onMouseEnter);
        base.$el.mouseleave(base.onMouseLeave);
      }
      
      base.onLoad = function(event) {
        // start jumping for attention
        base.timeout_id = setTimeout(base.jumpForward, 3500);
      }
      
      base.jumpForward = function() {
         base.$link.animate({ right: -20 }, 200, function() {
           base.jumpBackward();
         });
      }
      
      base.jumpBackward = function() {
        base.$link.animate({ right: -60 }, 1000, 'easeOutElastic', function() {
          base.timeout_id = setTimeout(base.jumpForward, 1200);
        });
      }
      
      base.onMouseEnter = function(event) {
        clearTimeout(base.timeout_id);
        base.$link.stop().animate({ right: -20 }, 200, function() {
          base.$title.show();
        });
      }
      
      base.onMouseLeave = function(event) {
        base.$title.hide();
        base.$link.stop().animate({ right: -60 }, 1000, 'easeOutElastic', function() {
          base.timeout_id = setTimeout(base.jumpForward, 1200);
        });
      }
      
      base.init();
    }
  

    $.fn.jump = function() {
      return this.each(function() {
        (new $.Junior.Jump(this));
      });
    }
  
})(jQuery);
