/*
* Namespace for RF Verdura
*/
var RF = window.RF ||
{};

RF.Verdura = {};
RF.Verdura.Navigation = (function() {

	function init() {

		// main navigation items
		$('#mainnavigation > li').mouseover(function() {
			$(this).siblings('li').removeClass('active');
			$(this).addClass('active');
		});

		// enable a page click to turn off menus (mouseout fails to work because of positioning)
		$('html').click(function() {
			$('#mainnavigation > li').removeClass('active');
		});

		// "make a reservation" menu
		$('#resoptions').hover(function() {
			$(this).addClass('active');
		}, function() {
			$(this).removeClass('active');
		});

		//Sifr header		
		$(".h1_sifr").sifr({
			path: "/common/templates/resort/resort_1/flash/",
			font: "itc_advent_gothic_book",
			fontSize: "30"
		});

		//Panel images
		$(".panel_image_container").each(function() {
			var slideshowEngine = new imageslideshowEngine();
			slideshowEngine.images = $(this).children("img").get();
			slideshowEngine.init(5000, "fast");
		});

		$(".category_rates_image_container").each(function() {
			if ($(this).children("img").size() > 1) {
				slideshowEngine = new imageslideshowEngine();
				slideshowEngine.images = $(this).children("img").get();
				slideshowEngine.init(5000, 1000);
			}
		});
		//Keep session alive
		try {
			setInterval("PageMethods.Heartbeat();", 300000);
		} catch (e) { }
	}

	return {
		init: init
	};

})();


RF.Verdura.MainImages = (function() {
	var slideshowEngine;
	var config = {
		fadeInterval: 5000,
		fadeSpeed: 500
	};

	function createSlideshow() {
		if ($('body').hasClass('gallery') == false) {
			// create slideshow and set up image array
			slideshowEngine = new imageslideshowEngine();
			slideshowEngine.images = $('#images img').get();

			// set up control click event handlers
			$('#thumbs img').css({
				cursor: 'pointer'
			});
			$('#thumbs img').each(function(i) {
				$(this).click(function(e) {
					slideshowEngine.jumpToPair(i, false);
					var altText = $(this).attr("alt");
					//					$(".image_caption").fadeOut("fast", function() {
					//						$(".image_caption").html(altText).removeClass("sifr");
					//						$(".image_caption").sifr({
					//							path: "/common/templates/resort/resort_1/flash/",
					//							font: "nobel_book_light",
					//							fontSize: "30"
					//						});
					//						$(".image_caption").fadeIn("fast");
					//					});
					e.preventDefault();
				});
			});
			// start the engine
			slideshowEngine.init(config.fadeInterval, config.fadeSpeed, mainImageSwitched);
			offersAndPackages();
		}
	}

	function mainImageSwitched(i) {
		try {
			if ($("#images img").size() > 0) {
				var altText = $("#images img").eq(i.to).attr("alt");
				//				$(".image_caption").fadeOut("fast", function() {
				//					$(".image_caption").html(altText).removeClass("sifr");					
				//					$(".image_caption").sifr({
				//						path: "/common/templates/resort/resort_1/flash/",
				//						font: "nobel_book_light",
				//						fontSize: "30"
				//					});
				//					$(".image_caption").fadeIn("fast");
				//				});
			}
		} catch (e) { }
	}

	function selectImage(i) {
		slideshowEngine.jumpToPair(i, true);
	}

	function offersAndPackages() {
		$(".dlink_rate_images").each(function() {
			if ($(this).children("img").size() > 1) {
				slideshowEngine = new imageslideshowEngine();
				slideshowEngine.images = $(this).children("img").get();
				slideshowEngine.init(5000, 1000);
			}
		});
		$(".dlink_room_images").each(function() {
			if ($(this).children("img").size() > 1) {
				slideshowEngine = new imageslideshowEngine();
				slideshowEngine.images = $(this).children("img").get();
				slideshowEngine.init(5000, 1000);
			}
		});
	}

	return {
		createSlideshow: createSlideshow,
		selectImage: selectImage
	};

} ());


/* Gallery */
RF.Verdura.Gallery = (function() {
	var slideshowEngine;
	var config = {
		fadeInterval: 6000,
		fadeSpeed: 1000
	};

	function create() {
		if ($('body').hasClass('gallery')) {
			// create slideshow and set up image array
			slideshowEngine = new imageslideshowEngine();
			slideshowEngine.images = $('#images img').get();

			// set up control click event handlers
			$('#imagestrip img').css({
				cursor: 'pointer'
			});
			$('#imagestrip img').each(function(i) {
				$(this).click(function(event) {
					selectImage(i);
					event.preventDefault();
				});
			});

			//setup move left event handler
			$("#gallerygroup #moveright").click(function(event) {

				var imageCount = $("#imagestrip").children().size();
				var containerWidth = (imageCount * 75);
				containerWidth = containerWidth - (containerWidth * 2);

				var vleft = $("#imagestrip").css("left");
				var vwidth = $("#imagestrip").css("width");

				vleft = parseInt(vleft.substring(0, vleft.length - 2)) - 300;
				if (vleft >= containerWidth) {
					$("#imagestrip").animate({
						left: vleft
					}, 500);
				}
				event.preventDefault();
			});

			//setup move right event handler
			$("#gallerygroup #moveleft").click(function(event) {
				var vleft = $("#imagestrip").css("left");
				vleft = parseInt(vleft.substring(0, vleft.length - 2)) + 300;
				if (vleft <= 0) {
					$("#imagestrip").animate({
						left: vleft
					}, 500);
				}
				event.preventDefault();
			});

			$('#pause').click(function(event) {
				pause();
				event.preventDefault();
			});
			$('#play').click(function(event) {
				unpause();
				event.preventDefault();
			});

			// start the engine
			slideshowEngine.init(config.fadeInterval, config.fadeSpeed, imageSwitched);
		}
	}

	function selectImage(i) {
		slideshowEngine.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();
		}
	}
	function unpause() {
		if (slideshowEngine) {
			slideshowEngine.unpauseEngine();
		}
	}

	return {
		create: create,
		selectImage: selectImage,
		imageSwitched: imageSwitched,
		pause: pause,
		unpause: unpause
	};

} ());

$(document).ready(RF.Verdura.Navigation.init);
$(document).ready(RF.Verdura.MainImages.createSlideshow);
$(document).ready(RF.Verdura.Gallery.create);