function selectTab(idTab, parameters) {
	var tabs = $('hotel_tabs').getElementsByTagName('li');

	var selected, found, tabToSelect, tabToUnSelect;
	var i, j;

	for (i = 0; i < tabs.length; i++) {
		classNames = tabs[i].className.split(' ');
		found = false;
		selected = false;
		for (j = 0; j < classNames.length; j++) {
			if (classNames[j] == idTab) {
				found = true;
			} else if (classNames[j] == 'selected') {
				selected = true;
			}
		}
		if (found && selected && !parameters) {
			//return true;
		} else if (found && (!selected)) {
			tabToSelect = tabs[i];
		} else if ((!found) && selected) {
			tabToUnSelect = tabs[i];
		}
	}
	var matches = document.URL.match(/\?(.*)$/);
	if (!matches) {
		return false;
	}
	var query = matches[1];

	if (parameters) {
		var scriptName = 'modules/reservations_hotel-' + idTab + '.php?' + parameters;
	} else {
		var scriptName = 'modules/reservations_hotel-' + idTab + '.php';
	}
	var request = new Ajax.Request(
			scriptName,
			{
				method: 'get',
				parameters: query,
				onComplete: function(originalRequest) {
					$('tab_contents').innerHTML = originalRequest.responseText;
					tabToUnSelect.className = tabToUnSelect.className.replace('selected', 'unselected');
					tabToSelect.className = tabToSelect.className.replace('unselected', 'selected');
					initLightbox();
				}
			}
	);
}

function calculateReservationPrice() {
	var roomTypes = $('room_types').getElementsByTagName('tbody')[0].getElementsByTagName('tr');
	roomTypes = $A(roomTypes);

	var roomTypeIds = new Array();
	var roomTypePrices = new Array();
	var roomTypeCount = new Array();
	roomTypes.each(
			function(row) {
				var matches = new Array();
				if (row.id && (matches = row.id.match(/^room\_type\_(\d+)$/))) {
					var idRoomType = parseInt(matches[1]);
					roomTypeIds.push(idRoomType);
					roomTypePrices[idRoomType] = parseFloat($('room_price_' + idRoomType).innerHTML.replace('.', '').replace(',', '.'));
					roomTypeCount[idRoomType] = parseInt($('room_count_' + idRoomType).options[$('room_count_' + idRoomType).selectedIndex].value);
				}
			}
	);

	var idRoomType, priceTotal = 0.0;
	for (var i = 0; i < roomTypeIds.length; i++) {
		idRoomType = roomTypeIds[i];
		priceTotal += (roomTypeCount[idRoomType] * roomTypePrices[idRoomType]);
	}

	priceTotal = Math.round(priceTotal * 100);
	priceTotal += '';
	if (priceTotal == '0') {
		priceTotal = '000';
	}
	var rest = ',' + priceTotal.substr(priceTotal.length - 2);
	priceTotal = priceTotal.substr(0, priceTotal.length - 2);
	while (priceTotal.length > 3) {
		rest = '.' + priceTotal.substr(priceTotal.length - 3) + rest;
		priceTotal = priceTotal.substr(0, priceTotal.length - 3);
	}
	priceTotal += rest;

	$('reservation_price').value = priceTotal;
}

function submitReservationRequest(frmReservation) {
	var reservationDetails = new Array();
	var go = false, roomCountTest = /^room\_count\_\d+$/;
	for (var i = 0; i < frmReservation.length - 1; i++) {
		if ((frmReservation[i].name == '')
				|| (frmReservation[i].value == '')
				|| (frmReservation[i].value == '0')) {
			continue;
		}
		if (roomCountTest.test(frmReservation[i].name)) {
			go = true;
		}
		reservationDetails[frmReservation[i].name] = frmReservation[i].value;
	}
	if (go) {
		reservationDetails = $H(reservationDetails);
		var queryString = reservationDetails.toQueryString();
		document.location.href = frmReservation.action + '?' + queryString;
	} else {
		window.alert('Por favor, seleccione algún tipo de habitación para realizar la reserva.');
	}
}

