var enable_three_sixty = function() {

  $('.product_image_360').each(function(){
    $this = $(this);

    // Initial Setup
    $steps = $this.find('li');
    $num_steps = $steps.size();

    $index = 1;     // Currently displayed LI
    mdown = false;  // Current mouse state

    // Display first frame
    $steps.hide();
    $steps.filter(':first').show();

    // Get multiplier for zoom
    zoomImage = new Image();
    zoomImage.src = $steps.filter(':nth-child('+$index+')').attr('bigsrc');

    // Prelaod all zoom images
    if ($("#product_image_360_zoom")) {
      var myimages = new Array();
      for (x=0; x<$steps.size(); x++){
        myimages[x] = new Image();
        myimages[x].src = $steps.filter(':nth-child('+x+')').attr('bigsrc');
      }
    }

    // Slider
    $this.parent().find(".product_image_360_slider").slider({
      value: 1,
      min: 1,
      max: $num_steps,
      slide: function(event, ui) {
        $index = 36 - ui.value;
        rotate();
      }
    });

    // Mouse functions
    $this.mousedown(function(e){
      e.preventDefault(); // firefox/ie crazyness
      mdown = true;
      posXoriginal = e.pageX;
    });

    $this.mouseup(function(e){
      if(mdown){
        mdown = false;
      }
    });

    $this.mouseleave(function(e){
      mdown = false;
      $("#product_image_360_zoom").fadeOut('fast');
    });

    $this.mousemove(function(e){
      if(mdown){
        px_per_step =  parseInt($this.width() / $this.find('li').size() / 2, 10);
        var moveto = parseInt((e.pageX - posXoriginal)/px_per_step, 10);

        if(moveto >= 1){
          posXoriginal = e.pageX;
          $index = $index - 1;
          rotate();
        }
        if(moveto <= -1){
          posXoriginal = e.pageX;
          $index = $index + 1;
          rotate();
        }
      } else {
        if ($("#product_image_360_zoom")) {
          // Moved here because of Firefox initial page load wierdness.
          $liFirstImage = $steps.filter(':nth-child('+$index+')').find('img:first');
          $width_multiplier = zoomImage.width / $liFirstImage.width();
          $height_multiplier = zoomImage.height / $liFirstImage.height();
          //$image_center_adjust_width = ((300 - $liFirstImage.width()) / 2 );
          //$image_center_adjust_height = ((300 - $liFirstImage.height()) / 2 );

          // calculated on the fly because users like to resize windows.
          $image_offsetLeft = $liFirstImage.offset().left;
          $image_offsetTop = $liFirstImage.offset().top;

          posX = -( (e.pageX - $image_offsetLeft) * $width_multiplier - 157 );
          posY = -( (e.pageY - $image_offsetTop)  * $height_multiplier - 157 );

          $("#product_image_360_zoom").fadeIn('fast');
          $("#product_image_360_zoom").css({'background':'url(\'' + $steps.filter(':nth-child('+$index+')').attr('bigsrc') + '\') ' + posX + 'px ' + posY + 'px white no-repeat'});
        }
      }

    });

    var rotate = function(){
      // Make sure rotations stay withing number of steps
      if ($index < 1 ) { $index = $num_steps; }
      if ($index > $num_steps ) { $index = 1; }

      // Hide all the steps
      $steps.hide();
      // Show the new desired step
      $steps.filter(':nth-child('+$index+')').show();
      // Update slider
      $this.parent().find(".product_image_360_slider").slider( 'value' , (36-$index) );
    };

  });
};
