/*
 * 	Easy Tooltip 1.0 - jQuery plugin
 *	written by Alen Grakalic	
 *	http://cssglobe.com/post/4380/easy-tooltip--jquery-plugin
 *
 *	Copyright (c) 2009 Alen Grakalic (http://cssglobe.com)
 *	Dual licensed under the MIT (MIT-LICENSE.txt)
 *	and GPL (GPL-LICENSE.txt) licenses.
 *
 *	Built for jQuery library
 *	http://jquery.com
 *
 */
 
(function($) {

	$.fn.easyTooltip = function(options){
	  
		// default configuration properties
		var defaults = {	
			xOffset: 10,		
			yOffset: 25,
			tooltipId: "easyTooltip",
			clickRemove: false,
			content: "",
			useElement: "",
                        delay: 1000
		}; 
			
		var options = $.extend(defaults, options);  
		var content;
				
		this.each(function() {  				
			var title = $(this).attr("title");				
			$(this).hover(function(e){											 							   
				content = (options.content != "") ? options.content : title;
				content = (options.useElement != "") ? $("#" + options.useElement).html() : content;
				$(this).attr("title","");									  				
				if (content != "" && content != undefined){			
					$("body").append("<div id='"+ options.tooltipId +"'>"+ content +"</div>");		
					$("#" + options.tooltipId)
						.css("position","absolute")
						.css("top",(e.pageY - options.yOffset) + "px")
						.css("left",(e.pageX + options.xOffset) + "px")						
						.css("display","none")
//						.fadeIn("fast")
                                        if (options.delay > 0) setTimeout(function() { $("#" + options.tooltipId).fadeIn("fast"); }, options.delay);
                                        else $("#" + options.tooltipId).fadeIn("fast");
				}
			},
			function(){	
				$("#" + options.tooltipId).remove();
				$(this).attr("title",title);
			});	
			$(this).mousemove(function(e){
				$("#" + options.tooltipId)
					.css("top",(e.pageY - options.yOffset) + "px")
					.css("left",(e.pageX + options.xOffset) + "px")					
			});	
			if(options.clickRemove){
				$(this).mousedown(function(e){
					$("#" + options.tooltipId).remove();
					$(this).attr("title",title);
				});				
			}
		});
	  
	};


        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){
                        this.t = this.title;
                        this.title = "";
                        var c = (this.t != "") ? "<br/>" + this.t : "";
                        var href = $(this).children().filter("span").html();
                        //alert(href);
                        $("body").append("<p id='preview'><img src='"+ href +"' alt='Загрузка изображения...' />"+ c +"</p>");
                        if ($("#preview").width() > window.screen.availWidth-200) $("#preview").add("#preview img").width(window.screen.availWidth-200);
                        if ($("#preview").height() > window.screen.availHeight-200) $("#preview").add("#preview img").height(window.screen.availHeight-200);
                        yOffset = $("#preview").width() + 20;
                        xOffset = $("#preview").height();
                        if (e.pageY - document.documentElement.scrollTop < xOffset)
                        {
                            if (document.documentElement.scrollTop + window.screen.availHeight - e.pageY < xOffset)
                                xOffset = e.pageY - document.documentElement.scrollTop;
                            else xOffset = -10;
                        }
                        if (e.pageX - window.scrollX < yOffset) yOffset = -20;
                        $("#preview")
                                .css("top",(e.pageY - xOffset) + "px")
                                .css("left",(e.pageX - yOffset) + "px")
                                .fadeIn("fast");
            },
                function(){
                        this.title = this.t;
                        $("#preview").remove();
            });
                $(".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);
