// 
//  crown-homepage.js
//  Crown Homepage JavaScript
//  
//  Created by Victor Nguyen on 2009-08-12.
//  Copyright 2009 igloo digital. All rights reserved.
// 

// Homepage-specific functionality
Crown.Homepage = {
	
	init: function () {
		this.BookRestaurant.init();
		this.BookShow.init();
		this.BookPackage.init();
		this.Carousel.init();
		this.AudioPlayer.init();
		Crown.Common.HotelBooking.init();
	},
	
	BookRestaurant: {
		LIST_ID: 'home_form_restaurant',
		DETAILS_ID: 'home_bookings_restaurant_info',
		AJAX_URL: '/ajax/home-restaurant/', // restaurant id from selected option[value] gets appended to this below
		
		ActiveRequest: null,
		
		init: function () {
			$('#'+this.LIST_ID).change(function(){
				var restaurant_param = this.value;
				if (Crown.Homepage.BookRestaurant.ActiveRequest) { Crown.Homepage.BookRestaurant.ActiveRequest.abort(); }
				Crown.Homepage.BookRestaurant.ActiveRequest = $.ajax({
				  url: Crown.Homepage.BookRestaurant.AJAX_URL + restaurant_param,
				  cache: false,
				  success: function(html){
				    $('#' + Crown.Homepage.BookRestaurant.DETAILS_ID).replaceWith(html);
				  }
				});
			});
		}
	},
	
	BookShow: {
		LIST_ID: 'home_form_show',
		DETAILS_ID: 'home_bookings_show_info',
		AJAX_URL: '/ajax/home-show/', // show id from selected option[value] gets appended to this below
		
		ActiveRequest: null,
		
		init: function () {
			$('#'+this.LIST_ID).change(function(){
				var show_param = this.value;
				var replace_me = $('#' + Crown.Homepage.BookShow.DETAILS_ID);
				if (Crown.Homepage.BookShow.ActiveRequest) { Crown.Homepage.BookShow.ActiveRequest.abort(); }
				Crown.Homepage.BookShow.ActiveRequest = $.ajax({
				  url: Crown.Homepage.BookShow.AJAX_URL + show_param,
				  cache: false,
				  success: function(html){
				    replace_me.replaceWith(html);
				  }
				});
			});
		}
	},

	BookPackage: {
		LIST_ID: 'home_form_package',
		DETAILS_ID: 'home_bookings_package_info',
		AJAX_URL: '/ajax/home-package/', // show id from selected option[value] gets appended to this below

		ActiveRequest: null,

		init: function()
		{
			$('#' + this.LIST_ID).change(function(){
				var show_param = this.value;
				var replace_me = $('#' + Crown.Homepage.BookPackage.DETAILS_ID);
				if (Crown.Homepage.BookPackage.ActiveRequest) { Crown.Homepage.BookPackage.ActiveRequest.abort(); }
				Crown.Homepage.BookPackage.ActiveRequest = $.ajax({
					url: Crown.Homepage.BookPackage.AJAX_URL + show_param,
					cache: false,
					success: function(html){
						replace_me.replaceWith(html);
					}
				});
			});
		}
	},
	
	Carousel: {
		
		XML_PATH: '/xml/home',
		// XML_PATH: '/library/flash/homepage-carousel-test.xml', // test XML schema
		WHATSON_PATH: '/whats-on/',
		SWF_PATH: '/library/flash/homepage-carousel.swf',
		EMBED_ID: 'home_flash',
	
		init: function () {

			var flashvars = {
				'domain': Crown.Homepage.Carousel.XML_PATH,
				'eventsUrl': Crown.Homepage.Carousel.WHATSON_PATH
			};

			var params = {
				allowScriptAccess: "always",
				wmode: 'opaque'
			};

			var attributes = {
				
			};

			swfobject.embedSWF(Crown.Homepage.Carousel.SWF_PATH, Crown.Homepage.Carousel.EMBED_ID, "958", "391", Crown.Common.Flash.MIN_VERSION, Crown.Common.Flash.EXPRESSINSTALL_PATH,  flashvars, params, attributes, Crown.Common.Flash.embedCallBack);
		}
		
	},
	
	AudioPlayer: {
		
		SWF_PATH: '/library/flash/audioplayer/sound.swf',
		MP3_PATH: '/library/audio/fusion3.mp3',
		EMBED_ID: 'home_audio_player',
		
		init: function () {
			
			if ($('#' + Crown.Homepage.AudioPlayer.EMBED_ID).length == 0) { return; }
			
			var flashvars = {
				mp3file: Crown.Homepage.AudioPlayer.MP3_PATH,
				autoStart: Crown.Homepage.AudioPlayer._setAutoplayFromCookie()
			};

			var params = {
				allowScriptAccess: "always",
				wmode: 'opaque'
			};

			var attributes = {
				
			};

			swfobject.embedSWF(Crown.Homepage.AudioPlayer.SWF_PATH, Crown.Homepage.AudioPlayer.EMBED_ID, "88", "19", Crown.Common.Flash.MIN_VERSION, Crown.Common.Flash.EXPRESSINSTALL_PATH,  flashvars, params, attributes);
			
		},
		
		_setAutoplayFromCookie: function () {
			if (!document.cookie) {
				return 'true';
			} else {
				return Crown.Common.Cookies.read('autoplay');
			}
		},
		
		// Called by Flash when user toggles sound, passes 'on' or 'off' as a String
		toggleSoundCookie: function (onOff) {
			var autoplay = (onOff == 'off') ? 'false': 'true';
			Crown.Common.Cookies.create('autoplay', autoplay, 365);
		},
		
		// functions to control Audio Player, not currently used
		playSoundJS: function (){
			document.getElementById(Crown.Homepage.AudioPlayer.EMBED_ID).playStopSoundJS('on');
		},

		stopSoundJS: function (){
			document.getElementById(Crown.Homepage.AudioPlayer.EMBED_ID).playStopSoundJS('off');
		}
		
	}
	
};


// on domready
$(document).ready(function(){
	Crown.Homepage.init();
});