images = new Array();
descriptions = new Array();
imgThemes = new Array();
themes = new Array();

noImages = 0;
currentImg = 0;
allLoaded = false;
imgLoaded = [true];

$(window).load(function(){

$('body').append('<div class="galleries" style="display: none;"></div>')
$('.galleries').load('/gallery-src .photoGalleryElement', function(){
    currimg = 0;
    currthm = 0;
    $('.photoGalleryElement').each(function(){
        themes[currthm] = currimg;
        $(this).children('.thumbShadow').each(function(){
            images[currimg] = $(this).find('a').attr('href');
            descriptions[currimg] = $(this).find('p').text();
            imgThemes[currimg] = currthm;
            currimg++;
        })
        currthm++;
    })
    $('.images').html('<img alt="" class="image0" style="left: 0" src="'+images[0]+'" />');
    $('.info p').html(descriptions[0]);
    noImages = images.length;
    for (j=1;j<noImages; j++){
        imgLoaded[j] = false;
    }
    $('#gallery_nav li:not(:first)').fadeOut();
    $('#quantity').html('1 of ' + noImages);
    loadImage(currentImg);
});


// close button
    if(document.referrer.indexOf(window.location.host) > 0) {
        $('#info_bar .close').click(function(){
            $(this).attr('href', '#')
            history.back();
            return false;
        });
    }

// left and right nav buttons
    $('#l_arrow').click(function(){
        iSlideRight();
        return false;
    });
    $('#r_arrow').click(function(){
        iSlideLeft();
        return false;
    });

// theme navigation
    $('#gallery_nav a:eq(0)').click(function(){
        if (currentImg != themes[0]) {
            if(imgLoaded[themes[0]]) {
                $('.images_wrap').stopTime('slideshow');
                $('.images_wrap').oneTime(5000, 'slideshow', startSlideshow);
                nextImg = themes[0];
                moveImage(nextImg, 'fw');
            }
        }
        return false;
    });
    $('#gallery_nav a:eq(1)').click(function(){
        if (currentImg != themes[1]) {
            if(imgLoaded[themes[1]]) {
                $('.images_wrap').stopTime('slideshow');
                $('.images_wrap').oneTime(5000, 'slideshow', startSlideshow);
                nextImg = themes[1];
                moveImage(nextImg, 'fw');
            }
        }
        return false;
    });
    $('#gallery_nav a:eq(2)').click(function(){
        if (currentImg != themes[2]) {
            if(imgLoaded[themes[2]]) {
                $('.images_wrap').stopTime('slideshow');
                $('.images_wrap').oneTime(5000, 'slideshow', startSlideshow);
                nextImg = themes[2];
                moveImage(nextImg, 'fw');
            }
        }
        return false;
    });

});



function loadImage(i) {
    if(i+1 < noImages) {
        $('.images img:eq('+i+')').after('<img class="image'+(++i)+'" alt="" src="'+images[i]+'">');
        if (currentImg == i) {
            $('.image'+i).css({left: '0px', display: 'none'}).load(function(){
                $(this).fadeIn('fast');
            })
        }
        $('.image'+i).load(function(){
            $('#gallery_nav li:eq('+imgThemes[i]+')').not(':visible').fadeIn();
            imgLoaded[i] = true;
            loadImage(i);
            if(i == noImages-1)
                allLoaded = true;
                startSlideshow();
        });
    }
}
function startSlideshow() {
    if(allLoaded) {
        $('.images_wrap').everyTime(5000, 'slideshow', function(){
            nextImg = currentImg + 1;
            if(nextImg == noImages) nextImg = 0;
            moveImage(nextImg, 'fw');
        });
    }
}
function iSlideLeft() {
    $('.images_wrap').stopTime('slideshow');
    $('.images_wrap').oneTime(5000, 'slideshow', startSlideshow);
    move = false;
    if((currentImg < noImages-1) ) {
        if($('.image'+currentImg).length < 1) {
            return;
        }
        nextImg = currentImg + 1;
        move = true;
    } else if(currentImg == noImages-1) {
        nextImg = 0;
        move = true;
    }
    if (move) {
        moveImage(nextImg, 'fw')
    }
}
function iSlideRight() {
    $('.images_wrap').stopTime('slideshow');
    $('.images_wrap').oneTime(5000, 'slideshow', startSlideshow);
    move = false;
    if(currentImg > 0){
        nextImg = currentImg - 1;
        move = true;
    } else if((currentImg == 0) && imgLoaded[noImages-1]) {
        move = true;
        nextImg = noImages-1;
    }
    if (move) {
        moveImage(nextImg, 'bk')
    }
}
function moveImage(nextImg, dir) {
    if (dir == 'fw') {
        $('.images img:eq('+nextImg+')').css({left: '1330px'}).animate({left: '0px'});
        $('.images img:eq('+currentImg+')').animate({left: '-1330px'});
    } else if (dir == 'bk') {
        $('.images img:eq('+nextImg+')').css({left: '-1330px'}).animate({left: '0px'});
        $('.images img:eq('+currentImg+')').animate({left: '1330px'});
    } else {
        return false;
    }
    currentImg = nextImg;

    $('#gallery_nav a').removeClass('on');
    $('#gallery_nav a:eq('+imgThemes[currentImg]+')').addClass('on');
    $('.info p').html(descriptions[currentImg]);
    if( $('.info p').height() > 16 ) {
        $('.info p').animate({marginTop: '-7px'});
    }  else {
        $('.info p').animate({marginTop: '0px'});
    }
    $('#quantity').html(currentImg+1 + ' of ' + noImages)
}









