/*
 * 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
 *
 */

this.screenshotPreview = function () {
    /* CONFIG */
    inPreviw = false;
    xOffset = 0;
    yOffset = 0;

    // these 2 variable determine popup's distance from the cursor
    // you might want to adjust to get the right result

    /* END CONFIG */
    $("a.screenshot").click(function () {
        
        var t = this.title;
        var c = (t != "") ? "<br/>" + t : "";

        /* append the image preview to the body and call positionScreenshot() on img.load and  removeAllScreenshots() on click */
        $("body").append("<p id='screenshot' class='screenshotPreviewFrame' onclick='removeAllScreenshots()' ><img src='" + this.rel + "' alt='"+t+"' onload='positioningScreenshot(" + xOffset + "," + yOffset + ")'  class='screenshotPreviewFrame' onclick='removeAllScreenshots()' style='display:none'/>" + c + "</p>");
        $("body").append("<div id='screenshotLayer' class='screenshotPreviewFrame' onclick='removeAllScreenshots()'></div>");

    });
};


/* function to remove all displayed image previews */

function removeAllScreenshots() {

    $("p.screenshotPreviewFrame").each(function (index) {
        $(this).remove();
    });

    $("#screenshotLayer").remove();
}

/* function to positioning the image preview in the middle of the display */

function positioningScreenshot(xOffset, yOffset) {
    //load display dimensions
    var windowSize = getPageDimensions();
    var windowWidth = windowSize[0];
    var windowHeight = windowSize[1];

    //load image
    var img = $("#screenshot img");

    // calculate top and left for the image using the hidden input '#ScrollTop'
    var left = ((windowWidth - img.width()) / 2) - xOffset;
    var top = ((windowHeight - img.height()) / 2) - yOffset;

    //positioning and styling the image

  
    
    $("#screenshotLayer")
        .css("top", "0px")
        .css("bottom", "0px")
        .css("left", "0px")
        .css("right", "0px")
        .css("position","fixed")
        .css("background-color", "#000")
        .css("opacity", "0.7")
        .css("z-index", "1000");

    if ($.browser.msie && parseInt($.browser.version, 10) == 7) {
        $("#screenshotLayer").css("filter", "Alpha(Opacity=70)");
    }

    $("p#screenshot")
            .css("top", top + "px")
            .css("left", left + "px")
            .css("position", "fixed")
            .css("opacity", "1")
            .css("z-index", "1100")
            .fadeIn("fast");
    $("p#screenshot img")
        .css("border", "1px solid #111")
        .css("display", "inline");



}

// starting the script on page load
$(document).ready(function(){
	screenshotPreview();
});
