$(document).ready(function() {
  $("#light_source, #div1, #div2").draggable({
    scroll: false,
    drag: function() { make_shade() }
  });
  
  if ($.browser.msie) $("#div1, #div2").after("<div class='ie-shadow'></div>");
  make_shade();  
});

function make_shade() {
  var light = $("div#light_source");
  var light_pos = light.position();
  var light_x = light_pos.left + (light.width() / 2);
  var light_y = light_pos.top + (light.height() / 2);
  
  $(".needs_shadow").each(function() {
    var div1 = $(this);
    var div1_pos = div1.position();
    
    var div_x = div1_pos.left + (div1.width() / 2);
    var div_y = div1_pos.top + (div1.height() / 2);
    
    var left_diff = light_x - div_x;
    var top_diff = light_y - div_y;
    
    var left = (left_diff / 10) * -1;
    var top = (top_diff / 10) * -1;
    
    var distance = Math.sqrt(Math.pow(left_diff, 2) + Math.pow(top_diff, 2));
    distance = distance / 10;
    
    shadow_style = left + "px " + top + "px " + distance + "px #3f3f3f";
    div1.css("-moz-box-shadow", shadow_style);
    div1.css("-webkit-box-shadow", shadow_style);
    div1.css("box-shadow", shadow_style);
    
    if ($.browser.msie) {
      if (distance > 50) distance = 50;
      div1.next(".ie-shadow").width(div1.width()).height(div1.height()).css("left", parseInt(div1_pos.left + left) + "px").css("top", parseInt(div1_pos.top + top) + "px");
      div1.next(".ie-shadow").css("filter", "progid:DXImageTransform.Microsoft.Blur(PixelRadius='" + distance + "', MakeShadow='true', ShadowOpacity='0.40')");
    }
  });
}
