/// <reference path="jquery-1.3.2-vsdoc.js" />

/*
* Namespace for RF Resort 2
*/
var RF = window.RF ||
{};
RF.Resort_2 = {};
RF.Resort_2 = (function() {

	var imageWidth = 111;
	var numberOfImage = 9;
	var mainSlideshowEngine;
	var config = {
		fadeInterval: 6000,
		fadeSpeed: 1000
	};

	function init() {
		createEventListeners();
	}

	function createEventListeners() {

		//Images
		//		$.each(jQuery.browser, function(i, val) {
		//			if (val == true) {
		//				if (i != "msie" && jQuery.browser.version.substr(0, 3) == "6.0") {
		//					$(".loader").each(function(i) {
		//						$(this)._load(function() {
		//							$(this).fadeIn(2000);
		//						});
		//					});
		//				}
		//			}
		//		});


		$("#page").fadeIn(2000);
		//Fix cleartype bug in IE
		if (jQuery.browser.msie) {
			$(".content").attr("filter", "");
		}
		$("#page-links li .hover-fader").css("opacity", "0");

		//Image slideshow
		try {
			//Main
			mainSlideshowEngine = new imageslideshowEngine();
			mainSlideshowEngine.images = $('#content-area > .images > img').get();
			mainSlideshowEngine.init(config.fadeInterval, config.fadeSpeed);
			//Page
			var slideshowEngine2 = new imageslideshowEngine();
			slideshowEngine2.images = $('.backdrop > .images > img').get();
			slideshowEngine2.init(config.fadeInterval, config.fadeSpeed);
		} catch (e) { }

		//Background switch
		if ($("body").attr("class").indexOf("previous_background_", 0) > -1) {
			$("#backgroundcover").hide();
			setTimeout(function showbg() {
				$("#backgroundcover").fadeIn(1000);
			}, 3000);
		}

		//Pages
		if ($("#page-links li .backdrop").size() > 0) {
			$("#page-links li .backdrop").hover(function() {
				if (!$(this).prev().hasClass('animating')) {
					$(this).prev().animate({
						opacity: 1
					}, 700, function() {
						$(this).removeClass('animating');
					}).addClass('animating');
				}
			},
		function() {
			$(this).prev().animate({
				opacity: 0
			}, 2000);
		});
		}

		/*None default with siblings with children*/
		if ($(".none_default_with_siblings_with_children").size() > 0) {
			$(".none_default_with_siblings_with_children #page li .backdrop #sub_pagelinks li").hover(function() {
				$(this).nextAll().children("img").stop().fadeTo(1000, 0.5);
				$(this).prevAll().children("img").stop().fadeTo(1000, 0.5);

			}, function() {
				$(this).nextAll().children("img").fadeTo(500, 1);
				$(this).prevAll().children("img").fadeTo(500, 1);
			});
		}

		//Gallery thumbnail trigger
		var galleryElement = $("#gallerygroup");
		var hoverStrip = $("#hover-strip");
		if ($(galleryElement).size() > 0) {
			var isShowingThumbnails = false;
			var cancelhideGallery = window.setInterval(function hideGallery() {
				if (isShowingThumbnails == false && $(galleryElement).is(":visible") == true) {
					$(galleryElement).hide("slide", { direction: "down" }, 1000);
					$(hoverStrip).show();
				}
			}, 4000);
			$(hoverStrip).hover(function() {
				$(galleryElement).show("slide", { direction: "down" }, 1000);
				$(hoverStrip).hide();
			});

			$(galleryElement).mouseover(function() {
				isShowingThumbnails = true;
			});
			$(galleryElement).mouseout(function() {
				isShowingThumbnails = false;
			});


			//Set up control click event handlers
			$('#imagestrip img').each(function(i) {
				$(this).click(function(e) {
					selectImage(i);
					e.preventDefault();
				});
			});

			//Slider
			$("#slider").slider({ animate: true, animate_if_programmed: true });
			$("#slider a").attr({ id: "handlebar" });
			var width = (mainSlideshowEngine.images.length * 108) + 30;
			$("#imagestrip").css("width", width + "px");

			//Setup move left/right event handler
			$("#gallerygroup #moveleft").click(function(e) {
				var totalWidth = $("#imagestrip").css("width");
				totalWidth = totalWidth.substring(0, totalWidth.length - 1);
				totalWidth = parseInt(totalWidth, 10)

				var visibleArea = imageWidth * numberOfImage;
				var valueToMove = (visibleArea * 100) / totalWidth;
				var theRest = 100 - valueToMove;
				if (theRest < valueToMove) {
					valueToMove = 0;
				}
				else {
					valueToMove = valueToMove;
				}
				//			var leftValue = $("#handlebar").css("left");
				//			leftValue = leftValue.substring(0, leftValue.length - 1);
				//			leftValue = parseInt(leftValue, 10)
				//			leftValue = leftValue > 0 ? leftValue - 10 : leftValue;
				//			leftValue = leftValue < 0 ? 0 : leftValue;
				$('#slider').slider('option', 'value', valueToMove);
				e.preventDefault();
			});

			$("#gallerygroup #moveright").click(function(e) {
				var totalWidth = $("#imagestrip").css("width");
				totalWidth = totalWidth.substring(0, totalWidth.length - 1);
				totalWidth = parseInt(totalWidth, 10)

				var visibleArea = imageWidth * numberOfImage;
				var valueToMove = (visibleArea * 100) / totalWidth;
				var theRest = 100 - valueToMove;
				if (theRest > valueToMove) {
					valueToMove = valueToMove;
				}
				else {
					valueToMove = 100;
				}
				//			var leftValue = $("#handlebar").css("left");
				//			leftValue = leftValue.substring(0, leftValue.length - 1);
				//			leftValue = parseInt(leftValue, 10);
				//			leftValue = leftValue < 100 ? leftValue + 10 : leftValue;
				//			leftValue = leftValue > 100 ? 100 : leftValue;
				$('#slider').slider('option', 'value', valueToMove);
				e.preventDefault();
			});
		}

		//Sifr header
		var fontSize = "27";
		$.each(jQuery.browser, function(i, val) {
			if (val == true) {
				if (i == "msie") {
					fontSize = "20";
				}
			}
		});
		setTimeout(function() { RF.Resort_2.sifrReplace(fontSize, "#807875"); }, 1000);

		//Map table (HTML to FLASH)
		$(".map_table tr").hover(function() {
			var selectedElement = $(this);
			var selectedValue = $(selectedElement).children(":first").html();
			selectedValue = parseInt(selectedValue, 10);
			if (selectedValue != NaN || selectedValue != undefined) {
				$(selectedElement).addClass("active").siblings().removeClass("active");
			}
			window.document.Map.SetVariable("highlight_zone", selectedValue);
		});
		//Map zoom
		$(".zoom_trigger a").click(function(e) {
			var element = $(this);
			var value = $(element).attr("title");
			if (value == null || value == "" || value == undefined || value == "zoom_out") {
				value = "zoom_in";
			}
			else {
				value = "zoom_out";
			}
			window.document.Map.SetVariable("highlight_zone", value);
			$(element).attr("title", value);
			e.preventDefault;
		});

		setTimeout(function() { $(".map_table tr").removeClass("active"); }, 2000);
	}

	//Map table (FLASH to HTML)
	function highlightTable(value) {
		var mapTableRows = $(".map_table tr");
		for (var i = 1; i < mapTableRows.size(); i++) {
			if ($(mapTableRows).eq(i).children(":first").html() == value) {
				$(mapTableRows).eq(i).addClass("active").siblings().removeClass("active");
				break;
			}
		}
	}

	function selectImage(i) {
		mainSlideshowEngine.jumpToPair(i, true);
		imageSwitched({
			to: i
		});
	}

	function imageSwitched(indexes) {
		// activate the indexed thumbnail, deactivate others
		$('#imagestrip img').eq(indexes.to).addClass('active').siblings().removeClass('active');
	}

	function pause() {
		if (slideshowEngine) {
			slideshowEngine.pauseEngine();
			$('#pause').addClass('active');
			$('#play').removeClass('active');
		}
	}

	function unpause() {
		if (slideshowEngine) {
			slideshowEngine.unpauseEngine();
			$('#pause').removeClass('active');
			$('#play').addClass('active');
		}
	}

	//SiFr
	function sifrReplace(fontSize, colour) {
		//$(".sifr_quote").hide();
		if ($(".sifr_text").html() != "") {
			$(".sifr_text").sifr({
				path: "/common/templates/resort/resort_2/flash/",
				font: "nobel_book_light",
				fontSize: fontSize,
				color: colour
			});
			//$(".sifr_text").css("display", "none");		
			setTimeout('RF.Resort_2.fadeElement(".sifr_quote", "in")', 5000);
		}
	}

	function fadeElement(element, direction) {
		switch (direction) {
			case "in":
				$(element).fadeIn("slow");
				break;
			case "out":
				$(element).fadeOut("slow");
				break;
		}
	}

	return {
		init: init,
		sifrReplace: sifrReplace,
		fadeElement: fadeElement,
		highlightTable: highlightTable
	};
})();

$(RF.Resort_2.init);