/**
 * @author marks
 */

/**
 * Regions: a Javascript Class for maintaining Continent & Country/Trip associations
 * Used primarily for drop down selection of Region / Country / Trips
 * 
 * Note: we're using Prototype on the main site, not MooTools as I built it for :(
 * As such, I've hacked it back together for Prototype 1.5. Even upgrading to 1.6 would
 * solve a number of the problems but alas, who has time for such things? 
 * 
 */

// re-create MooTools fns
if (typeof($type) != 'function')
	$type = myType;
if (typeof($defined) != 'function')
	$defined = myDefined;

function myType(obj){
	if (!$defined(obj)) return false;
	if (obj.htmlElement) return 'element';
	var type = typeof obj;
	if (type == 'object' && obj.nodeName){
		switch(obj.nodeType){
			case 1: return 'element';
			case 3: return (/\S/).test(obj.nodeValue) ? 'textnode' : 'whitespace';
		}
	}
	if (type == 'object' || type == 'function'){
		switch(obj.constructor){
			case Array: return 'array';
			case RegExp: return 'regexp';
			case Class: return 'class';
		}
		if (typeof obj.length == 'number'){
			if (obj.item) return 'collection';
			if (obj.callee) return 'arguments';
		}
	}
	return type;
};

function myDefined(obj){
	return (obj != undefined);
};
// end re-creation

// MooTools version
// var Regions = new Class({

// Prototype 1.5
var Regions = Class.create();
Regions.prototype = {
	initialize: function(){
		this.continent = '';
	},

	createContinent: function(s, sDisplay){
		// as a shortcut, create a continent and assign it as a local object
		this[s] = new Continent(s, sDisplay);
	},
	
	getContinents: function(){
		var conts = new Array();
		// if it's an object, it was created using createContinent
		// therefore, it's a Continent - otherwise it's a function 
		for (var el in this) {
			if ($type(this[el]) == 'object') 
				conts.push(this[el]);
		}
		return conts;
	},
	
	attachForm: function(frm){
		this.form = $(frm);
		// fill available continents
		var sel = this.form.selRegion;	// TODO: make this configurable
		if (sel) {
			// MooTools
			// sel.addEvent('change', this.updateCountries.bindWithEvent(this));
			// Prototype
			Event.observe(sel, 'change', this.updateCountries.bindAsEventListener(this));
			sel.options.length = 1; // always leave default
			this.getContinents().each(function(cont){
				sel.options[sel.options.length] = new Option(cont.displayName, cont.name);
			});
		}
		var sel = this.form.selCountry;
		if (sel) {
			// MooTools
			// sel.addEvent('change', this.updateTrips.bindWithEvent(this));
			// Prototype
			Event.observe(sel, 'change', this.updateTrips.bindAsEventListener(this));
		}
	},
	
	updateCountries: function(a, b){
		var cont = this[this.form.selRegion.value]; // get Continent object
		this.continent = cont;
		var sel = this.form.selCountry;
		if (sel) {
			sel.options.length = 1; // always leave default
			if (cont) {
				// MooTools
				// counts = cont.getCountries();
				// Prototype
				counts = cont.getCountries(cont);
				counts.each(function(s){
					sel.options[sel.options.length] = new Option(s, s);
				}, cont);
			}
		}
		if (this.onRegionSelect) 
			this.onRegionSelect(this.form.selRegion.value);
	},
	
	updateTrips: function(event){
		var cont = this[this.form.selRegion.value]; // get Continent object
		var sCountry = (this.form.selCountry) ? this.form.selCountry.value : '';
		var sel = this.form.selTrips;
		if (sel) {
			sel.options.length = 1; // always leave default
			if (cont) {
				cont.getTrips(sCountry).each(function(trip){
					sel.options[sel.options.length] = new Option(trip.name, trip.code);
				});
			}
		}
		if (this.onCountrySelect) 
			this.onCountrySelect(sCountry);
	},
	
	getCurrentTrips: function() {
		if (this.continent) {
			var sCountry = (this.form.selCountry) ? this.form.selCountry.value : '';
			return this.continent.getCurrentTrips(sCountry);
		}
		else 
			return new Array() ;
	}

// MooTools end brace
// });
// Prototype 1.5
};
/*
 * end class Region
 */

// MooTools version
// var Trip = new Class({

// Prototype 1.5
var Trip = Class.create();
Trip.prototype = {

	initialize: function(id, country, code, name) {
		this.id = id;
		this.country = country;
		this.code = code;
		this.name = name;
	},
	
	getOption: function() {
		// return '<option value="'+this.country+'">'+this.country+'</option>\n'; 
	}
		
// MooTools end brace
// });
// Prototype 1.5
};	
/*
 * end class Trip
 */

