var current = 0,
    images = [];

$(document).ready(function() {    
    initPictures();
});

function initPictures() {
    $.post('/ajax/getFade/',{}, function(xml) {
        $('item',xml).each(function(id) {
            images.push({'path':$('path',$('item',xml).get(id)).text(),'text':$('text',$('item',xml).get(id)).text()});
        });
        bindThem();
    });
}

function bindThem() {
    $('.picture').load(function(){
        $('.picture').fadeIn(1500,function(){
            if(current + 1 >= images.length){current = 0;}else{current++;}
            $('#img2').attr('src', '/images/fader/' + decodeURIComponent(images[current].path)).show();
            $('.quote').fadeIn(1500, function(){
                $('.quote').delay(3500).fadeOut(1500,function(){
                    $('.quote-text').html(decodeURIComponent(images[current].text));
                    changeUp();
                });
            });
        });
    });
    $('.picture').attr('src', '/images/fader/' + decodeURIComponent(images[current].path));
    $('.quote-text').html(decodeURIComponent(images[current].text));
}

function changeUp() {
    //$('.picture').fadeOut(1500,function(){ $('.picture').attr('src', '/images/fader/' + decodeURIComponent(images[current].path)); });
    $('.picture').fadeOut(1500, function() {
        if(current + 1 >= images.length){current = 0;}else{current++;}
        $('.quote').fadeIn(1500, function() {
            $('.quote').delay(3500).fadeOut(1500, function() {
                $('.quote-text').html(decodeURIComponent(images[current].text));
                $('.picture').attr('src', '/images/fader/' + decodeURIComponent(images[current].path));
            });
        });
    });
}
