/*
 * 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.imagePreview = function(){	
	/* CONFIG */
		
		//xOffsetPreview = 50;
		//yOffsetPreview = -230;
		
		// 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.hoversrc = this.getAttribute('hoversrc');
		if (this.hoversrc) { src_new = this.hoversrc; } else { src_new = this.href; } 
		this.title = "";	
		var c = (this.t != "") ? "<br/>" + this.t : "";
		$("body").append("<div id='preview'><img src='"+ src_new +"' alt='Image preview' border='0' />"+ c +"</div>");
		$("#preview")
			.css("top",(e.pageY - xOffsetPreview) + "px")
			.css("left",(e.pageX + yOffsetPreview) + "px")
			.fadeIn("fast");						
    },
	function(){
		this.title = this.t;	
		$("#preview").remove();
    });	
	$("a.preview").mousemove(function(e){
		$("#preview")
			.css("top",(e.pageY - xOffsetPreview) + "px")
			.css("left",(e.pageX + yOffsetPreview) + "px");
	});
	$("a.preview").click(function(e){
	  $("#preview").remove();
	});
};

function previewPreloader()
{
  var ar = document.getElementsByTagName('a');
  var img_cache = new Array();
  var j = 0;

  for (var x = 0; ar[x]; x++) 
  {
    var im = ar[x];
		var hoversrc = im.getAttribute('hoversrc');
		if (hoversrc)
		{
  		img_cache[j] = new Image();
			img_cache[j].src = hoversrc;
			j++;
		}
  }
}

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