function viewRoomTypeDetails(idRoomType) {
	var target = $('room_type_' + idRoomType + '_details');
	if (!(target && $('room_types'))) return;
	$A($('room_types').getElementsByTagName('tr')).each(
			function(node) {
				node = $(node);
				if (!node.hasClassName('room_type_details')) return;
				if (node != target && node.visible()) {
					Effect.Fade(node, { duration: 0.5, queue: 'end' });
				}
			}
	);

	if (target.visible()) {
		Effect.Fade(target, { duration: 0.5, queue: 'end' });
	} else {
		Effect.Appear(target, { duration: 0.5, queue: 'end' });
	}

}

/*
author: Vlad Roman (vlad@afian.com)
*/
var MyJSSscrollerHASDragging = false;

var MyJSScrollerHandler = {
	scrollers : [],
	intvals : [],
	
	initByClassName : function(className) {
		i = this.scrollers.length;
		document.getElementsByClassName(className).each(function(node) {
			var orient, dir, speed;
			MyJSScrollerHandler.scrollers[i] = new MyJSScroller(node, i, orient, dir, speed);
			i++;
		});
	},
	
	attachMouseWheel : function(el, index) {
		if (window.addEventListener)
		        /** DOMMouseScroll is for mozilla. */
		       el.addEventListener('DOMMouseScroll', function(event){MyJSScrollerHandler.wheel(event, index);}, false);
		/** IE/Opera. */
			el.onmousewheel = function(event){MyJSScrollerHandler.wheel(event, index);};
	},
		
	handleWheelMovement : function(delta, index) {
		MyJSScrollerHandler.scrollers[index].move(-(delta*10));
	},
	
	wheel : function(event, index){
	        var delta = 0;
	        if (!event) /* For IE. */
	                event = window.event;
	        if (event.wheelDelta) { /* IE/Opera. */
	                delta = event.wheelDelta/120;
	                if (window.opera)
	                        delta = -delta;
	        } else if (event.detail) { /** Mozilla case. */
	                delta = -event.detail/3;
	        }
	        if (delta)
	                MyJSScrollerHandler.handleWheelMovement(delta, index);
	        if (event.preventDefault)
	                event.preventDefault();
		event.returnValue = false;
	}
};


