Shadowbox.init({
	language:'es',
	players:['img', 'html'],
	player:'img'
});

document.observe('dom:loaded', initDatepicker);

function gallery()
{
	Shadowbox.open(Shadowbox.cache[0]);
}

function showMap()
{
	var divMap = document.createElement('div');
	divMap.id = 'map';
	divMap.style.width = '700px';
	divMap.style.height = '500px';

	var center = new GLatLng(40.99560946915047, 0.93059778213500);
	document.onunload = GUnload;

	Shadowbox.open({
		content:'',
		player:'html',
		width:parseInt(divMap.style.width, 10),
		height:parseInt(divMap.style.height, 10),
		options:{
			onFinish:function()
			{
				$(Shadowbox.contentId()).appendChild(divMap);
				var map = new GMap2(divMap);
				map.setCenter(center, 15);
				map.addControl(new GLargeMapControl());
				map.addControl(new GMapTypeControl());
				map.addOverlay(new GMarker(center));
			}
		}
	});
}

function initDatepicker()
{
	$('fecha_entrada', 'fecha_salida').each(function(elemento)
	{
		options =
		{
			clickCallback:function()
			{
				if (elemento.id == 'fecha_salida' && !$F(elemento).isDate() && $F('fecha_entrada').isDate())
				{
					elemento.value = $F('fecha_entrada').toDate().addDays(7).toStrDate();
				}

				document.observe('click', function(event)
				{
					if (event.target != elemento && event.target != elemento.next('img') && !$(event.target).descendantOf($("datepicker-" + elemento.id)))
					{
						this.close();
					}
				}.bind(this));
			},
			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);
		
		//Abrir el calendario también al hacer click en la imagen
		elemento.next('img').onclick = obj.click.bind(obj);
	});
}

String.prototype.isDate = function()
{
	return /^\d{1,2}\/\d{1,2}\/\d{4}$/.test(this);
}

String.prototype.toDate = function()
{
	if(!this.isDate())
	{
		return null;
	}

	var date_s = this.split("/");
	var new_date = [date_s[1],date_s[0],date_s[2]];

	// Formato mm/dd/yyyy
	var dDate = new Date(new_date.join("/"));

	return dDate;

}

Date.prototype.addDays = function(d)
{
	this.setDate(this.getDate() + parseInt(d, 10));
	return this;
}

Date.prototype.toStrDate = function()
{
	var day = parseInt(this.getDate(), 10);
	var month = parseInt(this.getMonth(), 10)+1;

	return (day < 10 ? "0"+day : day) + "/" + (month < 10 ? "0"+month : month) + "/" + this.getFullYear();
}
