/***************************/
/***************************/
/*						   */
/*   Reservation widget    */
/*						   */
/***************************/
/***************************/
// Given a dictionary (input), convert each element into an <option> and replace it with the existing ones in output
jQuery.fn.optionify = function(input, type) {
	// Find the output element (ie where the options are going)
	var output = $(this[0]);

	// Remove all the options from the output select
	$(output).find("option").remove();

	// Take the input array, make each element into an option, then replace with output's options
	var i = 0;
	$.each(input, function(value, label) {
		$(output).append(
		$('<option></option>').val(value).html(label)
	);
		i++;
	});

	// If input has more than one element, put the All option back in the output select
	if (i > 1) {
		switch (type) {
			case 'provinces':
				var text = '- Province -';
				break;
			case 'cities':
				var text = '- Destination -';
				break;
			case 'hotels':
				var text = '- Hotels & Resorts -';
				break;
		}
		$(output).prepend($('<option></option>').val('All').html(text).attr('selected','selected'));
	}
};

// Given an arrival date, set the departure date and set datepicker2
jQuery.fn.changeDepartureDate = function(arrivalDate) {
	// Find the output element
	var output = $(this[0]);

	// Arrival date should be an array = [month, day, year] = [1/31/2009]
	var date = arrivalDate;
	var month = parseInt(date[0])-1; // Month needs to be adjusted
	var a = new Date(date[2],month,date[1]);
	var d = new Date(date[2],month,date[1]);
	d.setDate(a.getDate()+1); // Add one day to departure
	var newMonth = parseInt(d.getMonth())+1; // Month needs to be re-adjusted

	// Change the departure selects
	$("#res-date-month2 > option[value="+ newMonth +"]").attr('selected','selected');
	$("#res-date-day2 > option[value="+ d.getDate() +"]").attr('selected','selected');
	$("#res-date-year2 > option[value="+ d.getFullYear() +"]").attr('selected','selected');

	// Change datepicker2
	$(output).val(newMonth+'/'+d.getDate()+'/'+d.getFullYear());
}

