var scrollTimeout=false;
var galleryImages=new Array();
var curGalleryIndex=0;
var slideShowTimeout=false;
var allowSlideShow=true;
var monthPhotos=new Array('7.jpg', '8.jpg','9.jpg','10.jpg','11.jpg','12.jpg','13.jpg','14.jpg','15.jpg','16.jpg','17.jpg','18.jpg','19.jpg','20.jpg','21.jpg','22.jpg');

function scrollImages(direction){
	if(direction==0){
		document.getElementById('galId').scrollTop+=3;
	}
	else{
		document.getElementById('galId').scrollTop-=3;
	}
	stopScrollingImages();
	scrollTimeout=setTimeout('scrollImages('+direction+')', 10);
}

function stopScrollingImages(){
	clearTimeout(scrollTimeout);
	scrollTimeout=false;
}

function showBox(divId, width, height, content, contentDiv){
	$(divId).css('width', width+'px').css('height', height+'px');
	$(divId+' '+contentDiv).html(content);
	
	$('div.full_overlay').css('width', $(document).width()+'px').css('height', $(document).height());
	$(divId).css('left', (($(window).width()/2)-($(divId).width()/2))+'px');
    $(divId).css('top', (($(window).height()/2)-($(divId).height()/2)+$(document).scrollTop())+'px');
	$('div.full_overlay, '+divId).fadeIn('fast');
	
	if($(divId+' '+contentDiv).first().height()!=0){
		$(divId).height($(divId+' '+contentDiv).first().height()+20);
	}
}

function hideBox(divId){
	$(divId+', .full_overlay').fadeOut('fast');
    if(slideShowTimeout){
        allowSlideShow=false;
        clearTimeout(slideShowTimeout);
        slideShowTimeout=false;
    }
}

function nextSlide(pr){

	if(pr){
		if(!galleryImages[curGalleryIndex-1]){
			curGalleryIndex=galleryImages.length-1;
		}
		else{
			curGalleryIndex-=1;
		}
	}
	else{
		if(!galleryImages[curGalleryIndex+1]){
			curGalleryIndex=0;
		}
		else{
			curGalleryIndex+=1;
		}
	}
	
    $('.message').fadeOut('slow', function(){
        var w=0;
        var h=0;
        var t=0;

        $('#invisibleGallery img').each(function(){

            if($(this).attr('src')==galleryImages[curGalleryIndex]){
                w=$(this).width()+50;
                h=$(this).height()+20;
                t=parseInt(($(window).height()-h)/2);
            }
        });

        $('.message').width(w+'px').height(h+'px').css('top', t+'px');

        $('.message .message_content img').attr('src', galleryImages[curGalleryIndex]);
        $(this).fadeIn('slow', function(){
            if(allowSlideShow)
                slideShowTimeout=setTimeout('nextSlide(false)', 4000);
			else
				allowSlideShow=true;
        });
    });
}

function monthPhoto(){
                        
    var toHide=false;
    var toShow=false;
    $('.month_photo').children('img').each(function(){
        if(!$(this).is(':hidden')){
            toHide=$(this);
            if($(this).next().attr('src')){
                toShow=$(this).next();
            }
            else{
                toShow=$('.month_photo').children('img').first();
            }
        }
    });


    toHide.fadeOut(2000);
    toShow.fadeIn(2000, function(){
          setTimeout('monthPhoto()', 4000);
    });
}

$(document).ready(function(){
	$('img.gallery').click(function(){
	    allowSlideShow=true;
		var s=$(this).attr('src').replace('/thumbs', '');
        var curSrc=$(this).attr('src');
		
		showBox('.message', 600, 450, '<img src="'+s+'" />', '.message_content');

        slideShowTimeout=setTimeout('nextSlide(false)', 4000);
	});
	
	$('.message img.close').click(function(){hideBox('.message')});

    $('body').append('<div id="invisibleGallery"></div>');

    for(i=0; i<galleryImages.length; i++){
        var s=galleryImages[i];
        var imageTag = "<img src='" + s + "' style='display:none' />";

        var w=0;
        var h=0;

        $(imageTag).load(function () {
            w = $(this).width();
            h = $(this).height();

            $(this).attr('width', w);
            $(this).attr('height', h);

        }).appendTo($('#invisibleGallery'));
    };

	$('.previous').click(function(){
		if(slideShowTimeout){
			allowSlideShow=false
			clearTimeout(slideShowTimeout);
			slideShowTimeout=false;
			slideShowTimeout=setTimeout('nextSlide(false)', 4000);
		}
		
		nextSlide(true);
	});

	$('.forward').click(function(){
		if(slideShowTimeout){
			allowSlideShow=false
			clearTimeout(slideShowTimeout);
			slideShowTimeout=false;
			slideShowTimeout=setTimeout('nextSlide(false)', 4000);
		}
		
		nextSlide(false);
	});

	$('.pause').click(function(){
		$(this).css('opacity', 0.5)
		$('.play').css('opacity', 1);
		allowSlideShow=false
		clearTimeout(slideShowTimeout);
		slideShowTimeout=false;
	});

	$('.play').click(function(){
		$(this).css('opacity', 0.5);
		$('.pause').css('opacity', 1);
		allowSlideShow=false
		clearTimeout(slideShowTimeout);
		slideShowTimeout=false;
		slideShowTimeout=setTimeout('nextSlide(false)', 4000);
		
		nextSlide(false);
	});
	
	$('.play').css('opacity', 0.5);
	
    new monthPhoto();
});
















