﻿
function getPhotoUrl(id, width, height) {

}

function initFotoGallery(imgId, href, photoArray) {
    if (photoArray.length > 0) {

        var img = $("#" + imgId);

        var photoSrc = photoArray[0].url;
        var parent = img.parent()[0];
        parent.photoArray = photoArray;
        parent.lastIndex = 0;
        img[0].src = photoSrc;
        ieResize(imgId, photoArray[0], 200, 150);
        img.show();
        if (photoArray.length > 1) {
            setInterval("switchImage('" + imgId + "','" + href + "');", 10000);
        }

        for (var index = 0; index < photoArray.length; index++) {
            var tempImg = new Image(dimArray[index].x, dimArray[index].y);
            tempImg.id = "tempImageId";
            tempImg.src = photoArray[index].url;
            _doIeResize(tempImg, photoArray[index], 200, 150);
        }
    }
    else {
        $("#" + imgId).hide();
    }
}

function switchImage(imgId, href) {
    var img = document.getElementById(imgId);
    if (img) {
        var jimg = $(img);
        var parent = jimg.parent()[0];
        var newIndex = parent.lastIndex;
        var counter = parent.photoArray.length * 10;
        while (newIndex == parent.lastIndex && counter > 0) {
            newIndex = Math.floor((Math.random() * parent.photoArray.length));
            counter--;
        }

        if (newIndex != parent.lastIndex) {
            jimg.fadeOut("slow", function() {
                img.src = parent.photoArray[newIndex].url;
                ieResize(imgId, parent.photoArray[newIndex], 200, 150);
                var a = document.getElementById(href);
                if (a) {
                    a.index = newIndex;
                }
            });
        }
        jimg.fadeIn("slow");
        parent.lastIndex = newIndex;
    }
}

function ieResize(imgId, dim, maxx, maxy) {
    var elem = document.getElementById(imgId);
    if (!elem || !dim) return;
    _doIeResize(elem, dim, maxx, maxy);
}

function _doIeResize(elem, dim, maxx, maxy) {
    var ratioX = maxx / dim.x;
    var ratioY = maxy / dim.y;

    var smallerRatio = Math.min(ratioX, ratioY);

    var newX = dim.x * smallerRatio;
    var newY = dim.y * smallerRatio;

    var newTop = maxy / 2 - (newY / 2);
    elem.style.marginTop = newTop + "px";
}


function resize(which, dim, max, maxy) {
    var elem = document.getElementById(which);
    if (elem == undefined || elem == null) return false;
    max = Math.min(max, maxy);
    if (elem.width > elem.height) {
        if (elem.width > max) elem.width = max;
    } else {
        if (elem.height > max) elem.height = max;
    }
}


function simpleMakeCheckAll(cbxName, context, exclude) {
    $(document).ready(function() {
        $("#" + cbxName).click(function(e) {
            var cbx = e.srcElement;
            if (!cbx)
                cbx = e.currentTarget;
            cbx = $(cbx);
            if (cbx.attr("checked")) {
                $("input:checkbox[class!='" + exclude + "']", $("#" + context)).attr("checked", "checked");
                $("label[for=" + cbx[0].id + "]").text("Odznacz wszystkie");
            }
            else {
                $("input:checkbox[class!='" + exclude + "']", $("#" + context)).removeAttr("checked");
                $("label[for=" + cbx[0].id + "]").text("Zaznacz wszystkie");
            }
        });
    });
}


$(document).ready(function() {
    $('.detail_list .item .text').textOverflow("...");
});
