// 
//  jquery.junior.sticky.js
//  stickies 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.Sticky = 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.init = function() {
        if ($.browser.msie) {
           // use correct image for AlphaImageLoader
           var src = base.$el.children('img').attr('src');
           base.$el.css({ 
             filter: "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "', sizingMethod='scale')" 
           });
         }
         
         base.$el.draggable({ stack: { group: '.sticky', min: 50 } });
         base.$el.mouseenter(base.onMouseEnter);
         base.$el.mouseleave(base.onMouseLeave);
      }
      
      base.onMouseEnter = function(event) {
        base.$el.bind('mousedown', base.onMouseDown);
      }
      
      base.onMouseLeave = function(event) {
        base.$el.unbind('mousedown', base.onMouseDown);
      }
      
      base.onMouseDown = function(event) {
        var min = 50;
        $('.sticky').each(function() {
          if (base.$el.get(0) != this) $(this).css({ zIndex: min++ });
        })
        base.$el.css({ zIndex: min++ });
      }
      
      base.init();
    }
  

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