
var currentImage = 0;
var totalImages = 0;
var image_title = new Array();
var image_desc = new Array();
var image_file = new Array();
var gallery_show = 0;
var upload_image_id = 0;

/*
$(document).ready(function(){
    $("#menu li").hover(function () {
        $(this).css({ backgroundImage:"url('/images/button_s.jpg')" });
    }, function () {
        $(this).css({ backgroundImage:"url('/images/button.jpg')" });
    });
});
*/

function showPage(number) {
    $("#content").load("content_" + number + ".php");
}

function showAdmin(page, parms) {
    $("#content").load("admin_" + page + ".php" + parms);
}

function showGallery(number, gallery) {
    gallery_show = gallery;
    $("#content").load("content_" + number + ".php?gallery_id=" + gallery, getGalleryData);
}

function getGalleryData() {
    $.get("gallery_images.php", { gallery_id: gallery_show },
        function(data){
            galleryLoaded(data);
        });
}

function showImage() {
    $("#galleryTitle").html(image_title[currentImage]);
    $("#galleryDesc").html(image_desc[currentImage]);
    $("#image").attr({
        src: "/gallery/" + image_file[currentImage],
        title: image_title[currentImage],
        alt: image_title[currentImage]
    });

    $("#image_link").attr({
        href: "/gallery/big/" + image_file[currentImage],
        title: image_title[currentImage]
    });

    $("#galleryImage a").fancybox({ 'hideOnContentClick': true });


}

function navGallery(direction) {
    if (direction == 'prev') {
        if (currentImage > 0) {
            currentImage--;
        }
        if (currentImage == 0) {
            $("#galleryNavPrev").hide();
        }
        if (totalImages > 1) {
            $("#galleryNavNext").show();
        }
    } else if (direction == 'next') {
        if (currentImage < totalImages - 1) {
            currentImage++;
        }
        if (currentImage == totalImages - 1) {
            $("#galleryNavNext").hide();
        }
        if (currentImage > 0) {
            $("#galleryNavPrev").show();
        }
    }
    showImage();
}

function galleryLoaded(gallery_data) {
    image_id = new Array();
    image_title = new Array();
    image_desc = new Array();
    image_file = new Array();

    var tmp = gallery_data.split("####");
    for(i = 0; i <= tmp.length - 1; i++) {
        var img = tmp[i].split("|");
        image_id[i] = img[0];
        image_title[i] = img[1];
        image_desc[i] = img[2];
        image_file[i] = img[3];
    }

    totalImages = tmp.length;
    currentImage = 0;

    if (currentImage == 0) {
        $("#galleryNavPrev").hide();
    }
    if (totalImages <= 1) {
        $("#galleryNavNext").hide();
    }
    showImage();
}

function sendForm(post_url) {

    var request = $("form").serialize();
    $.post(post_url, request, function(data){
            formResponse(data);
        });
}

function formResponse(resp_data) {
    if (resp_data.substr(0, 14) == 'gallery_images') {
        var tmp = resp_data.split("|");
        uploadImage(tmp[1]);
    } else if (resp_data.substr(0, 7) == 'contact') {
        var tmp = resp_data.split("|");
        $("#contact_message").text(tmp[1]);
    } else {
        $("#message").text(resp_data);
    }
}

function uploadImage(image_id) {
    upload_image_id = image_id;
    ajaxFileUpload();
}

function ajaxFileUpload() {
    if (upload_image_id < 1) {
        alert('Brak ID zdj�cia');
    }
    //starting setting some animation when the ajax starts and completes
    $("#loading")
    .ajaxStart(function(){
        $(this).show();
    })
    .ajaxComplete(function(){
        $(this).hide();
    });

    $.ajaxFileUpload (
        {
            url:'admin_file_upload.php?image_id=' + upload_image_id,
            secureuri:false,
            fileElementId:'image_file',
            dataType: 'json',
            success: function (data, status) {
                if (typeof(data.error) != 'undefined') {
                    if(data.error != '') {
                        $("#message").html(data.error);
                    } else {
                        $("#message").html(data.msg);
                    }
                }
            },
            error: function (data, status, e) {
                alert(e);
            }
        }
    )
    return false;
}

function refreshFtp() {
    showAdmin('upload', '');
}
