// Place your application-specific JavaScript functions and classes here

/* Product page */
var selected_detail_image = null;
var detail_image_thumb_width = 66+28;
var detail_images_list_current = 0;
var detail_images_list_length = 0;
var detail_images_list_effect = null;

function initialize_detail_images () {
  var detail_image = $('detail_image');
  var magnify = $('detail_image_magnify'),
    huge_img = magnify.down('img');
  var offset = detail_image.cumulativeOffset();
  var over_header = false, enter, leave;
   
  function hide () {
    magnify.style.visibility = "hidden";
  }
  
  if ( Prototype.Browser.IE ) {
    enter = "mouseenter";
    leave = "mouseleave";
  } else {
    enter = "mouseover";
    leave = "mouseout";
  }
  
  $('header').observe(enter, function () { over_header = true; hide(); });
  $('header').observe(leave, function () { over_header = false; });
  
  Event.observe(document, 'mousemove', function ( event ) {
    var width = detail_image.offsetWidth,
      height = detail_image.offsetHeight,
      x = event.pointerX() - offset.left,
      y = event.pointerY() - offset.top,
      max_x_range = huge_img.offsetWidth - magnify.offsetWidth,
      max_y_range = huge_img.offsetHeight - magnify.offsetHeight;     
    
    if ( over_header
        || ( huge_img.offsetWidth <= width + 10
          && huge_img.offsetHeight <= height + 10 ) ) {
      hide();
      return;
    }
       
    if ( x < -20 || x > width + 20 ) {
      magnify.style.visibility = "hidden";
      return;
    } else if ( x < 0 ) {
      x = 0;
    } else if ( x > width ) {
      x = width;
    }
    
    if ( y < -20 || y > height + 20 ) {
      magnify.style.visibility = "hidden";
      return;
    } else if ( y < 0 ) {
      y = 0;
    } else if ( y > height ) {
      y = height;
    }
    
    magnify.style.visibility = "visible";
    
    huge_img.style.left = Math.floor(-max_x_range*x/width) + "px";
    //if (height > 1000){alert(height);
    huge_img.style.top = Math.floor(-max_y_range*y/height-40) + "px";
    //}
    //else
    //huge_img.style.top = Math.floor(-max_y_range*y/height) + "px";
    //alert(magnify.style.left);alert(magnify.style.top);
    magnify.style.left = (width-magnify.offsetWidth)*x/width + "px";
    magnify.style.top = (height-magnify.offsetHeight)*y/height + "px";
  });
}