var img_loading = new Element('img').setProperty('src', 'images/big_loading.gif').setProperty('alt', 'loading');
var bar_load = new Element('img').setProperty('src', 'images/bar_load.gif').setProperty('alt', 'loading');
window.addEvent('domready', function(){
	if( $('d-hotel') ){
		if($('moteurRecherche'))$('moteurRecherche').addEvent('submit', function(event){
			if( $('chambre').get('value') == '-1' || $('chambre').get('value') == -1 ){
				event.stop();
				label = $('chambre').getPrevious('label');
				label.setStyles({'color':'#c0000d','background-color':'#ffff00','font-weight':'bold'});
			}
		});
		var block_h = $('o-hotel');
		w_load = block_h.getSize();
		var offer = new Fx.Tween( $('b-hotel'),{duration: 500} );
		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: 'TCVMOD/ajax/offer.php?ref=' + el.get('rel'),
				    onRequest: function() {
					    $('b-hotel').set('opacity', 0 );
						div.setStyles({'position':'absolute', 'top':( ( w_load.y / 2 ) + 16 ) + 'px', 'left':( ( w_load.x / 2 ) - 16 ) + 'px'})
							.setProperty('id', 'e-loading').injectInside( $('d-hotel'));
						im = new Element('img').setProperty('src', 'images/loading.gif').setProperty('alt', 'loading').injectInside(div);
				    },
				    onComplete: function() {
				    	div.destroy();
				    	honeymoon();
				    	tmp.each(function(ele){ele.set('class', 'inactive');});
						el.set('class', 'active');
				        offer.start( 'opacity', 0.0, 1.0 );
				    },
				    update: $('o-hotel')
				}).send();
			});
		});
	}
   	$$('input[name=dateArrivee]').each(function(el){
   		el.addEvent('click', function(e){
   			new Calendar(el);
   		});
   	});
   	honeymoon();
   	if( $('p_list') ){
   		$('p_list').setStyle('position','relative');
   		form = $('p_package');
   		form.addEvent('submit', function(event){
			event.stop();
			var myHTMLRequest = new Request.HTML({
				url:'TCVMOD/ajax/list.php?list',
				update: $('p_list'),
				onComplete: function(){$('contenu').set('opacity',1);},
				onRequest: function(){
					$('contenu').set('opacity',0.3);
					img_loading.setStyles({'left':'45%','position':'absolute','top':'70px'}).inject($('p_list'));
				}
			}).post($('p_package'));
   		});
   	}
   	$('mi_opacity').set('opacity','0.8');
   	if( $chk($('item_1')))var mySort = new Sortables($('summary'),{
   		'clone':true,
   		'opacity':0.6,
   		'handle': '.s_move',
   		onComplete : function(){
   			$('arrange').set('html','');
   			var data = 'list';
   			var tab = this.serialize();
   			tab.each(function(el){
   				var num = el.replace(/item_/, '');
   				data += '&order[]=' + num;
   			});
   			$$('span.b-order a').each(function(el){
   				el.set('href','TCVMOD/ajax/order.php?'+data);
   			});
   			$$('span.b-order').set('opacity',1);
   		}
   	});
   	$$('span.b-order').set('opacity',0);
   	$$('span.b-order a').each(function(el){
   		el.addEvent('click', function(e){
	   		e.stop();
	   		$$('span.b-order').set('opacity',0);
	   		var myReq = new Request.HTML({'url':el.get('href'), 'update': $('arrange'),
		   		onRequest : function(){
		   			bar_load.setStyles({'position': 'absolute', 'left': '40%'}).inject($('arrange'));
		   		},
	   			onComplete: function(){
	   				$$('a.mod').each(function(el, ind){
	   					href_mod = el.get('href');
	   					href_mod = href_mod.replace(/\d$/, ind);
	   					el.set('href',href_mod);
	   				});
	   				$$('.s-item span a[title=delete],.titre_sejour a[title=delete]').each(function(el, ind){
	   					href_mod = el.get('href');
	   					href_mod = href_mod.replace(/\d$/, ind);
	   					el.set('href',href_mod);
	   				});
	   			}
	   		}).send();
	   		//$('contenu').appendText( el.get('href') );
	   	});
   	});
});
function honeymoon(){
	$$('input[name=dateHoneymoon]').each( function(el){
		el.addEvent('click', function(e){
			var arrivee = $(document.body)
						  .getElement('input[name=dateArrivee]')
						  .get('value');
			if ( !validate_date(arrivee) ) {
	        	date_arrivee = new Date();
	      	} else {
	      		var dateinp = arrivee.split('/');
	    		date_arrivee = new Date(dateinp[2],(dateinp[1]-1),dateinp[0],0,0,0);
			}
			date_min = new Date(( date_arrivee.getFullYear() - 1),date_arrivee.getMonth(),date_arrivee.getDate(),0,0,0);
    		new Calendar(el,{maxDate: date_arrivee, minDate: date_min});
    	});
	});
}
function validate_date (date) {
  	var regex = /^(\d{1,2})\/(\d{1,2})\/(\d{4})$/;
  	return date.test(regex);
}

function next_page(param){
	tmp = new Request.HTML( {
		method: 'get',
		url:'TCVMOD/ajax/list.php',
		update: $('p_list'),
		onComplete: function(){$('p_list').set('opacity',1);},
		onRequest: function(){
			$('p_list').set('opacity',0.3);
			img_loading.setStyles({'left':'40%','position':'absolute','top':'70px'}).inject($('p_list'));
		}
	} ).send('list&page=' + param);
}