/*
 * 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
 *
 */
/* 
this.getHeight=function(){
  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;
  }
  window.alert( 'Width = ' + myWidth );
  window.alert( 'Height = ' + myHeight );
}
*/



this.imagePreview = function(){	
	var style=
	"display:none;position:absolute;top:0px; left:0px;border:1px solid #ccc;background:#333;padding:5px;display:none;color:#fff;z-index:20;margin:0px;";
	jQuery("body").append("<p id='preview' style='"+style+"'><img src=''  alt='Image preview' /><br /><span>&nbsp;</span></p>");
	jQuery("#preview").hide();
	/* CONFIG */
		
		xOffset = 10
		yOffset = 20;
		
		// these 2 variable determine popup's distance from the cursor
		// you might want to adjust to get the right result
		
	/* END CONFIG */
	function setPossitionOfPreview(e){
		var t=0;
		var l=0;
		var h=jQuery("#preview").height();
		var wh=jQuery(window).height();
		if (wh-e.screenY<h)
			t=e.pageY-(e.screenY-(wh-h));
		else
			t=e.pageY - xOffset;
		//if ((e.screenY)<jQuery(window).height()/2)
	/*		if (e.screenY>h+xOffset)
				t=e.pageY - xOffset;
			else
				t=e.pageY - xOffset+(e.screenY-h-xOffset);
		//else
		//	t=e.pageY - xOffset - jQuery("#preview").height();
		*/
		
		if ((e.screenX)<jQuery(window).width()/2)
			l=e.pageX + yOffset;
		else
			l=e.pageX - yOffset - jQuery("#preview").width();
		jQuery("#preview")
			.css("top",(t) + "px")
			.css("left",(l) + "px");
	}
	
	jQuery("a.preview").hover(
		function(e){
			this.t = this.title;
			this.title = "";	
			var c = (this.t != "") ? '<br/>'+ this.t : "";
			jQuery('#preview').html("<img src='"+ this.href +"' alt='Image preview' />"+ c);
			jQuery("#preview").fadeIn("fast");
			setPossitionOfPreview(e);


		},
		function(){
			this.title = this.t;	
			jQuery("#preview").hide();
		});
	
	jQuery("a.preview").mousemove(function(e){
		setPossitionOfPreview(e);
	});
	
	jQuery("#preview").mousemove(function(){
		this.title = this.t;	
		jQuery("#preview").hide();
	});
};


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