var IMAGEGALLERY = {

	//------------------------------------------------------------------------
	
	image:null,
	bigImage:null,
	imageWrap:null,
	activeUID:null,
	page:{active:1, count:0, distance:492},
	bufferImage:null,
	fx:null,
	pageCounter:null,
	thumbContainer:null,

	btPrev:null,
	btNext:null,
	btClose:null,

	isRunning:false,
	isVisible:false,
	
	//------------------------------------------------------------------------
	
	init:function() {
		var thumbs = $$('.imageGallery_thumb');
		if(thumbs.length > 0) {
			
			this.thumbContainer = $('imageGallery_thumbs');
			this.imageWrap = $('imageGallery_imageWrap');
			this.bigImage = $('imageGallery_image');
			this.btNext = $('imageGallery_btNext');
			this.btPrev = $('imageGallery_btPrev');
			this.btClose = $('imageGallery_btClose');
			this.pageCounter = $('imageGallery_counter');
			
			this.init_image( thumbs );
			this.init_page();
			this.init_buttons();
			this.init_keyEvents();
			
		}
		delete thumbs;
	},

	//------------------------------------------------------------------------

	init_image:function( thumbs ) {
		var uid = 0;
		this.image = new Array();
		for(var i=0;i<thumbs.length;i++) {
			var el = thumbs[i];
			el.uid = uid;
			el.image = ( el.cHash == undefined ) ? el.href : el.cHash;
			el.href = '#';
			el.onclick = function() {
				IMAGEGALLERY.show(this);
				return false;
			}
			IMAGEGALLERY.image[uid] = el;
			uid++;
		}
		delete i;
		delete el;
		delete uid;
		delete thumbs;
	},


	//------------------------------------------------------------------------
	
	init_page:function() {
		this.page.count = Math.ceil(this.image.length / 16);
		this.page.active = 1;
		this.set_page();
	},

	//------------------------------------------------------------------------

	init_buttons:function() {
		this.set_naviButtons();
		this.btNext.onclick = function() {
			if(IMAGEGALLERY.isVisible) {
				if( IMAGEGALLERY.activeUID >= (IMAGEGALLERY.image.length-1) ) {
					IMAGEGALLERY.show(IMAGEGALLERY.image[0]);
				} else {
					IMAGEGALLERY.show(IMAGEGALLERY.image[IMAGEGALLERY.activeUID+1]);
				}
			} else {
				if( IMAGEGALLERY.page.active >= IMAGEGALLERY.page.count ) {
					IMAGEGALLERY.page.active = 1;
				} else {
					IMAGEGALLERY.page.active++;
				}
				IMAGEGALLERY.set_page();
			}
		}
		this.btPrev.onclick = function() {
			if(IMAGEGALLERY.isVisible) {
				if( IMAGEGALLERY.activeUID <= 0 ) {
					IMAGEGALLERY.show(IMAGEGALLERY.image[(IMAGEGALLERY.image.length-1)]);
				} else {
					IMAGEGALLERY.show(IMAGEGALLERY.image[(IMAGEGALLERY.activeUID-1)]);
				}
			} else {
				if( IMAGEGALLERY.page.active <= 1 ) {
					IMAGEGALLERY.page.active = IMAGEGALLERY.page.count;
				} else {
					IMAGEGALLERY.page.active--;
				}
				IMAGEGALLERY.set_page();
			}
		}
		this.btClose.onclick = function() {
			IMAGEGALLERY.hide();
		}
	},
	
	//------------------------------------------------------------------------

	init_keyEvents:function() {
		$(document).addEvent('keydown', function(event) {
			if( IMAGEGALLERY.isVisible === true ) {
				if(event.key == 'left') IMAGEGALLERY.btPrev.onclick();
				if(event.key == 'right') IMAGEGALLERY.btNext.onclick();
				if(event.key == 'esc') IMAGEGALLERY.hide();
			}
		});
	},

	//------------------------------------------------------------------------

	set_page:function() {
		this.pageCounter.innerHTML = this.page.active + ' / ' + this.page.count;
		var pos = this.page.distance * (this.page.active-1);
		this.fx = new Fx.Scroll(this.thumbContainer,{duration:500,transition: Fx.Transitions.Sine.easeInOut});
		this.fx.start(0,pos);
		delete pos;
	},

	//------------------------------------------------------------------------

	show:function( el ) {
		this.isRunning = true;
		this.isVisible = true;
		if( this.activeUID !== null ) {
			this.image[this.activeUID].removeClass('active');
		}
		this.set_naviButtons();
		el.addClass('active');
		this.activeUID = el.uid;
		this.bigImage.style.display = 'none';
		this.pageCounter.style.display = 'none';
		this.imageWrap.style.display = 'block';
		this.bufferImage = new Image;
		this.bufferImage.onload = function(){
			IMAGEGALLERY.bigImage.style.backgroundImage = 'url('+this.src+')';
			IMAGEGALLERY.fade_bigImage();
		};
		this.bufferImage.src = el.image;
	},
	
	//------------------------------------------------------------------------
	
	hide:function() {
		this.imageWrap.style.display = 'none';
		this.pageCounter.style.display = 'block';
		this.isVisible = false;
		this.set_naviButtons();
	},
	
	//------------------------------------------------------------------------
	
	fade_bigImage:function() {
		this.bigImage.setOpacity(0);
		this.bigImage.style.display = 'block';
		this.fx = new Fx.Morph(this.bigImage,{duration:500,transition: Fx.Transitions.Sine.easeInOut,onComplete:function(){
			IMAGEGALLERY.isRunning = false;
		}});
		this.fx.start({opacity:1});
	},

	//------------------------------------------------------------------------
	
	set_naviButtons:function() {
		if( this.isVisible === true ) {
			this.btNext.className = '';
			this.btPrev.className = '';
			this.btNext.style.display = 'block';
			this.btPrev.style.display = 'block';
		} else if( this.page.count > 1 ) {
			this.btNext.className = 'thumb';
			this.btPrev.className = 'thumb';
			this.btNext.style.display = 'block';
			this.btPrev.style.display = 'block';
		} else {
			this.btNext.style.display = 'none';
			this.btPrev.style.display = 'none';
		}
	}
	
	//------------------------------------------------------------------------
	
};

//----------------------------------------------------------------------------

window.addEvent('load',function() {
	IMAGEGALLERY.init();
});




