// Katalog - aktualizacja licznika
function change_count() {
    if(current_images_box > 1) {
        var prev_images_box_count = (prev_images_box)*20;
        if(prev_images_box_count>total_images) {
            prev_images_box_count = total_images;
        }                            

        $("#galleryList .nav .prev span").text((((prev_images_box)*20)-20+1)+"-"+prev_images_box_count);
        $("#galleryList .nav .prev").removeClass("prevNonAct");
    }
    else {
        $("#galleryList .nav .prev span").text("");
        $("#galleryList .nav .prev").addClass("prevNonAct");
    }

    var current_fotos_total = current_images_box*20;
    if(current_fotos_total>total_images) {
        current_fotos_total = total_images;
    }

    $("#galleryList .nav .index span").text("["+((current_images_box*20)-20+1)+"-"+current_fotos_total + " " + from_text + " " + total_images+"]");

    if(current_images_box<total_images_box) {       
        var next_images_box_count = (next_images_box)*20;
        if(next_images_box_count>total_images) {
            next_images_box_count = total_images;
        }

        $("#galleryList .nav .next span").text(((next_images_box*20)-20+1)+"-"+next_images_box_count);
        $("#galleryList .nav .next").removeClass("nextNonAct");
    }
    else {
        $("#galleryList .nav .next span").text("");
        $("#galleryList .nav .next").addClass("nextNonAct");
    }   
}   

// Katalog - klawisz poprzednie miniatury
$("#galleryList .prev").click(function () { 
    if(current_images_box>1) {
        current_images_box--;
        prev_images_box--;
        next_images_box--;
        change_count();
    }
    return false;
});

// Katalog - klawisz nastepne miniatury
$("#galleryList .next").click(function () {  
    if(current_images_box<total_images_box) {
        current_images_box++;
        prev_images_box++;
        next_images_box++;
        change_count();
    }
    return false;
});        

varShowOverlayGallery = 0;

$(window).resize(function() {
    if(varShowOverlayGallery) {
        var windowWidth = $(window).width();
        var windowHeight = $(window).height();
        setDimensionsOverlayGallery(windowWidth,windowHeight);
    }
});

function showOverlayGallery(url) {
    $('html').css("overflow", "hidden");

    varShowOverlayGallery = 1;
    var windowWidth = $(window).width();
    var windowHeight = $(window).height();

    /*  DODAJEMY KOD HTML  */
    var toInject  = "";

    toInject  = '<div id="showOverlayGallery_background"> </div>';
    toInject += '<div id="showOverlayGallery_content"> </div>';

    $('body').append(toInject);

    /*  DEFINIUJEMY CSS-y  */
    $("#showOverlayGallery_background").css('display',"block");
    $("#showOverlayGallery_content").css('display',"block");
    $("#showOverlayGallery_content").css('display',"block");  

    setDimensionsOverlayGallery(windowWidth, windowHeight);

    $("#showOverlayGallery_background").css("background", "#000");
    $("#showOverlayGallery_background").css("z-index", "9500");

    $("#showOverlayGallery_content").css("z-index", "9501");

    $("#showOverlayGallery_background").css('opacity',0).fadeTo(100,0.94);
    $("#showOverlayGallery_content").css('opacity',0).fadeTo(1000,1);

    /* POBIERAMY GRAFIKĘ */
    $.ajax({
        type: "POST",
        url: url,
        data: '',
        success: function(msg){
            $("#showOverlayGallery_content").html(msg);     

            $("#GalleryLightbox .galleryPhoto").css("overflow", "auto");
            $("#GalleryLightbox .galleryPhoto .showPhoto").css("padding", "0px");
            $("#GalleryLightbox .galleryPhoto .head").css("text-align", "center");
            setDimensionsOverlayGallery(windowWidth, windowHeight);
        }
    });  

    $(document).keypress(function(e) {
        switch(e.which){
            case 32:
            return false;
            break;
        };
        switch(e.keyCode){
            case 27:
            hideOverlayGallery();
            break;
        };
    });  

    $("#showOverlayGallery_content").click(function () {  
        //hideOverlayGallery();
    });
}

function hideOverlayGallery() {
    $("#showOverlayGallery_background").css('display',"none");
    $("#showOverlayGallery_content").css('display',"none");
    $("#showOverlayGallery_content").css('display',"none");
    $('html').css("overflow", "auto");

    varShowOverlayGallery = 0;
}

function setDimensionsOverlayGallery(windowWidth, windowHeight) {
    $("#showOverlayGallery_background").css("position", "absolute");
    $("#showOverlayGallery_background").css("width", windowWidth);
    $("#showOverlayGallery_background").css("height", windowHeight);
    $("#showOverlayGallery_background").css("top", "0");
    $("#showOverlayGallery_background").css("left", "0");

    $("#showOverlayGallery_content").css("position", "absolute");
    $("#showOverlayGallery_content").css("width", windowWidth);
    $("#showOverlayGallery_content").css("height", windowHeight);
    $("#showOverlayGallery_content").css("top", "0");
    $("#showOverlayGallery_content").css("left", "0");

    $("#GalleryLightbox .galleryPhoto").css("width", windowWidth);
    $("#GalleryLightbox .galleryPhoto").css("height", windowHeight-70);
}

