var Box = new Class({

initialize: function(el){
	lThis = this;
	this.button = el;
	this.parent = this.button.getParent();
	this.act = '';
	this.diapo = null;
	if(this.parent.hasClass('e_activity')){
		this.type = 'activity';
		if(this.button.hasClass('e_more'))
			this.act = '&description';
		else{
			if(el.get('tag') == 'strong'){
				var act = el.getProperty('class');
				act = act.replace(/a_/,'');
				this.act = '&theme=' + act;
			}else{
				this.act = '&choosen';
			}
		}
	}
	else if(this.button.hasClass('desc-ile'))
		this.type = 'island';
	else if(this.button.hasClass('e_more'))
		this.type = 'description';
	else if(this.button.hasClass('q_preview'))
		this.type = 'preview';
	else if(this.button.hasClass('e_record'))
		this.type = 'save';
	else if(this.button.hasClass('e_send'))
		this.type = 'send';
	else if(this.button.hasClass('e_mod'))
		this.type='modify';
	else if(this.button.hasClass('e_del'))
		this.type = 'delete';
	else if(this.button.getProperty('title') == "delete")
		this.type = 'qdelete';
	else this.type = 'rooms';
	this.create();
	
	$$( this.overlay, this.inClose, this.cancelP, this.promptClose ).addEvent('click', lThis.close.bind(lThis));
	this.overlayScroll = this.posOverlay.bind(this);
	this.contentResize = this.posContent.bind(this);
	if( Browser.Engine.version < 5 && Browser.Engine.trident ){
		$$(this.overlay, this.iframe).setStyle('position','absolute');
		$$(this.overlay,this.iframe).setStyle('top', window.getScrollTop()+'px');
		window.addEvent('scroll', lThis.overlayScroll);
	}else
		$$(this.overlay, this.iframe).setStyle('position','fixed');
	
	window.addEvent('resize', lThis.contentResize);
	this.boxFx = new Fx.Tween(this.overlay).start('opacity', 0.5);
	this.ajaxBox = new Request.HTML({
		'url'		: 'ajax/' + lThis.type + '.php',
		'method'	: 'get',
		'update'	: lThis.inContent,
		'onComplete': function(){
			if($chk($('a_cancel')))$('a_cancel').addEvent('click', function(e){e.stop();lThis.close();});
			lThis.content.setStyle('display', 'block');
			lThis.posContent();
			lThis.loading.setStyle('display', 'none');
			if( lThis.type == 'description' || lThis.type  ==  'activity' || lThis.type  ==  'island'){
				new Carrousel( $('dFrame'), $('inFrame'), $$('.d_show'), $$('.sliderIn'));
				if($chk($('test')))
				lThis.diapo = new Diaporama( $('test'), $$('.d-ico'), {stop:$('stop'), play:$('play')} )
			}
			if (GBrowserIsCompatible()&&$chk($('latitude'))){
				   	var Lat = $('latitude').getProperty('value');
				   	var Lng = $('longitude').getProperty('value');
				   	var Zoom = 10;
				   	var TextAffiche =  $$('#inOverlay h1')[0].get('html');
					var map = new GMap2(document.getElementById("map"), {mapTypes: [G_SATELLITE_MAP]});
					map.setCenter(new GLatLng(Lat,Lng ),Zoom );
					map.addControl(new GSmallMapControl());
   					map.addControl(new GMapTypeControl());
   					var point = new GLatLng(Lat,Lng);
					function createMarker(point,html) {
						var marker = new GMarker(point);
							GEvent.addListener(marker, "click", function() {
							marker.openInfoWindowHtml(html);
								});
							return marker;
										} 
   					var marker = createMarker(point,TextAffiche);
   					map.addOverlay(marker);
   				}
			
			else if(lThis.type == 'preview' && $chk($('e_send')) ){
				var ref = ($chk($('e_ref')))?$('e_ref').get('text'):null;
				$('e_send').addEvent('click',function(e){
					e.stop();
					lThis.type = 'send';
					lThis.content.destroy();
					lThis.prompt(ref);
				});
			}
			else if( lThis.type == 'activity' || lThis.type == 'preview' || lThis.type == 'save' || lThis.type == 'send'|| lThis.type=='modify' ){}
			else{lThis.roomList();}
		}
	});
	if (this.type == 'island') {
	var id = this.parent.get('id'); 
	
	this.ajaxBox.send('island='+id);}
	(this.type == 'delete' || this.type == 'save' || this.type == 'send' || this.type == 'qdelete'||this.type=='modify')?this.prompt(null):this.fillIn();
},
create: function(){
	lThis = this;
	this.overlay 	= new Element('div',{'id':'e_overlay'}).inject($(document.body));
	this.iframe 	= new IFrame('e_frame').inject(this.overlay, 'after');
	this.content 	= new Element('div', {'id':'inOverlay'}).inject(this.iframe, 'after');
	this.inClose 	= new Element('div', {'id':'close_box','html':close_message}).inject(this.content);
	this.inContent 	= new Element('div').inject(this.inClose, 'after');
	this.loading 	= new Element('img',{'src':'images/big_loading.gif', 'alt': 'Loading'}).inject(this.content, 'after');
	this.callAbs	= new Element('div').addClass('e_prompt').inject(this.iframe, 'after');
	this.call	 	= new Element('div', {'id':'prompt'}).inject(this.callAbs);
	this.pHead		= new Element('div', {'id':'prompt_head', 'html':'<span>Delete confirm</span>'}).inject(this.call);
	this.promptClose= new Element('a', {'id':'prompt_close'}).inject(this.pHead, 'bottom');
	this.bodyP		= new Element('div', {'id':'prompt_body'}).inject(this.call, 'bottom');
	this.buttonP	= new Element('div', {'id':'prompt_button'}).inject(this.call, 'bottom');
	this.confirmP	= new Element('a', {'id':'prompt_confirm', 'href':'#','html':'Confirm'}).inject(this.buttonP, 'bottom');
	this.cancelP	= new Element('a', {'id':'prompt_cancel', 'html':'Cancel'}).inject(this.buttonP, 'bottom');

	var size = $(document.body).getSize();
	var srollH = window.getScrollTop();
	var posE = this.position = $chk($('engine'))?$('engine').getPosition():$chk($('account'))?$('account').getPosition():$chk($('p_package'))?$('p_package').getPosition():$chk($('p_preview'))?$('p_preview').getPosition():$('c_result').getPosition();

	$$(this.overlay,this.iframe).setStyles({
		'height': (size.y + 20 ) + 'px',
		'z-index':100,
		'left':'0',
		'top':'0',
		'display':'block',
		'border':0,
		'width':'100%',
		'background-color':'#000000'
	}).set('opacity',0.01);

	this.iframe.setStyles({'z-index':99});

	this.content.setStyles({
		'position':'absolute',
		'display':'none',
		'top': (srollH + 50)+'px',
		'z-index':102,
		'width':'669px',
		'padding':'2px',
		'background-color':'#ffffff'
	});
	var topP = (this.type == 'delete')?50:-10;
	this.callAbs.setStyles({
		'position':'absolute',
		'display':'none',
		'top': (srollH + posE.y + topP) + 'px',
		'z-index':102,
		'width':'100%',
		'height':'1px',
		'overflow':'visible',
		'background':'none'
	});
	this.call.setStyles({
		'margin':'0 auto'
	});

	this.loading.setStyles({
		'position':'absolute',
		'top': (srollH + 100)+'px',
		'left':'45%',
		'z-index':101
	});
},
fillIn: function(){
	lThis = this;
	var title = this.parent.get('title');
	var stay;
	if( title == null || title == '' )stay = '';
	else{
		stay = '&stay=' + title.replace(/room_/, '');
	}
	if(lThis.type == 'preview'){
		var type = lThis.type;
		var id = lThis.button.get('href').split('#');
		id = id[1];
	}else{
		var id = this.parent.get('id');
		if( id == null || id == '' )id = this.parent.get('dir');;
		if( lThis.type == 'activity' )var type = 'service';
		else if(lThis.type == 'island') var c = 'island';
		else if( lThis.type == 'rooms' && this.parent.hasClass('cr_rate') ){
			id = lThis.button.get('href').split('#');
			id = id[1];
			type = 'offer='+lThis.button.get('title')+'&room';
		}else type = 'hotel';
		//var type = ( lThis.type == 'activity'  )?'service':'hotel';
	}
	var data = (lThis.type == 'save' || lThis.type == 'send' )?'':type + '=' + id + stay + lThis.act;
	//if(lThis.type != 'preview'){
	this.ajaxBox.send( data ); //return true; }
	/*lThis.loading.setStyle('display', 'none');
	lThis.content.setStyle('display', 'block');
	lThis.posContent();
	new IFrame({'src':'ajax/preview.php?'+data,
		load: function(){
            alert('The iframe has finished loading.');
        }
	}).inject(this.inContent);*/
},
prompt: function(ref){
	lThis = this;
	lThis.loading.setStyle('display', 'none');
	var topPrompt = window.getScrollTop() + this.position.y;
	(function(){lThis.callAbs.setStyles({'display': 'block','top':topPrompt});}).delay(700);

	if(this.type == 'delete'){
		this.overlay.setStyle('background-color','#ffffff');
		var rel = lThis.button.get('rel');
		var title = $('room_'+rel).getElement('.s_hotel');
		this.bodyP.set('html','<br/>Do you really want to supress this stay?<br/><strong>'+ title.get('html')+'</strong>');
		this.confirmP.setProperty('href',lThis.button.getProperty('href'));
	}else if(this.type=='modify'){	
		this.overlay.setStyle('background-color','#ffffff');
		var rel = lThis.button.get('rel');
		this.pHead.set('html','<span>Modify confirm</span>');
		this.bodyP.set('html','<br/>This package contains special offers that may be lost during editing<br/>Do you want to continue?');		
		this.confirmP.setProperty('href',lThis.button.getProperty('href'));		
	}else if(this.type == 'qdelete'){
		this.overlay.setStyle('background-color','#ffffff');
		this.bodyP.set('html','<br/>Do you really want to supress this stay?<br/>');
		this.confirmP.setProperty('href',lThis.button.getProperty('href'));
	}else{
		this.overlay.tween('background-color','#ffffff');
		var update = '&nbsp;';
		var refer  = '';
		var remind = '';

		if(this.type == 'send'){
			remind = '<p class="s_remind">Reminder: sended quotation will be amended only by our consultants</p>';
			this.callAbs.addClass('e_send');
		}
		var updt = lThis.button.get('href');
		updt = updt.split('#');
		if( updt[1] > 0 && ($chk($('e_ref')) || ref != null) ){
			update = '<label for="update">&nbsp;</label>'
				   + '<input type="checkbox" id="update" name="update" checked="checked"/> &nbsp; &nbsp; Uncheck to save as';
			refer  = ($chk($('e_ref')))?$('e_ref').get('text'):ref;
		}

		var id = lThis.button.get('href').split('#');
		id = id[1];
		var hidden_quote = $chk($('engine'))?'':'<input type="hidden" name="quote" value="'+id+'"/>';
		this.formP = new Element('form',{'id':'formP','name':'formP', 'action':'ajax/'+this.type+'.php','method':'post',
			'html': '<strong>Customer details</strong>'+
				'<p><label for="ref">Client reference</label><input type="text" id="ref" name="ref" value="'+refer+'"/></p>'+
				'<p>'+update+'</p>'+
				'<strong>Various</strong>'+
				'<p><label for="comment">Comments</label><textarea id="comment" name="comment"></textarea></p>'+remind+
				hidden_quote

		}).inject(this.bodyP);
		this.callAbs.addClass('e_save');
		var t = (this.type == 'save')?'Save':'Send';
		this.pHead.set('html','<span>'+t+'</span>');
		this.promptClose.inject(this.pHead,'bottom');
		this.confirmP.set('html',t).addEvent('click', function(e){
			e.stop();
			lThis.formP.set('send',{
				'onRequest':function(){
					lThis.callAbs.setStyle('display', 'none');
					lThis.loading.setStyle('display', 'block');
				},
				'onComplete':function(reponse){
					lThis.close();
					if( !$chk($('e_ref')) ){
						new Element('h2',{'id':'e_ref','html':reponse}).inject($('engine'),'top');
					}
					else $('e_ref').set('html',reponse);
					if( updt[1] == 0 && $chk($('engine')) && lThis.type == 'save' ){
						$$('.e_send').set('href',updt[0]+'#1');
						lThis.button.set('href',updt[0]+'#1');
					}else if(lThis.type == 'send' && $chk($('engine'))){
						lThis.button.set('href',updt[0]+'#0');
						$$('.e_record').set('href',updt[0]+'#0');
					}
				}
			});
			lThis.formP.send();
		});
		this.formP.addEvent('submit', function(e){
			e.stop();
			lThis.formP.set('send',{
				'onRequest':function(){
					lThis.callAbs.setStyle('display', 'none');
					lThis.loading.setStyle('display', 'block');
				},
				'onComplete':function(reponse){
					lThis.close();
					if( !$chk($('e_ref')) ){
						new Element('h2',{'id':'e_ref','html':reponse}).inject($('engine'),'top');
					}
					else $('e_ref').set('html',reponse);
					if( updt[1] == 0 && $chk($('engine')) && lThis.type == 'save' ){
						$$('.e_send').set('href',updt[0]+'#1');
						lThis.button.set('href',updt[0]+'#1');
					}else if(lThis.type == 'send' && $chk($('engine'))){
						lThis.button.set('href',updt[0]+'#0');
						$$('.e_record').set('href',updt[0]+'#0');
					}
				}
			});
			lThis.formP.send();
		});

	}


},
posOverlay: function(){
	this.overlay.setStyle('top', window.getScrollTop()+'px');
},
posContent: function(){
	var size = this.content.getSize();
	var leftPos = Math.floor((window.getWidth() - size.x) /2);
	this.content.setStyles({'left': leftPos+'px'});
	this.overlay.setStyle('height', window.getHeight());
},
close: function(){
	lThis = this;
	if( Browser.Engine.version < 5 && Browser.Engine.trident )
		window.removeEvent('scroll', lThis.overlayScroll);
	window.removeEvent('resize', lThis.contentResize);
	$$( this.iframe, this.content, this.loading, this.callAbs ).destroy();
	this.boxFx.removeEvent('complete');
	this.boxFx.start('opacity', 0).addEvent('complete', function(){lThis.overlay.destroy();});
},
	roomList: function(){
	lThis = this;
		//if( $('d-hotel') ){
		if($('moteurRecherche'))$('moteurRecherche').addEvent('submit', function(event){
			if( $('room').get('value') == '-1' || $('room').get('value') == -1 ){
				event.stop();
				label = $('room').getPrevious('label');
				label.setStyles({'color':'#c0000d','background-color':'#ffff00','font-weight':'bold'});
			}
		});
		
		var block_h = $('o-hotel');
		var offer = new Fx.Tween( $('b-hotel'),{duration: 500} );
		var h = $$('#chTrans, #d-hotel p.e_button');
		var tmp = $$('a.offre00');
		tmp.each(function(el){
			el.addEvent('click', function(event) {
				event.stop();
				if( $('e-loading'))div.destroy();
				div = new Element('div');
				var req = new Request.HTML({
					method: 'get',
				    url: 'ajax/offer.php?ref=' + el.get('rel'),
				    onRequest: function() {
					    $('b-hotel').set('opacity', 0 );
						var w_load = block_h.getSize();
						div.setStyles({'position':'absolute', 'top': '15px', 'left':( ( w_load.x / 2 ) - 64 ) + 'px'})
							.setProperty('id', 'e-loading').injectInside( $('d-hotel'));
						//im = new Element('img').setProperty('src', 'images/loading.gif').setProperty('alt', 'loading').injectInside(div);
						var im = lThis.loading.clone();
						im.setStyles({'display': 'block', 'top': 0}).inject(div);
				    },
				    onComplete: function() {
				    	div.destroy();
				    	//honeymoon();
						if(el.getParent('ul.unoffer'))h.setStyle('display','none');
   						else h.setStyle('display','block');
						
				    	tmp.each(function(ele){ele.set('class', 'inactive');});
						el.set('class', 'active');
				        offer.start( 'opacity', 0.0, 1.0 );
				    },
				    update: $('o-hotel')
				}).send();
			});
		});
	//}
	}

});


