
function TabManager(tabs, tabContents, className, firstClassName, initialIndex, callback)
{
	this.Tabs = $(tabs);
	this.TabContents = tabContents.collect(function(e) {return $(e)});
	this.ClassName = className;
	this.FirstClassName = firstClassName;
	this.SelectedIndex = null; 
	this.TabsChildren = this.Tabs.childElements();
	this.TabContentsChildren = this.TabContents.invoke('childElements');
	this.Callback = callback;
		
	this.Select = function(index)
	{
		if(this.SelectedIndex != null)
		{
			this.TabsChildren[this.SelectedIndex].removeClassName(this.ClassName);
			this.TabContentsChildren.each(function(e){ e[this.SelectedIndex].removeClassName(this.ClassName) }, this);
		}
	
		if(this.SelectedIndex == 0)
			this.TabsChildren[0].removeClassName(this.FirstClassName);
	
		this.TabsChildren[index].addClassName(this.ClassName);
		this.TabContentsChildren.each(function(e){ e[index].addClassName(this.ClassName) }, this);

		if(index == 0)
			this.TabsChildren[0].addClassName(this.FirstClassName);
	
		this.SelectedIndex = index; 
		
		if(this.Callback == null)
			return;
		
		if(Object.isFunction(this.Callback))
			this.Callback();
		else if(Object.isArray(this.Callback) && Object.isFunction(this.Callback[index]))
			this.Callback[index]();		
	}
	
	this.BubbleSelect = function(event)
	{
		this.Select(IndexOf(event.findElement('a')));
	}
	
  Event.observe(tabs, 'click', this.BubbleSelect.bindAsEventListener(this));
  this.Select(initialIndex);
}

function IndexOf(element)
{
	var sieblings = element.up().childElements();
	
	for(i=0; i<sieblings.length; i++)
		if(element == sieblings[i])
			return i;
}

function TypeOf(value) {
    var s = typeof value;
    if (s === 'object') {
        if (value) {
            if (value instanceof Array) {
                s = 'array';
            }
        } else {
            s = 'null';
        }
    }
    return s;
}

function Gallery(imageElement, imageSrcs)
{
	this.ImageElement = $(imageElement);
	this.ImageSrcs = imageSrcs;
	this.Count = imageSrcs.length;
	this.SelectedIndex = null; 
	this.Preloaded = null;
		
	this.Select = function(index)
	{
		if(index < 0 || index >= this.Count)
			return;

		this.ImageElement.src = this.ImageSrcs[index];
		
		this.SelectedIndex = index; 
	}
	
	this.Prev = function()
	{
		this.Select(this.SelectedIndex - 1);
	}
	
	this.Next = function()
	{
		this.Select(this.SelectedIndex + 1);
	}
	
	this.Preload = function()
	{
		this.Preloaded = this.ImageSrcs.collect(function(src) { img = new Image(); img.src = src; return img;});
	}
	
	this.Select(0);
	
	Event.observe(window, 'load', this.Preload.bindAsEventListener(this));
}

function checkAvailabilityDate(isCheckIn)
{
	var parts = getAvailabilityDateParts(isCheckIn);		
	var max = getDaysInMonth(parts[1], parts[0]);

	if(parts[2] > max)
		(isCheckIn ? $('check_in_day') : $('check_out_day')).selectedIndex = max - 1;
	
	if(!isCheckIn) return;
			
	var checkIn = getAvailabilityDate(true);	
	var checkOut = getAvailabilityDate(false);	
	
	var minNights = arguments.length == 1 ? 2 : arguments[1];

	if(checkOut - checkIn < (minNights * 24 - 1) * 60 * 60 * 1000) //daylight saving time
		setCheckOutDate(new Date(checkIn.getTime() + minNights * 24 * 60 * 60 * 1000));
}

var monthDays  = new Array(31, -1, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31)
function getDaysInMonth(month, year) {
  if (month == 2)
    return isLeapYear(year) ? 29 : 28;
  else
    return monthDays[month - 1];
}

function isLeapYear(year) {
  return (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) ? 1 : 0
}

function getAvailabilityDateParts(isCheckIn) 
{
	var day = isCheckIn ? $F('check_in_day') : $F('check_out_day');
	var ym =  (isCheckIn ? $F('check_in_ym') : $F('check_out_ym')).split('-');

	return new Array(parseInt(ym[0]), parseInt(ym[1]), parseInt(day));  
}

function getAvailabilityDate(isCheckIn) 
{
  var parts = getAvailabilityDateParts(isCheckIn);

  return new Date(parts[0], parts[1] - 1, parts[2]);
}

function setCheckOutDate(date) 
{
	$('check_out_day').selectedIndex = date.getDate() - 1;
	$('check_out_ym').selectedIndex = (date.getFullYear() * 12 + date.getMonth()) - (new Date().getFullYear() * 12 + new Date().getMonth());
}

