/*
	
	Globals

*/
var bp_path = '/apps/booking_panel/';

/*
	bp_site
	- defines which site the user is one
*/
if (typeof(bp_site) == 'undefined') {
	var bp_site = 'vb';
}
/*
	bp_engine
	- defines which engine the booking panel is using. We need this as a separate
	variable because you could be on the Virgin Blue site, but because you've selected a V Australia destination
	the booking panel will be using the V Australia engine and rules
*/
if (typeof(bp_engine) == 'undefined') {
	var bp_engine = 'va';
}
/*
	
	Preferences
	
*/
var bp_prefs = {
	'vb'  : {
		'maxPax': 20,
		'resNum': '3295 3000'
	},
	'va'  : {
		'maxPax': 9
	}
};
/*
	
	Errors
	
*/
var bp_errors_val;
var vb_errors = {
	'missingDepartCity'	: 'Please select the city you will be departing from',
	'missingArriveCity'	: 'Please choose your destination.',
	'maxpax'			: 'You are allowed a maximum of '+bp_prefs.vb.maxPax+' passengers per booking online. If your party is larger than this, please call our reservation center on '+bp_prefs.vb.resNum+'.',
	'infants'			: 'You are unable to book a greater number of Infants than Adults',
	'dateDiff'			: 'Your return date is earlier than your departing date',
	'departdateEarly'	: 'Your departing date is earlier than today',
	'returndateEarly'	: 'Your return date is earlier than today',
	'departdateEarlyb'	: 'Flights commence 27 February 2009. Please select a departing date on or after this date',
	'returndateEarlyb'	: 'Flights commence 27 February 2009. Please select a return date date on or after this date',
	'monthDaysDepart'	: 'You\'ve selected an invalid date, please try again. Why not try clicking the calendar icon?',
	'monthDaysReturn'	: 'You\'ve selected an invalid date, please try again. Why not try clicking the calendar icon?',
	'tipDate'			: 'Tip: Use the calendar icon to select your days. V Australia commences daily flights from March 20! ',
	'departVADate'		: 'Please select a new Departing Date. Flights operate from Australia on Wednesdays, Fridays and Sundays.',
	'returnVADate'		: 'Please select a new Return Date. Flights operate from USA on Mondays, Wednesdays and Fridays.',
	'departVADateUS'	: 'Please select a new Departing Date. Flights operate from the USA on Mondays, Wednesdays and Fridays.',
	'returnVADateUS'	: 'Please select a new Return Date. Flights operate from Australia on Wednesdays, Fridays and Sundays.',
	'badHKTDate'      : 'Our flights to Phuket start operating on 22 November 2009. Please select a date for travel after this date.',
	'badJNBDate'      : 'Our flights to Johannesburg start operating on 13 March 2010. Please select a date for travel after this date',
	'badPERHKTDate'		:	'Flights from Perth to Phuket start operating on 14 November 2009. Please select a date for travel after this date.',
	'HKTReturnOnly'		:	'These flights are return only.'
};
var va_errors = {
	'missingDepartCity'	: 'Please select the city you will be departing from',
	'missingArriveCity'	: 'Please choose your destination.',
	'maxpax'			: 'You are allowed a maximum of '+bp_prefs.va.maxPax+' passengers per booking online. If your party is larger than this, please call our reservation center, Australia - 13 V AUS (13 82 87), United States of America - 1-800 444 0260',
	'infants'			: 'You are unable to book a greater number of Infants than Adults',
	'dateDiff'			: 'Your return date is earlier than your departing date',
	'departdateEarly'	: 'Your departing date is earlier than today',
	'returndateEarly'	: 'Your return date is earlier than today',
	'departdateEarlyb'	: 'Flights commence 27 February 2009. Please select a departing date on or after this date',
	'returndateEarlyb'	: 'Flights commence 27 February 2009. Please select a return date date on or after this date',
	'monthDaysDepart'	: 'You\'ve selected an invalid date, please try again. Why not try clicking the calendar icon?',
	'monthDaysReturn'	: 'You\'ve selected an invalid date, please try again. Why not try clicking the calendar icon?',
	'tipDate'			: 'Tip: Use the calendar icon to select your days. ',
	'departVADate'		: 'Please select a new Departing Date. Flights operate from Australia on Wednesdays, Fridays and Sundays.',
	'returnVADate'		: 'Please select a new Return Date. Flights operate from USA on Mondays, Wednesdays and Fridays.',
	'departVADateUS'	: 'Please select a new Departing Date. Flights operate from the USA on Mondays, Wednesdays and Fridays.',
	'returnVADateUS'	: 'Please select a new Return Date. Flights operate from Australia on Wednesdays, Fridays and Sundays.',
	'badHKTDate'		:	'Our flights to Phuket start operating on 22 November 2009. Please select a date for travel after this date.',
	'badJNBDate'		: 'Our flights to/from Johannesburg start operating on 13 March 2010. Please select a date for travel after this date',
	'badPERHKTDate'   	: 'Flights from Perth to Phuket start operating on 14 November 2009. Please select a date for travel after this date',
	'HKTReturnOnly'		: 'These flights are return only.'
};
/*
	
	Dom Ready Instructions
	
*/
//Event.observe(window, 'load',  function() {   
document.observe('dom:loaded', function() {
	
	// new booking engine instance
	if($('booking-panel')) {
		if (typeof(bp_filter) == 'undefined') {
			bp_filter = false;
		}
		vb_bp = new BookingPanel($('booking-panel'), 
			{ 
				filter_search : bp_filter,
				show_countries 	: true, 
				test_server		: true,
				countries 		: bp_countries, 
				cities 			: bp_cities
			}
		);
		// create date pickers for booking panel
		cal_objs = new Array();
		init_DatePickers(cal_objs, 'flights');
		
		// in case one way is checked
		vb_bp.setTripType();
	}
	if (typeof(bp_site) != 'undefined') {
		if (bp_site == 'vb' && $('bp-tabs')) {
			bp_tabs = new CompactTabs();
		}
	}
//});
}); 
/*

	Date Pickers aren't bound to the booking panel class
	they're merely helpers to pick dates and are therefore
	separate
	
*/


