var Diapo = new Class({
	/* ELEMENTS */
	next_elt:null,
	hexa_elt:null,
	cont_elt:null,
	cont_next_elt:null,
	active_next_elt:null,
	
	/* EFFETS */
	transition_fx:null,
	next_fx:null,
	
	/* VARIABLES */
	current : 0,
	files : new Array(),
	is_open : false,
	url:null,
	full_dir:null,
	thumbs_dir:null,
	extension: '.jpg',
	
	timer : null,
	timer_delay : 5000,
	timer_next:null,
	timer_next_delay : 2000,
	
	next_dest:null,
	next_src:null,
	next_width:null,
	active_width:null,
	active_left:null,
	active_zindex_src:null,
	active_zindex_dest:null,
	next_zindex_src:null,
	next_zindex_dest:null,
	
	bound_active_move_evt:null,
	bound_active_leave_evt:null,
	bound_next_over_evt:null,
	bound_next_out_evt:null,
	bound_next_fx:null,
	bound_transition_in_fx:null,
	
	count_move:null,
	limit_move:null,
	
	initialize:function(files_list, url, full_dir, thumbs_dir) {
		conslog('Initialize Menu Class');
		conslog("liste des images : " + files_list);
		
		this.init_elt();
		this.init_fx();
		this.init_constants(files_list, url, full_dir, thumbs_dir);
		this.init();
	},
	
	init_elt:function() {
		this.hexa_elt = $('hexagons');
		this.cont_elt = $('diapos');
		if($('diapo_next')) this.next_elt = $('diapo_next');
		if($('nav_cont')) this.cont_next_elt = $('nav_cont');
		if($('active_next')) this.active_next_elt = $('active_next');
	},
	
	init_fx:function() {
		this.transition_in_fx = new Fx.Tween(this.cont_elt, {
			duration: 1000,
			//onComplete: this.fade_in_complete.bindWithEvent(this),
			link:'cancel'
		});
		this.transition_out_fx = new Fx.Tween(this.cont_elt, {
			duration: 1000,
			onComplete: this.fade_out_complete.bindWithEvent(this),
			link:'cancel'
		});
		this.next_fx = new Fx.Tween(this.next_elt, {
			transition:Fx.Transitions.Quad.easeOut, 
			duration:800, 
			link:'cancel'
		});
	},
	
	init_constants:function(files_list, url, full_dir, thumbs_dir) {
		this.files = files_list.split(',');
		this.url = url;
		this.full_dir = full_dir;
		this.thumbs_dir = thumbs_dir;

		if(this.next_elt != null) {
			this.next_dest = this.next_elt.getStyle('right').replace('px','');
			this.next_src = -this.next_elt.getSize().x;
			this.next_width = this.cont_next_elt.getSize().x;
		}
		
		this.active_zindex_src = 7;
		this.active_zindex_dest = 1000;
		this.next_zindex_src = 8;
		this.next_zindex_dest = 1001;
		
		this.bound_active_move_evt = this.active_move_evt.bind(this);
		this.bound_active_leave_evt = this.active_leave_evt.bind(this);
		this.bound_next_over_evt = this.next_over_evt.bind(this);
		this.bound_next_out_evt = this.next_out_evt.bind(this);
		this.bound_next_click_evt = this.next_click_evt.bindWithEvent(this);
		
		this.count_move = 0;
		this.limit_move = 10;
	},
	
	init:function() {
		// Préchargement des images
		var urls = new Array();
		for(var i = 0; i < this.files.length; i ++) urls.push(this.url + this.full_dir + this.files[i] );
		var myImages = new Asset.images(urls, {
			onComplete: this.launch_timer.bind(this)
		});
		
		if(this.next_elt != null) {
			this.next_elt.setStyle('right', this.next_src);
		}
		
		this.bound_transition_in_fx = this.fade_in_complete.bindWithEvent(this);
		this.transition_in_fx.addEvent('complete', this.bound_transition_in_fx);
	},
	
	/* METHODES */
	
	launch_timer : function() {
		if(!this.is_open){
			if(this.timer != null) $clear(this.timer);
			this.timer = this.next_click_evt.periodical(this.timer_delay, this);
		}
		return false;
	},
	
	load_image:function(url) { 
		conslog('start load_image of '+url+' with current : '+this.current);
		//var file = url.slice(url.lastIndexOf('/')+1, url.lastIndexOf('.'));
		var file = url.slice(url.lastIndexOf('/')+1);

		this.current = this.files.indexOf(file);

		// Appel de la fonction Flash pour lui dire au'on change de diapo
		$('hexagons').onStartChangeDiapo(file.replace('.jpg', ''));
		
		this.freeze_actions();
		
		if(this.timer != null) $clear(this.timer);

		this.transition_in_fx.start('opacity', 0);

	},
	
	fade_in_complete:function() {
		//var file = this.url + this.full_dir + this.files[this.current] + this.extension;
		var file = this.url + this.full_dir + this.files[this.current];
		this.cont_elt.setStyle('background-image','url('+file+')');
		
		this.transition_out_fx.start('opacity', 1);
	},
	
	fade_out_complete:function() {
		this.launch_timer();
		this.unfreeze_actions();
		$('hexagons').onCompleteChangeDiapo();
	},
	
	fade : function(to, fn) {
		var diapos = $('diapos');
		var diapo_fx = new Fx.Tween(diapos, {duration: 1000, onComplete:fn, link:'cancel'});
		diapo_fx.start('opacity', to);
	},
	
	freeze_actions : function() {
		if(this.cont_next_elt != null) {
			this.cont_next_elt.removeEvents({
				'click': this.bound_next_click_evt
			});
		}
	},
	
	unfreeze_actions : function() {
		if(this.cont_next_elt != null) {
			this.cont_next_elt.addEvents({
				'click': this.bound_next_click_evt
			});
		}
	},
	
	show_nav:function() {
		this.next_fx.start('right', this.next_dest);
		if(this.timer != null) $clear(this.timer);
		if(this.timer_next != null) $clear(this.timer_next);
		this.timer_next = this.active_nav.periodical(this.timer_next_delay, this);
		
		this.place_nav();
		
		window.onresize = this.place_nav.bind(this);
	},
	
	hide_nav:function() {
		conslog('hide_nav');
		this.next_fx.start('right', this.next_src);
		this.active_next_elt.setStyle('display', 'none');
		this.active_next_elt.setStyle('z-index', this.active_zindex_src);
		this.cont_next_elt.setStyle('z-index', this.next_zindex_src);
		
		this.active_next_elt.removeEvents({
			'mousemove': this.bound_active_move_evt,
			'mouseleave': this.bound_active_leave_evt
		});
		
		this.cont_next_elt.removeEvents({
			'mouseover': this.bound_next_over_evt,
			'mouseout': this.bound_next_out_evt,
			'click': this.bound_next_click_evt
		});
		
		if(this.timer != null) $clear(this.timer);
		this.timer = this.next_click_evt.periodical(this.timer_delay, this);
		
		window.onresize = null;
	},
	
	place_nav:function() {
		this.active_next_elt.setStyle('display', 'block');
		this.active_next_elt.setStyle('z-index', this.active_zindex_dest);
		this.cont_next_elt.setStyle('z-index', this.next_zindex_dest);
		
		var win_width = $(window).getSize().x;
		var body_width = $('container').getSize().x;
		var diapo_left = win_width < 1600 ? ( body_width - win_width ) / 2 + win_width - this.next_width : 1600 - this.next_width;
		this.active_width = diapo_left - 700;
		
		if(Browser.Engine.trident) diapo_left += 8;
		
		this.cont_next_elt.setStyles({	
			'left' : diapo_left
		});
		
		this.active_next_elt.setStyles({
			'left': diapo_left - this.active_width,
			'width': this.active_width
		});
		
		this.active_left = this.active_next_elt.getCoordinates().left;
	},
	
	active_nav:function() {
		if(this.timer_next != null) $clear(this.timer_next);
		this.next_fx.start('right', this.next_src);

		this.active_next_elt.addEvents({
			'mousemove': this.bound_active_move_evt,
			'mouseleave': this.bound_active_leave_evt
		});
		
		this.cont_next_elt.addEvents({
			'mouseover': this.bound_next_over_evt,
			'mouseout': this.bound_next_out_evt,
			'click': this.bound_next_click_evt
		});
	},
	
	next_mousemove:function(x, a, b) {
		v = -1;
		//b -= 200;
		return -(b*(1-x/a));
		//return v*((-b/(a*a))*(x*x) + b)
		//return b*(Math.cos(x*Math.PI/a) + 1);
	},
	
	/* EVENTS */
	
	next_click_evt : function(e) {
		if(e) e.stop();
		this.current++;
		if(this.current == this.files.length) this.current = 0;
		//this.load_image(this.url + this.full_dir + this.files[this.current] + this.extension);
		this.load_image(this.files[this.current]);
	},
	
	active_move_evt:function(e) {
		this.count_move ++;
		if(this.count_move == 1) this.next_fx.start('right',this.next_mousemove((e.client.x-this.active_left), this.active_width, this.next_width));
		else if(this.count_move == this.limit_move) this.count_move = 0;
	},
	
	active_leave_evt:function() {
		conslog('ctive_leave_evt');
		this.next_fx.start('right',this.next_src);
	},
	
	next_over_evt:function() {
		//this.next_elt.getFirst('img').src = '/images/diapo_next_over.png';
		this.next_fx.start('right', this.next_dest);
		this.count_move = 0;
	},
	
	next_out_evt:function() {
		//this.next_elt.getFirst('img').src = '/images/diapo_next.png';
		this.next_fx.start('right', this.next_src);
	},
	
	
	
	reloadSWF:function(thumbs, thumb_dir, lang, is_home, is_offres, timer_delay, url_offres, files_offres){
		var flashvars = {};
		flashvars.thumbs = thumbs;
		flashvars.dir = thumb_dir;
		flashvars.lng = lang;
		flashvars.is_home = is_home;
		flashvars.is_offres = is_offres;
		flashvars.timer = timer_delay;
		flashvars.url_offres = url_offres;
		flashvars.files_offres = files_offres;
		var params = {};
		params.wmode = "transparent";
		var attributes = {};
		swfobject.embedSWF("/swf/hexagons.swf", "hexagons", "245", "540", "9.0.0", false, flashvars, params, attributes); 

	},
	
	
	//reInit:function(files_list, url, full_dir, thumbs_dir){		
	reInit:function(diapo_vars, flash_vars){		
		this.reloadSWF(
									 flash_vars.thumbs,
									 flash_vars.thumb_dir,
									 flash_vars.lang,
									 flash_vars.is_home,
									 flash_vars.is_offres,
									 flash_vars.timer_delay,
									 flash_vars.url_offres,
									 flash_vars.files_offres
									 );
		
		this.current = 0;
		this.files = diapo_vars.files_list.split(',');
		this.url = diapo_vars.url;
		this.full_dir = diapo_vars.full_dir;
		this.thumbs_dir = diapo_vars.thumbs_dir;
		
		var file = this.url + this.full_dir + this.files[0];
		this.cont_elt.setStyle('background-image','url('+file+')');
	}
	
	

});

