$(document).ready(function(){ 
    var src = [];
    var current = 0;
	var scrolled = 0;
	var photos = Math.floor(($(document).height()-276)/120);
    var outPhoto = 0;
    var outPhoto2 = src.length-1;
	$('#gallery-footer .thumbs').css({height:photos*120+127+'px'});
	$('#gallery-footer .strip').css({height:photos*120+'px'});
    for(i=0;i<$('.absolute a').length;i++){
        numb = parseInt($('.absolute a').eq(i).attr('index'))+1;
        src.push(numb+'.jpg');
    }
    $('#gallery-footer .absolute').css({height:src.length*120+'px'});
    $('#switch .next').click(function(){
        changeSlide(true,1);
        return false;
    });   
	
    $('#switch .prev').click(function(){
        changeSlide(true,-1);
        return false;
    });   
	
    $('#gallery-footer .next').live('click',function(){
        moveThumbs(1);
        return false;
    });
	
	$('#gallery-footer .prev').live('click',function(){
        moveThumbs(-1);
        return false;
    });
	
	$('.strip a').live('click',function(){
		var index = $(this).attr('index');
        changeSlide(false,index);
        return false;
    });
	
    function changeSlide(ifSwitch,index){
        var active = $("#slideshow .active");
        var inactive = active.siblings('img').eq(0);
        var img;
        var length = src.length-1;
        if(ifSwitch){
            if((current == 0)&&(index < 0)){
                current = length;
                img = src[current];
            }else if((current == length)&&(index > 0)){
                current = 0;
                img = src[current];
            }else{
                current+=index;
                img = src[current];                
            }
        }else{
			current = index;
			img = src[current];
		}
        //alert(inactive.attr("class"));
        inactive.attr('src','images/'+img);
        inactive.load(function(){
            active.animate({opacity: 0.0}, 1000, function(){
                        active.removeClass('active').css({opacity: 1.0});
                        inactive.addClass('active');
						$('#text-container .text').removeClass('visible');
						$('#text-container .text').eq(current).addClass('visible');
                    });
        });
    }
	
	function moveThumbs(direction){
		var shift = direction*120;
		var left = parseInt($('.strip .absolute').css('top'));
        if(left%120 == 0){
            var width = $('.strip .absolute').innerWidth();
            left -= shift;
            if(direction < 0){
                $('.absolute a').eq(outPhoto2).prependTo('.absolute');
                $('.absolute').css({top: '-120px'});
                $('.strip .absolute').animate({top: '0px'},1000);
            }
            if(direction > 0){
                $('.strip .absolute').animate({top: '-120px'},1000,function(){
                    $('.absolute a').eq(outPhoto).appendTo('.absolute');
                    $('.absolute').css({top: '0px'});
                });
            }
        }
	}
	
});
