(function($) {
    $.fn.tawImagePreview = function(options) {

        var defaults = {
            targetContainerId: '',
            targetImageContainerId: '',
            targetImageContainerErrorClass: '',
            targetDescriptionId: '',
            max_width: '100px',
            max_height: '100px',
            offsetLeft: '0px',
            offsetRight: '0px',
            offsetTop: '0px',
            offsetBottom: '0px',
            substractCssSize: false,
            substractPadding: false,
            substractBorder: false,
            float: "right",
            delay: 0
        };

        var options = $.extend(defaults, options);

        return this.each(function() {

            var alt = $(this).attr("alt");
            var title = $(this).attr("title");
            var longdesc = $(this).attr("longdesc");


            var resizePreloadingBox = function(magnifier, image) {

                var offset = $(magnifier).offset();
                var offset_left = checkFloatDirection(offset, parseInt(options.max_width));
                var offset_top = offset.top - parseInt(options.max_height);


                $("#" + options.targetImageContainerId).width(options.max_width).height(options.max_height);

                $("#" + options.targetContainerId).css({
                    "position": "absolute",
                    "top": offset_top + "px",
                    "left": offset_left + "px",
                    "width": options.max_width,
                    "height": "auto"
                });

                var tmp_height = $("#" + options.targetContainerId).height();
                offset_top = offset.top - tmp_height;
                $("#" + options.targetContainerId).css({ "top": offset_top + "px" });

                $("#" + options.targetContainerId).show();

            }



            var resizeLoadedBox = function(magnifier, image) {

                $(image).width("auto").height("auto");
                var img_width = $(image).width();
                var img_height = $(image).height();

                if (img_width > parseInt(options.max_width)) {
                    var factorX = parseInt(options.max_width) / img_width;
                    img_width = Math.round(parseInt(options.max_width));
                    img_height = Math.round(img_height * factorX);
                }
                if (img_height > parseInt(options.max_height)) {
                    var factorY = parseInt(options.max_height) / img_height;
                    img_height = Math.round(parseInt(options.max_height));
                    img_width = Math.round(img_width * factorY);
                }

                if (img_width <= 0) img_width = options.max_width;
                if (img_height <= 0) img_height = options.max_height;

                var infobox_width = img_width - parseInt($("#" + options.targetDescriptionId).css("paddingLeft")) - parseInt($("#" + options.targetDescriptionId).css("paddingRight"))

                $(image).width(img_width + "px").height(img_height + "px");
                $("#" + options.targetImageContainerId).width(img_width + "px").height(img_height + "px");
                $("#" + options.targetDescriptionId).width(infobox_width + "px");
                $("#" + options.targetContainerId).width(img_width);

                var offset = $(magnifier).offset();
                var offset_left = checkFloatDirection(offset, img_width);
                var offset_top = offset.top - parseInt(options.max_height);

                $("#" + options.targetContainerId).height("auto").css({ "top": (offset.top - $("#" + options.targetContainerId).height()) + "px" });
                $("#" + options.targetContainerId).show();
            }


            var checkFloatDirection = function(offset, img_width) {
                if (options.float == "right") {
                    var offset_left = offset.left + parseInt(options.offsetRight);

                } else if (options.float == "left") {
                    var offset_left = offset.left - parseInt(options.offsetLeft) - img_width;

                } else if (options.float == "dynamic") {
                    if (offset.left < ($(window).width() / 2)) {
                        var offset_left = offset.left + parseInt(options.offsetRight);
                    } else {
                        var offset_left = offset.left - parseInt(options.offsetLeft) - img_width;
                    }
                }
                return offset_left;
            }


            var stillMouseOver = false;
			
            $(this).hover(
				function() {
					
					stillMouseOver = true;
					
				    $("#" + options.targetContainerId).hide();
				    $(this).attr("title", "");
				    var magnifier = this;
				    var image = $("#" + options.targetImageContainerId + " img");

				    window.tawImagePreviewTimer = setTimeout(function() {

				        resizePreloadingBox(magnifier, image);

				        $("#" + options.targetDescriptionId).html("");
				        $.post(longdesc, function(data) {
							if(stillMouseOver == true){
								$("#" + options.targetDescriptionId).replaceWith('<div id="' + options.targetDescriptionId + '">' + data + "</div>");
								resizeLoadedBox(magnifier, image);
							}
				        });


				        $(image).attr("src", title);
				        $(image).bind('load', function() {
				            $(image).unbind('load');
				            resizeLoadedBox(magnifier, image);
				        }).error(function() {
				            $("#" + options.targetImageContainerId).html('<div class="' + options.targetImageContainerErrorClass + '">' + options.targetImageContainerErrorMessage + '</div>')
				        });
				    }, options.delay);

				},
				function() {
				    clearTimeout(window.tawImagePreviewTimer);
					stillMouseOver = false;
				    $(this).attr("title", title);
				    $("#" + options.targetContainerId).hide();
				    $("#" + options.targetImageContainerId).html('<img src="" alt="" />');
				}
			);
        });
    };
})(jQuery);