$(function(){
	// Datepicker
	$("#datepicker").datepicker({
		dateFormat: 'm/d/yy',
		showOn: 'both',
		buttonImage: 'assets/images/icons/icon_calendar.gif',
		buttonImageOnly: true,
		onSelect: function() {
			var date = document.getElementById('datepicker').value.split('/',3);
			$("#res-date-month > option[value="+ date[0] +"]").attr('selected','selected');
			$("#res-date-day > option[value="+ date[1] +"]").attr('selected','selected');
			$("#res-date-year > option[value="+ date[2] +"]").attr('selected','selected');

			// Change departure date (datepicker2)
			$("#datepicker2").changeDepartureDate([date[0], date[1], date[2]]);
		}
	});
	$("#datepicker2").datepicker({
		dateFormat: 'm/d/yy',
		showOn: 'both',
		buttonImage: 'assets/images/icons/icon_calendar.gif',
		buttonImageOnly: true,
		onSelect: function() {
			var date = document.getElementById('datepicker2').value.split('/',3);
			$("#res-date-month2 > option[value="+ date[0] +"]").attr('selected','selected');
			$("#res-date-day2 > option[value="+ date[1] +"]").attr('selected','selected');
			$("#res-date-year2 > option[value="+ date[2] +"]").attr('selected','selected');
		}
	});

	// If change manual select boxes, update #datepicker's
	$("select[id^=res-date]:not(select[id$=2])").change(function() {
		var date =	$("select#res-date-month").val()
			+'/'+	$("select#res-date-day").val()
			+'/'+	$("select#res-date-year").val();
		$("#datepicker").val(date);

		// Change departure date (datepicker2)
		$("#datepicker2").changeDepartureDate([$("select#res-date-month").val(), $("select#res-date-day").val(), $("select#res-date-year").val()]);
	});
	$("select[id^=res-date][id$=2]").change(function() {
		var date =	$("select#res-date-month2").val()
			+'/'+	$("select#res-date-day2").val()
			+'/'+	$("select#res-date-year2").val();
		$("#datepicker2").val(date);
	});

	// Prepare the arrays for the reservation widget
	var provinces = [];
	var cities = [];
	var hotels = [];

	// Provinces
	provinces['Atlantic'] = {NB:'New Brunswick',NL:'Newfoundland',NS:'Nova Scotia',PE:'Prince Edward Island'};
	provinces['Central'] = {ON:'Ontario',QC:'Quebec'};
	provinces['Western'] = {BC:'British Columbia',AB:'Alberta',SK:'Saskatchewan',MB:'Manitoba'};
	provinces['All'] = {AB:'Alberta',BC:'British Columbia',MB:'Manitoba',NB:'New Brunswick',NL:'Newfoundland',NS:'Nova Scotia',ON:'Ontario',PE:'Prince Edward Island',QC:'Quebec',SK:'Saskatchewan'};

	// Cities
	cities['ON'] = {Guelph:'Guelph, ON', 'Kitchener-Waterloo':'Kitchener-Waterloo, ON', London:'London, ON', Ottawa:'Ottawa, ON', 'Sault Ste. Marie':'Sault Ste. Marie, ON', Toronto:'Toronto, ON'};
	cities['AB'] = {'Alberta Resorts':'Alberta Resorts', 'Calgary':'Calgary, AB', 'Edmonton':'Edmonton, AB'};
	cities['BC'] = {'BC Resorts':'BC Resorts','Burnaby':'Burnaby, BC','Vancouver':'Vancouver, BC','Victoria':'Victoria, BC'};
	cities['MB'] = {'Winnipeg':'Winnipeg, MB'};
	cities['SK'] = {'Regina':'Regina, SK','Winnipeg':'Winnipeg, SK'};
	cities['NB'] = {'Fredericton':'Fredericton, NB','Moncton':'Moncton, NB','Saint John':'Saint John, NB'};
	cities['NL'] = {'St. John\'s':'St. John\'s, NL'};
	cities['NS'] = {'Halifax':'Halifax, NS','Sydney':'Sydney, NS'};
	cities['PE'] = {'Charlottetown':'Charlottetown, PE'};
	cities['QC'] = {'Montreal':'Montreal, QC','Quebec City':'Quebec City, QC','Sherbrooke':'Sherbrooke, QC','Trois-Rivieres':'Trois-Rivieres, QC'};
	cities['All'] = {'Alberta Resorts':'Alberta Resorts', 'BC Resorts':'BC Resorts', 'Ontario Resorts':'Ontario Resorts', 'Banff':'Banff, AB', 'Burnaby':'Burnaby, BC', 'Calgary':'Calgary, AB', 'Edmonton':'Edmonton, AB', 'Fredericton':'Fredericton, NB', 'Guelph':'Guelph, ON', 'Halifax':'Halifax, NS', 'Kananaskis':'Kananaskis, AB', 'Kelowna':'Kelowna, BC', 'Kitchener-Waterloo':'Kitchener-Waterloo, ON', 'London':'London, ON', 'Moncton':'Moncton, NB', 'Montreal':'Montreal, QC', 'Muskoka':'Muskoka, ON', 'Ottawa':'Ottawa, ON', 'Charlottetown':'Charlottetown, PE', 'Quebec City':'Quebec City, QC', 'Regina':'Regina, SK', 'Saint John':'Saint John, NB', 'Saskatoon':'Saskatoon, SK', 'Sherbrooke':'Sherbrooke, QC', 'St. John\'s':'St. John\'s, NL', 'Sun Peaks':'Sun Peaks, BC', 'Sydney':'Sydney, NS', 'Toronto':'Toronto, ON', 'Trois-Rivieres':'Trois-Rivieres, QC', 'Vancouver':'Vancouver, BC', 'Victoria':'Victoria, BC', 'Whistler':'Whistler, BC', 'Winnipeg':'Winnipeg, MB'};

	// Hotels
	hotels['Alberta Resorts'] = {'BAN':'Delta Banff Royal Canadian Lodge','KAN':'Delta Lodge at Kananaskis'};
	hotels['BC Resorts'] = {'DGO':'Delta Grand Okanagan Resort and Conference Centre','SUR':'Delta Residences at Sun Peaks','SUN':'Delta Sun Peaks Resort','VIC':'Delta Victoria Ocean Pointe Resort and Spa','DWV':'Delta Whistler Village Suites','DGR':'Royal Private Residence Club at Delta Grand Okanagan Resort','TAN':'Tantalus Resort Lodge'};
	hotels['Ontario Resorts'] = {'GRA':'Delta Grandview Resort','ROC':'Delta Rocky Crest Resort','SHI':'Delta Sherwood Inn'};
	hotels['Banff'] = {'BAN':'Delta Banff Royal Canadian Lodge'};
	hotels['Burnaby'] = {'BUR':'Delta Burnaby Hotel and Conference Centre'};
	hotels['Calgary'] = {'BOW':'Delta Bow Valley','DCA':'Delta Calgary Airport','DCS':'Delta Calgary South'};
	hotels['Charlottetown'] = {'PRI':'Delta Prince Edward'};
	hotels['Edmonton'] = {'DEC':'Delta Edmonton Centre Suite Hotel', 'DES':'Delta Edmonton South Hotel and Conference Centre'};
	hotels['Fredericton'] = {'FRE':'Delta Fredericton'};
	hotels['Guelph'] = {'DGU':'Delta Guelph Hotel and Conference Centre'};
	hotels['Halifax'] = {'BAR':'Delta Barrington','HAL':'Delta Halifax'};
	hotels['Kananaskis'] = {'KAN':'Delta Lodge at Kananaskis'};
	hotels['Kelowna'] = {'DGO':'Delta Grand Okanagan Resort and Conference Centre','DGR':'Royal Private Residence Club at Delta Grand Okanagan Resort'};
	hotels['Kitchener-Waterloo'] = {'KIT':'Delta Kitchener-Waterloo'};
	hotels['London'] = {'LON':'Delta London Armouries'};
	hotels['Moncton'] = {'BEA':'Delta Beausejour'};
	hotels['Montreal'] = {'DCV':'Delta Centre-Ville','MON':'Delta Montreal'};
	hotels['Muskoka'] = {'GRA':'Delta Grandview Resort','ROC':'Delta Rocky Crest Resort','SHI':'Delta Sherwood Inn'};
	hotels['Ottawa'] = {'OTT':'Delta Ottawa Hotel and Suites'};
	hotels['Quebec City'] = {'QUE':'Delta Quebec'};
	hotels['Regina'] = {'REG':'Delta Regina'};
	hotels['St. John\'s'] = {'BRU':'Delta Brunswick'};
	hotels['Saskatoon'] = {'BES':'Delta Bessborough'};
	hotels['Sault Ste. Marie'] = {'SSM':'Delta Sault Ste. Marie'};
	hotels['Sherbrooke'] = {'SHE':'Delta Sherbrooke Hotel and Conference Centre'};
	hotels['St. John\'s'] = {'STJ':'Delta St. John\'s Hotel and Conference Centre'};
	hotels['Sun Peaks'] = {'SUR':'Delta Residences at Sun Peaks','SUN':'Delta Sun Peaks Resort'};
	hotels['Sydney'] = {'SYD':'Delta Sydney'};
	hotels['Toronto'] = {'CHE':'Delta Chelsea Hotel','MRK':'Delta Markham','MEA':'Delta Meadowvale Resort and Conference Centre','TAW':'Delta Toronto Airport West', 'DTE':'Delta Toronto East'};
	hotels['Trois-Rivieres'] = {'TRO':'Delta Trois-Rivieres Hotel and Conference Centre'};
	hotels['Vancouver'] = {'DVA':'Delta Vancouver Airport', 'DVS':'Delta Vancouver Suites'};
	hotels['Victoria'] = {'VIC':'Delta Victoria Ocean Pointe Resort and Spa'};
	hotels['Whistler'] = {'DWV':'Delta Whistler Village Suites','TAN':'Tantalus Resort Lodge'};
	hotels['Winnipeg'] = {'WIN':'Delta Winnipeg'};
	hotels['All'] = {'BAN':'Delta Banff Royal Canadian Lodge', 'BAR':'Delta Barrington', 'BEA':'Delta Beausejour', 'BES':'Delta Bessborough', 'BOW':'Delta Bow Valley', 'BRU':'Delta Brunswick', 'BUR':'Delta Burnaby Hotel and Conference Centre', 'DCA':'Delta Calgary Airport', 'DCS':'Delta Calgary South', 'DCV':'Delta Centre-Ville', 'CHE':'Delta Chelsea Hotel', 'DEC':'Delta Edmonton Centre Suite Hotel', 'DES':'Delta Edmonton South Hotel and Conference Centre', 'FRE':'Delta Fredericton', 'DGO':'Delta Grand Okanagan Resort and Conference Centre', 'GRA':'Delta Grandview Resort', 'DGU':'Delta Guelph Hotel and Conference Centre', 'HAL':'Delta Halifax', 'KIT':'Delta Kitchener-Waterloo', 'KAN':'Delta Lodge at Kananaskis', 'LON':'Delta London Armouries', 'MRK':'Delta Markham', 'MEA':'Delta Meadowvale Resort and Conference Centre', 'MON':'Delta Montreal', 'OTT':'Delta Ottawa Hotel and Suites', 'PRI':'Delta Prince Edward', 'QUE':'Delta Quebec', 'REG':'Delta Regina', 'SUR':'Delta Residences at Sun Peaks', 'ROC':'Delta Rocky Crest Resort', 'SHE':'Delta Sherbrooke Hotel and Conference Centre', 'SHI':'Delta Sherwood Inn', 'STJ':'Delta St. John\'s Hotel and Conference Centre', 'SUN':'Delta Sun Peaks Resort', 'SYD':'Delta Sydney', 'TAW':'Delta Toronto Airport West', 'DTE':'Delta Toronto East', 'TRO':'Delta Trois-Rivieres Hotel and Conference Centre', 'DVA':'Delta Vancouver Airport', 'DVS':'Delta Vancouver Suites', 'VIC':'Delta Victoria Ocean Pointe Resort and Spa', 'DWV':'Delta Whistler Village Suites', 'WIN':'Delta Winnipeg', 'DGR':'Royal Private Residence Club at Delta Grand Okanagan Resort', 'TAN':'Tantalus Resort Lodge'};

	// Picked a region
	$("#res-region").change(function () {
		// Region has been picked, now update #res-province accordingly
		$("#res-province").optionify(provinces[$(this).val()], 'provinces');

		// Also update the others: city, hotel
		$("#res-city").optionify(cities['All'], 'cities');
		$("#res-hotel").optionify(hotels['All'], 'hotels');
	});

	// Picked a province
	$("#res-province").change(function () {
		// Province has been picked, now update #res-city accordingly
		$("#res-city").optionify(cities[$(this).val()], 'cities');

		// Find what city was chosen so we can update hotels
		var city = $("#res-city").val();

		// Also update the others: hotel
		$("#res-hotel").optionify(hotels[city], 'hotels');
	});

	// Picked a city
	$("#res-city").change(function () {
		// City has been picked, now update #res-hotel accordingly
		$("#res-hotel").optionify(hotels[$(this).val()], 'hotels');
	});
});

