/*
 * 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
 *
 */
 
jQuery.fn.center = function () {
    this.css("position","absolute");
    this.css("top", ( $(window).height() - this.height() ) / 2+$(window).scrollTop() + "px");
    this.css("left", ( $(window).width() - this.width() ) / 2+$(window).scrollLeft() + "px");
    return this;
}
 
 
this.screenshotPreview = function(){	
	/* CONFIG */
		
	var xOffset = 10;
	var yOffset = 30;
	var self = this;
    var blockObj = null;

    var blockMouseOver = false;
	// these 2 variable determine popup's distance from the cursor
	// you might want to adjust to get the right result
	debug = function(aEvent){
        //$("#debug").append("<p>" + aEvent+ "</p>");
    };
        
    
    removeBlock = function(){
        $("#screenshot").fadeOut("fast");
        $("#screenshot").remove();
    }

    createBlock = function(aHref, aSrc){
        removeBlock();
        $("body").append("<p id=\"screenshot\"><a href=\"" + aHref + "\"><img src=\""+ aSrc +"\" alt=\"\" border=\"0\"/></a></p>");                                 
    }
        
    getBlockObj = function(){
        var obj = $("#screenshot")[0];
        if (obj){
          return obj.childNodes[0].childNodes[0];
        }
        
        return 0;
    }    
	/* END CONFIG */
	$(".screenshot").mouseover(function(){
        debug('.screenshot mouse over');
        
        if (blockMouseOver) return 0;
        
        //creating block
        objCreating = true;
        
        var linkUrl = $(this).attr("linkUrl");
        if (!linkUrl)linkUrl = this.href;
        
        var imgUrl = $(this).attr("imgUrl");
        if (!imgUrl)imgUrl = this.rel;
        
        if (imgUrl)createBlock(linkUrl, imgUrl);

        //image mouse out
        $("#screenshot").mouseout(function(){
            debug('#screenshot mouseout');
         
            removeBlock();
            
            blockMouseOver = false;
        });
        //
    

        if ($("#screenshot").width()==0) {
            blockMouseOver = false;
            return 0;
        }   
         
        $("#screenshot").center().fadeIn("fast");
        blockMouseOver = true;

        //end creating
    },

    $(".screenshot").mouseout(function(e){
        debug('screenshot mouseout');
        if (e.relatedTarget != getBlockObj()) {
            removeBlock();

            blockMouseOver = false;
        }    
    }),
	
    function(){
		$("#screenshot").remove();
    });	

   
/*	$("a.screenshot").mousemove(function(e){
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px");
	});	*/
};


// starting the script on page load
$(document).ready(function(){
	screenshotPreview();
    //$("body").append("<div id='debug' style=\"position: absolute; width: 400px; background-color: #fff; height: 250px; overflow: scroll; left: 0; top: 0\"></div>");                                 
});
