/*--------------------------------------------------------------------------*
 * 
 * Copyright (C) 2009-2010 Brand Labs LLC
 * 
 *--------------------------------------------------------------------------*/

var CruiseHome = Class.create({
	LINE_FIELD_ID: 'cruise_select_line',
	SHIP_FIELD_ID: 'cruise_select_ship',
	ARRIVAL_FIELD_ID: 'cruise_select_arrival',
	NIGHTS_FIELD_ID: 'cruise_select_nights',
	NIGHTS_FIELD_HIDDEN_ID: 'cruise_select_arrival_hidden',
	FORM_ID: 'cruise_select_form',
	
	initialize: function() {
		this.selection = null;	
		this.arrivalElement = null;
		this.formElement = null;
		this.arrivalHiddenElement = null;
		
		//Start once loaded
		Event.observe(window, 'load', this.load.bind(this));
	},
	
	load: function() {
		var clazz = null;
		var thisObj = this;
		
		try {
			//Get the elements
			this.arrivalElement = $(this.ARRIVAL_FIELD_ID);
			this.formElement = $(this.FORM_ID);
			this.arrivalHiddenElement = $(this.NIGHTS_FIELD_HIDDEN_ID);
			
			//Create sub-class of itinerary selection
			clazz = Class.create(CruiseItinerarySelection, {
				change: function($super){
					thisObj.updateDate();
					$super();
				}
			});
			
			//Create the itinerary selection object
			this.selection = new clazz($(this.LINE_FIELD_ID), $(this.SHIP_FIELD_ID), this.arrivalElement, $(this.NIGHTS_FIELD_ID), {alternateLines: false});
					
			//Force the update of the date
			this.updateDate();
		}
		catch(e){/*Ignore*/}	
	},
	
	updateDate: function() {
		var date = null;
		
		try {
			date = new Date(this.arrivalElement.getValue());
			
			this.arrivalHiddenElement.setValue(
				this._pad(date.getFullYear(), 4, '0') + '-' + this._pad(date.getMonth()+1, 2, '0') + '-' + this._pad(date.getDate(), 2, '0')				
			);
		}
		catch(e){/*Ignore*/}		
	},
	
	_pad: function(value, length, character) {
		value = new String(value);
		
		while(value.length < length) {
			value = character + value;		
		}
		
		return value;
	}
});

try {
	new CruiseHome();
}
catch(e){/*Ignore*/}
