var current = 0,
    images = [];

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

function initPictures2() {   
    var page_id = $('#page-id').val();
    $.post('/ajax/getFade/',{ pageId:page_id }, function(xml) {  
        $('item',xml).each(function(id) { 
            $('.pics').append('<img src="/images/properties/banners/' +  $('path',$('item',xml).get(id)).text() + '" style="width: 100%; height: 100%;" />');  
        });
        $('.pics').cycle({fx: 'fade'});        
    });  
}


function initPictures() {
    var page_id = $('#page-id').val();
    $.post('/ajax/getFade/',{ pageId:page_id }, 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() {
    if(images.length > 0){
        $('.picture').load(function(){
            $('.picture').fadeIn(1500,function(){
                if(current + 1 >= images.length){current = 0;}else{current++;}
                $('#img2').attr('src', '/images/properties/banners/' + 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/properties/banners/' + decodeURIComponent(images[current].path));
        $('.quote-text').html(decodeURIComponent(images[current].text));    
    }
    
}

function changeUp() {
    $('.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/properties/banners/' + decodeURIComponent(images[current].path));
            });
        });
    });
}
