var OL = {};

(function(context) {
	
	jQuery(document).ready(function() {		
		context.nav.init();		
		context.hotspots.init();
		context.location.init();
		context.rates.init();
		
	});
	
	context.settings = {
		frame: (jQuery.browser.webkit) ? 'body' : 'html',
		fixed_position_supported: true
	};
	context.hotspots = {
			init: function(){
				var self = this;
				self.capture_hotspots_clicks();
		},
		capture_hotspots_clicks: function() {
			var self = this;	
			$('.hotspot').tipsy({gravity: 'w',fade: true});
			$('.icon-more').tipsy({gravity: 'w',fade: true});
			
			$("#hot-home-outside-1").click(function() {				
				$.fancybox([
				    'images/beach-0.jpg',
					'images/beach-4.jpg',
					'images/beach-8.jpg',
					'images/beach-9.jpg',				
					'images/beach-11.jpg',				
					'images/beach-13.jpg',
					], {
					'overlayColor' : '#231f20',
					'centerOnScroll' : true,
					'padding' : 0,
					'transitionIn' : 'none',
					'transitionOut' : 'none',
					'type' : 'image',
					'changeFade' : 0
				});
			}); 
			$("#hot-accom-outside-1").click(function() {				
				$.fancybox([
					'images/pavilion-1.jpg',					
					'images/pavilion-3.jpg',
					'images/pavilion-4.jpg',
					'images/pavilion-5.jpg'	
					], {
					'overlayColor' : '#231f20',
					'centerOnScroll' : true,
					'padding' : 0,
					'transitionIn' : 'none',
					'transitionOut' : 'none',
					'type' : 'image',
					'changeFade' : 0
				});
			}); 
			$("#hot-accom-outside-2").click(function() {				
				$.fancybox([
				    'images/pool-0.jpg',
					'images/pool-1.jpg',
					'images/pool-2.jpg',
					'images/pool-3.jpg',
					'images/pool-5.jpg',
					'images/pool-6.jpg'
					], {
					'overlayColor' : '#231f20',
					'centerOnScroll' : true,
					'padding' : 0,
					'transitionIn' : 'none',
					'transitionOut' : 'none',
					'type' : 'image',
					'changeFade' : 0
				});
			}); 
			$("#hot-accom-outside-3").click(function() {				
				$.fancybox([
					'images/court-yard-1.jpg',
					'images/court-yard-2.jpg'					
					], {
					'overlayColor' : '#231f20',
					'centerOnScroll' : true,
					'padding' : 0,
					'transitionIn' : 'none',
					'transitionOut' : 'none',
					'type' : 'image',
					'changeFade' : 0
				});
			}); 
			$("#img-getaway").click(function() {				
				$.fancybox([
				    'images/getaway-5.jpg',   				        
					'images/getaway-1.jpg',
					'images/getaway-2.jpg',
					'images/getaway-3.jpg',
					'images/getaway-4.jpg',	
					'images/getaway-7.jpg'	
					], {
					'overlayColor' : '#231f20',
					'centerOnScroll' : true,
					'padding' : 0,
					'transitionIn' : 'none',
					'transitionOut' : 'none',
					'type' : 'image',
					'changeFade' : 0
				});
			}); 
			$("#img-cuisine").click(function() {				
				$.fancybox([					
					'images/cuisine-4.jpg',
					'images/cuisine-5.jpg'					
					], {
					'overlayColor' : '#231f20',
					'centerOnScroll' : true,
					'padding' : 0,
					'transitionIn' : 'none',
					'transitionOut' : 'none',
					'type' : 'image',
					'changeFade' : 0
				});
			}); 
			$("#img-beauty-sri-lanka").click(function() {				
				$.fancybox([
					'images/beauty-sri-lanka-1.jpg',
					'images/beauty-sri-lanka-2.jpg',
					'images/beauty-sri-lanka-3.jpg',
					'images/beauty-sri-lanka-4.jpg',
					'images/beauty-sri-lanka-5.jpg',
					'images/beauty-sri-lanka-6.jpg',
					'images/beauty-sri-lanka-7.jpg',					
					'images/beauty-sri-lanka-9.jpg'
					
					], {
					'overlayColor' : '#231f20',
					'centerOnScroll' : true,
					'padding' : 0,
					'transitionIn' : 'none',
					'transitionOut' : 'none',
					'type' : 'image',
					'changeFade' : 0
				});
			}); 
			$("#room-anthurium").click(function() {				
				$.fancybox([
					'images/room-anturium-1.jpg',
					'images/room-anturium-2.jpg',
					'images/room-anturium-3.jpg'
					], {
					'overlayColor' : '#231f20',
					'centerOnScroll' : true,
					'padding' : 0,
					'transitionIn' : 'none',
					'transitionOut' : 'none',
					'type' : 'image',
					'changeFade' : 0
				});
				return false;
			});
			$("#room-jasmine").click(function() {				
				$.fancybox([
					'images/room-jasmine-1.jpg'
					], {
					'overlayColor' : '#231f20',
					'centerOnScroll' : true,
					'padding' : 0,
					'transitionIn' : 'none',
					'transitionOut' : 'none',
					'type' : 'image',
					'changeFade' : 0
				});
				return false;
			});
			$("#room-orchid").click(function() {				
				$.fancybox([
					'images/room-orchid-1.jpg'
					], {
					'overlayColor' : '#231f20',
					'centerOnScroll' : true,
					'padding' : 0,
					'transitionIn' : 'none',
					'transitionOut' : 'none',
					'type' : 'image',
					'changeFade' : 0
				});
				return false;
			});
			$("#room-veranda").click(function() {				
				$.fancybox([
					'images/room-veranda.jpg'
					], {
					'overlayColor' : '#231f20',
					'centerOnScroll' : true,
					'padding' : 0,
					'transitionIn' : 'none',
					'transitionOut' : 'none',
					'type' : 'image',
					'changeFade' : 0
				});
				return false;
			});
			$("#room-sitting").click(function() {				
				$.fancybox([
					'images/room-sitting-1.jpg',
					'images/room-sitting-2.jpg'
					], {
					'overlayColor' : '#231f20',
					'centerOnScroll' : true,
					'padding' : 0,
					'transitionIn' : 'none',
					'transitionOut' : 'none',
					'type' : 'image',
					'changeFade' : 0
				});
				return false;
			});
			$("#room-sunflower").click(function() {				
				$.fancybox([
					'images/room-sunflower-1.jpg',				
					'images/room-sunflower-3.jpg'
					], {
					'overlayColor' : '#231f20',
					'centerOnScroll' : true,
					'padding' : 0,
					'transitionIn' : 'none',
					'transitionOut' : 'none',
					'type' : 'image',
					'changeFade' : 0
				});
				return false;
			});
			
			$("#hot-accom-roof-1").hover(
				  function () {
					  $("#villa-house-small-roof").fadeTo("fast", 0.50);
				  },
				  function () {
					  $("#villa-house-small-roof").fadeTo("fast", 1);
				  }
			);
			$("#hot-accom-roof-1").click(function() {		
				$("#hot-accom-roof-1").unbind('mouseenter mouseleave');
				$(".tipsy").hide();
				$(this).hide();
				$("#villa-house-small-roof").fadeTo("slow", 0).hide();
			});
			$("#hot-accom-roof-2").hover(
					  function () {
						  $("#villa-house-large-roof").fadeTo("fast", 0.50);
					  },
					  function () {
						  $("#villa-house-large-roof").fadeTo("fast", 1);
					  }
				);
				$("#hot-accom-roof-2").click(function() {		
					$("#hot-accom-roof-2").unbind('mouseenter mouseleave');
					$(".tipsy").hide();
					$(this).hide();
					$("#villa-house-large-roof").fadeTo("slow", 0).hide();
				});
			

		}
	};
	context.rates = {
			init: function(){
				var self = this;
				self.capture_rates_clicks();
		},
		capture_rates_clicks: function() {
			var self = this;
			
			jQuery("#rates-villa-link").click(function(event) {
				event.preventDefault();		
				jQuery("#rates-villa").show();
				jQuery("#rates-room").hide();
				jQuery(this).addClass("active");
				jQuery("#rates-room-link").removeClass("active");
				
			});
			jQuery("#rates-room-link").click(function(event) {
				event.preventDefault();		
				jQuery("#rates-room").show();
				jQuery("#rates-villa").hide();
				jQuery(this).addClass("active");
				jQuery("#rates-villa-link").removeClass("active");
				
			});
		}
	};
	context.location = {
			init: function(){
				var self = this;
				self.capture_places_clicks();
		},
		capture_places_clicks: function() {
			var self = this;
			
			jQuery(".place-list-item").click(function(event) {
				event.preventDefault();		
				jQuery(".place-list-item").removeClass("active");
				jQuery(this).addClass("active");
				jQuery(".place-content").hide();
				jQuery(".place-content[rel='"+jQuery(this).attr("rel")+"']").show();
				
			});			
		}
	};
	context.nav = {
		init: function() {
			var self = this;			
				self.check_position_fixed();
				self.capture_nav_clicks();
				self.watch_scroll();				
			
		},
		check_position_fixed: function() { 
					
			var container = document.body;
			
			if (document.createElement && container && container.appendChild && container.removeChild) {
				var el = document.createElement('div');
				
				if (!el.getBoundingClientRect) return null;				
				
				el.innerHTML = 'x';
				el.style.cssText = 'position:fixed;top:100px;';
				container.appendChild(el);
			
				var originalHeight = container.style.height,
					originalScrollTop = container.scrollTop;
			
				container.style.height = '3000px';
				container.scrollTop = 500;
			
				var elementTop = el.getBoundingClientRect().top;
				container.style.height = originalHeight;
				
				var isSupported = (elementTop === 100);
				container.removeChild(el);
				container.scrollTop = originalScrollTop;
			
				context.settings.fixed_position_supported = isSupported;
			}			
		},
		capture_nav_clicks: function() {
			var self = this;
			
			jQuery("nav a.internal, .logo a, section a.internal").click(function(event) {
				event.preventDefault();		
				var href = jQuery(this).attr("href");
				var id = href.split("#");
				
				self.slide_page(id[1]);
				self.update_location(id[1]);		
			});			
			
		},
		watch_scroll: function() {	
		
			if (context.settings.fixed_position_supported) {
				var self = this;
				
				var possibles = jQuery("nav a.internal");
				var sections = [];
				
				jQuery(possibles).each(function() {
					var section = {};
					
					var href = jQuery(this).attr("href");
			
					// strip stuff before hash
					var href = href.split("#");
					
					section.id = href[1];
					section.offset = jQuery("#" + section.id).offset();
					
					sections.push(section);
				});
				
				var buffer = jQuery(window).height() / 3;
	
				jQuery(document).everyTime(100, "scroll", function() {
					
					var scroll_top = jQuery(context.settings.frame).scrollTop();
					var target;
					var last = jQuery(sections).size() - 1;
					
					jQuery(sections).each(function(index, section) {
						// If we've scrolled past the beginning of the zone
						if ( scroll_top > (sections[index].offset.top - buffer) ) {
							// But not into the next zone
							if (
								scroll_top > (sections[last].offset.top - buffer)
								||
								scroll_top < (sections[index + 1].offset.top - buffer)
							) 
							{
								target = jQuery("nav a:eq(" + index + ")");
							}
						}
					});
					
					self.set_active(target);
				});
			}
		},
		set_active: function(el) {
			jQuery("nav .active").removeClass("active");
			jQuery(el).addClass("active");
		},
		slide_page: function(target) {
			var container_offset = jQuery("#" + target).offset();
			var true_offset = container_offset.top - 52;
			
			jQuery(context.settings.frame).animate({scrollTop: true_offset}, 1000);
		},
		update_location: function(frag) {
			var hist_api_supported = !!(window.history && history.pushState);
			if (frag != undefined) {
				if (hist_api_supported) {
					history.pushState(null, "", "#" + frag);
				}
				else {
					//window.location.href = "#" + frag;
				}
			}
		}
	};
})(OL);
