/*
 * 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 calvalue(e,url)
	{
	  
		var t = document.documentElement.clientHeight - 400;
		var f = e.pageY - xOffset;
		var k = 0;
		var imgh;
	//	alert( document.body.clientHeight);
//    y = self.innerHeight;

		y = document.body.clientHeight;
	//	alert(document.getElementById("imgh"));
		//imgh = document.getElementById("imgh").clientheigth;
		//alert(imgh);http://localhost:8080/file:/E:/phpproject/Listings%20Results%20-%20Valley%20By%20Owner.htm?DBGSESSID=-1
		var img = new Image(); 
        img.src =url; 
//alert(img.width + 'x' + img.height); 
        imgh= img.height;

		var ScrollTop = document.body.scrollTop;

		
            if (ScrollTop == 0)

            {

                if (window.pageYOffset)

                    ScrollTop = window.pageYOffset;

                else

                    ScrollTop = (document.body.parentElement) ? document.body.parentElement.scrollTop : 0;

            }
        y =  ScrollTop + y;
        //if(imgh)
	if(img){   
	//alert("dfdff"+y);
	//alert("hi");
		var difH = (f + img.height) - y;
		//alert(img.height);
		
	}
        //alert(y);
		if(difH > 0)
		{
		    k = f - difH - 10;
		}
		else{
		
		    k = f;
		}

		//alert(k);
        return k;
	}
	
this.imagePreview = function(){	
	/* CONFIG */
		
		xOffset = 10;
		yOffset = 30;
		
		// 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 : "";
		//alert(this.href);
		$("body").append("<p id='preview'><img id=\"imgh\"' src='"+ this.href +"' alt='Image preview' />"+ c +"</p>");
		
							 
							
var k =calvalue(e,this.href);
		$("#preview")
			.css("top",k + "px")
			.css("left",(e.pageX + yOffset) + "px")
			.fadeIn("fast");
			
									
    },
	function(){
		this.title = this.t;	
		$("#preview").remove();
    });	
	$("a.preview").mousemove(function(e){
	
		
		var k =calvalue(e,this.href);
		$("#preview")
			.css("top",k + "px")
			.css("left",(e.pageX + yOffset) + "px");
	});			
};


// starting the script on page load
$(document).ready(function(){
	imagePreview();
});