/***************************/
/***************************/
/*						   */
/*   Show amenities        */
/*	 ie8+/ff only		   */
/***************************/
/***************************/
$(function(){
	// Unless browser is IE and < 8.0
	if (!($.browser.msie && $.browser.version < 8.0)) {
		// Wrap a div around all those list items
		$("li.show-amenities ~ li").wrapAll('<div>');

		// Hide that div
		$("li.show-amenities + div").hide();

		// Find whether this page is English or French
		if (window.location.pathname.match(/\/fr\//)) {
			// French
			var words = ['Plus de détails','Moins de détails'];
		} else {
			// English
			var words = ['Show more','Show less'];
		}

		// Add the 'Show all' link and show its list item
		$("li.show-amenities").html('<a href="javascript:return false" class="show-amenities">'+words[0]+'</a>').css('display','block');

		// Bind toggle event to a.show-amenities
		$("a.show-amenities").toggle(
			// Show the div and change anchor to 'Show less'
			function() {
				$("li.show-amenities + div").slideDown('fast');
				$(this).html(words[1]);
			},

			// Hide the div and change anchor to 'Show more'
			function() {
				$("li.show-amenities + div").slideUp();
				$(this).html(words[0]);
			}
		);
	}
});

/***************************/
/***************************/
/*						   */
/*   Equal heights         */
/*						   */
/***************************/
/***************************/
// Given an array of selectors/elements, find the max height, then set all elements to max height
function equalHeights(selectors) {
	// Find the heights of all the columns
	var col_heights = [];
	for (selector in selectors) { col_heights.push( $(selectors[selector]).height() ); }

	// Find the maximum height
	var col_max_height = 0;
	for (height in col_heights) {
		if (col_heights[height] > col_max_height) { col_max_height = col_heights[height]; }
	}

	// Set the height of all columns
	for (selector in selectors) { $(selectors[selector]).css('min-height',col_max_height); }
}

$(function(){ equalHeights(['div.sidebar:first', 'div#content', 'div.sidebar:last']); });

