function closeImgBox() {
    $("#imgBoxZoom").hide();
    $("#imgBox").animate({
        opacity: 0
    }, 250, "linear", function() {
    $("#imgBoximg").remove();
    $("#imgBox").hide();
    });
    $("#imgBackground").animate({
        opacity: 0
    }, 500, "linear", function() {
        $("#imgBackground").hide();
    });
    $("#imgBoxLoader").hide();
}
$(window).resize(function() {
resizeImgBox()
});
var imgBoxLoadedPicW=10
var imgBoxLoadedPicH = 10
var imgBoxScaling =1
function resizeImgBox() {
    var mW = $("#imgBackground").width() * 0.8;
    var mH = $("#imgBackground").height() * 0.8;
    var w = imgBoxLoadedPicW;
    var h = imgBoxLoadedPicH;
    w = (w > mW) ? mW : w;
    h = (h > mH) ? mH : h;
    if (mW / mH < w / h) {
        imgBoxScaling = imgBoxLoadedPicW / w;
        $("#imgBoximg").width(w);
        h = $("#imgBox img").height()
    } else {

        imgBoxScaling = imgBoxLoadedPicH / h;
        $("#imgBoximg").height(h);
        w = $("#imgBox div img").width()
    }
    $("#imgBox div").width(w + 40);
    $("#imgBox div").height(h + 80);
    $("#imgBox").css("left", ($("#imgBackground").width() - $("#imgBox div").width()) / 2);
    $("#imgBox").css("top", ($("#imgBackground").height() - $("#imgBox div").height()) / 2);
}
var zoomSize = 80;
function moveImgBoxZoom(e) {
    var offset = $("#imgBoximg").offset();
    $("#imgBoxZoom").css("margin-left", e.pageX - (zoomSize / 2));
    $("#imgBoxZoom").css("margin-top", e.pageY - (zoomSize / 2));
    //console.log(((offset.left - e.pageX) * imgBoxScaling).toString() + " - " + imgBoxScaling.toString());
    $("#imgBoxZoom div div img").css("position", "absolute");
    $("#imgBoxZoom div div img").css("left", ((offset.left - e.pageX) * imgBoxScaling) + (zoomSize / 2));
    $("#imgBoxZoom div div img").css("top", ((offset.top - e.pageY) * imgBoxScaling) + (zoomSize / 2));

}
function createImgBoxZoom() {
    return;
    $("#imgBoxZoom").show();
    $("#imgBoxZoom").width(zoomSize+13);
    $("#imgBoxZoom").height(zoomSize+15);
    $("#imgBoxZoom div div").width(zoomSize);
    $("#imgBoxZoom div div").height(zoomSize);
    $("#imgBoxZoom div div img").attr("src", $("#imgBoximg").attr("src"));
    $("#imgBoximg").mousemove(moveImgBoxZoom);
    $("#imgBoxZoom").mousemove(moveImgBoxZoom);
}
function openImgBox(img) {
    $("#imgBackground").css("opacity", 0);
    $("#imgBackground").show();
    $("#imgBoxLoader").css("opacity", 0);
    $("#imgBoxLoader").show();
    $("#imgBoxLoader").animate({
        opacity: 1
    }, 300);
    $("#imgBackground").animate({
        opacity: 0.7
    }, 500, "linear", function() {

        $("#imgBox div").prepend('<img id="imgBoximg" />');
        $("#imgBoximg").load(function() {
            $("#imgBoximg").mouseover(function() {
                createImgBoxZoom();
            });
            $("#imgBoximg").mouseout(function() {
                //$("#imgBoxZoom").fadeOut("fast");
            });
            $("#imgBoxLoader").hide();
            $("#imgBox").css("opacity", 0);
            $("#imgBox").show()
            imgBoxLoadedPicW = $("#imgBoximg").width()
            imgBoxLoadedPicH = $("#imgBoximg").height()
            resizeImgBox();
            $("#imgBox").animate({
                opacity: 1
            }, 500);
        });

        $("#imgBoximg").attr("src", img);


    });
}
jQuery(function($) {


    $("body").append('<div id="imgBackground" />');
    $("#imgBackground").hide();
    $("body").prepend('<div id="imgBoxLoader">Loading...</div>');
    $("#imgBoxLoader").hide();
    $("body").append('<div id="imgBox"><div><p><a href="#" onclick="closeImgBox()">Close</a></p></div></div>');
    $("#imgBox").hide();
    $("body").append('<div id="imgBoxZoom"><div><div><img /></div></div></div>');
    $("#imgBoxZoom").hide();
    $("#imgBackground").click(closeImgBox);
    $("a[rel^='lightbox']").click(function() {
        openImgBox($(this).attr("href"))

        return false;
    });
});