// MooTools version
// var Continent = new Class({

// Prototype 1.5
var Continent = Class.create();
Continent.prototype = {
	
	initialize: function(s,sDisplay) {
		this.name = s;
		this.displayName = sDisplay;
		this.trips = new Array();
		this.countries = new Array();
	},
	
	addTrip: function(id, country, code, name) {
		var trip = new Trip(id, country, code, name)
		this.trips.push(trip);
	},
	
	getCountries: function(obj) {
		// MooTools will correctly take this as the class instance
		// Prototype - will only bind this to the fucking event. Piece of shit.
		// so if this isn't the window (i.e. MooTools), set obj as this
		// otherwise it's been passed thru by function call
		if (this!=window)
			obj=this;
		obj.trips.each(function(trip){
			if (obj.countries.indexOf(trip.country) < 0) {
				obj.countries.push(trip.country);
			}
		}, obj);
		obj.countries.sort();
		return obj.countries;
	},
	
	getCurrentTrips: function(sCountry) {
		return this.getTrips(sCountry);
	},
	
	getTrips: function(sCountry) {
		var result = new Array();
		this.trips.each(function(trip) {
			if (trip.country == sCountry || sCountry == '')
				result.push(trip);
		});
		return result;
	}
	
// MooTools end brace
// });
// Prototype 1.5
};	
/*
 * end class Continent
 */

/*
 * Begin Trip List
 * This section created dynamically by dest_seect_create_js.php
 */
