var Curtain = new Class({
  open_btn:null,
  files:null,
  timer:null,
  current:0,
  timer_delay: 5000,
  plan_min:Object({'top':178, 'right': 330, 'bottom': 150, 'left': 90}),
  min_width:null,
  min_height:null,
  plan_ratio:null,
  menu_cat:null,
  
  initialize: function(files) {
	this.files = files;
    this.init();
  },
  
  init:function() {
	this.min_width = $('container').getStyle('min-width').replace('px', '');
	this.min_height = $('container').getStyle('min-height').replace('px', '');

    this.open_btn = $('diapos').getFirst('.bkg');
	
	var myTips = new Tips('.diapos .bkg', {
		showDelay: 20,
		hideDelay: 20,
		className: 'cursor',
		offset: {'x': 15, 'y': 30},
		onShow: function(toolTip) {
			toolTip.set('tween', {duration: 20}).tween('opacity', 1);
		},
		onHide: function(toolTip) {
			toolTip.set('tween', {duration: 20}).tween('opacity', 0);
		}

	});
    this.open_btn.bound = this.open_curtain.bindWithEvent(this);
	
    // left
    this.left = new Movable($$('.content')[0]);
    this.left.fx = new Fx.Tween(this.left.cont, {
		link:'cancel'
	});
	this.left.property_left = 'left';
	this.left.dest_left = - this.left.cont.getSize().x - 4 + 'px';
	this.left.property_right = 'top';
	this.left.dest_right = - this.left.cont.getSize().y - 4 + 'px';
	if(type == 'TYPE') {
		this.left.property = this.left.property_left;
		this.left.dest = this.left.dest_left;
	}
	else {
		this.left.property = this.left.property_right;
		this.left.dest = this.left.dest_right;
	}
	this.left.src = this.left.cont.getStyle(this.left.property);

    // right
    this.right = new Movable($$('.menu_right')[0]);
    this.right.fx = new Fx.Tween(this.right.cont, {
		link:'cancel'
	});
    this.right.src = this.right.cont.getStyle('right');
    this.right.property = 'right';
    this.right.dest = - this.right.cont.getSize().x + 'px';
	
	// diapo left
	this.diapo_left = new Movable($('diapos').getFirst('.left'));
    this.diapo_left.fx = new Fx.Tween(this.diapo_left.cont, {
		link:'cancel'
	});
    this.diapo_left.src = this.diapo_left.cont.getStyle('left');
    this.diapo_left.property = 'left';
    this.diapo_left.dest = 0;
	this.diapo_left.bound = this.prev_diapo.bindWithEvent(this);
	this.diapo_left.cont.addEvent('click', this.diapo_left.bound);
	
	// diapo right
	this.diapo_right = new Movable($('diapos').getFirst('.right'));
    this.diapo_right.fx = new Fx.Tween(this.diapo_right.cont, {
		link:'cancel'
	});
    this.diapo_right.src = this.diapo_right.cont.getStyle('right');
    this.diapo_right.property = 'right';
    this.diapo_right.dest = !is_ipad ? 0 : 21;
	this.diapo_right.bound = this.next_diapo.bindWithEvent(this);
	this.diapo_right.cont.addEvent('click', this.diapo_right.bound);
	
	// diapos
	this.diapo = new Movable($('diapos').getFirst('.bkg'));
	this.diapo.property = 'opacity';
	this.diapo.dest = 0;
	this.diapo.src = 1;
	this.diapo.fx = new Fx.Tween(this.diapo.cont);
	this.diapo.bound = null;
	
	// footer
	this.footer = null;
	if(type != 'TYPE') {
		this.footer = new Movable($('footer_top'));
		this.footer.property = 'bottom';
		this.footer.src = this.footer.cont.getStyle('bottom');
		this.footer.dest = - this.footer.cont.getSize().y;
		this.footer.fx = new Fx.Tween(this.footer.cont);
	}
	
	// Wheel
	if($('wheel')) {
	this.wheel = new Movable($('wheel_div'));
	this.wheel.property = 'bottom';
	this.wheel.src = this.wheel.cont.getStyle(this.wheel.property);
	this.wheel.dest = - this.wheel.cont.getSize().y;
	this.wheel.fx = new Fx.Tween(this.wheel.cont);
	}
	
	// Booking
	this.booking = new Movable($('booking'));
	var to_hide = false;
	if(this.booking.cont.getStyle('display') == 'none') {
		this.booking.cont.setStyle('display', 'block');
		to_hide = true;
	}
	this.booking.property = 'top';
	this.booking.src = this.booking.cont.getStyle('top');
	this.booking.dest = - this.booking.cont.getSize().y;
	this.booking.fx = new Fx.Tween(this.booking.cont);
	//if(to_hide) {
		//this.booking.fx.set(this.booking.property, this.booking.dest);
		this.booking.is_open = false;
	//}
	
	// Booking close button
	if(type != 'HOME') {
		//this.booking_close_btn = $('booking').getFirst('.close');
		//this.booking_close_btn.addEvent('click', this.booking_click.bindWithEvent(this));
		$$('.reserver')[0].addEvent('click', this.booking_click.bindWithEvent(this));
		$('booking').getFirst('.close').addEvent('click', this.booking_click.bindWithEvent(this));
	}
	/*
	this.booking_btn = $$('.reserver')[0].getFirst('a');
	this.booking_btn.addEvent('click', this.booking_click.bindWithEvent(this));
	*/
	// Chargement des images
	this.init_diapos(true); // mathias : ajout de true pour qu'il demarre a l'image 0 et pas 1
	
	window.addEvent('resize', this.on_window_resize.bindWithEvent(this));
	this.on_window_resize();
	
	// Menu Catégories
	if($('menu_cat')) this.menu_cat = new Menu(this);
	
	var menu_cat = this.menu_cat;
	$$('.lien_interne').addEvent('click', function(e) {
		e.stop();
		var reg = new RegExp('([0-9]{4})');
		var href = this.get('href');
		var cat = this.get('href').match(reg);
		var menu_item = menu_cat.getItemById(cat[0]);
		menu_item.clickHandlerCallback();
	 });

	// Actus
	if(type == 'HOME') {
		var amp = 100;
		this.actu = new Movable($('actualites'));
		this.actu.src_1 = 1;
		this.actu.dest_1 = 0;
		this.actu.src_2 = this.actu.cont.getStyle('left');
		this.actu.dest_2 = null;
		this.actu.fx = new Fx.Morph(this.actu.cont, {
										duration: 300
									});
		this.actu.open = function() {
			switch(this.way) {
				case 'left': this.dest_2 = amp; break;
				case 'right': this.dest_2 = - amp; break;
			}
			this.fx.start({
				'opacity': this.dest_1,
				'left': this.dest_2
			});
		};
		this.actu.close = function() {
			switch(this.way) {
				case 'left': this.set = - amp; break;
				case 'right': this.set = amp; break;
			}
			this.cont.setStyle('left', this.set);
			this.fx.start({
				'opacity': this.src_1,
				'left': this.src_2
			});
		};
		
		this.init_actu_boutons();
	}
	
	// Zooms plans
	$$('.zoom').each(function(el) {
		el.addEvent('click', this.zoom_click.bindWithEvent(this));
	}, this);
  },
  
  open_curtain:function(e) {
    e.stop();

    this.open_btn.removeEvent('click', this.open_btn.bound);
	this.open_btn.removeEvent('click', this.left.bound);
	
	if(main_cat != activite_cat) {
		this.left.bound = this.open_boutons.bind(this);
	}else {
		this.left.bound = this.open_activite.bind(this);
	}
		
	this.left.fx.addEvent('complete', this.left.bound);
	this.left.open();
    this.right.open();
	if(this.footer) this.footer.open();
	this.booking.open();
	if(this.wheel) this.wheel.open();
	$$('.cursor')[0].setStyle('background-image', 'url(/images/cursor_close.gif)');
  },
  
  close_curtain:function(e) {
	this.diapo_left.fx.removeEvent('complete', this.left.bound);
	this.open_btn.removeEvent('click', this.open_btn.bound);
   	this.left.close();
    this.right.close();
	if(this.footer) this.footer.close();
	//if(type == 'HOME') this.booking.close();
	//else this.booking.open();
	this.booking.close();
	if(this.wheel) this.wheel.close();
	
	this.open_btn.bound = this.open_curtain.bindWithEvent(this);
    this.open_btn.addEvent('click', this.open_btn.bound);
	
	$$('.cursor')[0].setStyle('background-image', 'url(/images/cursor_open.gif)');
  },
  
  open_boutons:function() {
	this.left.fx.removeEvent('complete', this.left.bound);
	
	this.open_btn.bound = this.close_boutons.bindWithEvent(this);
    this.open_btn.addEvent('click', this.open_btn.bound);
	
	this.diapo_left.open();
	this.diapo_right.open();
  },
  
  close_boutons:function() {
	  
	this.left.bound = this.close_curtain.bindWithEvent(this);
	this.diapo_left.fx.addEvent('complete', this.left.bound);

    this.open_btn.removeEvent('click', this.open_btn.bound);
	
	this.diapo_left.close();
	this.diapo_right.close();
	
  },
  
  open_activite:function() {
	this.left.fx.removeEvent('complete', this.left.bound);
	this.open_btn.bound = this.close_curtain.bindWithEvent(this);
    this.open_btn.addEvent('click', this.open_btn.bound);
  },
  
  next_diapo:function(e) {
	  if(e != null) e.stop();
	  if(this.timer != null) $clear(this.timer);
	  this.diapo_right.cont.removeEvent('click', this.diapo_right.bound);
	  this.diapo_left.cont.removeEvent('click', this.diapo_left.bound);
	  if(main_cat != activite_cat) this.current = (this.current + 1 >= this.bkgs.length) ? 0 : this.current+1;
	  this.diapo.bound = this.change_diapo.bindWithEvent(this, this.current);
	  this.diapo.fx.addEvent('complete', this.diapo.bound);
	  this.diapo.open();
  },
  
  prev_diapo:function(e) {
	  if(e != null) e.stop();
	  if(this.timer != null) $clear(this.timer);
	  this.diapo_right.cont.removeEvent('click', this.diapo_right.bound);
	  this.diapo_left.cont.removeEvent('click', this.diapo_left.bound);
	  this.current = (this.current - 1 < 0) ? this.bkgs.length - 1 : this.current-1;
	  this.diapo.bound = this.change_diapo.bindWithEvent(this, this.current);
	  this.diapo.fx.addEvent('complete', this.diapo.bound);
	  this.diapo.open();
  },
	
  launch_timer:function() {
	this.diapo.fx.removeEvent('complete', this.diapo.bound);
	if(main_cat != activite_cat) this.timer = this.next_diapo.delay(this.timer_delay, this);
	
	this.diapo_right.cont.removeEvent('click', this.diapo_right.bound);
	this.diapo_left.cont.removeEvent('click', this.diapo_left.bound);
	this.diapo_right.cont.addEvent('click', this.diapo_right.bound);
	this.diapo_left.cont.addEvent('click', this.diapo_left.bound);
  },
  
  init_diapos:function(launch) {
	if(this.timer != null) $clear(this.timer);
	this.bkgs = this.files.split(',');
	var myImages = new Asset.images(this.bkgs, {
		onComplete: function() {
			this.launch_timer();
			if(launch) {
				this.current = -1;
				this.next_diapo();
			}
			this.open_btn.addEvent('click', this.open_btn.bound);
		}.bind(this)
	});  
  },
  
  change_diapo:function(e, ind) {
	  this.diapo.fx.removeEvent('complete', this.diapo.bound);
	  this.diapo.cont.getFirst('.image').destroy();

	  var myImages = new Asset.images(this.bkgs[ind], {
		onComplete: function() {
			var img = new Element('img', {
				'src': this.bkgs[ind],
				'class': 'image'
			}).inject(this.diapo.cont);	
			var sizes = this.image_size();
			img.setStyles({
				'width': sizes.width,
				'height': sizes.height,
				'top': sizes.x,
				'left': sizes.y
			});
			this.diapo_resize();
			this.diapo.bound = this.launch_timer.bind(this);
			this.diapo.fx.addEvent('complete', this.diapo.bound);
			this.diapo.close();
		}.bind(this)
	  });
  },
  
  on_window_resize:function() {
	var win = $(window).getSize();
	if(this.min_width < win.x || this.min_height < win.y) {
		if(ie7) {
			if(type != 'HOME' && type != 'ACTIVITE') this.left.cont.setStyle('height', $('container').getSize().y);
		}
		this.diapo_resize();
	}
  },
  
  booking_click:function(e) {
	e.stop();
	if(type != 'HOME') {
		if(this.booking.is_open) {
			this.booking.close();	
		}
		else {
			this.booking.open();	
		}
	}
	else {
		if(this.left.is_open) this.open_btn.fireEvent('click');
	}
  },
  
  change_type_content:function(cat) {
	  if(this.plan) this.zoom_close();
	  this.left.fx.removeEvent('complete', this.left.bound);
	  this.diapo_left.fx.removeEvent('complete', this.diapo_left.bound);
	  var from = '';
	  // Si On veut afficher une offre sur la page activité, on le transmet au php
	  if(type == 'ACTIVITE') from = '&from=activite';
	  var myRequest = new Request({
		method: 'get',
		url: '/cmn/request.php',
		onComplete: function(response) {
			$('include_type').set('html', response);
			var src = $('include_titre').getFirst('img').src;
			var old = src.match(new RegExp("(.*)?text(.*)color=(.*)"));
			old = old[2];
			$('include_titre').getFirst('img').set('src', src.replace(old, '='+$('titre_request').value+'&'));
			
			this.init_js();
			
			this.left.close();
			this.right.close();
			if(this.footer) this.footer.close();
			if(this.wheel) this.wheel.close();
			
			// Si il le faut changer d'iamges pour le diapo
			if($('img_request')) {
				this.files = $('img_request').get('value');
				this.init_diapos(true);	
			}
		}.bind(this)
	  }).send('cat='+cat+'&lang='+lang+from);
  },
  
  change_activite_content:function(cat) {
	  this.left.fx.removeEvent('complete', this.left.bound);
	  if(this.diapo_left.is_open) {
	    this.diapo_left.fx.removeEvent('complete', this.left.bound);
		this.diapo_left.bound = this.change_type_content.bind(this, cat);
		this.diapo_left.fx.addEvent('complete', this.diapo_left.bound);
		this.diapo_left.close();
		this.diapo_right.close();
		$$('.cursor')[0].setStyle('background-image', 'url(/images/cursor_open.gif)');
	  }
	  else {
		this.left.fx.removeEvent('complete', this.left.bound);
		this.left.bound = this.change_type_content.bind(this, cat);
		this.left.fx.addEvent('complete', this.left.bound);
		this.left.open();
		this.footer.open();
	  }
	  for(var i = 0; i < this.bkgs.length; i++) {
		if(this.bkgs[i].match(new RegExp('(.*)'+cat+'.jpg'))) {
			this.current = i;
			break;
		}
	  }
	  this.next_diapo();
  },
  
  actu_left_click:function(e) {
	e.stop();
	if(current_actu > 0) {
		current_actu --;
		this.actu_left.removeEvent('click', this.actu_left.bound);
		this.actu_right.removeEvent('click', this.actu_right.bound);
		this.actu.bound = this.actu_open_complete.bind(this);
		this.actu.fx.addEvent('complete', this.actu.bound);
		this.actu.way = 'left';
		this.actu.open();
	}
  },
  
  actu_right_click:function(e) {
	e.stop();
	if(current_actu <= (total_actu - 3)) {
		current_actu ++;
		this.actu_left.removeEvent('click', this.actu_left.bound);
		this.actu_right.removeEvent('click', this.actu_right.bound);
		this.actu.bound = this.actu_open_complete.bind(this);
		this.actu.fx.addEvent('complete', this.actu.bound);
		this.actu.way = 'right';
		this.actu.open();
	}
  },
  
  actu_open_complete:function() {
	  this.actu.fx.removeEvent('complete', this.actu.bound);
	  this.actu.bound = this.actu_close_complete.bind(this);
	  this.actu.fx.addEvent('complete', this.actu.bound);
	  var myRequest = new Request({
		method: 'get',
		url: '/cmn/actus.php',
		onComplete: function(response) {
			this.actu.cont.set('html', response);
			this.actu.close();
			this.init_actu_boutons();
		}.bind(this)
	  }).send('actu='+current_actu+'&lang='+lang);
  },
  
  actu_close_complete:function() {
	  this.actu.fx.removeEvent('complete', this.actu.bound);
	  this.actu_left.addEvent('click', this.actu_left.bound);
	  this.actu_right.addEvent('click', this.actu_right.bound);
  },
  
  init_actu_boutons:function() {
	this.actu_left = $$('.arrow.left')[0];
	this.actu_left.bound = this.actu_left_click.bindWithEvent(this)
	this.actu_left.addEvents({
		'click': this.actu_left.bound
	});
	
	this.actu_right = $$('.arrow.right')[0];
	this.actu_right.bound = this.actu_right_click.bindWithEvent(this)
	this.actu_right.addEvents({
		'click': this.actu_right.bound
	});
	if(current_actu > 0) this.actu_left.setStyle('display', 'block');
	else this.actu_left.setStyle('display', 'none');
	if(current_actu <= (total_actu - 3)) this.actu_right.setStyle('display', 'block');
	else this.actu_right.setStyle('display', 'none');
  },
  
  zoom_click:function(e) {
	e.stop();
	//var url = e.target.getNext('img').src.replace('mini/', 'full/');
	var url = e.target.get('rel');
	this.left.fx.removeEvent('complete', this.left.bound);
	this.left.bound = this.zoom_open_complete.bindWithEvent(this, url);
	this.left.fx.addEvent('complete', this.left.bound);
	this.open_btn.removeEvent('click', this.open_btn.bound);
	this.left.open();
	this.right.open();
	if(this.footer) this.footer.open();
	if(this.wheel) this.wheel.open();
	this.booking.open();
	if(this.timer != null) $clear(this.timer);
	$$('.cursor')[0].setStyle('background-image', 'url(/images/cursor_close.gif)');
  },
  
  zoom_open_complete:function(e, url) {
	if(this.timer != null) $clear(this.timer);
	this.left.fx.removeEvent('complete', this.left.bound);
	var myImages = new Asset.images(url, {
		onComplete: this.zoom_load_complete.bind(this, url)
	});
  },
  
  zoom_load_complete:function(url) {
	var plan = new Element('img', {
		'src': url
	});
	var plan_bkg = new Element('div', {
		'class': 'plan_bkg',
		'events': {
			'click': this.zoom_close.bindWithEvent(this)
		}
	});
	this.plan = new Movable(plan_bkg);
	this.plan.src = 0;
	this.plan.dest = 1;
	this.plan.property = 'opacity';
	this.plan.fx = new Fx.Tween(this.plan.cont);
	this.plan.fx.set(this.plan.property, this.plan.src);
	plan.inject(this.plan.cont);
	this.plan.cont.inject(this.diapo.cont, 'top');
	var pl = this.plan.cont.getFirst('img').getSize();
	this.plan_ratio = pl.x / pl.y;
	this.plan.fx.start(this.plan.property, this.plan.dest);
	this.plan_resize();
  },
  
  zoom_close:function() {
	 this.plan.bound = this.zoom_close_complete.bind(this);
	 this.plan.fx.addEvent('complete', this.plan.bound);
	 this.plan.fx.start(this.plan.property, this.plan.src);
  },
  
  zoom_close_complete:function() {
	 this.plan.fx.removeEvent('complete', this.plan.bound);
	 this.launch_timer();
	 this.close_curtain();
	 this.plan.cont.destroy();
	 this.plan = null;
  },
  
  diapo_resize:function() {
	var sizes = this.image_size();
	if(this.diapo.cont.getFirst('.image')) {
		this.diapo.cont.getFirst('.image').setStyles({
			'width': sizes.width,
			'height': sizes.height,
			'top': sizes.x,
			'left': sizes.y
		});
	}
	if(this.diapo.cont.getFirst('.plan_bkg')) {
		this.plan_resize();
	}
  },
  
  plan_resize:function() {
	var win = $(window).getSize();
	var plan = this.diapo.cont.getFirst('.plan_bkg').getFirst('img');
	var pl_1 = plan.getCoordinates();
	var pl_2 = this.plan_min;
	if(!(win.x > this.min_width && win.y > this.min_height)) {
		var w = this.min_width;
		var h = this.min_height;
	}
	else {
		var w = win.x;
		var h = win.y;
	}
	var free_width = w - pl_2.left - pl_2.right;
	var free_height = h - pl_2.top - pl_2.bottom;
	var free_ratio = free_width / free_height;

	if(this.plan_ratio < free_ratio) {
		plan.setStyle('height', free_height);
		plan.setStyle('width', free_height * this.plan_ratio);
	}
	else {
		plan.setStyle('width', free_width);
		plan.setStyle('height', free_width / this.plan_ratio);
	}
	
	plan.setStyle('top', pl_2.top+'px');
	plan.setStyle('left', pl_2.left+'px');
  },
  
  image_size:function() {
	var cont = $('container').getSize();
	var image = this.diapo.cont.getFirst('.image');
	var win = $(window).getSize();
	if(image) {
		var img = image.getSize();
		var ratio = img.x / img.y;
		var ret = new Object();
		if(win.x > win.y && win.x / win.y > ratio) {
		  ret.width = win.x;
		  ret.height = win.x / ratio;
		}
		else {
		  ret.width = win.y * ratio;
		  ret.height = win.y;
		}
		
		if(win.y > win.x) {
			ret.x = 0;
			ret.y = -(ret.width - win.x) / 2;
		}
		else {
			ret.x = -(ret.height - win.y) / 2;
			ret.y = 0;
		}
		return ret;
	}
	return null;
  },
  
  init_js:function() {
	init_content();
	$$('.zoom').each(function(el) {
		el.addEvent('click', this.zoom_click.bindWithEvent(this));
	}, this);
	if($('form_resa')) {
		init_form();
	}
  }
  
});
