var cities = new Array();
cities['ATL'] = 'Atlanta';
cities['BWI'] = 'Baltimore';
cities['BOS'] = 'Boston';
cities['ORD'] = 'Chicago';
cities['DFW'] = 'Dallas Fort Worth';
cities['DEN'] = 'Denver';
cities['IAH'] = 'Houston';
cities['LAS'] = 'Las Vegas';
cities['LAX'] = 'Los Angeles';
cities['MIA'] = 'Miami';
cities['JFK'] = 'New York (JFK)';
cities['EWR'] = 'New York (Newark)';
cities['MCO'] = 'Orlando';
cities['PHL'] = 'Philadelphia';
cities['PHX'] = 'Phoenix';
cities['SFO'] = 'San Francisco';
cities['SEA'] = 'Seattle';
cities['TPA'] = 'Tampa';
cities['IAD'] = 'Washington D.C.';
cities['YYC'] = 'Calgary';
cities['YUL'] = 'Montreal';
cities['YYZ'] = 'Toronto';
cities['YVR'] = 'Vancouver';
cities['AMS'] = 'Amsterdam';
cities['ATH'] = 'Athens';
cities['BOM'] = 'Mumbai';
cities['CAI'] = 'Cairo';
cities['DEL'] = 'Delhi';
cities['DXB'] = 'Dubai';
cities['EBB'] = 'Entebbe';
cities['FRA'] = 'Frankfurt';
cities['HKG'] = 'Hong Kong';
cities['IST'] = 'Istanbul';
cities['JNB'] = 'Johannesburg';
cities['LIN'] = 'Milan';
cities['LON'] = 'London';
cities['MAD'] = 'Madrid';
cities['MIL'] = 'Milan';
cities['MLE'] = 'Maldives';
cities['MRU'] = 'Mauritius';
cities['NBO'] = 'Nairobi';
cities['PAR'] = 'Paris';
cities['PRG'] = 'Prague';
cities['ROM'] = 'Rome';
cities['TLV'] = 'Tel Aviv';
cities['VIE'] = 'Vienna';
var gStars = new Array();
gStars[1] = '<img height="9" width="10" src="/cms/global/assets/images/promotion/uk/may_campaign_2009/star.gif" alt="one star"/>';
gStars[2] = '<img height="9" width="20" src="/cms/global/assets/images/promotion/uk/may_campaign_2009/twostars.gif" alt="two star"/>';
gStars[3] = '<img height="9" width="30" src="/cms/global/assets/images/promotion/uk/may_campaign_2009/threestars.gif" alt="three star"/>';
gStars[4] = '<img height="9" width="39" src="/cms/global/assets/images/promotion/uk/may_campaign_2009/fourstars.gif" alt="four star"/>';
gStars[5] = '<img height="9" width="50" src="/cms/global/assets/images/promotion/uk/may_campaign_2009/fivestars.gif" alt="five star"/>';
var OJinfo = new Array();
OJinfo['AMS'] = 'Netherlands, Amsterdam&ojDd=CITY_AMS_NL&ojGw=AMS';
OJinfo['ATH'] = 'Greece, Athens, (ATH)&ojDd=CITY_ATH_GR&ojGw=ATH';
OJinfo['FRA'] = 'Germany, Frankfurt, (FRA)&ojDd=CITY_FRA_DE&ojGw=FRA';
OJinfo['LON'] = 'England, London, Central, Deals+Offers&ojDd=CitGbSale942&ojGw=LHR';
OJinfo['MAD'] = 'Spain, Madrid, (MAD)&ojDd=STA_ES_Madrid&ojGw=MAD';
OJinfo['PAR'] = 'France, Paris, all areas (CDG)&ojDd=CITY_PAR_FR&ojGw=CDG';
OJinfo['PRG'] = 'Czech Republic, Prague, (PRG)&ojDd=CITY_PRG_CZ&ojGw=PRG';
OJinfo['ROM'] = 'Italy, Rome&ojDd=CITY_ROM_IT&ojGw=FCO';
OJinfo['VIE'] = 'Austria, Vienna&ojDd=REG_AT_Vienna&ojGw=VIE';
OJinfo['BOM'] = 'India, Mumbai, (BOM)&ojDd=StaInMaMumba342&ojGw=BOM';
OJinfo['CAI'] = 'Egypt, Cairo, (CAI)&ojDd=CITY_CAI_EG&ojGw=CAI';
OJinfo['DEL'] = 'India, New Delhi, (DEL)&ojDd=CITY_DEL_IN&ojGw=DEL';
OJinfo['DXB'] = 'United Arab Emirates, Dubai, (DXB)&ojDd=CITY_DXB_AE&ojGw=DXB';
OJinfo['IST'] = 'Turkey, Istanbul, (IST)&ojDd=CITY_IST_TR&ojGw=IST';
OJinfo['JNB'] = 'South Africa, Johannesburg, (JNB)&ojDd=CITY_JNB_ZA&ojGw=JNB';
OJinfo['NBO'] = 'Kenya, Nairobi, (NBO)&ojDd=CITY_NBO_KE&ojGw=NBO';
OJinfo['TLV'] = 'Israel, Tel Aviv, (TLV)&ojDd=STA_IL_TelAviv&ojGw=TLV';
OJinfo['EBB'] = 'Entebbe, Uganda, EBB, Entebbe&ojDd=CITY_EBB_UG&ojGw=EBB';
OJinfo['LIN'] = 'Italy, Lombardy, Milan, (MIL All Airports)&ojDd=CITY_MIL_IT&ojGw=LIN';
OJinfo['MIL'] = 'Italy, Lombardy, Milan, (MIL All Airports)&ojDd=CITY_MIL_IT&ojGw=LIN';
OJinfo['MRU'] = 'Mauritius, (MRU)&ojDd=COUNTRY_MU&ojGw=MRU';
OJinfo['MLE'] = 'The Maldives, (MLE)&ojDd=CouMvMaldive407&ojGw=MLE';
OJinfo['HKG'] = 'China, Hong Kong, (HKG)&ojDd=CITY_HKG_CN&ojGw=JHKG';
var gCUGclause = '';
var gPackageCUG = '';
var gDepartureCountry = '';
var gFlightDates = '';
var gOffers = null;
$(processPage);
function processPage() {
gDepartureCountry = $('form[@name="nav_form"] input[@name="country"]').val().toUpperCase();

if($('form[@name="nav_form"] input[@name="logintype"]').val() != 'public') {

gPackageCUG = '/BAHPromo_Code-A005?';

if(gDepartureCountry == 'US') {
gCUGclause = '/cugAlias-kl917?source=CUG_kl917&eId=111042&';
}

else {
gCUGclause = '/cugAlias-qd915?source=CUG_qd91&eId=111042&';
}
}

else {
gCUGclause = '?';
gPackageCUG = '?';
}

$("#loginSummary #loginSummaryButton").click(function() {openLogin();});
$("#loginForm #cancelLogin").click(function() {closeLogin();});





var SOURCE_URL;

if(gDepartureCountry == 'US') {


SOURCE_URL = '/cms/global/json/pages/offerus/offerus209.json';

$('#fromCity option[@value="JFK"]').attr('selected', 'selected');

gFlightDates = '&depDate=11/22/09&retDate=11/29/09';
}

else {
SOURCE_URL = '/cms/global/json/pages/offerus/offerca209.json';

$('#fromCity option[@value="YYZ"]').attr('selected', 'selected');

gFlightDates = '&depDate=22/11/09&retDate=29/11/09';
}


$.getJSON(SOURCE_URL, function (data) {

gOffers = data;



if(gDepartureCountry == 'US') {
populateTable('JFK');
}

else {
populateTable('YYZ');
}
});

$('#fromCity').change(function () {
var city = $('#fromCity option:selected').val();


$("#nights3").attr("checked", "checked");

$('#flights').hide(750, function() {populateTable(city); $('#flights').show(750);});
});

$("#journeySelection input[@name='numNightsSelect']").click(function() {processTravelChange(this);});
}
function populateTable(cityCode) {
var flightOffers = gOffers[cityCode + '_F'];
var packageOffers = gOffers[cityCode + '_FH'];
var sixNightOffers = gOffers[cityCode + '_FH6'];
var carOffers = gOffers[cityCode + '_FC'];
var departureCity = cityCode;
var wayness;
var currentFlightDestination;
var currentDestinationCityCode;
var html="";

if(gDepartureCountry == 'US') {
wayness = ' each way';
}

else {
wayness = ' round-trip';
}

html = '<table id="flights" cellspacing="0">';
html +=	'<tr>';
html +=	'<th class="column destinationColumn">Destination</th>';
html += '<th class="column priceColumn">Flights from</th>';
html += '<th class="column linkColumn">Hotel packages and more</th>';
html +=	'</tr>';


for(var index=0; index < flightOffers.length; ++index) {
currentFlightDestination = flightOffers[index];
currentDestinationCityCode = currentFlightDestination['CityCode'];




html += '<tr"><td class="destinationCell">' + 
				'<img height="61" width="61" src="/cms/global/assets/images/promotion/uk/may_campaign_2009/thumbnail_' +
currentDestinationCityCode.toLowerCase() +
'.gif" alt="' + cities[currentDestinationCityCode] + '."/>' + 
				'<span class="destinationName">' + cities[currentDestinationCityCode] + '</span>';
		
		html += '<br/>' + 'from ' + cities[departureCity] + '</td>';
		
		//
		// Price column
		//
		html += '<td class="flightCell"><a class="priceLink" href="/main/fx' +
gCUGclause +
'fromPkg=' + departureCity +
'&to=' + currentFlightDestination['CityCode'] +
gFlightDates + '&cabin=M&ad=1' +'"><span class="priceAmount">$' + 
				currentFlightDestination['Price'] + '</span><br />' + wayness + '</a>';
	
		html += '</td>';
		
		//
		// Package column
		//
		html += '<td class="packageCell">';
		
		if(packageOffers) {
			currentpackageDestination = getPackageDetails(packageOffers, currentDestinationCityCode);
			
			if(currentpackageDestination){
				html += '<div class="threeNights">';
				html += '<a class="priceLink" href="/main/fx' + gPackageCUG +
'saleOption=PAKFH&depCountryPkg=' + gDepartureCountry +
'&fromPkg=' + departureCity +
'&packageTo=' +	OJinfo[currentDestinationCityCode] +
'"><span class="priceAmount">$' + currentpackageDestination['Price'] + '</span> round-trip</a>';
				html += '<p>Economy flight + <span class="stars">' + 
						gStars[currentpackageDestination['Stars']] + '</span> ';
				if(currentpackageDestination['Lightbox'] != ''){
					html += '<a class="tcobatbox" href="/main/' +
currentpackageDestination['Lightbox'] +
'/">' +
							currentpackageDestination['HotelName'] +
							'</a>';
				}
				else {
					html += currentpackageDestination['HotelName'];
				}
				html += ' ' + currentpackageDestination['Nights'] + ' nights, ' +
						currentpackageDestination['Board'] + '</p>';
				html += '</div>';
			}
			
			else {
				html += "&nbsp;";
			}
		}
		else {
			html += '<div class="threeNights">';
			html += "<p>We are not able to offer packages from Baltimore</p>";
			html += '</div>';
		}
		
		if(sixNightOffers) {
			currentpackageDestination = getPackageDetails(sixNightOffers, currentDestinationCityCode);
			
			if(currentpackageDestination){
				html += '<div class="sixNights">';
				html += '<a class="priceLink" href="/main/fx' + gPackageCUG +
'saleOption=PAKFH&depCountryPkg=' + gDepartureCountry +
'&fromPkg=' + departureCity +
'&packageTo=' +	OJinfo[currentDestinationCityCode] +
'"><span class="priceAmount">$' + currentpackageDestination['Price'] + '</span> round-trip</a>';
				html += '<p>Economy flight + <span class="stars">' + 
						gStars[currentpackageDestination['Stars']] + '</span> ' + 
						currentpackageDestination['HotelName'] + ' ' +
						currentpackageDestination['Nights'] + ' nights, ' +
						currentpackageDestination['Board'] + '</p>';
				html += '</div>';
			}
			
			else {
				html += "&nbsp;";
				 
			}
		}
		else {
			html += '<div class="sixNights">';
			html += "<p>We are not able to offer packages from Baltimore</p>";
			html += '</div>';
		}
		
		if(carOffers) {
			currentpackageDestination = getPackageDetails(carOffers, currentDestinationCityCode);
			
			if(currentpackageDestination){
				html += '<div class="carOffers">';
				html += '<p><img height="18" width="37" src="/cms/global/assets/images/promotion/uk/may_campaign_2009/flight_car_icon.gif" alt="Flight + car"/>&nbsp;&nbsp;'
				html += '<a class="priceLink" href="/main/fx' + gPackageCUG +
'saleOption=PAKFC&depCountryPkg=' + gDepartureCountry +
'&fromPkg=' + departureCity +
'&packageTo=' +	OJinfo[currentDestinationCityCode] +
'"><span class="carAmount">$' + currentpackageDestination['Price'] + '</span></a>';
				html += '&nbsp;&nbsp;Economy flight + <span class="stars">' + 
						currentpackageDestination['Nights'] + ' days car rental, ' + '</p>';
				html += '</div>';
			}
			
			else {
				html += "&nbsp;";
//				html += "<p>Sorry we do not offer car rental here</p>";
			}
		}
				else {
			html += "&nbsp;";
		}
		
		html += '</td>';		
		
		// finish off the row
		html += '</tr>';
		
	} // End for(all flight offers)
	
	// Close the table
	html += '</table>';
	
// Now that the table has been built initialise all of the jQuery functions
	// insert the new contents
	$('#tableHolder').html(html);

	// Initialise showing & hiding of 3/6 night content
	$(".threeNights").show();
	$(".sixNights").hide();

	// Initialise the lightboxes
    $(".tcobatbox").batbox( { iframe:true, width: 700, height: 550 }  );

} // End populate table


