var Reservation = new Object;
Reservation.lock = false;
Reservation.id_offers = false;
Reservation.ident = 'room';

//new PeriodicalExecuter(cookiesDump, 3);

function cookiesDump() {
	console.log(document.cookie);
}

Reservation.reset = function() {
	$('res_btn').hide();
	$('res_loading').hide();
}

Reservation.setDateFrom = function(d) {
	$('reservations__date_from').value = d;
}

Reservation.setDateTo = function(d) {
	$('reservations__date_to').value = d;
}

Reservation.setParams = function(p) {	
	if(p.date_from) { $('reservations__date_from').value = p.date_from; }
	if(p.date_to) { $('reservations__date_to').value = p.date_to; }
	if(p.id_offers) { Reservation.loadOffer(p.id_offers); }
	Reservation.updateRes();
}

Reservation.switchOffer = function(id,step2) {
	$$('.offer').each(function(n) { n.removeClassName('act'); });
	$('res_pokoj').removeClassName('act');
	$('res_promocje').removeClassName('act');
	$$('.offer'+id)[0].addClassName('act');
	Reservation.loadOffer(id,step2);
	$('res_content_inner').scrollTop = 0;
}

Reservation.switchStep = function(id) {
	$$('#steps .step').each(function(n) { n.removeClassName('act') });
	$('step'+id).addClassName('act');
}

Reservation.bookRooms = function(re) {	
	$('res_content_inner').makeClipping();
	Reservation.ident = 'room';
	$$('.offer').each(function(n) { n.removeClassName('act'); });
	$('res_pokoj').addClassName('act');
	$('res_promocje').removeClassName('act');
	if (re) {
		Navi.loadContent('room',false,false,false,true);
	} else {
		Navi.loadContent('room',Reservation.pars);
	}
	Reservation.id_offers = false;
	$('res_btn').show();
	$('res_btn_submit').show();
	$('res_btn_submit').addClassName('next');
	$('res_btn_submit').removeClassName('close');
	$('res_btn_submit').show();
	$('res_btn_cancel').hide();
	$('step1').onclick = function() {
		Reservation.bookRooms();
	}
	$('res_btn_submit').onclick = function() {
		Reservation.bookingForm();
	}
	Reservation.switchStep(1);	
}

Reservation.loadOffer = function(id,step2) {
	$('res_content_inner').undoClipping();
	$('res_content_inner').style.overflow = 'auto';
	Reservation.ident = 'offer';
	
	if(!id) { var id = Reservation.id_offers; }
	if(id) { var idtag = '/id/'+id; } else { var idtag = ''; }
	
	if (!step2) {
		var step2 = false;
	}
	
	Navi.loadContent('offer'+idtag,Reservation.pars,false,step2);
	Reservation.id_offers = id;
	Reservation.switchStep(1);
	$('res_btn').show();
	$('res_btn_submit').show();
	$('res_btn_submit').addClassName('next');
	$('res_btn_submit').removeClassName('close');
	$('res_btn_submit').show();
	$('res_btn_cancel').hide();
	
	if ($('offer_date_from')) {
		$('offer_date_from').show();
	}
	
	$('step1').onclick = function() {
		Reservation.loadOffer(Reservation.id_offers);
	}
	$('res_btn_submit').onclick = function() {
		Reservation.bookingForm();
	}
}

Reservation.selectRoom = function(id) {
	$('reservations__id_rooms_types').value = id;
	$('rooms_select').hide();
	Reservation.updateRes();
}

Reservation.updateRes = function() {
	var pars = Form.serialize('res_form');
	Reservation.pars = pars;
	$('res_btn_submit').show();
	Navi.loadContent(Reservation.ident,pars);
}

Reservation.bookingForm = function() {	
	$('res_content_inner').scrollTop = 0;
	if($('res_btn_submit').style.display != 'none') {		
		$('res_content_inner').makeClipping();		
		Navi.loadContent('bookingform');
		$('res_btn_cancel').addClassName('prev');
		$('res_btn_submit').removeClassName('next');
		$('res_btn_submit').removeClassName('close');
		$('res_btn_cancel').show();
		$('res_btn_cancel').onclick = function() {
			if(Reservation.ident == 'room') { Reservation.bookRooms(); }
			if(Reservation.ident == 'offer') { Reservation.loadOffer(false); }
		}
		$('res_btn_submit').onclick = function() {
			Reservation.book();
		}
		Reservation.switchStep(2);
	}	
}

Reservation.book = function() {
	if(validate($('res_form'))) {		
		pars = Form.serialize('res_form');
		var payment = $('payment__method').value;
		if (payment == 3) {
			Payment.validateCard();
		} else {
			Reservation.submitBooking();
		}
	}
	
}

Reservation.submitBooking = function() {
	pars = Form.serialize('res_form');
	Navi.loadContent('book',pars);
	$('res_btn_cancel').hide();
	$('res_btn_submit').onclick = function() { window.parent.HotelSystems.hide(); }
	$('res_btn_submit').addClassName('close');
	Reservation.switchStep(3);
}

Reservation.offersPage = function(page) {
	var nx = 0-((230*(page-1)));
	new Effect.Move('res_offers_pages', { x: nx, y: 0, mode: 'absolute' });
	//$('res_offers_pages').style.left = +'px';
	$$('#res_pages a').each(function(n) { n.removeClassName('act'); });
	$('res_page'+page).addClassName('act');
}

Reservation.discounts = function() {
	$$('.offer').each(function(n) { n.removeClassName('act'); });
	$('res_pokoj').removeClassName('act');
	$('res_promocje').addClassName('act');
	
	$('res_btn_cancel').hide();
	$('res_btn_submit').hide();
	Navi.loadContent('discounts');
}