// Polozenie strzalek - poprzednie/nastepne zdjecie
function position_arrow() {  
    $("#GalleryLightbox .galleryPhoto .photo").css("margin","0px auto 0px auto");
    $("#GalleryLightbox .galleryPhoto .photo").css("width",$(".galleryPhoto .photo .showPhoto img").width());
    $("#GalleryLightbox .galleryPhoto .photo").css("height",$(".galleryPhoto .photo .showPhoto img").height());

    $(".galleryPhoto .nav_photo div:nth-child(1)").removeClass("prev_photo").addClass("prev_photo");      
    $(".galleryPhoto .nav_photo div:nth-child(2)").removeClass("next_photo").addClass("next_photo");

    $(".galleryPhoto .nav_photo .prev_photo").css('display',"block");
    $(".galleryPhoto .nav_photo .next_photo").css('display',"block");

    $(".galleryPhoto .nav_photo .prev_photo").css('height',$(".galleryPhoto").height()-200);
    $(".galleryPhoto .nav_photo .prev_photo .bg").css('height',$(".galleryPhoto").height()-200);
    $(".galleryPhoto .nav_photo .prev_photo .ar").css('height',$(".galleryPhoto").height()-200);

    $(".galleryPhoto .nav_photo .next_photo").css('height',$(".galleryPhoto").height()-200);
    $(".galleryPhoto .nav_photo .next_photo .bg").css('height',$(".galleryPhoto").height()-200);
    $(".galleryPhoto .nav_photo .next_photo .ar").css('height',$(".galleryPhoto").height()-200);

    $(".galleryPhoto .nav_photo .next_photo").css('top',(($(window).height()-$(".galleryPhoto .nav_photo .next_photo").height())/2)+15);
    $(".galleryPhoto .nav_photo .next_photo .bg").css('top',(($(window).height()-$(".galleryPhoto .nav_photo .next_photo").height())/2)+15);
    $(".galleryPhoto .nav_photo .next_photo .ar").css('top',(($(window).height()-$(".galleryPhoto .nav_photo .next_photo").height())/2)+15);

    $(".galleryPhoto .nav_photo .prev_photo").css('top',(($(window).height()-$(".galleryPhoto .nav_photo .prev_photo").height())/2)+15);
    $(".galleryPhoto .nav_photo .prev_photo .bg").css('top',(($(window).height()-$(".galleryPhoto .nav_photo .prev_photo").height())/2)+15);
    $(".galleryPhoto .nav_photo .prev_photo .ar").css('top',(($(window).height()-$(".galleryPhoto .nav_photo .prev_photo").height())/2)+15);

    $(".galleryPhoto .nav_photo .prev_photo .bg").css('opacity',0.5);
    $(".galleryPhoto .nav_photo .next_photo .bg").css('opacity',0.5);

    if(current_image==total_images) $(".galleryPhoto .nav_photo div:nth-child(2)").removeClass("next_photo");    
    if(current_image==1) $(".galleryPhoto .nav_photo div:nth-child(1)").removeClass("prev_photo");    
    if (update_arrows==true) {      
        $("#galleryListZoom .nav .prev_gallery_zoom").show();
        $("#galleryListZoom .nav .next_gallery_zoom").show();
        if(current_image==total_images) $("#galleryListZoom .nav .next_gallery_zoom").hide();    
        if(current_image==1) $("#galleryListZoom .nav .prev_gallery_zoom").hide();
    }
}

// Zamienienie duzego zdjecia i aktualizacja licznika
function change_image() {
    $("#GalleryLightbox .galleryPhoto .photo .showPhoto img").fadeOut("fast",function(){
        $(".galleryPhoto .photo .showPhoto img").attr("src",$("#photo_" + current_image).attr("href")).load(function() {
            $(".galleryPhoto .index span").text(current_image + " " + from_text + " " + total_images);
            $(".galleryPhoto .photo .showPhoto img").fadeIn("fast");
            position_arrow();
            change_act_thumb();         
        });
    });
    return;
}

// Ustawia aktywna miniature
function change_act_thumb() { 
    $("#listImgBoxZoom .act").removeClass("act");
    $("#listImgBoxZoom #thumb_"+current_image).addClass("act");
}

// Klikniecie na jakas miniaturke
$("#listImgBoxZoom a").click(function () {
    tmp = current_image;
    current_image = Number($(this).attr("id").substr(6));
    if(tmp!=current_image) {
        update_arrows = false;    
        change_image();
        change_act_thumb();
    }
    return false;
});

// Klawisz kolejne zdjecie
$(".nav_photo  .prev_photo").click(function () {  
    if(current_image>1) {
        update_arrows=true;    
        current_image--;
        change_image();
    }
    return false;
});

// Klawisz poprzednie zdjecie
$(".nav_photo .next_photo").click(function () {   
    if(current_image<total_images) {
        update_arrows=true;    
        current_image++;
        change_image();
    }
    return false;
});

$(".galleryPhoto .photo .showPhoto img").click(function () {   
    if(current_image<total_images) {
        update_arrows=true;    
        current_image++;
        change_image();
    }
    return false;
});
