/*
 * 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($) {
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 */
	$(".preview").hover(function(e){
        //$("body").append("<p id='preview'><img id='preview_image' src='' alt='Image preview' /></p>");
        jQuery("<p id='preview'><img id='preview_image' src='' alt='Image preview' /></p>").appendTo('body');
        $('#preview_image').load(function() {
          //form http://stackoverflow.com/questions/318630/get-real-image-width-and-height-with-javascript-in-safari-chrome
          // Remove attributes in case img-element has set width and height
          //$(this).removeAttr("width")
//                .removeAttr("height")
//                .css({ width: "", height: "" }); // Remove css dimensions as well
//          var width = $(this).width();
//
          //alert(width);
          //if (width>100) {
//            $(this).css('width','100px');
          //}
        }).attr("src", this.src);


        var pageWidth = $(document).width();
        //console.log("calc:" + (0+ e.pageX + yOffset + 400));
        //console.log("pw:" + pageWidth);

        if ((0 + e.pageX + yOffset + 400) > pageWidth){
           yOffset =  0 - 400;
        }
        //console.log("yO:" + yOffset);

		$("#preview")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px")
			.fadeIn("fast");
    },
	function(){
		this.title = this.t;
		$("#preview").remove();
    });
    /*
	$("a.preview").mousemove(function(e){
		$("#preview")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px");
	});
    */
};


// starting the script on page load
$(document).ready(function(){
	imagePreview();
});

})(jQuery);

