/*
 * 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 */
		
		var xOffset = 15;
		var yOffset = 0;
		var yOffsetDefault = yOffset;
    var xOffsetDefault = xOffset;
		
		// these 2 variable determine popup's distance from the cursor
		// you might want to adjust to get the right result
	
    var windowHeight = window.innerHeight ? window.innerHeight : document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body.clientHeight;
    var windowWidth = window.innerWidth ? window.innerWidth : document.documentElement.clientWidth ? document.documentElement.clientWidth : document.body.clientWidth;
    
    var imgPadding = 40; /* extra offset so image border doesn't go beyond bottom window edge */
    var cssMaxWidth = 550;
	
    var curHeight;
    var curWidth;

	/* END CONFIG */
	
	/* preload the images */
	$("a.imgPreview").each(function(){
    var newImg = new Image();
    $(newImg).load().attr('src', this.id);
	});
	
	$("a.imgPreview").hover(function(e){
		this.t = this.title;
		this.title = "";	
		var c = (this.t != "") ? "<br/>" + this.t : "";
    imgPadding = (this.t != "") ? ((this.t.length > 120) ? 80 : 60) : 40;
		var imgAlt = $(this).find('img').attr("alt");
		$("body").append("<p style='font-size:15px' id='preview'><img src='"+ this.id +"' alt='"+ imgAlt +"' />"+ c +"</p>");
		
		//get Image Size
		var newImg = new Image();
    $(newImg).load(function(){
      curHeight = newImg.height;
      curWidth = newImg.width;
      /* get height based on CSS max-width; step 2 kill IE */
      if (curWidth > cssMaxWidth) {
        curHeight = Math.round(newImg.height * (cssMaxWidth / curWidth));
        curWidth = cssMaxWidth;
      }
    }).attr('src', this.id);
    
		if (curHeight + imgPadding >  windowHeight - e.clientY) {
      yOffset = windowHeight - e.clientY - curHeight - imgPadding;
		}
		else {
      yOffset = yOffsetDefault;
		}
		
		$("#preview")
			.css("top",(e.pageY + yOffset) + "px")
			.css("left",(e.pageX + xOffset) + "px")
			.fadeIn("fast");						
    },
	function(){
		this.title = this.t;	
		$("#preview").remove();
    });	
	$("a.imgPreview").mousemove(function(e){
    // prevent image from extending off bottom of the page
    if (curHeight + imgPadding > windowHeight - e.clientY) {
      yOffset = windowHeight - e.clientY - curHeight - imgPadding;
		}
		else {
      yOffset = yOffsetDefault;
		}
    
    // flip flop image on left or right of mouse
    if (e.clientX > (windowWidth / 2)){
      xOffset = 0 - curWidth - 20;
      //alert (e.clientX + ' - ' + curWidth + ' - 15';);
		}
		else {
      xOffset = xOffsetDefault;
		}

		$("#preview")
			.css("top",(e.pageY + yOffset) + "px")
			.css("left",(e.pageX + xOffset) + "px");
	});			
};


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