function init_DatePickers (cal_objs, mode) {
	$$('.calendar').each(function(elem) {
		cal_obj	= new DatePicker( elem, mode );
		cal_objs.push(cal_obj);
	});
	/*
		Manually pair calendar 1 to calendar 2
		gggggrrrrrrr
	*/
	cal_objs[0].pair(cal_objs[1], 1);
	cal_objs[1].pair(cal_objs[0], 2);
}

/*

	Tab Switching for Virgin Blue
	
*/
/* booking tabs
----------------------------------------------------------------------------------------------- */
var CompactTabs = Class.create();
CompactTabs.prototype = {

_flightTab 		: "",
_holidaysTab 	: "",
_checkTab 		: "",
_currentTab		: "flights",
_currentHover 	: "",
_tabsElem 		: "",

	initialize: function( ) {
		
		this._tabsElem 		= $('bp-tabs');
		this._flightsTab 	= 'tab-flights';
		this._holidaysTab 	= 'tab-holidays';
		this._checkTab 		= 'tab-webcheck';
		this._outer_div 	= 'booking-panel';
		this._preload		= 'bp-tab-preloader';
	  
		flightsTab = $(this._flightsTab);
		holidaysTab = $(this._holidaysTab);
		checkTab = $(this._checkTab);
		
		this.attachLoader();
		
		//for (i = 0; i < class.length; i++) {
			if (this._tabsElem.className.indexOf('tab') != -1) {
				tab = this._tabsElem.className.split('-')[1];
				// load the tab
				this.load_tab( tab );
				// set the new current tab
				this._currentTab = tab;
			}
		//}
	  
		Event.observe(flightsTab, 'click', this.swap.bindAsEventListener(this));
		Event.observe(holidaysTab, 'click', this.swap.bindAsEventListener(this));
		Event.observe(checkTab, 'click', this.swap.bindAsEventListener(this));
		
		flightsTab.onclick = function(){ this.blur(); return false; };
		holidaysTab.onclick = function(){ this.blur(); return false; };
		checkTab.onclick = function(){ this.blur(); return false; };
	},
	swap : function ( event ) {
  		nextTab = Event.element(event).id.split("-")[1];
		
		this.load_tab(nextTab);
		
  		if ( nextTab != this._currentTab ) {
			Element.removeClassName(this._tabsElem, this._flightsTab+"-on");
			Element.removeClassName(this._tabsElem, this._holidaysTab+"-on");
			Element.removeClassName(this._tabsElem, this._checkTab+"-on");
			
			switch (Event.element(event).id) {
				case this._flightsTab:
					Element.addClassName(this._tabsElem, this._flightsTab+"-on");
			  	break;
			  	case this._holidaysTab:
					Element.addClassName(this._tabsElem, this._holidaysTab+"-on");
			 	break;
			  	case this._checkTab:
					Element.addClassName(this._tabsElem, this._checkTab+"-on");
			  	break;
			}
			//Element.removeClassName( $('booking-'+nextTab), "no-display" );
			this._currentTab = nextTab;
  		}
  		return false;
	},
	load_tab : function ( tab ) {
		
		h = parseFloat($('bp-'+this._currentTab).getStyle('height'));
		$('bp-'+this._currentTab).hide();
		
		empty = false;
		// if the div exists but there are no childnodes (meaning the ajax wasn't loaded previously)
		if ($('bp-'+tab)) {
			if ($('bp-'+tab).childNodes.length <= 0) {
				empty = true;
			}
		}
		if (!$('bp-'+tab) || empty) {
			
			$(this._preload).show();
			
			$(this._preload+'-img').setStyle( { marginTop: ((h/2)-40)+'px' } );
			$(this._preload).setStyle( { height: h+'px' } );
			$(this._preload+'-inner').setStyle( { height: (h-65)+'px' } ); 
			
			// create the empty div and attach it to 'booking-panel'
			tmp = document.createElement('div');
			tmp.id = 'bp-'+tab;
			
			$(this._outer_div).appendChild(tmp);
			
			path = '';
			if (typeof(bp_path) != 'undefined') {
				path = bp_path;
			}
			
			// blue holidays javascripts
			if ('tab-'+tab == this._holidaysTab) {
				// blue hol hack code
			//alert('testtttttt');
				import_javascript(path+'js/bh_cities_extend.js');
				import_javascript(path+'js/DateValidation.class.js');
				import_javascript(path+'js/holidays_validation.js');				
				import_stylesheet(path+'css/holidays.css');
			}
			// web checkin javascripts
			if ('tab-'+tab == this._checkTab) {
				import_javascript(path+'js/webcheck_validation.js');
				import_stylesheet(path+'css/webcheck.css');
			}
			
			rand = new Date().getTime();


			url = path+'tab_'+tab+'.php?site='+bp_site+'&rand='+rand;
			//alert(url);
			new Ajax.Request(url, {
			  method		: 'get',
			  evalScripts 	: true,
			  onSuccess 	: function(transport) { $('bp-'+tab).update(transport.responseText)},
			  onComplete 	: $(this._preload).hide()
			});
			// ajax load
			// new Ajax.Updater( tmp.id, 'tab_'+tab+'.php?site='+bp_site, { evalScripts : true, onComplete : $(this._preload).hide() } );
		} else {
			// just in case the preloader is still open
			$(this._preload).hide();
			Element.show( $('bp-'+tab) );
		}
	},
	attachLoader : function () {
		tmp = document.createElement('div');
		tmp.id = this._preload;
		
		tmp_b = document.createElement('div');
		tmp_b.className = 'clearfix bp-inner';
		tmp_b.id = this._preload+'-inner';
		
		tmp_c = document.createElement('div');
		tmp_c.className = 'submit-outer';
		tmp_c.id = this._preload+'-submit';
		
		// bp_site GLOBAL
		img = document.createElement('img');
		img.id = this._preload+'-img';
		img.src = '/apps/booking_panel/' + bp_site+'/images/preload.gif';
		
		tmp_b.appendChild(img);
		tmp.appendChild(tmp_b);
		tmp.appendChild(tmp_c);
		$(this._outer_div).appendChild(tmp);
		
		$(this._preload).hide();
	}
}

function import_javascript(file)
{
    try {
        script = document.createElement("script");
        script.type = "text/javascript";
        script.src = file;
        head = document.getElementsByTagName("head");
        head[0].appendChild(script);
    } catch(e) {
		rand = new Date().getTime();
        document.write('<script type="text/javascript" src="' + file + '?rand=' + rand + '"></script>');
    }
}

function import_stylesheet(file)
{
    try {
        el = document.createElement("link");
        el.type = "text/css";
		el.rel = "stylesheet";
		el.media = "screen";
        el.href = file;
        head = document.getElementsByTagName("head");
        head[0].appendChild(el);
    } catch(e) {
		rand = new Date().getTime();
        document.write('<link type="text/css" rel="stylesheet" href="' + file + '?rand=' + rand + '" media="screen" />');
    }
}
