/**
 * @author jfdesgagne
 */
var DesignCenter = new Class({
	Implements: Options,
  	datas:null,
	imageWidth:438,
	imageX:0,
	currentImage:1,
	imageTween:null,
	infoTween:null,
	options:{
		sensibility:80
	},
	
    initialize: function(options){
		this.setOptions(options);
		this.initConfig();
		this.initEvents();
    },
	
	initEvents:function() {
		if ($('pageNext')) {
			$('pageNext').addEvent('click', this.pageNext.bind(this));
			$('pageNext').setStyle('visibility', 'visible');
			
		}
		if ($('pagePrev')) {
			$('pagePrev').addEvent('click', this.pagePrev.bind(this));
			$('pagePrev').setStyle('visibility', 'visible');
		}
			
		//$('photoViewer').addEvent('mousemove', this.checkPageButton.bind(this));
	},
	
	checkPageButton:function(event) {
		//alert('x:'+event.page.x);
		//alert('imageX:'+(this.imageX))
		
		/*
		if(event.page.x > this.imageWidth + this.imageX - this.options.sensibility  && event.page.x < this.imageWidth + this.imageX + 10) {
			$('pageNext').setStyle('visibility', 'visible');
			$('pagePrev').setStyle('visibility', 'hidden');
		} else if(event.page.x > this.imageX - 10  && event.page.x < this.imageX + this.options.sensibility) {
			$('pageNext').setStyle('visibility', 'hidden');
			$('pagePrev').setStyle('visibility', 'visible');
		} else {
			$('pageNext').setStyle('visibility', 'hidden');
			$('pagePrev').setStyle('visibility', 'hidden');
		}
		*/
		
	},
	
	pageNext:function(event) {
		this.currentImage++;
		if(this.currentImage>this.datas.length) this.currentImage = 1;
		this.hideImage();
	},
	
	pagePrev:function(event) {
		this.currentImage--;
		if(this.currentImage<1) this.currentImage = this.datas.length;
		this.hideImage();
	},
	
	hideImage:function() {
		//this.hideInfo();
		
		if(this.imageTween) this.imageTween.cancel();
		this.imageTween = new Fx.Tween($('photoViewer').getElement('img'), {duration: 'long', transition: Fx.Transitions.Expo.easeOut});
		this.imageTween.addEvent('complete', this.showImage.bind(this));
		this.imageTween.start('opacity', 0);
	},
	
	showImage:function() {
		this.changeImage();
		if(this.imageTween) this.imageTween.cancel();
		this.imageTween = new Fx.Tween($('photoViewer').getElement('img'), {duration: 'long', transition: Fx.Transitions.Expo.easeOut});
		this.imageTween.start('opacity', 1);
	},
	
	changeImage:function() {
		$('photoViewer').getElement('img').src = root+"assets/file_library/designcenter/"+this.datas[this.currentImage-1].src;
	},
	
	hideInfo:function() {
		if(this.infoTween) this.infoTween.cancel();
		this.infoTween = new Fx.Tween($('info'), {duration: 'long', transition: Fx.Transitions.Expo.easeOut});
		this.infoTween.addEvent('complete', this.showInfo.bind(this));	
		this.infoTween.start('margin-top', -80);			
	},	
	
	showInfo:function() {
		if(this.infoTween) this.infoTween.cancel();
		this.infoTween = new Fx.Tween($('info'), {duration: 'long', transition: Fx.Transitions.Expo.easeOut});
		this.infoTween.start('margin-top', -45);	
		
		$('textInfo').set('text', this.datas[this.currentImage-1].alt);
		$('pagination').set('text', this.currentImage + "/" + this.datas.length);		
	},
	
	initConfig:function() {
		if ($('photoViewer')) {
			if (Browser.Engine.trident4 || Browser.Engine.trident5) {
				this.imageX = 308;
			}
			else {
				this.imageX = $('photoViewer').getElement('img').getLeft();
			}
		}
		if(designcenter) this.datas = designcenter;
		
		//$('pagination').set('text', this.currentImage + "/" + this.datas.length);
	}
	
});

window.addEvent('domready', function() {
	var designcenter = new DesignCenter();
});