var current = [], started = [false,false,false];

var timerFadeIn = 1500;
var timerFadeOut = 1500;
var timerDisplay = 3500;

//This is set to stagger each of the 3
var gapTimer = 1000;

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

function initPictures() {
    bindThem();
    $('.picture1').each(function() {
        var temp = $(this).attr('rel');
        if(images[temp].length > 0) {
            if(typeof(current[temp]) == 'undefined')
                current[temp] = 0;
            
            var child = $(this).parent().children('div.description');
            $(this).attr('src', '/images/gallery/' + images[temp][current[temp]].path).show();
            $(child).children('div.title').html(images[temp][current[temp]].name);
            $(child).children('div.text').html(images[temp][current[temp]].text);
            $(child).children('a').attr('href', '/news/default/view/' + images[temp][current[temp]].link);
        }
    });
    $('.picture1').trigger('bound');
}

function bindThem() {
    $('.picture1').bind('bound',function(){
        var temp = $(this).attr('rel');
        var child = $(this).parent().children('div.description');
        $(this).fadeIn(timerFadeIn,function(){
            $(child).children('div.title').html(images[temp][current[temp]].name);
            $(child).children('div.text').html(images[temp][current[temp]].text);
            $(child).children('a').attr('href', '/news/default/view/' + images[temp][current[temp]].link);
            if(current[temp] + 1 >= images[temp].length){current[temp] = 0;}else{current[temp]++;}
            $('.picture2[rel="' + temp + '"]').attr('src', '/images/gallery/' + decodeURIComponent(images[temp][current[temp]].path)).show();
            $(this).delay((temp == 3 ? (timerDisplay / 2) : timerDisplay)).fadeOut(timerFadeOut,function(){
                changeUp(false, temp);
            });
        });
    });
}

function changeUp(newcurrent, temp) {
    $('.picture1[rel="' + temp + '"]').fadeOut(timerFadeOut, function() {
        var child = $(this).parent().children('div.description');
        $(child).children('div.title').html(images[temp][current[temp]].name);
        $(child).children('div.text').html(images[temp][current[temp]].text);
        $(child).children('a').attr('href', '/news/default/view/' + images[temp][current[temp]].link);
        if(current[temp] + 1 >= images[temp].length){current[temp] = 0;}else{current[temp]++;}
        $(this).delay((temp == 3 ? (timerDisplay / 2) : timerDisplay)).fadeOut(timerFadeOut, function() {
            $(this).attr('src', '/images/gallery/' + decodeURIComponent(images[temp][current[temp]].path));
            $(this).trigger('bound');
        });
    });
}