function MyJSScroller(node, index,  orient, dir, speed) {
	
	if (MyJSScrollerHandler.scrollers[index]) {
		return false;
	}
	

	if (node.getAttribute('myjsscroller') == 'true') {
		return false;
	}
	
	node.setAttribute('myjsscroller', 'true');
	
	this.pausePoints = [];
	this.pauseTimer = 0;
	this.node = node;
	this.index = index;
	this.orient = orient || node.getAttribute('orientation');
	this.dir = dir || node.getAttribute('direction');
	spd = speed || node.getAttribute('scrollspeed');
	if (!spd) {
		spd = 75;
	}
	
	this.speed = 1000/spd;
	wraper = document.createElement('DIV');
	//wraper.style.border = '1px solid red';
	wraper.style.height = '100%';
	node.style.overflow = 'hidden';
	wraper.style.overflow = 'hidden';
	
	wraper.style.position = 'relative';
	wraper.style.top = '0px';
	wraper.style.left = '0px';
	this.wraper = wraper;
	movingWraper = document.createElement('DIV');
	if (this.orient == 'horizontal') {
		movingWraper.style.whiteSpace = 'nowrap';
	}
	
	//movingWraper.style.border = '1px solid blue';
	movingWraper.style.position='absolute';
	this.movingWraper = movingWraper;
	wraper.appendChild(movingWraper);
	movingWraper.innerHTML = node.innerHTML;
	node.innerHTML = '';
	node.appendChild(wraper);
	this.w = movingWraper.offsetWidth;
	this.h = movingWraper.offsetHeight;
	
	if (this.orient != 'horizontal' && this.dir == 'down') {
		this.movingWraper.style.top = -this.h+'px';
	}
	
	
	if (MyJSSscrollerHASDragging) {
		if (this.orient == 'horizontal') {
	 		drg = Drag.init(movingWraper, null, -this.w, this.w, 0, 0);
		} else {
	 		drg = Drag.init(movingWraper, null, 0, 0, -this.h, this.h);
		}
	}
	
	
	
	
	for (var i = 0 ; i < movingWraper.childNodes.length ; i++) {
		ch = movingWraper.childNodes[i];
		if (ch.className == 'scrollpauser') {
			
			
			/* if (ch.getAttribute('scrollhide') == "1") { */
				ch.style.visibility = 'hidden';
				ch.style.display ='inline';
				ch.style.margin = '0px';
				ch.style.border = '0px';
			/* } */
			if (ch.getAttribute('scrolldelay') > 0) {
				delay = ch.getAttribute('scrolldelay');
			} else {
				delay = 1500;			
			}
			this.pausePoints.push([ch.offsetTop, ch.offsetLeft, delay]);
		}
	}
	
	
	
	
	movingWraper.onDragStart = function(x,y) {
		MyJSScrollerHandler.scrollers[index].dragged = true;
		MyJSScrollerHandler.scrollers[index].pause();
	}
	movingWraper.onDragEnd = function() {
		MyJSScrollerHandler.scrollers[index].dragged = false;
		MyJSScrollerHandler.scrollers[index].resume();
	}
	
	
	node.onmouseover = function() {
		MyJSScrollerHandler.scrollers[index].pause();
	}
	node.onmouseout = function() {
		if (!MyJSScrollerHandler.scrollers[index].dragged) {
			MyJSScrollerHandler.scrollers[index].resume();
		}
	}
	MyJSScrollerHandler.attachMouseWheel(node, index);
	
	this.start();
	
}

MyJSScroller.prototype.start = function() {
	
	if (!MyJSScrollerHandler.intvals[this.index]) {
		MyJSScrollerHandler.intvals[this.index] = window.setInterval('MyJSScrollerHandler.scrollers[\''+this.index+'\'].move()', this.speed);
	}
}

MyJSScroller.prototype.move = function(delta) {
	
	mw = this.movingWraper;
	slf = this;
	
	if (!delta) {
			delta = 1
		}
		
	if (this.orient == 'horizontal') {
		if (this.dir != "right") {
			if (mw.offsetLeft < -this.w) {
				mw.style.left = this.wraper.offsetWidth+'px';
			} else {
			 	mw.style.left = (mw.offsetLeft-delta)+'px';
			}
		} else {
			if (mw.offsetLeft > this.wraper.offsetWidth) {
				mw.style.left = -mw.offsetWidth+'px';
			} else {
			 	mw.style.left = (mw.offsetLeft+delta)+'px';
			}
		}
	} else {
		if (this.dir != "down") {
			if (mw.offsetTop > -this.h) {
			
				//pausers
				this.pausePoints.each(function(point){
					if (-point[0] == mw.offsetTop) {
						slf.pause();
						window.setTimeout('MyJSScrollerHandler.scrollers[\''+slf.index+'\'].start()', point[2]);
					}
				});
				
				mw.style.top = (mw.offsetTop-delta)+'px';
				
			} else {
			 	mw.style.top = this.wraper.offsetHeight+'px';
			}
		} else {
			if (-mw.offsetTop+this.wraper.offsetHeight > 0) {
				mw.style.top = (mw.offsetTop+delta)+'px';
			} else {
			 	mw.style.top = -mw.offsetHeight+'px';
			}
		}
	}
}

MyJSScroller.prototype.pause = function() {
	window.clearInterval(MyJSScrollerHandler.intvals[this.index]);
	MyJSScrollerHandler.intvals[this.index] = false;
}

MyJSScroller.prototype.resume = function() {
	this.start();
}

//window.onload = function() {MyJSScrollerHandler.initByClassName('scrollable');}
