function mouseX(e) {
  return e.clientX;
}
function mouseY(e) {
  return e.clientY;
}

this.imagePreview = function(){
  xOffset = 10;
  yOffset = 30;
  imgWidth = 450;
  imgHeight = 350;
  var xPosition = 0, yPosition = 0;
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  dirX = myWidth - xOffset - imgWidth;
  var img = $('img.products_image').get(0);
  $("a.products_image").hover(function(e) {
    this.t = this.title;
    xPosition = mouseX(e);
    yPosition = mouseY(e);
    if(yPosition < (myHeight / 2)) {
      top = yPosition + yOffset;
    }
    else
    {
      top = yPosition - yOffset - imgHeight;
    }
    if(xPosition < dirX) {
      left = xPosition + xOffset;
    }
    else
    {
      left = xPosition - xOffset - imgWidth;
    }
    $("body").append("<div id='preview'><img class='products_image' src='"+ this.rel +"' alt='"+ this.t +"' width='400'/><a href='"+ this.href +"' title='"+ this.t +"'>"+ this.t +"<br/><!-- w:"+ myWidth +";h:"+ myHeight +"<br/>w:"+ mouseX(e) +";h:"+ mouseY(e) +"<br/>w:"+ xPosition +";h:"+ yPosition +"--></a></div>");
    $("#preview")
    .css("top",top + "px")
    .css("left",left + "px")
    .css("display","block")
    .css("position","absolute")
    .fadeIn("slow")
    .tooltip({ efect: 'slide'});
//     $("#preview img").load({
//       
//     })
//     $("#preview img").lazyload({
//       placeholder : "/img/img_load2.gif",
//       efect       : "fadeIn"
//     });
  },
  function() {
    this.title = this.t;
    $("#preview").fadeOut("slow").remove();
  });
  $("a.products_image").mousemove(function(e){
    xPosition = mouseX(e);
    yPosition = mouseY(e);
    if(yPosition < (myHeight / 2)) {
      top = e.pageY + yOffset;
    }
    else
    {
      top = e.pageY - yOffset - imgHeight;
    }
    if(xPosition < dirX) {
      left = e.pageX + xOffset;
    }
    else
    {
      left = e.pageX - xOffset - imgWidth;
    }
    $("#preview")
    .css("left",left +"px")
    .css("top",top +"px");
  });
};

$(document).ready(function(){
  imagePreview();
});
$(function() {
  $("img.products_image").lazyload({
    placeholder : "/img/img_load2.gif",
    efect       : "fadeIn"
  });
});
$(window).bind("load", function() {
  var timeout = setTimeout(function() {$("img").trigger("sporty")}, 5000);
});


