var MachimaRotation = new Class({
	initialize: function(){
		this.area = $('MachimaRotationArea');
		this.buttonNext = $('MachimaRotationNext');
		this.buttonPrevious = $('MachimaRotationPrevious');
		this.cache = 1;
		this.storage = new Element('div',{id:'MachimaRotationStorage',style:'display:none;'}).inject($(document.body));
		$(document).getElements('a').addEvent('click',function(){this.blur();});
		$(window).addEvent('resize', (function(){ this.fitScreen(); }).bind(this));
	},
	fitScreen: function(){
		this.setAreaSize();
		this.area.getElements('.image').each((function(el){ this.fitImage(el); }).bind(this));
	},
	setAreaSize: function(){
		this.areaSize = $(window).getSize();
		this.area.setStyle('width',this.areaSize.x);
		this.area.setStyle('height',this.areaSize.y);
		this.areaRatio = this.areaSize.x/this.areaSize.y;
	},
	fitImage: function(div){
		img = div.getElement('img');

		var coords = div.getCoordinates();
		var left = coords.left;

		if(left < 0) div.setStyle('left', -div.getSize().x);
		if(left > 0) div.setStyle('left', this.areaSize.x);
//		if(left != 0) div.setStyle('visibility','hidden');
		
		var imgRatio = div.retrieve('width') / div.retrieve('height')+0.00000000000001;
		if(imgRatio < this.areaRatio){
			img.setStyles({
				width: this.areaSize.x,
				height: 'auto'
			});
		} else {
			img.setStyles({
				width: 'auto',
				height: this.areaSize.y
			});
		}
	},
	buildGallery: function(data){
		if(!$chk(data.images)) return false;
		var name = 'default'; //data.gallery;
		var images = data.images;
		this.setAreaSize();
		var gallery = new Element('div',{
			id: 'gallery_'+name
		}).inject(this.storage);
		for(var i=0;i<images.length;i++){
			var div = new Element('div').setStyles({
				display:'none'
			}).addClass('image').inject(gallery);
			var img = new Element('img',{
				src:images[i].file
			}).inject(div);

			div.store('height',images[i].height);
			div.store('width',images[i].width);
			div.store('brightness',images[i].brightness);

			this.fitImage(div);
		}
		this.showGallery(name);
	},
	showGallery: function(gallery){
		if(!$chk($('gallery_'+gallery))) return this.loadGallery.get({'gallery':gallery,'cache':this.cache});
		if($('gallery_'+gallery) == this.gallery) return false;
		this.gallery = $('gallery_'+gallery);
		
		this.firstImage = this.gallery.getFirst('.image');
		this.lastImage = this.gallery.getLast('.image');
		this.currentImage = this.gallery.retrieve('currentImage');
		if(!$chk(this.currentImage)){
			this.currentImage = this.firstImage;
			this.gallery.store('currentImage',this.currentImage);
		}

		this.currentImage.setStyles({opacity:0,display:'block',visibility:'visible',position:'absolute'});
		this.currentImage.set('tween',{duration:500}).tween('opacity',1);

		this.gallery.inject(this.area,'top').setStyles({display:'block',visibility:'visible'}).set('tween',{duration:300}).tween('opacity',1); //setStyles({display:'block',opacity:1});
		this.gallery.addEvent('click',(function(e){ this.galleryClick(e); }).bind(this));

		this.setBrightness();
		this.fitScreen();

		if(this.firstImage != this.lastImage){
			this.buttonPrevious.setStyles({display:'',cursor:'pointer',opacity:0.5}).removeEvent('click').addEvents({
				click: (function(){this.previousGalleryImage()}).bind(this),
				mousedown: function(e){e.stop();},
				mouseenter: function(){this.set('tween',{duration:100}).tween('opacity',1);},
				mouseout: function(){this.set('tween',{duration:1000}).tween('opacity',0.2);}
			});
			this.buttonNext.setStyles({display:'',cursor:'pointer',opacity:0.5}).removeEvent('click').addEvents({
				click: (function(){this.nextGalleryImage()}).bind(this),
				mousedown: function(e){e.stop();},
				mouseenter: function(){this.set('tween',{duration:100}).tween('opacity',1);},
				mouseout: function(){this.set('tween',{duration:1000}).tween('opacity',0.2);}
			});
		}

	},
	galleryClick: function(e){
		if(!this.gallery) return false;
		if(e.event.layerX < this.areaSize.x/2){
			this.previousGalleryImage();
		} else {
			this.nextGalleryImage();
		}
	},
	nextGalleryImage: function(){
		if(!this.gallery) return false;
		this.setAreaSize();
		if($chk(MSM)) MSM.slideOut();
		var upcomingImage = this.currentImage.getNext();
		if(!$chk(upcomingImage)){
			upcomingImage = this.gallery.getFirst('div');
			if(!$chk(upcomingImage) || upcomingImage == this.currentImage) return false;
		}
		upcomingImage.setStyles({position:'absolute', left:this.areaSize.x, display:'block',visibility:'visible'}).set('tween',{duration:500}).tween('left',0);
		this.currentImage.setStyles({position:'absolute'}).set('tween',{duration:500}).tween('left',-this.currentImage.getSize().x);
		this.currentImage = upcomingImage;
		this.setBrightness();
	},
	previousGalleryImage: function(){
		if(!this.gallery) return false;
		this.setAreaSize();
		if($chk(MSM)) MSM.slideOut();
		var upcomingImage = this.currentImage.getPrevious();
		if(!$chk(upcomingImage)){
			upcomingImage = this.gallery.getLast('div');
			if(!$chk(upcomingImage) || upcomingImage == this.currentImage) return false;
		}
		upcomingImage.setStyles({position:'absolute', left:-this.areaSize.x, display:'block',visibility:'visible'}).set('tween',{duration:500}).tween('left',0);
		this.currentImage.setStyles({position:'absolute'}).set('tween',{duration:500}).tween('left',this.currentImage.getSize().x);
		this.currentImage = upcomingImage;
		this.setBrightness();
	},
	setBrightness: function(){
		var brightness = this.currentImage.retrieve('brightness');
		if(brightness != 'dark' && brightness != 'light') brightness = 'dark';
		if($chk(MSM)) MSM.setBackgroundBrightness(brightness);
	}
});