//////////////////////////////////////////////////////
//
//////////////////////////////////////////////////////
function getPackageDetails(packageOffers, requiredCityCode) {
	var currentPackage = null;
	var returnPackage = null;

	for(var index=0; index < packageOffers.length; ++index) {
		currentPackage = packageOffers[index];
		
		if(currentPackage['CityCode'] == requiredCityCode) {
			returnPackage = currentPackage;
			break;
		}
	}
	
	return(returnPackage);	
}


//////////////////////////////////////////////////////
//
//////////////////////////////////////////////////////
function openLogin() {
$("#loginSummary").slideUp(500,function(){$("#loginForm").slideDown(500);});
}


//////////////////////////////////////////////////////
//
//////////////////////////////////////////////////////
function closeLogin() {
$("#loginForm").slideUp(500, function(){$("#loginSummary").slideDown(500, function(){});});
}

//////////////////////////////////////////////////////
processTravelChange = function (element) {
var requiredNights = element.getAttribute("value");
switch(requiredNights) {
	case "NIGHTS3": {
		$(".threeNights").show();
		$(".sixNights").hide();
		break;
		}
	case "NIGHTS6": {
		$(".threeNights").hide();
		$(".sixNights").show();
		break;
		}
	default: {
		$(".threeNights").show();
		$(".sixNights").hide();
		}
	}
} 
