/*
 * Url 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
 *
 */
 var myWidth = 0, myHeight = 0;
 var imgWidth=400 , imgHeight =500 ;
 var scrOfX = 0, scrOfY = 0;
 var newXPos;
 var newYPos;
 
this.screenshotPreview = 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.screenshot").hover(function(e){
									 
		//alert ("mouse: " + e.pageX +" - "+ e.pageY);
		calculateSize();
		calculateScrollXY();
		//window.alert( 'Width = ' + myWidth + 'Height = ' + myHeight );
		
		newXPos = e.pageX + yOffset;
		newYPos = e.pageY - xOffset;
		
		if (newXPos+imgWidth > myWidth) {
			//alert ('here 1 - '+newXPos);
			newXPos = e.pageX - 400 - 50; //myWidth - imgWidth - 10;
		}
 		
		//myHeight = myHeight + scrOfY;
		newYPos = newYPos - scrOfY;
		if (newYPos+imgHeight > myHeight) {
			//alert ('here 2 - newYPos:'+ newYPos +" imgHeight:" + imgHeight +" myHeight:" + myHeight );
			//newYPos = e.pageY - myHeight - imgHeight - 10;
			newYPos = newYPos - (imgHeight - (myHeight - newYPos)) - 30;
			//alert (" newYPos:"+newYPos);
		}
 		newYPos = newYPos + scrOfY;
		
		this.t = this.title;
		this.title = "";	
		var c = (this.t != "") ? "<br/>" + this.t : "";
		$("body").append("<p id='screenshot'><img src='"+ this.rel +"' alt='url preview' width='auto' height='auto' />"+ c +"</p>");								 
		$("#screenshot")
			.css("top",(newYPos) + "px")
			.css("left",(newXPos) + "px")
			.fadeIn("fast");						
    },
	function(){
		this.title = this.t;	
		$("#screenshot").remove();
    });	
	$("a.screenshot").mousemove(function(e){
		//alert (' here now 2');
		$("#screenshot")
			.css("top",(newYPos) + "px")
			.css("left",(newXPos) + "px");
	});			
};


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

function calculateSize() {	
	
	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
	}
}

function calculateScrollXY() {
  
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  //return [ scrOfX, scrOfY ];
}
