var loaderIndex=0;
var loaderImage=new Image();
loaderImage.src='css/images/loader.png';

function loaderAnimation(){
	$('.loader').css('backgroundPosition',(-(((loaderIndex++)%8)*64))+'px center');
	setTimeout('loaderAnimation();',65);
}
loaderAnimation();

function setGalleryImage(src,alt,title,flag){
	gallery.loaderTimeout=setTimeout("$('#gallery .loader').css('display','block');",500);
	var image=new Image();
	$(image).load(function(){
		try{
			clearTimeout(gallery.loaderTimeout);
		}catch(e){}
	   	$('#gallery .loader').css('display','none');
		var imageContainer=document.createElement('div');
		imageContainer.className='imageContainer';
		$(imageContainer).append(this);		
		if(jQuery.support.opacity){
			var delay;
			if(gallery.autoPlay){
				delay=1200;
			}else{
				delay=600;
			}
			$('#gallery .imageContainer').fadeOut(delay,function(){
				$(this).remove();
			});
			$('#gallery .imageContainer img').animate({
				left:'-30px'
			},delay);
			imageContainer.style.display='none';
			$('#gallery .imagePane').append(imageContainer);
			$(imageContainer).fadeIn(delay);			
		}else{			
			$('#gallery .imageContainer').remove();
			$('#gallery .imagePane').append(imageContainer);			
		}
		$('#galleryCounter').html((gallery.index+1)+'&nbsp; / &nbsp;'+gallery.size());
		if(gallery.autoPlay){
			gallery.autoPlayTimeout=setTimeout('gallery.next();',8000);
		}		
	});
	image.alt=alt;
	image.title=title;
	image.src=src;
}

var gallery={
	images:new Array(),
	index:-1,
	loaderTimeout:null,
	autoPlay:true,
	autoPlayTimeout:null,
	size:function(){
		return this.images.length;
	},
	next:function(){
		try{
			clearTimeout(this.autoPlayTimeout);
		}catch(e){}
		if(this.size()>0){
			this.index++;
			this.index%=this.size();
			setGalleryImage(this.images[this.index].src,this.images[this.index].alt,this.images[this.index].title,'next');
		}
	},
	prev:function(){
		try{
			clearTimeout(this.autoPlayTimeout);
		}catch(e){}
		if(this.size()>0){
			this.index--;
			this.index+=this.size();
			this.index%=this.size();
			setGalleryImage(this.images[this.index].src,'prev');
		}
	}
};

$(document).ready(function(){
	$('#gallery img').each(function(){
		var preload=new Image();
		preload.src=this.src;
		preload.alt=this.alt;
		preload.title=this.title;
		gallery.images[gallery.size()]=preload;
	});
	var glassPane=document.createElement('div');
	var imagePane=document.createElement('div');
	var loader=document.createElement('div');
	glassPane.className='glassPane';
	imagePane.className='imagePane';
	loader.className='loader';
	$('#gallery img').remove();
	$('#gallery').append(imagePane);
	$('#gallery').append(glassPane);
	$(glassPane).append(loader);
	glassPane.title='next image';
	if(gallery.size()>2){
		$(glassPane).click(function(){
			gallery.autoPlay=false;
			gallery.next();
		});
		$(glassPane).css('cursor','pointer');
		$('#next').click(function(){
			gallery.autoPlay=false;
			gallery.next();
		});
		$('#prev').click(function(){
			gallery.autoPlay=false;
			gallery.prev();
		});
	}else{
		$('#galleryNav').css('display','none');
	}
	gallery.next();
});