function addTrips(o) {
	o.createContinent('Africa', 'Africa');
	o.Africa.addTrip('2094', 'Botswana', 'USB', 'Okavango and Beyond');
	o.Africa.addTrip('1741', 'Kenya', 'YSE', 'Kenya Explorer');
	o.Africa.addTrip('1688', 'Madagascar', 'YSY', 'Madagascan Explorer');
	o.Africa.addTrip('1737', 'Mali', 'YSJ', 'Journey to Timbuktu');
	o.Africa.addTrip('2039', 'Rwanda', 'YSR', 'Rwanda - Gorillas and Chimps');
	o.Africa.addTrip('2094', 'South Africa', 'USB', 'Okavango and Beyond');
	o.Africa.addTrip('1681', 'Tanzania', 'YSA', 'Tanzania Adventure');
	o.Africa.addTrip('2094', 'Zambia', 'USB', 'Okavango and Beyond');
	
	o.createContinent('Asia', 'Asia');
	o.Asia.addTrip('1948', 'Borneo', 'BSC', 'Sabah - the land beneath the wind');
	o.Asia.addTrip('1759', 'Cambodia', 'KSM', 'Heart of Cambodia');
	o.Asia.addTrip('1751', 'China', 'CSB', 'Taste of China - Southbound');
	o.Asia.addTrip('2003', 'Hong Kong', 'CSV', 'Hanoi to Hong Kong');
	o.Asia.addTrip('1921', 'India', 'HSC', 'Classic Rajasthan');
	o.Asia.addTrip('2015', 'Indonesia', 'ISG', 'Islands of the Gods');
	o.Asia.addTrip('1651', 'Japan', 'JST', 'Land of the Rising Sun');
	o.Asia.addTrip('1657', 'Kyrgyzstan', 'OSK', 'Trans-Asia Express');
	o.Asia.addTrip('1898', 'Laos', 'LSV', 'Bangkok to Hanoi');
	o.Asia.addTrip('1900', 'Malaysia', 'MSA', 'Circle Malaysia');
	o.Asia.addTrip('1672', 'Mongolia', 'CSF', 'Mongolia');
	o.Asia.addTrip('1924', 'Nepal', 'HSK', 'Delhi to Kathmandu');
	o.Asia.addTrip('1901', 'Singapore', 'MSN', 'Colours of Asia');
	o.Asia.addTrip('1942', 'Sri Lanka', 'SSA', 'Circle Sri Lanka');
	o.Asia.addTrip('1898', 'Thailand', 'LSV', 'Bangkok to Hanoi');
	o.Asia.addTrip('1628', 'Tibet', 'FSA', 'Across the Roof of the World');
	o.Asia.addTrip('1657', 'Uzbekistan', 'OSK', 'Trans-Asia Express');
	o.Asia.addTrip('1890', 'Vietnam', 'VSF', 'The Spirit of Vietnam Southbound');
	
	o.createContinent('Australasia', 'Australasia');
	o.Australasia.addTrip('2023', 'Australia', 'PSB', 'Eastern Explorer Northbound');
	o.Australasia.addTrip('2028', 'Fiji', 'PSF', 'Fiji Explorer');
	o.Australasia.addTrip('2027', 'New Zealand', 'PSS', 'South Island Explorer');
	
	o.createContinent('Europe', 'Europe');
	o.Europe.addTrip('1560', 'Albania', 'WSO', 'Dubrovnik to Athens');
	o.Europe.addTrip('1549', 'Austria', 'WSB', 'The Road to Budapest');
	o.Europe.addTrip('1550', 'Bosnia and Herzegovina', 'WSK', 'Balkan Adventure');
	o.Europe.addTrip('1513', 'Croatia', 'WSS', 'Destination Dalmatian Coast');
	o.Europe.addTrip('1549', 'Czech Republic', 'WSB', 'The Road to Budapest');
	o.Europe.addTrip('1511', 'Estonia', 'WSE', 'Baltic Experience');
	o.Europe.addTrip('1511', 'Finland', 'WSE', 'Baltic Experience');
	o.Europe.addTrip('1511', 'Germany', 'WSE', 'Baltic Experience');
	o.Europe.addTrip('1639', 'Greece', 'ASE', 'Athens to Santorini');
	o.Europe.addTrip('1549', 'Hungary', 'WSB', 'The Road to Budapest');
	o.Europe.addTrip('1544', 'Italy', 'ZSR', 'Umbrian Escapade');
	o.Europe.addTrip('1511', 'Latvia', 'WSE', 'Baltic Experience');
	o.Europe.addTrip('1511', 'Lithuania', 'WSE', 'Baltic Experience');
	o.Europe.addTrip('1550', 'Montenegro', 'WSK', 'Balkan Adventure');
	o.Europe.addTrip('1575', 'Portugal', 'ASZ', 'Spain, Portugal and Morocco');
	o.Europe.addTrip('1664', 'Russia', 'OSR', 'A Taste of Russia');
	o.Europe.addTrip('1550', 'Serbia', 'WSK', 'Balkan Adventure');
	o.Europe.addTrip('1549', 'Slovakia', 'WSB', 'The Road to Budapest');
	o.Europe.addTrip('1575', 'Spain', 'ASZ', 'Spain, Portugal and Morocco');
	o.Europe.addTrip('1545', 'Switzerland', 'ZSP', 'The Heart of Europe');
	
	o.createContinent('LatinAmerica', 'Latin America');
	o.LatinAmerica.addTrip('1781', 'Belize', 'QSC', 'Mayan Highlights');
	o.LatinAmerica.addTrip('1727', 'Bolivia', 'GSQ', 'Sacred Land of the Incas');
	o.LatinAmerica.addTrip('1728', 'Brazil', 'GSS', 'Bountiful Brazil');
	o.LatinAmerica.addTrip('1659', 'Costa Rica', 'QSR', 'Costa Rica Discovery');
	o.LatinAmerica.addTrip('1772', 'Cuba', 'QSL', 'Classic Cuba');
	o.LatinAmerica.addTrip('1711', 'Galapagos Islands', 'GSH', 'Galapagos Adventure - 6 days');
	o.LatinAmerica.addTrip('1781', 'Guatemala', 'QSC', 'Mayan Highlights');
	o.LatinAmerica.addTrip('1781', 'Mexico', 'QSC', 'Mayan Highlights');
	o.LatinAmerica.addTrip('1716', 'Peru', 'GSE', 'Andes and the Amazon');
	
	o.createContinent('MiddleEast', 'Middle East');
	o.MiddleEast.addTrip('1840', 'Egypt', 'ESI', 'Discover Egypt');
	o.MiddleEast.addTrip('1843', 'Iran', 'ESN', 'Iran Adventure');
	o.MiddleEast.addTrip('1839', 'Jordan', 'ESW', 'Pyramids to Petra');
	o.MiddleEast.addTrip('1858', 'Morocco', 'XSC', 'Colours of Morocco');
	o.MiddleEast.addTrip('1842', 'Syria', 'ESV', 'Cairo to Istanbul');
	o.MiddleEast.addTrip('1842', 'Turkey', 'ESV', 'Cairo to Istanbul');
	o.MiddleEast.addTrip('1851', 'Yemen', 'ESY', 'Unforgettable Yemen');
}

/*
 * End Trip List 
 */
