/*
 * 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 */
		
		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 : "";
		$("body").append("<p id='preview'><img src='"+ this.href +"' alt='Image preview' />"+ c +"</p>");	

		var h=450;
		var ps=getPageScroll();
	  	var ph=getPageHeight();
	  	var fh=ph-h;
	  	var w=getPageWidth();
		
	  	var y=ps[1]+fh/2;
	  	var x=e.pageX+xOffset;	
		if(x<e.pageX && e.pageX<x+450)
		{
			x+=e.pageX-x+10;
		}
		if(x+450>w-70)
		{
			x-=520;
		}
		$("#preview")
			.css("top",y + "px")
			.css("left",x + "px")
			.fadeIn("fast");						
    },
	function(){
		this.title = this.t;	
		$("#preview").remove();
    });	
	$("a.preview").mousemove(function(e){		
		
		var h=450;
		var ps=getPageScroll();
	  	var ph=getPageHeight();
	  	var fh=ph-h;
	  	var w=getPageWidth();
		
	  	var y=ps[1]+fh/2;
	  	var x=e.pageX+xOffset;	
		if(x<e.pageX && e.pageX<x+450)
		{
			x+=e.pageX-x+10;
		}
		if(x+450>w-70)
		{
			x-=520;
		}
	  	
		
		$("#preview")
			.css("top",y + "px")
			.css("left",x + "px");
	});			
};

function getPageScroll() {
    var xScroll, yScroll;
    if (self.pageYOffset) {
      yScroll = self.pageYOffset;
      xScroll = self.pageXOffset;
    } else if (document.documentElement && document.documentElement.scrollTop) {	 // Explorer 6 Strict
      yScroll = document.documentElement.scrollTop;
      xScroll = document.documentElement.scrollLeft;
    } else if (document.body) {// all other Explorers
      yScroll = document.body.scrollTop;
      xScroll = document.body.scrollLeft;	
    }
    return new Array(xScroll,yScroll) 
  }

  // Adapted from getPageSize() by quirksmode.com
  function getPageHeight() {
    var windowHeight
    if (self.innerHeight) {	// all except Explorer
      windowHeight = self.innerHeight;
    } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
      windowHeight = document.documentElement.clientHeight;
    } else if (document.body) { // other Explorers
      windowHeight = document.body.clientHeight;
    }	
    return windowHeight
  }
  function getPageWidth() {
    var windowWidth
    if (self.innerWidth) {	// all except Explorer
      windowWidth = self.innerWidth;
    } else if (document.documentElement && document.documentElement.clientWidth) { // Explorer 6 Strict Mode
      windowWidth = document.documentElement.clientWidth;
    } else if (document.body) { // other Explorers
      windowWidth = document.body.clientWidth;
    }	
    return windowWidth
  }


// starting the script on page load
$(document).ready(function(){
	imagePreview();
});