function getX(obj, width){
	var iLeft = obj.offsetLeft;
	var oParent = obj.offsetParent;
	
	while(oParent.nodeName != 'BODY' && oParent.nodeName != 'HTML'){
		iLeft += oParent.offsetLeft;
		oParent = oParent.offsetParent;
	}

	if((iLeft + width + jQuery("#JQPreview1").attr("width") + 10) > jQuery(window).width()){
		return (iLeft - jQuery("#JQPreview1").attr("width") - 10) + "px";
	}else{
		return (iLeft + width + 10) + "px";
	}
}
 
function getY(obj, height, e){
	var iTop = obj.offsetTop;
	var oParent = obj.offsetParent;
	
	while(oParent.nodeName != 'BODY' && oParent.nodeName != 'HTML'){
		iTop += oParent.offsetTop;
		oParent = oParent.offsetParent;
	}
	
	var imgY = e.pageY - iTop;
	
	
		//alert(jQuery(window).height() + " " + ((e.clientY-+mgY) + jQuery("#JQPreview1").attr("height")));
	
	if(((e.clientY-imgY) - (jQuery("#JQPreview1").attr("height") - 10)) > 0){
		return (iTop - jQuery("#JQPreview1").attr("height") - 10) + "px";
	}else{
		//return (iTop + height + 10) + "px";
		if(((e.clientY+imgY) + jQuery("#JQPreview1").attr("height")) < jQuery(window).height()){
			return (iTop + height + 10) + "px";
		}else{
			return (iTop - ((jQuery("#JQPreview1").attr("height")/2) - (height/2))) + "px";
		}
	}
		
}
 
this.imagePreview = function(){	
	jQuery(".imgPrev").hover(function(e){
		if(this.id != ""){
			myWidth = this.width;
			myHeight = this.height;
			
			var img = this;
//			var dynamicHeight = parseInt(img.attr("height") * (450/img.attr("width")));
//			alert(img.attr("width"));

			//jQuery("body").append("<img src='" + this.id + "' style='width: 450px; height: "+dynamicHeight+"px' alt='Loading Image...' id='JQPreview1' />");
			jQuery("body").append("<img src='" + this.id + "' style='width: 450px;' alt='Loading Image...' id='JQPreview1' />");
			
			jQuery("#JQPreview1").load(function(){
				jQuery("#JQPreview1")
				.css("top",getY(img, myHeight, e))
				.css("left",getX(img, myWidth))
				.css("position","absolute");								
			});

			//jQuery("#JQPreview1")
//				.css("top",getY(this, myHeight, e))
//				.css("left",getX(this, myWidth))
//				.css("position","absolute");
		}
    },
	function(){
		jQuery("#JQPreview1").remove();
    });
};

jQuery.noConflict();  

jQuery(document).ready(function(){ 
	imagePreview();
});
