var imagePath = '/_mod_files/ce_images';
var imageNum = -1;
var oldImageNum = 0;
var sectionIndex = 1;
var imagesIDs = Array();
var imagesFiles = Array();
var imagesFolders = Array();
var imagesDescs = Array();
$(document).ready(function(){
    $.ajax({
        type: "GET",
        url: "/gallery.xml",
        dataType: "xml",
        success: function(xml) {
            var i = 0;
            $(xml).find('folder').each(function() {
                var quant = $(this).find('quant').text();
                if (quant > 0) {
                    var folderName = $(this).find('name').text();
                    var folder = $(this).find('cat').text();
                    $('#sections').append('<div id="gal_s' + (i + 1) + '"><span class="gal_title">' + folderName + '</span><span class="gal_quantity">' + quant + ' фото</span></div>');
                    var j = 0;
                    var IDs   = Array();
                    var descs = Array();
                    var files = Array();
                    
                    $(this).find('images').children().each(function() {
                        var file = $(this).attr('file');
                        IDs[j] = $(this).attr('id');
                        descs[j] = $(this).text();
                        files[j] = file;
                        $("#preload").append('<img src="' + imagePath + '/' + folder + '/' + file + '" />');
                        j++;
                    });
                    imagesIDs[i] = IDs;
                    imagesDescs[i] = descs;
                    imagesFiles[i] = files;
                    imagesFolders[i] = folder;
                }
                i++;
            });
        }, async: false
    });
    
    $("#sections div").hover(
        function() {
            $(this).css("background-image", "url('/_img/gallery/gallery_band_active.gif')")
                .css("color", "#fee79f")
                .effect("pulsate", { times: 1 }, 300);
        },
        function() {
            if (sectionIndex != this.id.slice(5)) {
                $(this).css("background-image", "none")
                    .css("color", "#ad9a6b");
            }
        }
    );
    
    $("#sections div").click(function() {
        $('#sections div').css("background-image", "none")
            .css("color", "#ad9a6b");
        $(this).css("background-image", "url('/_img/gallery/gallery_band_active.gif')")
            .css("color", "#fee79f");
        oldSectionIndex = sectionIndex;
        sectionIndex = this.id.slice(5);
        var id = imagesIDs[sectionIndex - 1];
        imageNum = 0;
        oldImageNum = 0;
            
        $("#gallery #description").html('<p>' + imagesDescs[sectionIndex - 1][imageNum] + '</p>');
        var cssObj = {
            "background-image" : "url('" + imagePath + '/' + imagesFolders[oldSectionIndex - 1] + '/' + imagesFiles[oldSectionIndex - 1][oldImageNum] + "')",
            "background-repeat" : "no-repeat"
        }
        $("#images").css(cssObj);
        $("#image").html('<div id="flash"></div>');
        $("#flash").fadeOut(1500);
        $("#image").append("<img src=\"" + imagePath + '/' + imagesFolders[sectionIndex - 1] + '/' + imagesFiles[sectionIndex - 1][imageNum] + "\" width=\"344\" height=\"448\" />");
        $("#image").show("slide", { direction: "up" }, 700);
    });
    
    $("#prev").hover(
        function() {
            $(this).css("background-image", "url(/_img/gallery/gallery_arrow_prev_active.png)");
        },
        function() {
            $(this).css("background-image", "url(/_img/gallery/gallery_arrow_prev.png)");
        }
    );
    
    $("#next").hover(
        function() {
            $(this).css("background-image", "url(/_img/gallery/gallery_arrow_next_active.png)");
        },
        function() {
            $(this).css("background-image", "url(/_img/gallery/gallery_arrow_next.png)");
        }
    );
    
    $("#prev").click(function() {
        oldImageNum = imageNum;
        imageNum--;
        var id = imagesIDs[sectionIndex - 1];
        if (imageNum >= 0) {
            $("#gallery #description").html('<p>' + imagesDescs[sectionIndex - 1][imageNum] + '</p>');
            var cssObj = {
                "background-image" : "url('" + imagePath + '/' + imagesFolders[sectionIndex - 1] + '/' + imagesFiles[sectionIndex - 1][oldImageNum] + "')",
                "background-repeat" : "no-repeat"
            }
            $("#images").css(cssObj);
            $("#image").html('<div id="flash"></div>');
            $("#flash").fadeOut(1500);
            $("#image").append("<img src=\"" + imagePath + '/' + imagesFolders[sectionIndex - 1] + '/' + imagesFiles[sectionIndex - 1][imageNum] + "\" width=\"344\" height=\"448\" />");
            $("#image").show("slide", { direction: "right" }, 400);
        } else {
            imageNum = 0;
        }
    });
    
    $("#next").click(function () {
        oldImageNum = imageNum;
        imageNum++;
        var id = imagesIDs[sectionIndex - 1];
        if (imageNum < imagesFiles[sectionIndex - 1].length) {
            $("#gallery #description").html('<p>' + imagesDescs[sectionIndex - 1][imageNum] + '</p>');
            var cssObj = {
                "background-image" : "url('" + imagePath + '/' + imagesFolders[sectionIndex - 1] + '/' + imagesFiles[sectionIndex - 1][oldImageNum] + "')",
                "background-repeat" : "no-repeat"
            }
            $("#images").css(cssObj);
            $("#image").html('<div id="flash"></div>');
            $("#flash").fadeOut(1500);
            $("#image").append("<img src=\"" + imagePath + '/' + imagesFolders[sectionIndex - 1] + '/' + imagesFiles[sectionIndex - 1][imageNum] + "\" width=\"344\" height=\"448\" />");
            $("#image").show("slide", { direction: "left" }, 400);
        } else {
            imageNum = imagesFiles[sectionIndex - 1].length - 1;
        }
    });
    
    $("#image").click(function() {
        $("#next").click();
    });
    
    $("#gal_s1").click();
});
