document.addEvent("domready", function(){
	externalLinks();
	if($("booking_aparthotel"))
	{
		$("booking_aparthotel").addEvent("change", function(e){
				$('booking_hotel').set("value", this.get("value"));
				checkAdults();
		});
		
		$("booking_numpax").addEvent("change", function(e){
			$('no_pax').set("value", this.get("value"));
			
		});
		
		$("booking_numapartments").addEvent("change", function(e){
			$('no_rooms').set("value", this.get("value"));
		});
		
		function checkAdults()
		{
			var numpax = $('booking_numpax'); 
			numpax.set("html", "");
			number = 0;
			var hotel = $("booking_aparthotel").get("value");
			
			if(hotel == 195) number = 6;
			if(hotel == 199) number = 3;
			if(hotel == 197) number = 6;
			if(hotel == 198) number = 5;
			if(hotel == 366) number = 6;
			
			if(hotel == 19486) number = 4;
			if(hotel == 7133) number = 9;
			if(hotel == 17) number = 9;
			
			options = getOptions(number);
			options.each(function(option){option.inject(numpax)});
		}
		
		function getOptions(number)
		{
			var options = [];
			for(var counter = 1; counter<=number;counter++)
				options.push(new Element("option", {value:counter, text:counter}));
			
			return options;
		}
		
		checkAdults();
		
		$("booking").addEvent("submit", function(e) {
			e.stop();
			var splitDate =  $("booking_dateIn").get("value").split("/");
			if(splitDate.length>2){
				var date = splitDate[2]+"-"+splitDate[1]+"-"+splitDate[0];
			}
			else{
				var hoy = new Date();
				var timestamp = hoy	.getTime();
				timestamp += Date.DAY;
		
				var newDate 	= new Date(timestamp);
				var curr_date 	= newDate.getDate();
				var curr_month 	= newDate.getMonth();
				var curr_year 	= newDate.getFullYear();
		
				//dateOutField.set("value", curr_date+"/"+curr_month+"/"+curr_year);
				if(splitDate.length==2){
					var date = curr_year+"-"+splitDate[1]+"-"+splitDate[0];
				}
				else{
					var date = curr_year+"-"+(curr_month+1)+"-"+curr_date;
				}
			}
			$('booking_arr').set("value",date);
			
			var splitDate =  $("booking_dateOut").get("value").split("/");
			var date = splitDate[2]+"-"+splitDate[1]+"-"+splitDate[0];
			$('booking_dep').set("value", date);

			$('booking_hotel').set("value", $("booking_aparthotel").get("value"));
			$('no_pax').set("value", $("booking_numpax").get("value"));
			$('no_rooms').set("value", $("booking_numapartments").get("value"));
			

			//pageTracker._trackPageview("/bookNow");
			
			
			this.submit();
		});
	
		//$("booking").set("target", "_blank");
	}

	var promotion = $("promotions"); 
	
	if(promotion && promotion.hasClass("topBlock"))
	{
		new AjaxPromotions({imageWidth:155, imageHeight:95});
	}
	else if(promotion) new AjaxPromotions();
	
	function parseNewslettersForm()
	{
		var newsletterForm  = $("newsletters");
		var loader = new Element("div", {"class":'newslettersLoader'});
		if(newsletterForm)
		{
			$("newsletters_name").addEvent("tab", function(e){e.stop();$("newsletters_email").focus()});
			$("newsletters_email").addEvent("shiftTab", function(e){e.stop();$("newsletters_name").focus()});
			newsletterForm.addEvent("submit", function(e){
				loader.inject(newsletterForm);
				e.stop();
				var data = {};
				newsletterForm.getElements("input").each(function(input){
					data[input.get("name")] = input.get("value");
				});
				
				new Request.JSON({url:document.location.href, data:data
					, onComplete: function(response){
					loader = loader.dispose();
					newsletterForm.getParent("div").set("html", response.form);
					parseNewslettersForm();
					try {
						document.inlineEdit.parseDom();
					}
					catch(exception){}
				}
				}).send();
			})
		}
	}
	parseNewslettersForm();
	
	if($("rotativeMessages")) new viewer($("rotativeMessages").getChildren(), {fxOptions: {duration:500, transition:"Quint:out"},mode:"left", interval:8000}).play();
	
	
	var promotionFile = $("promotionFile");
	if(promotionFile)
	{
		promotionFile.addEvent("click", function(e){
			e.stop();
			window.open(this.get("href"));
		});
	}
	
	
})

function dateInUpdate(cal)
{
	var dateOutField = $("booking_dateOut");
	var dateInField  = $("booking_dateIn");
	
	
	var values = dateInField.get("value").split("/");
	var dateIn = new Date();
	dateIn.setDate(values[0]);
	dateIn.setMonth(values[1]);
	dateIn.setFullYear(values[2]);
	
	var values = dateOutField.get("value").split("/");
	var dateOut = new Date();
	dateOut.setDate(values[0]);
	dateOut.setMonth(values[1]);
	dateOut.setFullYear(values[2]);
	
	if(dateIn > dateOut)
	{
		var timestamp = dateIn.getTime();
		timestamp += Date.DAY;
		
		var newDate 	= new Date(timestamp);
		var curr_date 	= newDate.getDate();
		var curr_month 	= newDate.getMonth();
		var curr_year 	= newDate.getFullYear();
		
		dateOutField.set("value", curr_date+"/"+curr_month+"/"+curr_year);
	}
}

function calendarUpdate(calendar)
{
	$(calendar.params.inputField).fireEvent("change");
}
/**
 * loops through all links in the current page, and sets their target
 * attribute as follows:
 * 
 * if the "rel" attribute is set to "external", the target is set to "_blank"
 *
 * if the "rel" attribute begins with "external:", the target is set to the
 * remainder of the "rel" attribute following the colon.
 *
 * For example, to target the "myframe" frame, a link should be
 * formatted as:
 *
 * <a href="someurl" rel="external:myframe">my link</a>
 *
 * this is based upon a simpler version at
 * http://www.sitepoint.com/article/1041/3
 */
 
 /**
 * Modificacion: hace externos los links "nofollow"
 * Para hacer links externos sin "nofollow" usar rel="external:_blank"
 */
function externalLinks() {
	if (!document.getElementsByTagName) return;
	var anchors = document.getElementsByTagName("a");
	for (var i=0; i<anchors.length; i++) {
		var anchor = anchors[i];
		if (anchor.getAttribute("href") && anchor.getAttribute("rel")) {
			var rel = anchor.getAttribute("rel");
			if ((rel == "nofollow") || (rel == "external")) {
				anchor.target = "_blank";			
			} else if (rel.substring(0,9) == "external:") {
				anchor.target = rel.substring(9, rel.length);
			} 
		}
	}
}
