///<reference path="jquery-1.3.2-vsdoc.js">
///<reference path="jquery.scale.js">

$(document).ready(function(event) {

    $("#closeInfoDiv").click(function(event) {
        if (GLargeImgFullSize) {
            CloseLargeImgWithAnimation();
        }
        jQuery.Event(event).stopPropagation();
    })


    $("body").click(function(event) {
        if (($("#LargePhotoInfoDiv").css("visibility") != "hidden") && (GLargeImgFullSize)) {
            CloseLargeImgWithAnimation();
        }
        jQuery.Event(event).stopPropagation();
    });


});

function CloseLargeImgWithAnimation() {

    if (GNoImage != true) {

        var LargeInfoImg = $("#LargeInfoImg");
        beginWidth = LargeInfoImg.width();
        beginHeight = LargeInfoImg.height();
        GLargeImgFullSize = false;


        if (!Gcenter) {
            $("#LargeInfoImg").animate({ width: GMedInfoImg.width() + 'px', height: GMedInfoImg.height() + 'px' }, 300, function() {
                $("#LargePhotoInfoDiv").css("visibility", "hidden");

                LargeInfoImg.height(beginHeight);
                LargeInfoImg.width(beginWidth);

            });
        }
        else {
            $("#LargePhotoInfoDiv").css("visibility", "hidden");

            LargeInfoImg.height(beginHeight);
            LargeInfoImg.width(beginWidth);
        }
    }
}

var GNoImage = false;
var GLargeImgFullSize = false;
var GMedInfoImg = null;
var Gcenter;


// LargeImgPath - путь к большой картинке, sender - объект маленькой картинки, senderId (для кнопки Zoom) - Id маленькой картинки
function GetMedImagePosition(LargeImgPath, sender, center, senderId) {

    Gcenter = center;

    (senderId != null) ? GMedInfoImg = $("#" + senderId) : GMedInfoImg = $(sender);

    var img = new Image();

    var LargeInfoImg = $("#LargeInfoImg");

    LargeInfoImg.load(function() {
        LoadImage(center);
    });

    // описание проблемы события onload для кешированных рисунков в IE
    // http://www.onyrix.com/computer-science/languages/javascript/javascript-onload-image-cache-problem-with-internet-explorer
    $(img).load(function() {

        LargeInfoImg.width(img.width);
        LargeInfoImg.height(img.height);

        LargeInfoImg.attr("src", "");

        LargeInfoImg.attr("src", LargeImgPath);
    });

    img.src = LargeImgPath;    
}

function LoadImage(center) {

    var LargeInfoImg = $("#LargeInfoImg");
    var beginWidth = LargeInfoImg.width();
    var beginHeight = LargeInfoImg.height();

    var LargePhotoInfoDiv = $("#LargePhotoInfoDiv");

    if (!GNoImage && !GLargeImgFullSize) {
        
        var MedInfoImgPos = GMedInfoImg.position();

        var LargeInfoImgPos = LargeInfoImg.position();

        var ResPos = MedInfoImgPos;

        if (Gcenter) {
            var scroll = getScrollXY();
            
            ResPos.left = (document.body.clientWidth / 2 + scroll[0]) - LargePhotoInfoDiv.width() / 2;
            ResPos.top = (document.documentElement.clientHeight / 2 + scroll[1]) - (LargePhotoInfoDiv.height() / 2);

            // уменьшаем большую картинку до маленькой
            LargeInfoImg.width(GMedInfoImg.width());
            LargeInfoImg.height(GMedInfoImg.height());

        }
        else {
            // уменьшаем большую картинку до маленькой
            LargeInfoImg.width(GMedInfoImg.width());
            LargeInfoImg.height(GMedInfoImg.height());
        
            ResPos.left = parseFloat(MedInfoImgPos.left) - parseFloat(LargeInfoImgPos.left);
            ResPos.top = parseFloat(MedInfoImgPos.top) - parseFloat(LargeInfoImgPos.top);
        }

        LargePhotoInfoDiv.css("left", (ResPos.left) + "px");
        LargePhotoInfoDiv.css("top", (ResPos.top) + "px");

        LargePhotoInfoDiv.css("visibility", "visible");

        if (!Gcenter) {
            LargeInfoImg.animate({ width: beginWidth + 'px', height: beginHeight + 'px' }, 300, function() {
                GLargeImgFullSize = true;
            });
        }
        else {
            LargeInfoImg.height(beginHeight);
            LargeInfoImg.width(beginWidth);
        }
        GLargeImgFullSize = true;
    }
}
