/*
 * Image preview script 
 * powered by jQuery (http://www.jquery.com)
 * 
 * written by Alen Grakalic (http://cssglobe.com)
 * 
 * for more info visit http://cssglobe.com/post/1695/easiest-tooltip-and-image-preview-using-jquery
 *
 */

function getWindowSize() {
  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;
  }
  return [ myWidth, myHeight ];
}

function getScrollXY() {
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  return [ scrOfX, scrOfY ];
}
 
this.imagePreview = function(){	
	/* CONFIG */
		
		xOffset = 20;
		yOffset = 10;
		marginTop = 180;
		
		// these 2 variable determine popup's distance from the cursor
		// you might want to adjust to get the right result
		
	/* END CONFIG */
	$("a.preview").hover(function(e){
		this.t = this.title;
		this.title = "";	
		var c = (this.t != "") ? "<br/>" + this.t : "";

		var d = getScrollXY();
		var s = getWindowSize();
		var dY = (e.pageY - yOffset);
		if ((dY - marginTop) < d[1]) dY = d[1] + marginTop;
		
		$("body").append("<p id='preview'><img id='im_most' src='"+ this.href +"' alt='Image preview' />"+ c +"</p>");
		//alert(document.getElementById('im_most').height + ' a ' + document.getElementById('im_most').width);
		$("#preview")
			.css("top",(dY) + "px")
			.css("left",(e.pageX + xOffset) + "px")
			.fadeIn("fast");						
    },
	function(){
		this.title = this.t;	
		$("#preview").remove();
    });	
	$("a.preview").mousemove(function(e){
		//alert(document.getElementById('im_most').height + ' b ' + document.getElementById('im_most').width);

		var d = getScrollXY();
		var dY = (e.pageY - yOffset);
		if ((dY - marginTop) < d[1]) dY = d[1] + marginTop;
		
		var s = getWindowSize();
		var dX = (e.pageX + xOffset);
		var wi = document.getElementById('im_most').width;
		if ((dX + wi) > s[0]) dX -= (2*xOffset + wi);
		if (dX < 0) dX = (e.pageX + xOffset);

		$("#preview")
			.css("top",(dY) + "px")
			.css("left",(dX) + "px");
	});			
};


// starting the script on page load
$(document).ready(function(){
	imagePreview();
});