function setupLinks()
{
	$$('a').each(function(anchor)
	{
		switch (anchor.readAttribute('rel'))
		{
			case 'popup' :

				var w = 600;
				var h = 400;
				var x = (screen.availWidth - w) / 2;
				var y = (screen.availHeight - h) / 2;
				window.open(this.url, "_blank", "status=no,scrollbars=no,width="+w+",height="+h+",left="+x+",top="+y);
				break;

			case 'external' :

				anchor.writeAttribute('target', '_blank');
				break;

			case 'modalbox' :

				Event.observe(anchor, 'click', function(e)
				{
					var options =  {title: this.title, width: 600};
					if(this.className == 'link_conditions') options.height = document.documentElement.clientHeight;
					Modalbox.show(this.href, options);
					Event.stop(e);
				});
				break;

			case 'jump' :

				Event.observe(anchor, 'keyup', function(e)
				{
					alert("es");
					if (this.size >= this.value.length())
					{
						alert('max');
					}
					Event.stop(e);
				});
				break;
		}
	});
}

function setupInputs()
{
	$$('input').each(function(input)
	{
		switch (input.readAttribute('rel'))
		{
			case 'jump' :

				Event.observe(input, 'keyup', function(e)
				{
					if (this.getValue().length >= this.size)
					{
						this.next("input").activate();
					}
					Event.stop(e);
				});
				break;
		}
	});
}

/* Funciones para el selector de idiomas */
function showSelector(selector, event)
{
	hideSelectors();

	$(selector).down('.selector-options').toggle();

	document.observe('click', hideSelectors);
	document.observe('blur', hideSelectors);

	event.stop();
}
function hideSelectors()
{
	$$('.selector').each(function(elm) { elm.down('.selector-options').hide(); });

	document.stopObserving('click', hideSelectors);
	document.stopObserving('blur', hideSelectors);
}

/* Setup del bloque header */

function setupSelector(selector)
{
	var button = $(selector).down('.button');
	button.observe('click', Event.stop);
	button.observe('mousedown', showSelector.curry(selector));
	button.observe('mouseover', button.addClassName.curry('button-hover'));
	button.observe('mouseout', button.removeClassName.curry('button-hover'));

	$(selector).down('.selector-options').setStyle({width: $(selector).getStyle('width')});
}

function setNewArrival(arrival)
{
	if ((typeof arrival == 'string') && (arrival.isDate()))
	{
		$('arrival').value = arrival;

		if (!$F('departure').isDate() || (arrival.getDaysBetween($F('departure')) < 1))
		{
			setNewDeparture(arrival.toDate().addDays(7).toStrDate());
		}

		if (typeof checkAvailability == 'function')
		{
			checkAvailability();
		}
	}
}

function setNewDeparture(departure)
{
	if ((typeof departure == 'string') && (departure.isDate()))
	{
		$('departure').value = departure;

		if (!$F('arrival').isDate() || ($F('arrival').getDaysBetween(departure) < 1))
		{
			setNewArrival(departure.toDate().removeDays(7).toStrDate());
		}

		if (typeof checkAvailability == 'function')
		{
			checkAvailability();
		}
	}
}

var InitDatePicker = Class.create();

/* Inicializar los inputs de arrival y departure */

InitDatePicker.prototype =
{
	initialize : function()
	{
		$$("input.datepicker").each(function(elemento,indx)
		{
			options =
			{
				// Evento al abrir el calendario
				clickCallback : function()
				{
					//Cerrar el calendario al clickar fuera de él
					var calendarObj = this;
					document.observe('click', function(event)
					{
						if (event.target != elemento && event.target != elemento.next('a#ico'+elemento.id) && !$(event.target).descendantOf($("datepicker-" + elemento.id)))
						{
							calendarObj.close();
						}
					});

					if (typeof ClickTaleExec == 'function') ClickTaleExec("$('" + elemento.id + "').fire('ct:click')");
				},

				// Evento al seleccionar una fecha y cerrarse el calendario
				cellCallback : function()
				{
					switch (elemento.id)
					{
						case 'arrival'   : setNewArrival(elemento.value); break;
						case 'departure' : setNewDeparture(elemento.value); break;
					}

					document.stopObserving('click');
					
					if (typeof ClickTaleExec == 'function') ClickTaleExec("$('" + elemento.id + "').fire('ct:close'); $('" + elemento.id + "').value = '" + elemento.value + "'");
				},

				relative			: elemento.id,
				language			: elemento.lang,
				keepFieldEmpty		: true,
				enableShowEffect	: false,
				enableCloseEffect	: false,
				topOffset			: 25,
				disableFutureDate	: false,
				disablePastDate		: true,
				topOffset			: elemento.offsetHeight + 1
				}

			var obj = new DatePicker(options);

			// Eventos para ClickTale
			elemento.observe('ct:click', obj.click.bind(obj));
			elemento.observe('ct:close', obj.close.bind(obj));

			// Añadir evento click de la imagen del calendario si existe
			if(elemento.adjacent('a#ico'+elemento.id).first() != undefined)
			{
				elemento.adjacent('a#ico'+elemento.id).first().onclick = obj.click.bind(obj);
			}

		}.bind(this))
	}
}

function tooltip(anchor)
{
	var div = new Element('div', {className: 'tooltip'});

	var input = new Element('input', {type:'text'});

	div.update(anchor.title).insert(input);
	anchor.up('div').insert(div);
	div.clonePosition(anchor, {setWidth:false, setHeight:false, offsetTop:-div.getHeight()});

	input.onblur = div.remove.bind(div);
	input.focus(); //al perder el foco hará el onblur
}

/* SETUP PRINCIPAL ONLOAD */

document.observe('dom:loaded', function()
{
	setupLinks();
	setupInputs();
	setupSelector('currencies-selector');
	setupSelector('languages-selector');
	new InitDatePicker();
});

/* ANALYTICS 
document.observe('dom:loaded', function()
{
	var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
	var script = new Element('script', { 'src': gaJsHost + 'google-analytics.com/ga.js', type: 'text/javascript'});

	var gaTrack = function()
	{
		if (!script.readyState || /loaded|complete/.test(script.readyState))
		{
			var pageTracker = _gat._getTracker(ga_id);
			pageTracker._trackPageview();
		}
	};
	script.observe('load', gaTrack);
	script.observe('readystatechange', gaTrack);
	$(document.body).insert(script);
}); 
*/
