﻿/******************************************************************************
popupHoverIn
******************************************************************************/
this.popupHoverIn = function(e) {
    //Acquire the src2 filename and abort the popup if it's not set
    var fileName = $(this).attr("src2");
    if (fileName == "") return;

    //Move the title to prevent the tool tip conflicting with our popup
    this.tempTitle = this.title;
    this.title = "";
    var caption = (this.tempTitle != "") ? "<br/>" + this.tempTitle : "";

    //Add the popup div
    $("body").append("<div id='imageView'><img src='" + fileName + "' alt='Image preview' />" + caption + "</div>");

    var imageView = $("#imageView");
    imageView
            .css("position", "absolute")
            .css("border", "1px solid #ccc")
            .css("background", "#005A88")
            .css("padding", "5px")
            .css("display", "none")
            .css("color", "#fff")
			.css("top", (e.pageY) + "px")
			.css("left", (e.pageX + 10) + "px")
			.css("width", "300px")
			.fadeIn("fast");


    //Flip the preview above the cursor if we're close to the bottom of the screen
    var documentHeight = $(document).height();
    var divHeight = $("#imageView").outerHeight();
    if (e.pageY + divHeight > documentHeight) imageView.css("top", e.pageY - 0 - divHeight);

    //Fire the popupResize method when the image is finished loading
    $("#imageView img").load(popupResize);
};

/******************************************************************************
popupResize
******************************************************************************/
this.popupResize = function() {
    //Resize the div to fit the image
    $("#imageView").css("width", $("#imageView img").outerWidth());
};

/******************************************************************************
popupHoverOut
******************************************************************************/
this.popupHoverOut = function() {
    //Remove the popup
    $("#imageView").remove();
    //Move the title back
    if (this.tempTitle) this.title = this.tempTitle;
};

/******************************************************************************
popupMouseMove
******************************************************************************/
this.popupMouseMove = function(e) {
    var imageView = $("#imageView");
    var documentHeight = $(document).height();
    imageView
        .css("top", (e.pageY) + "px")
		.css("left", (e.pageX + 10) + "px");
    var divHeight = imageView.outerHeight();
    if (e.pageY + divHeight > documentHeight) imageView.css("top", e.pageY - 0 - divHeight);
};

/******************************************************************************
popupInit
******************************************************************************/
this.popupInit = function() {
    var popupIcon = $(".popupIcon")
    popupIcon.hover(popupHoverIn, popupHoverOut);
    popupIcon.mousemove(popupMouseMove);
};

/******************************************************************************
onReady
******************************************************************************/
$(document).ready(function() {
    popupInit();
});