﻿/// <reference path="../../_shared/scripts/jquery-1.3.2-vsdoc.js" />

/*
* Namespace for RF Hotel_2
*/
var RF = window.RF ||
{};

RF.Hotel_2 = {};

RF.Hotel_2 = (function()
{
	var slideshowEngine;
	var allText = "";
	var altText = "";
	var currentIndex = -1;
	var landingImageCount = 0;
	var loop = 0;
	var targetIndex;
	var isPromotion = false;

	function init()
	{
		introduction();
		checkAvailability();
		groupRates();
		groupRatesSlideShow();
		quickBook();
		checkAvailabilityPanel();
	}

	function introduction()
	{
		var rfLogo = "<object id=\"RoccoForteCollectionLogo\" data=\"/common/templates/hotel/hotel_2/flash/rf_logo.swf\" type=\"application/x-shockwave-flash\" width=\"521\" height=\"156\">" +
					 "		<param name=\"movie\" value=\"/common/templates/hotel/hotel_2/flash/rf_logo.swf\" />" +
					 "		<param name=\"wmode\" value=\"transparent\" />" +
					 "		<param name=\"quality\" value=\"high\" />" +
					 "		<div class=\"logo_replaced_content\">" +
					 "			<img alt=\"\" src=\"/common/templates/hotel/hotel_2/images/rf_logo_static.jpg\">" +
					 "		</div>" +
					 "</object>";
		////No alternate content for IE9
		//if (jQuery.browser.msie && jQuery.browser.version == '9.0')
		//{
		//	rfLogo = "<object id=\"RoccoForteCollectionLogo\" data=\"/common/templates/hotel/hotel_2/flash/rf_logo.swf\" type=\"application/x-shockwave-flash\" width=\"521\" height=\"156\">" +
		//			 "		<param name=\"movie\" value=\"/common/templates/hotel/hotel_2/flash/rf_logo.swf\" />" +
		//			 "		<param name=\"wmode\" value=\"transparent\" />" +
		//			 "		<param name=\"quality\" value=\"high\" />" +
		//			 "</object>";
		//}

		var value = $(".landing_text_container").html();
		if (value != null) {
			value = value.trim().split('___')[0];
		}
		if (value == "promotion") {
			//Exception for promotions
			var width = "911";
			var height = "540";
			var path = "";
			var link = "";
			var staticImagePath = "";
			var currentLanguage = $("#current_language_hiddenfield").val();
			var currentDateTime = new Date();
			var isNewYear = false;

			//Trigger for new year (not in used)
			//if (currentDateTime.getDate() >= 1 && (currentDateTime.getMonth() + 1) >= 1 && currentDateTime.getFullYear() >= 2011) {
			//	isNewYear = true;
			//}

			if (currentLanguage == "en") {
				var paths = ["/common/templates/hotel/hotel_2/flash/abu_dhabi_launched.swf"];

				var randomElement = Math.floor(Math.random() * paths.length);
				path = paths[randomElement];
				link = "#";
				//staticImagePath = "/common/templates/hotel/hotel_2/images/summer_2011_en.jpg";
			}
			else {
				path = "/common/templates/hotel/hotel_2/flash/abu_dhabi_launched.swf";
				link = "#";
				//staticImagePath = "/common/templates/hotel/hotel_2/images/summer_2011_de.jpg";
			}
			//var link = '<a class=\"promotion_landing_link\" href=\"' + link + '"></a>';
			var promotion = '<object width="' + width + '" height="' + height + '" id="RoccoForteLandingText" data="' + path + '" type="application/x-shockwave-flash" >' +
							'<param value="' + path + '" name="movie" />' +
							'<param value="high" name="quality" />' +
							'<param value="transparent" name="wmode" />' +
							'<param value="dynamic_text=__value__" name="flashvars" />' +
							'<img alt="" src="' + staticImagePath + '">' +
							'</object>';

			promotion = promotion.replace("__value__", escape(value));
			promotion = promotion.replace("__value__", escape(value));
			//$(".landing_text_container").html(link + promotion).hide();
			$(".landing_text_container").html(promotion).hide();
			$(".landing_text_container").css("margin-top", "-10px");
			$(".landing_text_container").hide();
			setTimeout(function()
			{
				$(".landing_text_container").fadeIn("fast");
			}, 4000);
			isPromotion = true;
		}

		$(".landing_rf_logo_container").html(rfLogo).show();

		//Check if flash is not loadded
		setTimeout(function()
		{
			if ($(".logo_replaced_content").is(":visible")) {
				$(".landing_text_container").hide();
				setTimeout(function()
				{
					$(".landing_logo_container").fadeOut("slow");
					landingSlideShow();
				}, 3000);
			}
		}, 1000);
	}

	//This function is called by rfLogo flash after finished its animation.
	function animateLogo()
	{
		var value = $(".landing_text_container").html().trim().split('___')[0];
		var width = "";
		var height = "";
		var path = "";
		var flashTag = "";
		var currentLanguage = $("#current_language_hiddenfield").val();
		if (isPromotion) {
			var promotionFlashTag = $(".landing_text_container").html();
			//IE doesn't return param nodes for object tag
			if ((jQuery.browser.msie && jQuery.browser.version == '7.0') || (jQuery.browser.msie && jQuery.browser.version == '8.0')) {
				var paramNodes = $(".landing_text_container")[0].getElementsByTagName("param");
				for (var i = 0; i < $(paramNodes).size(); i++) {
					$(promotionFlashTag).append(paramNodes[i]);
				}
			}
			else {
				$(".landing_text_container").html("").html(promotionFlashTag);
			}


			//Exception for promotion texts
			//width = "911";
			//height = "540";
			//if (currentLanguage == "de") {
			//	path = "/common/templates/hotel/hotel_2/flash/gift_de.swf";
			//}
			//else {
			//	path = "/common/templates/hotel/hotel_2/flash/gift_en.swf";
			//}
			//$(".landing_text_container").css("margin-left", "75px");
			$(".landing_logo_container").fadeOut("slow", function loadDynamicText()
			{
				//$(".landing_text_container").fadeIn("fast");
			});
			return;
		}
		else {
			width = "911";
			height = "150";
			path = "/common/templates/hotel/hotel_2/flash/landing_text.swf";
		}
		flashTag = '<object width="' + width + '" height="' + height + '" id="RoccoForteLandingText" data="' + path + '" type="application/x-shockwave-flash" >' +
					   '<param value="' + path + '" name="movie" />' +
					   '<param value="high" name="quality" />' +
					   '<param value="transparent" name="wmode" />' +
					   '<param value="dynamic_text=__value__" name="flashvars" />' +
					   '</object>';
		if (value != "" && value != undefined && value.substring(0, 1) == "<") {
			value = "";
		}
		flashTag = flashTag.replace("__value__", escape(value));
		flashTag = flashTag.replace("__value__", escape(value));

		landingSlideShow();
		$(".landing_text_container").html("");
		$(".landing_logo_container").fadeOut("slow", function loadDynamicText()
		{
			$(".landing_text_container").html(flashTag).hide().delay(1000).fadeIn("fast");
		});
	}

	function landingSlideShow()
	{
		slideshowEngine = new imageslideshowEngine();
		landingImageCount = $(".landing_image_container a img").size();
		slideshowEngine.images = $(".landing_image_container a img").get();
		var displayTime = $(".landing_image_display_time").find("input[type=hidden]").val();
		displayTime = (parseInt(displayTime, 10) + 2) * 1000;
		allText = $(".landing_text_container").html().trim().split('___');
		altText = $(".landing_image_container a img").eq(0).attr("alt");
		slideshowEngine.init(displayTime, "slow", imageSwitch);
	}

	function imageSwitch(i)
	{
		if (i.to == (landingImageCount - 1)) {
			loop = loop + 1;
			//slideshowEngine.pauseEngine();
		}
		var currentAltText = $(".landing_image_container a img").eq(i.to).attr("alt");
		if (altText != currentAltText) {
			targetIndex = i.to;
			targetIndex = targetIndex == 0 ? landingImageCount : targetIndex;
		}
		if (i.from == targetIndex - 1) {
			if (currentIndex == -1) {
				currentIndex = 1;
			}
			else {
				currentIndex = (loop != 0 && currentIndex == (allText.length - 1)) ? 0 : currentIndex + 1;
				//console.log("landingImageCount=" + landingImageCount + "i.to=" + i.to + "i.from=" + i.from + "currentIndex=" + currentIndex);
			}
			if (window.document.RoccoForteLandingText) {
				try {
					//console.log("sent: " + allText[currentIndex]);
					window.document.RoccoForteLandingText.SetVariable("slogan", escape(allText[currentIndex]));
				} catch (e) { }
			}
		}
		altText = currentAltText;
	}

	function groupRates()
	{
		$(".group_rates_container .each .content").hide();
		$(".group_rates_container .each .findoutmore").click(function(e)
		{
			if ($(this).closest(".short_description").nextAll(".content").is(":visible") == false) {
				$(this).closest(".short_description").nextAll(".content").slideDown("fast");
			}
			else {
				$(this).closest(".short_description").nextAll(".content").slideUp("fast");
			}
			e.preventDefault();
		});
	}

	function checkAvailability()
	{
		$("#check_availability").find(".panel_arrival_date").click(function(e)
		{
			$(this).next("input").click();
			return false;
		});
		$("#check_availability").find(".panel_arrival_date").keypress(function(e)
		{
			$(this).next("input").click();
			return false;
		});
		$("#check_availability").find(".panel_departure_date").click(function(e)
		{
			$(this).next("input").click();
			return false;
		});
		$("#check_availability").find(".panel_departure_date").keypress(function(e)
		{
			$(this).next("input").click();
			return false;
		});
		$("#check_availability").find(".property_list_dropdownlist").change(function()
		{
			var sender = $(this);
			if ($(sender).val() == "-1") {
				if (!$(this).hasClass("check_availability_validation")) {
					$(this).addClass("check_availability_validation");
					return false;
				}
			}
			$(this).removeClass("check_availability_validation");
		});
		$("#check_availability").find(".purpose").change(function()
		{
			var sender = $(this);
			if ($(sender).val() == "-1") {
				if (!$(this).hasClass("check_availability_validation")) {
					$(this).addClass("check_availability_validation");
					return false;
				}
			}
			$(this).removeClass("check_availability_validation");
		});
	}

	function checkAvailabilityPanel()
	{
		$(".check_availability_button").click(function(e)
		{
			var sender = $(this);
			$("#check_availability").find(".check_availability_validation").removeClass("check_availability_validation");
			var selectedProperty = $("#check_availability").find(".property_list_dropdownlist");
			var selectedPurpose = $("#check_availability").find(".purpose");
			var arrivalDate = $("#check_availability").find(".panel_arrival_date");
			var departureDate = $("#check_availability").find(".panel_departure_date");
			var isValidProperty = true;
			var isValidArrivalDate = true;
			var isValidDepatureDate = true;
			var isValidPurpose = true;

			if ($(selectedProperty).val() == "-1") {
				isValidProperty = false;
			}
			if ($(selectedPurpose).val() == "-1") {
				isValidPurpose = false;
			}

			var todayDate = new Date();
			todayDate.setFullYear(todayDate.getFullYear(), todayDate.getMonth(), todayDate.getDate());
			var arrivalDateTime = new Date();
			var arrivalDateSplit = $(arrivalDate).val().split("/");
			arrivalDateTime.setFullYear(arrivalDateSplit[2], arrivalDateSplit[1] - 1, arrivalDateSplit[0]);
			if (arrivalDateTime < todayDate) {
				isValidArrivalDate = false;
			}
			var departureDateTime = new Date();
			var departureDateSplit = $(departureDate).val().split("/");
			departureDateTime.setFullYear(departureDateSplit[2], departureDateSplit[1] - 1, departureDateSplit[0]);
			if (departureDateTime < todayDate) {
				isValidDepatureDate = false;
			}

			if (!isValidProperty) {
				$(selectedProperty).addClass("check_availability_validation");
			}
			if (!isValidArrivalDate) {
				$(arrivalDate).addClass("check_availability_validation");
			}
			if (!isValidDepatureDate) {
				$(departureDate).addClass("check_availability_validation");
			}
			if (!isValidPurpose) {
				$(selectedPurpose).addClass("check_availability_validation");
			}

			if (!isValidProperty || !isValidArrivalDate || !isValidDepatureDate || !isValidPurpose) {
				return false;
			}

			var numberOfGuests = $(".panel_content").find("#guests").find("select");
			var numberOfRooms = $(".panel_content").find("#rooms").find("select");
			var currentLocale = $(".current_locale_hiddenfield").val().split('-')[0];
			if ($(numberOfGuests).val() == "2+" || $(numberOfRooms).val() == "3+") {
				var propertyName = $(selectedProperty).val();
				switch (currentLocale) {
					default:
					case "en":
						switch (propertyName) {
							case "amigo":
								window.location = "http://www.hotelamigo.com/special/speak-to-hotel";
								break;
							case "astoria":
								window.location = "http://www.thehotelastoria.com/special/speak-to-hotel";
								break;
							case "augustine":
								window.location = "http://www.theaugustine.com/special/speak-to-hotel";
								break;
							case "balmoral":
								window.location = "http://www.thebalmoralhotel.com/special/speak-to-hotel";
								break;
							case "browns":
								window.location = "http://www.brownshotel.com/special/speak-to-hotel";
								break;
							case "charles":
								window.location = "http://www.thecharleshotel.com/special/speak-to-hotel";
								break;
							case "derome":
								window.location = "http://www.hotelderome.com/special/speak-to-hotel";
								break;
							case "derussie":
								window.location = "http://www.hotelderussie.it/special/speak-to-hotel";
								break;
							case "lerichemond":
								window.location = "http://www.lerichemond.com/special/speak-to-hotel";
								break;
							case "lowry":
								window.location = "http://www.thelowryhotel.com/special/speak-to-hotel";
								break;
							case "savoy":
								window.location = "http://www.hotelsavoy.it/special/speak-to-hotel";
								break;
							case "verdura":
								window.location = "http://www.verduraresort.com/special/speak-to-hotel";
								break;
							case "villakennedy":
								window.location = "http://www.villakennedy.com/special/speak-to-hotel";
								break;
						}
						break;
					case "de":
						switch (propertyName) {
							case "amigo":
								window.location = "http://www.hotelamigobruessel.de/special/sofortkontakt";
								break;
							case "astoria":
								window.location = "http://www.hotelastoriastpetersburg.de/special/sofortkontakt";
								break;
							case "augustine":
								window.location = "http://www.theaugustinehotel.de/special/sofortkontakt";
								break;
							case "balmoral":
								window.location = "http://www.thebalmoral.de/special/sofortkontakt";
								break;
							case "browns":
								window.location = "http://www.brownshotel.de/special/sofortkontakt";
								break;
							case "charles":
								window.location = "http://www.charleshotel.de/special/sofortkontakt";
								break;
							case "derome":
								window.location = "http://www.hotelderome.de/special/sofortkontakt";
								break;
							case "derussie":
								window.location = "http://www.hotelderussie.de/special/sofortkontakt";
								break;
							case "lerichemond":
								window.location = "http://www.lerichemond.de/special/sofortkontakt";
								break;
							case "lowry":
								window.location = "http://www.thelowryhotel.de/special/sofortkontakt";
								break;
							case "savoy":
								window.location = "http://www.hotelsavoyflorenz.de/special/sofortkontakt";
								break;
							case "villakennedy":
								window.location = "http://www.villakennedyhotel.de/special/sofortkontakt";
								break;
						}
						break;
					case "it":
						switch (propertyName) {
							case "verdura":
								window.location = "http://www.verduraresort.it/special/sofortkontakt";
								break;
						}
						break;
					case "ru":
						switch (propertyName) {
							case "astoria":
								window.location = "http://www.hotelastoriastpetersburg.de/special/speak-to-hotel";
								break;
						}
						break;
				}
				return false;
			}

			RF.Hotels.setPPCTracking(sender);

			//Add property details to postback url
			try {
				WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions($(sender).attr("name"), "", false, "", "https://reservations.roccofortehotels.com/Default.aspx?property=" + $(selectedProperty).val() + "&language=" + $("#current_language_hiddenfield").val() + "&stage=2&sender=direct_post_panel", false, false))
			} catch (e) { }
		});
	}

	function quickBook()
	{
		$(".quickbook_arrival_date, .quickbook_departure_date").click(function(e)
		{
			$(this).next("input").click();
			return false;
		});
		$(".quickbook_arrival_date, .quickbook_departure_date").keypress(function(e)
		{
			$(this).next("input").click();
			return false;
		});
		$(".quickbook_arrival_date, .quickbook_departure_date").bind("cut copy paste", function(e)
		{
			return false;
		});
		$("#availability-form select.hotel").selectbox();
		$("#availability-form select.guest").selectbox();
		$("#availability-form select.number").selectbox();

		$("#availability-form select.hotel").parent().find(".jquery-selectbox-list").addClass("hotel_selectbox");
		$("#availability-form select").change(function()
		{
			var sender = $(this);
			if ($(sender).val() == "-1") {
				if (!$(this).parent().hasClass("quickbook_validation")) {
					$(this).parent().addClass("quickbook_validation");
					if (!$(".quickbook_error_summary").is(":visible")) {
						$(".quickbook_error_summary").slideDown("slow");
					}
					return false;
				}
			}
			$(this).parent().removeClass("quickbook_validation");
			if ($(".quickbook_error_summary").is(":visible")) {
				$(".quickbook_error_summary").slideUp("fast");
			}
		});

		$("#availability-form").hide();
		$(".open_form_triiger").click(function(e)
		{
			$(this).fadeOut("fast", function()
			{
				//Reset selected values
				var todayDate = new Date();
				$("#availability").find(".hotel").val("-1");
				$("#availability").find("span.jquery-selectbox-currentItem").html($("#availability").find(".hotel").children().eq(0).html());
				$(".quickbook_arrival_date").val(todayDate.getDate().toString() + "/" + (todayDate.getMonth() + 1).toString() + "/" + todayDate.getFullYear().toString());
				todayDate.setDate(todayDate.getDate() + 1);
				$(".quickbook_departure_date").val(todayDate.getDate().toString() + "/" + (todayDate.getMonth() + 1).toString() + "/" + todayDate.getFullYear().toString());
				//Fix bug for multiple select has the same value for first item
				$("#availability-form select.guest").prev("span").html($("#availability-form select.guest").find("option").eq(0).html());
				$("#availability-form select.number").prev("span").html($("#availability-form select.number").find("option").eq(0).html());
				$("#availability-form").fadeIn();
				//Disable slideshow link
				$(".landing_image_container").find("a").click(function(e)
				{
					e.preventDefault();
				});
			});
			e.preventDefault();
		});

		$(".submit_quickbook_trigger").click(function(e)
		{
			var isValidProperty = true;
			var isValidArrivalDate = true;
			var isValidDepatureDate = true;

			$("#availability").find(".quickbook_validation").removeClass("quickbook_validation");
			if ($(".quickbook_error_summary").is(":visible")) {
				$(".quickbook_error_summary").slideUp("fast");
			}

			var selectedProperty = $("#availability").find(".hotel").val();
			var arrivalDate = $(".quickbook_arrival_date").val();
			var departureDate = $(".quickbook_departure_date").val();

			if (selectedProperty == "-1") {
				isValidProperty = false;
			}
			if (arrivalDate == "") {
				isValidArrivalDate = false;
			}
			if (departureDate == "") {
				isValidDepatureDate = false;
			}

			var todayDate = new Date();
			todayDate.setFullYear(todayDate.getFullYear(), todayDate.getMonth(), todayDate.getDate());
			var arrivalDateTime = new Date();
			var arrivalDateSplit = arrivalDate.split("/");
			arrivalDateTime.setFullYear(arrivalDateSplit[2], arrivalDateSplit[1] - 1, arrivalDateSplit[0]);
			if (arrivalDateTime < todayDate) {
				isValidArrivalDate = false;
			}

			var departureDateTime = new Date();
			var departureDateSplit = departureDate.split("/");
			departureDateTime.setFullYear(departureDateSplit[2], departureDateSplit[1] - 1, departureDateSplit[0]);

			if (departureDateTime < todayDate) {
				isValidDepatureDate = false;
			}

			if (!isValidProperty) {
				$("#availability").find(".hotel").parent().addClass("quickbook_validation");
				$(".quickbook_error_summary").slideDown("slow");
			}
			if (!isValidArrivalDate) {
				$(".quickbook_arrival_date").addClass("quickbook_validation");
			}
			if (!isValidDepatureDate) {
				$(".quickbook_departure_date").addClass("quickbook_validation");
			}

			if (!isValidProperty || !isValidArrivalDate || !isValidDepatureDate) {
				return false;
			}
		});
	}

	//Internal functions
	function groupRatesSlideShow()
	{
		try {
			$(".group_rates_container .each").each(function()
			{
				slideshowEngine = new imageslideshowEngine();
				slideshowEngine.images = $(this).children(".group_rates_image_container").children("img").get();
				slideshowEngine.init(6000, "slow");
			});
		} catch (e) { }
	}

	return {
		init: init,
		introduction: introduction,
		animateLogo: animateLogo
	};
})();

$(RF.Hotel_2.init);
