var Gallery = Class.create();

Gallery.prototype = {
	
 	_imgs: null,
 	_assets: $H({}),
	_checkInterval: null,
	_options: {},
	_mode: 'thumb',
	_curr: 0,
 	
	initialize: function()
	{
		var options = arguments[0] || {};
	  	Gallery._options = options;
	},
	
	start: function()
	{
		setTimeout(function() {
					
		imgs = Gallery._imgs;
		for(i=0; i <= imgs.length-1; i++)
	    {
	    	img = './gallery/tn_'+imgs[i];
	    	Gallery.load(img);
	    	$('img_'+i).src = img;
	    	new Effect.Appear('img_'+i, { duration: 1.0, afterFinish: function() {
	    		if($('thumb_'+i))
					$('thumb_'+i).className = '';
			} });
	    }
		$('gallery').className = '';
	    },1000);
	},
	
	page: function(action)
	{
	  	if(parseInt(Gallery._curr) == 0 && action == 'prev')
	  		return;
	  		
	  	if(parseInt(Gallery._curr) == Gallery._imgs.length-1 && action == 'next')
	  		return;
	  		
	  	if(Gallery._mode == 'thumb')
		{ // in thumbs mode, scroll thumbs
		  	newy = 370;
		  	if(action == 'next')
		  		newy = -370;
		  	new Effect.Move('thumbs',{ x: 0, y: newy, mode: 'relative'});
		 }
		 else
		 { // in preview mode, load new image
		 	
		 	next = parseInt(Gallery._curr)+1;
		 	
		 	if(action == 'prev')
		 		next = parseInt(Gallery._curr)-1;
		 	
		 	Gallery.show(next);
		 }
		 
	},
	
	show: function(id)
	{
		
		if(Gallery._mode == 'thumb')
		{ // in thumbs mode, reset to preview
			
			new Effect.Fade('thumbs', { duration: 1.0, afterFinish: function() {
				new Effect.Appear('preview', { duration: 0.3, afterFinish: function() {
					img = './gallery/'+imgs[id];
			    	Gallery.load(img);
			    	$('src').src = img;
					new Effect.Appear('src', { duration: 1.0, afterFinish: function() {
						$('image').className = '';
					} });
				} });
			} });
			Gallery._mode = 'preview';
		}
		else
		{ // in preview mode, load new image
			new Effect.Fade('src', { duration: 1.0, afterFinish: function() {
				img = './gallery/'+imgs[id];
		    	Gallery.load(img);
		    	$('src').src = img;
				new Effect.Appear('src', { duration: 1.0, afterFinish: function() { } });
			} });
		}
		
		Gallery._curr = id;
	},
	
	close: function()
	{
		new Effect.Fade('preview', { duration: 0.3, afterFinish: function() {
			new Effect.Appear('thumbs', { duration: 1.0, afterFinish: function() { Gallery._mode = 'thumb'; } });
		} });
	},
	
	update: function() {
		var allLoaded = true;
	  	Gallery._assets.each(function(a) {
	    	if(!a[1].complete && a[1].image.complete) {
	      		a[1].complete = true;
	      		a[1].completed_at = new Date().getTime();;
	      		if(a[1].options.onComplete) a[1].options.onComplete(a[0]);
	    	}
	    	if(!a[1].complete && !a[1].image.complete) allLoaded = false;
	  	});
	  	
	  	if(allLoaded) {
	    	clearInterval(Gallery._checkInterval);
	    	Gallery._checkInterval = null;
	    	if(Gallery._options && Gallery._options.onComplete) Gallery._options.onComplete();
	    	Gallery._options = null;
	  	}
	},
		
	cacheOrLoad: function(url) {
	  	var options = arguments[1] || {};
	  	if(this.isLoaded(url)) {
	    	if(options.onComplete) options.onComplete();
	  	} else {
	    	this.load(url, options);
	  	}
	},
	
	load: function(url) {
		//alert('loading:'+url);
	  	if(Gallery._assets[url]) return;
	  	var options = arguments[1] || {};
		var a = {};
		a.image = new Image();
		a.image.src = url;
		a.complete = false;
		a.options  = options;
		a.loaded_at = new Date().getTime();
		Event.observe(a.image, 'error', function() { Gallery.error(url) });
		Gallery._assets[url] = a;
		if(!Gallery._checkInterval) Gallery._checkInterval = setInterval(Gallery.update,10);
	},
	
	error: function(url) {
		alert('cannot find image:'+url);
		var asset = Gallery._assets[url];
		asset.complete = true;
		if(asset.options.onComplete) asset.options.onComplete('/images/s.gif');
	},
	
	stats: function(url) {
		return (Gallery._assets[url]._complete ?
	    	(Gallery._assets[url]._completed_at - Gallery._assets[url]._loaded_at) : null);
	},
	
	isQueued: function(url) {
	  	return !!(Gallery._assets[url]);
	},
	
	isLoaded: function(url) {
	  	return (Gallery._assets[url] && Gallery._assets[url].complete);
	},
	
	reset: function() {
	  	Gallery._assets = $H({});
	}
}