
var LBH = { /* light box hover */
  
  cursor_elm   : null,
  grabber_elm  : null,
  zoom_elm     : null,
  zoom_img_elm : null,
  offset_x     : -1,
  offset_y     : -1,
  img_width    : null,
  img_height   : null,
  img_file     : '',

  mousemove : function(e) {

    e.stopPropagation();

    if(LBH.offset_x == -1) {
      // this is a hack.
      LBH.offset_x = LBH.grabber_elm.offset().left; //offsetLeft;
      LBH.offset_y = LBH.grabber_elm.offset().top; //offsetTop;
    }

    var x = e.pageX - LBH.offset_x - 50;
	  var y = e.pageY - LBH.offset_y - 50;
    
    if(x <   0) x =   0;
    if(x > 220) x = 220;
    if(y <   0) y =   0;
    if(y > 141) y = 141;

    LBH.cursor_elm.show();
    LBH.cursor_elm.css( { 'left': String(x) + 'px', 'top': String(y) + 'px' } );

    if(!LBH.zoom_elm.visible) {
      LBH.__get_new_image();
      LBH.zoom_elm.fadeIn(200);
      LBH.zoom_elm.visible = true;
    }

    if(!LBH.img_width) {
      LBH.img_width  = LBH.zoom_img_elm.width();
      LBH.img_height = LBH.zoom_img_elm.height();
    }

    var xofs = (x / 220) * (LBH.img_width  - 300);
    var yofs = (y / 141) * (LBH.img_height - 300);

    LBH.zoom_img_elm.css( { 'left': String(-xofs) + 'px', 'top': String(-yofs) + 'px' } );
  },

  __get_new_image : function() {

    var filename = $('#full_product_graphic').css( 'background-image' );
    //filename = filename.slice(5);
    //filename = filename.slice(0, filename.length - 2);
    
    filename = filename.replace( /^url\("?([^"]*)"?\).*$/, "$1");

    filename = OPL.__rstrsub(filename, 'full', 'zoom');
    
    LBH.zoom_img_elm.attr( 'src', filename );
  },

  __image_refresh : function() {
    LBH.img_width  = LBH.zoom_img_elm.width();
    LBH.img_height = LBH.zoom_img_elm.height();
  },

  resizewindow : function() {

    if(!LBH.grabber_elm) return;

    LBH.offset_x = -1; //LBH.grabber_elm.offset().left; //offsetLeft;
    LBH.offset_y = -1; //LBH.grabber_elm.offset().top; //offsetTop;
  },

  mouseout : function(e) {

    e.stopPropagation();

    LBH.cursor_elm.hide();
    LBH.zoom_elm.fadeOut(100);
    LBH.zoom_elm.visible = false;
  },

  init : function() {

    $(window).resize(LBH.resizewindow);

    LBH.cursor_elm = $('#product_hover_cursor');
    LBH.zoom_elm = $('#product_zoom_box');
    LBH.zoom_img_elm = $('img#product_zoom_image');
    LBH.grabber_elm = $('#product_hover_grabber');
    
    LBH.zoom_img_elm.load(LBH.__image_refresh);

    LBH.grabber_elm.mousemove(LBH.mousemove).mouseout(LBH.mouseout);

    LBH.zoom_elm.visible = false;

    LBH.resizewindow();
  }
};

$(LBH.init);


