$(document).ready(function(){
	
    Site.init();
			
});

var Site = {
	
	init:function() {	
		// HOMEPAGE SLIDER
		if ($('#feature-slider').length != 0){
		
			Site.homeFeatureSlider();
			
		}		
		
		//HANDLE EXTERNAL LINKS
		Site.externalLinks();
		
		//TEXT RESIZZLE
		if($('#usability-controls').length != 0) {
			$('#primary-inner').textResizzle();	
		}
		
		//PRINT
		if($('#print').length != 0) {
			Site.print();
		}

		// Hotel Landing Page
		if ( $('#hotel-hover-area').length != 0 && $('#hotel-buttons').length != 0 ){
			// Trigger popups when hovering over "hover area" and button
			$('#hotel-buttons a').each(function(i){
				i = i+1;
				var _hoverIn = function(){
					$('#hotel-'+i).find('.hotel-popup').show();
				};
				var _hoverOut = function(){
					$('#hotel-'+i).find('.hotel-popup').hide();
				};
				
				$(this).hover( _hoverIn, _hoverOut );
				$('#hotel-'+i).find('a').hover( _hoverIn, _hoverOut );
			});
		}
		
		if($('#hotel-images').length != 0) {
			Site.hotelImageFader();
		}
		
		if($('#about-motto-slider').length != 0) {
			Site.aboutMottoSlider();
		}
		
		// CONTACT MAP
		if ($('#contact-map').length != 0){
			Site.contactMapInitialise();
		}
		
		
	},
	
	homeFeatureSlider: function(){
	
		/*
			There are two sliders, one for the content and one for the background images.
		*/
		
		// Set to window resize so that it resizes (and re-centres) when the window resizes
		$(window).resize( function(){
		
			// Content slider
			contentSlider = $('#feature-slider .content-slides').carouFredSel({
				width: $('#feature-slider').innerWidth(),
				scroll: {
					duration: 1200,
					onBefore: function (oldItems, newItems, newSizes, duration){
						// Show scripty title of next slide before sliding it in
						newItems.find('#scripty-title').show();
					},
					onAfter: function (oldItems, newItems, newSizes, duration){
						// Hide scripty title of slide that has just left the screen
						oldItems.find('#scripty-title').hide();
					}
				},
				items: {
					visible: 1
				},
				auto: 10000,
				onCreate: function (visibleItems, size){
					// Hide scripty titles because the leading tail shows up on the right from the next slide offscreen.
					// Has to be done this way with each()/find() for some reason because ie7 wont hide them otherwise.
					$('#feature-slider .content-slides .slide').each(function (){
						$(this).find('#scripty-title').hide();
					});
					
					visibleItems.find('#scripty-title').show();	// show scripty title of current slide
				},
				synchronise: ['#feature-slider .backgrounds', false, true, 0],
				pagination: {
					container: '#feature-slider .pager',
					anchorBuilder: function (num, slide){
						var slide_id = slide.attr('id');
						var tooltip = $('#feature-slider .pager-tips li:nth-child('+num+')').html();
						var link = '<a href="#'+slide.attr('id')+'"><span class="pager-tooltip"><span>'+tooltip+'</span></span></a>';
						return link;
					}
				}
			})
			
			// contentSlider.trigger('linkAnchors', ['#feature-slider .pager', 'a']); // Join up the pager elements
			
			// Background image slider
			bgSlider = $('#feature-slider .backgrounds').carouFredSel({
				width: $('#feature-slider').innerWidth(),
				scroll: {
					duration: 800,
					easing: 'easeInQuad'
				},
				items: {
					visible: 1
				},
				auto: false
			});
		
		}).resize();
		
	},
	
	hotelImageFader:function() {
		
		$('#hotel-images ul').carouFredSel({
			scroll: {
				fx			: "crossfade",
				easing		: "linear",
				duration	: 500
			},
			items: {
				visible: 1
			},
			prev: '#prev-hotel',
			next: '#next-hotel',
			auto: 8000
		});

	},
	
	aboutMottoSlider:function() {
		
		$('#about-motto-slider ul').carouFredSel({
			scroll: {
				fx			: "slide",
				easing		: "linear",
				duration	: 500
			},
			items: {
				visible: 1
			},
			prev: '#prev-hotel',
			next: '#next-hotel',
			auto: 8000,
			pagination  : "#aboutslide-pag"

		});

	},

	print:function() {
		
		$('#print').click(function(){
			window.print();
		});
		
	},
	
	externalLinks:function() {

		var extLinks = $('a.external');
			if ( extLinks.length ) {
				extLinks.each(function() { 
					$(this).attr('target', '_blank');
			});
		}
	
	},
	
	contactMapInitialise: function(){
		var map_latlng = new google.maps.LatLng(-37.81260,144.9655);
		var myOptions = {
			zoom: 16,
			center: map_latlng,
			mapTypeId: google.maps.MapTypeId.ROADMAP,
			disableDefaultUI: true,
			scrollwheel: false,
			draggable: false,
			scaleControl: false,
			zoomControl: false,
			disableDoubleClickZoom: true,
			styles: [{
				stylers: [
					{ gamma: 0.88 },
					{ visibility: "on" },
					{ lightness: -11 },
					{ hue: "#00aaff" },
					{ saturation: -77 }
				]
			}]
		};
		var map = new google.maps.Map(document.getElementById("contact-map"), myOptions);
		
		var marker_image = '/assets/second-level/contact/map-marker-purple.png';
		var marker_latlng = new google.maps.LatLng(-37.81305,144.96515);	// The Swanston
		var beachMarker = new google.maps.Marker({
			position: marker_latlng,
			map: map,
			icon: marker_image
		});
	}

};
