﻿var Curtain = new Class({
	/* ELEMENTS */
	bg_left_1_elt:null,
	bg_left_2_elt:null,
	bg_right_elt:null,
	pano_elt:null,
	hexa_cont_elt:null,
	content_elt:null,
	title_elt:null,
	page_elt:null,
	tooltip_elt:null,
	listing_offre_elt:null,
	content_offre_elt:null,
	
	/* EFFETS */
	bg_left_1_fx:null,
	bg_left_2_fx:null,
	bg_right_fx:null,
	pano_fx:null,
	hexa_fx:null,
	content_fx:null,
	title_fx:null,
	tooltip_fx:null,
	listing_offre_fx:null,
	content_offre_fx:null,
	
	/* VARIABLES */
	
	is_open: false,
	is_right_open: false,
	is_offre_open: false,
	
	pano_opened_class:null,
	pano_closed_class:null,
	page_opened_class:null,
	page_closed_class:null,
	
	// Durations
	bg_left_1_dur_fast:null,
	bg_left_1_dur_slow:null,
	bg_left_2_dur_fast:null,
	bg_left_2_dur_slow:null,
	bg_right_dur_fast:null,
	bg_right_dur_slow:null,
	title_dur:null,
	
	// Deltas
	bg_left_1_src:null,
	bg_left_1_dest:null,
	bg_left_2_src:null,
	bg_left_2_dest:null,
	bg_left_2_dest_2:null,
	bg_right_src:null,
	bg_right_dest_1:null,
	bg_right_dest_2:null,
	pano_src:null,
	pano_dest:null,
	hexa_src:null,
	hexa_dest_1:null,
	hexa_dest_2:null,
	title_src:null,
	title_dest:null,
	tooltip_src:null,
	tooltip_dest:null,
	content_offre_src:null,
	content_offre_dest:null,
	
	bg_right_zindex_src:null,
	bg_right_zindex_dest:null,
	
	// Bounds
	bound_bg_left_2_fx:null,
	bound_bg_right_fx:null,
	bound_offre_click_evt:null,
	
	
	initialize:function() {
		conslog('Initialize Curtain Class');		
		
		this.init_elt();
		this.init_constants();
		this.init_fx();
		this.init();
		
		if(!(is_home || is_offres)) {
			this.init_elt_type();
			this.init_constants_type();
			this.init_fx_type();
			this.init_type();
		}
		
	},
	
	/* INITIALISATION */
	
	init_elt:function() {
		this.bg_right_elt = $('bg_right');
		this.hexa_cont_elt = $('hexagons_cont');
		this.listing_offre_elt = $('offers');
		this.content_offre_elt = $('content_offre');
		
		if($$('.popup')[0]) this.popup = $$('.popup')[0];
	},
	
	init_elt_type:function() {
		this.bg_left_1_elt = $('bg_left_type1');
		this.bg_left_2_elt = $('bg_left_type2');
		this.pano_elt = $('pano');
		this.content_elt = $('content');
		this.title_elt = $('content_title');
		this.page_elt = $('page');
		this.tooltip_elt = $('tooltip');
	},
	
	init_fx:function() {
		this.bg_right_fx = new Fx.Morph(this.bg_right_elt, {
			transition:Fx.Transitions.Quad.easeOut,
			duration:this.bg_right_dur_slow,
			link:'cancel'
		});	
		this.hexa_fx = new Fx.Tween(this.hexa_cont_elt, {
			transition:Fx.Transitions.Quad.easeOut, 
			duration:this.bg_right_dur_slow,
			link:'cancel'
		});
		this.listing_offre_fx = new Fx.Tween(this.listing_offre_elt, {
			transition:Fx.Transitions.Quad.easeOut,
			duration:this.bg_left_1_dur_fast,
			link:'cancel'
		});
		this.content_offre_fx = new Fx.Tween(this.content_offre_elt, {
			transition:Fx.Transitions.Quad.easeOut,
			duration:this.bg_right_dur_fast,
			link:'cancel'
		});
		
		if(this.popup) {
		this.popup_fx = new Fx.Tween(this.popup, {
			transition:Fx.Transitions.Quad.easeOut,
			duration:this.bg_right_dur_fast,
			link:'cancel'
		});
		}
	},
	
	init_fx_type:function() {
		this.bg_left_1_fx = new Fx.Tween(this.bg_left_1_elt, {
			transition:Fx.Transitions.Quad.easeOut,
			duration:this.bg_left_1_dur_slow,
			link:'cancel'
		});
		this.bg_left_2_fx = new Fx.Tween(this.bg_left_2_elt, {
			transition:Fx.Transitions.Quad.easeOut, 
			duration:this.bg_left_2_dur_slow,
			link:'cancel'
		});
		this.pano_fx = new Fx.Tween(this.pano_elt, {
			transition:Fx.Transitions.Quad.easeOut, 
			duration:this.bg_left_1_dur_slow,
			link:'cancel'
		});
		this.content_fx = new Fx.Slide(this.content_elt, {
			mode:'horizontal', 
			duration: this.bg_left_1_dur_slow,
			transition:Fx.Transitions.Quad.easeOut,
			link:'cancel'
		});
		this.title_fx = new Fx.Tween(this.title_elt, {
			transition:Fx.Transitions.Quad.easeOut,
			duration:this.title_dur,
			link:'cancel'
		});
		this.tooltip_fx = new Fx.Morph(this.tooltip_elt, {
			transition:Fx.Transitions.Quad.easeOut,
			duration:1000,
			link:'cancel'
		});
	},
	
	init_constants:function() {
		this.bg_left_1_dur_slow = 800;
		this.bg_left_1_dur_fast = 600;
		
		this.bg_right_dur_slow = 1000;
		this.bg_right_dur_fast = 1000;
		
		this.bg_right_src = 609;
		this.bg_right_dest_1 = this.bg_right_src + 700;
		this.bg_right_dest_2 = 0;
		//this.hexa_src = this.hexa_cont_elt.getStyle('left').replace('px','');
		this.hexa_src = 1159;
		this.hexa_dest_1 = this.bg_right_dest_1 + (this.hexa_src - this.bg_right_src);
		this.hexa_dest_2 = this.bg_right_dest_2 + (this.hexa_src - this.bg_right_src);
		this.content_offre_src = -this.content_offre_elt.getSize().x;
		this.content_offre_dest = 175;
		
		this.bg_right_zindex_src = this.bg_right_elt.getStyle('z-index');
		this.bg_right_zindex_dest = 18;
		this.hexa_zindex_src = this.hexa_cont_elt.getStyle('z-index');
		this.hexa_zindex_dest = 100;
		
		
	},
	
	init_constants_type:function() {
	
		this.bg_left_2_dur_slow = 900;
		this.bg_left_2_dur_fast = 700;
		this.title_dur = this.bg_left_1_dur_fast;
		
		this.bg_left_1_src = 0;
		this.bg_left_1_dest = -442;
		this.bg_left_2_src = 0;
		this.bg_left_2_dest = -495;
		this.bg_left_2_dest_2 = -600;
		this.pano_src = this.pano_elt.getStyle('left').replace('px','');
		this.pano_dest = parseInt(this.pano_src) + parseInt(this.bg_left_1_dest);
		this.title_src = 0;
		this.title_dest = -this.title_elt.getSize().x;
		
		this.pano_opened_class = 'show';
		this.pano_closed_class = 'hide';
		this.page_opened_class = 'close';
		this.page_closed_class = 'open';
		
	},
	
	init:function() {		
		if(!is_offres) this.bg_right_elt.setStyle('opacity', 0.9);
		else this.is_offre_open = true;
		//this.bound_offre_click_evt = this.offre_click_evt.bindWithEvent(this);

		$$('#offers .listing a').each(function(el) {
			el.addEvent('click', this.offre_click_evt.bindWithEvent(this, el.id));
		}, this);
	},
	
	init_type:function() {
		this.pano_elt.getFirst('a').addEvent('click', this.pano_click_evt.bindWithEvent(this));
	},
	
	init_tooltip:function() {
		if(Cookie.read("tooltip_seen") == null) {
		
			this.tooltip_elt.setStyles({
				'display': 'block',
				'opacity': 0
			});
			
			this.tooltip_dest = this.tooltip_elt.getStyle('top').replace('px','');
			this.tooltip_src = this.tooltip_dest - 30;
			
			this.tooltip_fx.start({
				'top': [this.tooltip_src, this.tooltip_dest],
				'opacity' : 1
			});
		}
	},
	
	/* METHODES */
	
	open:function() {
		conslog('open');
		this.open_right('slow');
		this.open_left('slow');
		
		this.page_elt.removeClass(this.page_closed_class);
		this.page_elt.addClass(this.page_opened_class);
		
		if(my_diapo != null) {
			this.bound_bg_left_2_fx = this.show_nav.bindWithEvent(this);
			this.bg_left_2_fx.addEvent('complete', this.bound_bg_left_2_fx);
		}
	},
	
	close:function() {
		conslog('close');
		this.set_duration_from_speed('slow');
		this.close_right();
		this.close_left();

		this.page_elt.removeClass(this.page_opened_class);
		this.page_elt.addClass(this.page_closed_class);
		
		if(my_diapo != null) my_diapo.hide_nav();
	},
	
	open_left:function(speed) {
		this.set_duration_from_speed(speed);
		this.bg_left_1_fx.start('left', this.bg_left_1_dest);
		this.bg_left_2_fx.start('left', this.bg_left_2_dest);
		this.pano_fx.start('left', this.pano_dest);
		this.pano_elt.removeClass(this.pano_opened_class);
		this.pano_elt.addClass(this.pano_closed_class);
		this.content_fx.slideOut();
	},
	
	open_right:function(speed) {
		this.set_duration_from_speed(speed);
		this.bg_right_fx.start({'left': this.bg_right_dest_1});
		this.hexa_fx.start('left', this.hexa_dest_1);
		this.is_right_open = true;
	},
	
	close_left:function(speed) {
		this.set_duration_from_speed(speed);
		this.bg_left_1_fx.start('left', this.bg_left_1_src);
		this.bg_left_2_fx.start('left', this.bg_left_2_src);
		this.pano_fx.start('left', this.pano_src);
		this.pano_elt.removeClass(this.pano_closed_class);
		this.pano_elt.addClass(this.pano_opened_class);
		this.content_fx.slideIn();
	},
	
	close_right:function(speed) {
		this.set_duration_from_speed(speed);
		this.bg_right_fx.start({'left': this.bg_right_src});
		this.hexa_fx.start('left', this.hexa_src);
		this.is_right_open = false;
	},
	
	open_offre_flash:function(include) {
		if(this.is_offre_open) {
			$('hexagons').onChangingOffreStart();
			this.bound_bg_right_fx = this.change_offre_complete.bindWithEvent(this, include, true);
			this.bg_right_fx.addEvent('complete', this.bound_bg_right_fx);
			this.bg_right_fx.start({
				'left': this.bg_right_src,
				'opacity': 0.9
			});
			this.hexa_fx.start('left', this.hexa_src);
			this.content_offre_fx.start('right', this.content_offre_src);
		}
		else this.open_offre(include, true);
	},
	
	open_offre:function(include, from_flash) {
		if(!from_flash) {
			var file = include.slice(include.lastIndexOf('/')+1);
			$('hexagons').onOpenOffre(file);
			$('hexagons').onChangingOffreStart();
		}	
		var curt = this;
		var myRequest = new Request({
			method: 'get',
			url: include,
			evalScripts: true,
			onSuccess: function(response) {
				conslog('chargement du contenu reussi pour la page '+include);
				curt.content_offre_elt.set('html', response);
				curt.init_open_offre();
			}
		});
		myRequest.send();
	},
	
	open_offre_home:function() {
		conslog('home');
		this.offre_left_complete_evt();
		if(this.popup) this.popup_fx.start('opacity', 0);
	},
	
	open_offre_type:function() {
		this.bg_left_2_fx.removeEvent('complete', this.bound_bg_left_2_fx);
		this.bound_bg_left_2_fx = this.offre_left_complete_evt.bind(this);
		this.bg_left_2_fx.addEvent('complete', this.bound_bg_left_2_fx);
		this.open_left('fast');
		this.title_fx.start('left', this.title_dest);
		this.page_elt.removeClass(this.page_closed_class);
		this.page_elt.addClass(this.page_opened_class);
	},
	
	init_open_offre:function() {
		this.bg_right_fx.removeEvent('complete', this.bound_bg_right_fx);
		if(this.is_right_open && my_diapo != null) my_diapo.hide_nav();
		if(this.listing_offre_elt != null) this.listing_offre_fx.start('opacity', 0);
		
		if(my_diapo != null) {
			my_diapo.transition_in_fx.removeEvent('complete', my_diapo.bound_transition_in_fx);
			my_diapo.transition_in_fx.start('opacity', 0);
			if(my_diapo.timer != null) $clear(my_diapo.timer);
		}
				
		
		this.is_offre_open = true;
		if(is_home || is_offres) this.open_offre_home();
		else this.open_offre_type();

	},
	
	close_offre_flash:function() {
		if(is_offres) document.location.href=lang+"/index.html";
		this.close_offre(true);
		$('hexagons').onChangingOffreStart();
	},
	
	close_offre:function(from_flash) {
	
		if(!from_flash) $('hexagons').onCloseOffre();
		if(!is_home) {
			this.bg_right_fx.removeEvent('complete', this.bound_bg_right_fx);
			this.bound_bg_right_fx = this.offre_bg_right_fx_complete.bind(this);
			this.bg_right_fx.addEvent('complete', this.bound_bg_right_fx);
		
		}
		
		this.bg_right_fx.start({
			'left': this.bg_right_src,
			'opacity': 0.9
		});
		
		this.hexa_fx.start('left', this.hexa_src);
		this.content_offre_fx.start('right', this.content_offre_src);
		this.is_right_open = false;
		this.is_offre_open = false;
		
		if(this.listing_offre_elt != null) this.listing_offre_fx.start('opacity', 1);
		
		if(my_diapo != null) {
			my_diapo.transition_in_fx.addEvent('complete', my_diapo.bound_transition_in_fx);
			my_diapo.transition_in_fx.start('opacity', 1);
			if(my_diapo.timer != null) $clear(my_diapo.timer);
			my_diapo.launch_timer();
		}
		
		if(is_home) this.close_offre_home();
		else this.close_offre_type();
	},
	
	open_change_offre:function(include) {
		var curt = this;
		var myRequest = new Request({
			method: 'get',
			url: include,
			evalScripts: true,
			onSuccess: function(response) {
				conslog('chargement du contenu reussi pour la page '+include);
				curt.content_offre_elt.set('html', response);
				curt.offre_left_complete_evt();
			}
		});
		myRequest.send();
	},
	
	close_offre_home:function() {
		if(this.popup) this.popup_fx.start('opacity', 1);
	},
	
	close_offre_type:function() {
		this.pano_elt.setStyle('display', 'block');
	},
	
	set_duration_from_speed:function(speed) {
		var speeds = new Array();
		switch(speed) {
			case 'fast': 
				speeds[1] = this.bg_left_1_dur_fast;
				speeds[2] = this.bg_left_2_dur_fast;
				speeds[3] = this.bg_right_dur_fast;
			break;
			case 'slow':
			default:
				speeds[1] = this.bg_left_1_dur_slow;
				speeds[2] = this.bg_left_2_dur_slow;
				speeds[3] = this.bg_right_dur_slow;				
		}
		this.bg_left_1_fx.options.duration = speeds[1];
		this.bg_left_2_fx.options.duration = speeds[2];
		this.bg_right_fx.options.duration = speeds[3];
		this.pano_fx.options.duration = speeds[1];
		this.content_fx.options.duration = speeds[1];
	},
	
	new_content:function(urls, target, rewritten_url) {
		if(this.is_offre_open == true) this.close_offre();
		
		this.open_left('fast');
		this.title_fx.start('left', this.title_dest);
		
		if(my_menu != null) {
			// Changement du select du menu
			$$('.level2').each(function(el) {
				if(el.getFirst('.select')) el.getFirst('.select').removeClass('select');
			});
			target.addClass('select');
			
			// Bloque le menu
			my_menu.submenu.getElements('.level2 a').each(function(el) {
				el.removeEvent('click', my_menu.bound_same_cat_click_evt);
				el.addEvent('click', function(e) {e.stop();});
			});
		}
		
		this.bg_left_2_fx.removeEvent('complete', this.bound_bg_left_2_fx);		
		this.bound_bg_left_2_fx = this.request_new_content.bindWithEvent(this, new Array(urls, rewritten_url));
		this.bg_left_2_fx.addEvent('complete', this.bound_bg_left_2_fx);
	},
	
	request_new_content:function(elt, adresses, rewritten_url) {
		conslog(elt);

		this.bg_left_2_fx.removeEvent('complete', this.bound_bg_left_2_fx);
		var urls = adresses.split('_%_');
		var include = urls[0];
		var title = urls[1];
		var curt = this;
		
		var rewritten_url =  '/' + rewritten_url.replace(ROOT_URL, '');
		var myRequest = new Request({
			method: 'get',
			url: include,
			onSuccess: function(response) {
				curt.change_content(response, title);
				
				conslog('chargement du contenu reussi pour la page '+include+' url public ' + rewritten_url);
				
				if (GOOGLE_ANALYTICS_ACTIVE) {
					pageTracker._trackPageview(rewritten_url);
				}
			}
		});
		myRequest.send();
	},
	
	change_content:function(response, title) {
		this.content_elt.set('html', response);
		
		/* on change de diaporama si on est sur une page chambre */
		if(IS_PAGE_CHAMBRE){
				var room_files = new Array();
				var tab_flashvar_files = new Array();
				
				var room_url = "";
				$$("#bloc_images a").each(function(elt){
						room_files.push($(elt).get('title'));
						tab_flashvar_files.push($(elt).get('title').replace('.jpg', ''));
						room_url = $(elt).get("href");
				});
				
				var url = room_url.substr(0, room_url.length - 1);
				var flashvar_files = tab_flashvar_files.join(',');

				var flash_vars = {
						thumbs:flashvar_files,
						thumb_dir:url + "/thumbs/",
						lang:lang, 
						is_home:is_home, 
						is_offres:is_offres,
						timer_delay:timer_delay,
						url_offres:url_offres, 
						files_offres:files_offres
					};
				var diapo_vars = {
						files_list:room_files.join(','),
						url:room_url.substr(0, room_url.length - 1),
						full_dir:'/full/', 
						thumbs_dir:'/thumbs/'
					};
					
					my_diapo.reInit(diapo_vars, flash_vars);
		}
		
		init_content();
		
		// Changement de 'image du titre
		if(ie6) {
			document.getElementById('img_title').runtimeStyle.behavior="none";
			document.getElementById('img_title').runtimeStyle.src = "none";
			document.getElementById('img_title').runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + title + "', sizingMethod='image')";
			document.getElementById('img_title').runtimeStyle.src = "/images/ie6/1px.gif";
			document.getElementById('img_title').pngSet=true;

		}
		else this.title_elt.getFirst('h2').getFirst('img').set('src', title);
		this.title_fx.start('left', this.title_src);
		this.bound_bg_left_2_fx = this.end_new_content.bindWithEvent(this);
		this.bg_left_2_fx.addEvent('complete', this.bound_bg_left_2_fx);
		this.close_left('fast');
		if(this.is_right_open) {
			this.close_right('fast');
			if(my_diapo != null) my_diapo.hide_nav();
		}
	},
	
	end_new_content:function() {
		this.bg_left_2_fx.removeEvent('complete', this.bound_bg_left_2_fx);
		if(my_menu != null) {
			// Bloque le menu
			my_menu.submenu.getElements('.level2 a').each(function(el) {
				el.addEvent('click', my_menu.bound_same_cat_click_evt);
			});
		}
	},
	
	show_nav:function() {
		my_diapo.show_nav();
		this.bg_left_2_fx.removeEvent('complete', this.bound_bg_left_2_fx);
	},
	
	/* EVENTS */
	
	pano_click_evt:function(e) {
		e.stop();
		this.bg_right_fx.removeEvent('complete', this.bound_bg_right_fx);
		if(this.pano_elt.hasClass(this.pano_opened_class)) {
			if(Cookie.read("tooltip_seen") == null) {
				this.tooltip_fx.start({
					'top': [this.tooltip_dest, this.tooltip_src],
					'opacity' : 0
				});
				var myCookie  = Cookie.write('tooltip_seen', 1, {duration: 15, path:'/'});
			}
			this.is_open = true;
			this.open();
		}
		else {
			this.is_open = false;
			this.close();
		}
		
	},
	
	offre_click_evt:function(e, id) {
		e.stop();
		this.open_offre(id);
	},
	
	offre_left_complete_evt:function() {
		this.bg_right_fx.removeEvent('complete', this.bound_bg_right_fx);
		this.bound_bg_right_fx = function() {
			if(this.pano_elt != null) this.pano_elt.setStyle('display', 'none');
			this.bg_right_fx.removeEvent('complete', this.bound_bg_right_fx);
			$('hexagons').onChangingOffreComplete();
		}.bind(this);
		this.bg_right_fx.addEvent('complete', this.bound_bg_right_fx);
		this.bg_right_fx.start({
			'left': this.bg_right_dest_2,
			'opacity': 1
		});
		this.hexa_fx.start('left', this.hexa_dest_2);
		this.bg_right_elt.setStyle('z-index', this.bg_right_zindex_dest);
		this.hexa_cont_elt.setStyle('z-index', this.hexa_zindex_dest);
		this.content_offre_fx.start('right', this.content_offre_dest);
		this.is_right_open = true;
	},
	
	close_offre_complete_evt:function() {
		this.bg_right_elt.setStyle('z-index', this.bg_right_zindex_src);
		this.hexa_cont_elt.setStyle('z-index', this.hexa_zindex_src);
	},
	
	change_offre_complete:function(elt, include, from_flash) {
		this.bg_right_fx.removeEvent('complete', this.bound_bg_right_fx);
		this.open_change_offre(include);
	},
	
	offre_bg_right_fx_complete:function() {
		$('hexagons').onChangingOffreComplete();
		this.bg_left_2_fx.removeEvent('complete', this.bound_bg_left_2_fx);
		this.close_left('fast');
		this.title_fx.start('left', this.title_src);
		this.page_elt.removeClass(this.page_opened_class);
		this.page_elt.addClass(this.page_closed_class);
		this.bg_right_elt.setStyle('z-index', this.bg_right_zindex_src);
		this.hexa_cont_elt.setStyle('z-index', this.hexa_zindex_src);
	}
});


