
var bahData = new Array;
var tableData = new Array;
var tableDataDepartures = new Array;
var tableDataFares = new Array;
var depStns = ["ABZ","EDI","GLA","JER","MAN","NCL","LGW","LHR","LCY","LON"];
var gFromCode = "ALL";
var maxPrice = 0;
var sortOrder = 0;
var IMG_SOURCE = "/cms/global/assets/images/promotion/uk/may_campaign_2009/";
var IMG_STAR = [["1*","1","<img height='9' width='10' alt='star' src='"+IMG_SOURCE+"star.gif'/>"],
["2*","2","<img height='9' width='20' alt='two star' src='"+IMG_SOURCE+"twostars.gif'/>"],
["2+*","2+","<img height='9' width='29' alt='two plus star' src='"+IMG_SOURCE+"twoplusstars.gif'/>"],
["2.5*","2.5","<img height='9' width='25' alt='two and a half star' src='"+IMG_SOURCE+"twohalfstars.gif'/>"],
["3*","3","<img height='9' width='30' alt='three star' src='"+IMG_SOURCE+"threestars.gif'/>"],
["3+*","3+","<img height='9' width='39' alt='three plus star' src='"+IMG_SOURCE+"threeplusstars.gif'/>"],
["3.5*","3.5","<img height='9' width='35' alt='three and a half star' src='"+IMG_SOURCE+"threehalfstars.gif'/>"],
["4*","4","<img height='9' width='39' alt='four star' src='"+IMG_SOURCE+"fourstars.gif'/>"],
["4+*","4+","<img height='9' width='50' alt='four plus star' src='"+IMG_SOURCE+"fourplusstars.gif'/>"],
["4.5*","4.5","<img height='9' width='45' alt='four and a half star' src='"+IMG_SOURCE+"fourhalfstars.gif'/>"],
["5*","5","<img height='9' width='50' alt='five star' src='"+IMG_SOURCE+"fivestars.gif'/>"]];
$(document).ready(function () {

$("#t-rhs").insertBefore("#t-centre-left");
$(function() {
$("#slider").slider({
value:130,
min: 50,
max: 150,
step: 10,
slide: function(event, ui) {
$("#amount").val('\u00A3' + ui.value);
}
});
$("#amount").val('\u00A3' + $("#slider").slider("value"));
});

writeSocialHtml();
$("th.destination, th.holiday").toggle(function () {
sortOrder = 0;
$(this).removeClass("up").addClass("down");
},
function () {
sortOrder = 1;
$(this).removeClass("down").addClass("up");
});

$("th.fare").toggle(function () {
sortOrder = 1;
$(this).removeClass("down").addClass("up");
},
function () {
sortOrder = 0;
$(this).removeClass("up").addClass("down");
});


$("#pleaseWait").slideDown(500);
getBAHData();
getData();
setPageParams();
tableStyle();

$("#departureSelection").keyup( function() {
departureChanged();
});
$("#departureSelection").change( function() {
departureChanged();
});

$('#slider').bind('slidestop', function(event, ui) {
var selectedDeparture = $("#departureSelection option:selected").val();
maxPrice = ui.value;
filterResults(selectedDeparture, maxPrice);
$("#fares").trigger("update");
var sorting = [[1,sortOrder]];

$("#fares").trigger("sorton",[sorting]);
});


});
function departureChanged(){
var selectedDeparture = $("#departureSelection option:selected").val();
maxPrice = $("#slider").slider("value");
filterResults(selectedDeparture, maxPrice);
$("#fares").trigger("update");
var sorting = [[1,0]];

$("#fares").trigger("sorton",[sorting]);
}
function writeSocialHtml() {
socialHtml = '<div id="facebook">';
socialHtml += '<iframe scrolling="no" frameborder="0" src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.britishairways.com%2Ftravel%2Fcheap-flights-europe%2Fpublic%2Fen_gb%3Futm_source%3DFacebookButton%26utm_medium%3DUK%26utm_campaign%3DHighValueEUR&amp;layout=button_count&amp;show_faces=false&amp;width=90&amp;action=like&amp;font=verdana&amp;colorscheme=light&amp;height=21" style="border:none; overflow:hidden; width:90px; height:21px;" allowTransparency="true"></iframe>';
socialHtml += '</div>';
socialHtml += '<div id="twitter">';
socialHtml += '<a href="http://twitter.com/share" class="twitter-share-button" data-url="http://t.co/23GqPFi" data-counturl="http://www.britishairways.com/travel/cheap-flights-europe/public/en_gb" data-text="High Value flying - book one way flights to Europe from &#163;49." data-count="horizontal" data-via="British_Airways">Tweet</a><script type="text/javascript" src="http://platform.twitter.com/widgets.js"><//script>';
socialHtml += '</div>';
$('#socialButtons').html(socialHtml);
}
function oc(arr)
{
var obj = {};
for(var i=0; i<arr.length; i++)
{
obj[arr[i]]='';
}
return obj;
}
function setPageParams() {



gFromCode = (getQueryValue("from").toUpperCase() in oc(depStns) ) ? getQueryValue("from").toUpperCase() : gFromCode;
$('#departureSelection').val(gFromCode);
departureChanged();

$('#departureSelection option[value="'+gFromCode+'"]').selected = true;

}
function getBAHData() {
var returnValue = "";

var urlData = "";

$.ajax({
type: "GET",
cache: false,
dataType: "json",
url: holidaysServiceUrl,
data: urlData,
async: false,
error: function (XMLHttpRequest, textStatus) {
displayErrMsg();
},

success: function (data, status) {
if (status == "success") {

$.each(data.extras, function (j, item) {
var bah_from = item.from;
var bah_to = item.to;
var bah_outbound_travel_from = item.outbound_travel_from;
var bah_outbound_travel_to = item.outbound_travel_to;
var bah_deal_type = item.deal_type;
var bah_car_days = item.car_days;
var bah_hotel_nights = item.hotel_nights;
var bah_hotel_stars = item.hotel_stars;
var bah_hotel_name = item.hotel_name;
var bah_hotel_board = item.hotel_board;
var bah_price = item.price;
var bah_url = item.url;
returnValue = "";
bahData.push(item);
});
}
}
});
return false;
}
function getData() {
var returnValue = "";

var urlData = "";

var lpfURL = "/main/low-price-finder/itemid-zzall?eId=113021";
var fxURL = "/main/fx?eId=111009";

$.ajax({
type: "GET",
cache: false,
dataType: "json",
url: serviceUrl,
data: urlData,
async: false,
complete: function () {
$("#pleaseWait").slideUp(500);

$("#fares-summary").slideDown(500);
},
error: function (XMLHttpRequest, textStatus) {
displayErrMsg();
},

success: function (data, status) {
if (status == "success") {



$.each(data.Cache, function (j, item) {	
var currency = item.Response.CurrencySymbol, from = item.Response.From, fromDisplay = item.Response.FromDisplay, to = item.Response.To, cabin = item.Response.CabinDisplay, destDisplay = item.Response.ToDisplay, minPrice = 999999, travelClass = item.Response.CabinCode, isOneWay = item.Response.IsOneWay;
var price, dateDisplay, lpfData, fxData, itemLpfURL, itemFxURL, depDate, retDate, arrivalAirport, arrivalAirportDisplay;
returnValue = "";

$.each(item.Response.FareData.Fare, function (i, item) {
if (parseFloat(item.Amount) < minPrice) {
minPrice = parseFloat(item.Amount);
price = parseFloat(item.Amount);
dateDisplay = item.DateDisplay;
if (country == "us") {
depDate = item.FXUSDepartureDate;
retDate = item.FXUSReturnDate;
} else {
depDate = item.FXDepartureDate;
retDate = item.FXReturnDate;
}
arrivalAirportDisplay = item.ArrivalAirportDisplay;
if(arrivalAirportDisplay == 'Palma') arrivalAirportDisplay='Mallorca';
arrivalAirport = item.ArrivalAirport;
}
});

if (isOneWay == "true") {
onewayClause = "&amp;oneway=true";
waynessTxt = txtOneWay;
} else {
onewayClause = "";	
waynessTxt = txtReturn;
}

lpfData = "&amp;origin=" + from + "&amp;destination=" + to + "&amp;cabin=" + travelClass + "&amp;source=LowPriceFinderList" + onewayClause;

fxData = "&amp;from=" + from + "&amp;to=" + arrivalAirport + "&amp;depDate=" + depDate + "&amp;cabin=" + travelClass + "&amp;ad=1&amp;restrictionType=LOWEST&amp;source=LowPriceFinderList";
if (onewayClause == "") {
fxData += "&amp;retDate=" + retDate;
}
itemLpfURL = lpfURL + lpfData;
itemFxURL = fxURL + fxData;
tempBAHHTML = "";
tempHotelHTML = "";
tempCarHTML = "";
var temp = "", tempFirstDate = "", tempMoreDates = "", tempMoreDates2 = "", leadLine = "", matched = 0, lastHotelName = "", lastHotelBoard = "", lastHotelNights = "", carData = 0, isFirst = true;

for(var index = 0; index < bahData.length; index++) {
if ((bahData[index].from == from) && (bahData[index].to == to)) {

if (bahData[index].deal_type == "Hotel") {
if((bahData[index].hotel_name == lastHotelName) && (bahData[index].hotel_board == lastHotelBoard) && (bahData[index].hotel_nights == lastHotelNights)) {
tempMoreDates = "<div class='moreDates'>";
tempMoreDates += "<span class='price'><a href='"+bahData[index].url+"'>"+currency+bahData[index].price+"</a> </span>";
tempMoreDates += "<span class='dates'>"+bahData[index].outbound_travel_from+" - "+bahData[index].outbound_travel_to+"</span>";
tempMoreDates += "</div>";
tempHotelHTML += tempMoreDates;
} else {
if (isFirst) isFirst = false;
else tempHotelHTML = tempHotelHTML+"</div>";
tempHotelHTML = tempHotelHTML+"<div class='hotel'>";
tempHotelHTML = tempHotelHTML+"<span class='fare'><span style='display:none;'>"+currency+bahData[index].price+"</span><img src='/cms/global/assets/images/promotion/uk/may_campaign_2009/flight_hotel_icon.gif' alt='Flight + Hotel' /><a href='"+bahData[index].url+"'>"+currency+bahData[index].price+"</a></span>"+" <span class='offerHeading'>Flight + "+bahData[index].hotel_nights+" nights hotel, "+bahData[index].hotel_board+"</span>";

if (bahData[index].additional_info && bahData[index].additional_info != "") tempHotelHTML = tempHotelHTML+"<br /><span class='additionalInfo'>"+bahData[index].additional_info+"</span>";
tempHotelHTML = tempHotelHTML+"<div class='clearBoth'>&nbsp;</div>"+getPicForStarRating(bahData[index].hotel_stars);

if (bahData[index].lightbox && bahData[index].lightbox != "") tempHotelHTML = tempHotelHTML+"<a href='/main/"+bahData[index].lightbox+"' class='tcobatbox'>"+bahData[index].hotel_name+"</a><br/>";
else tempHotelHTML = tempHotelHTML+bahData[index].hotel_name+"<br/>";

tempHotelHTML = tempHotelHTML+bahData[index].outbound_travel_from+" - "+bahData[index].outbound_travel_to;

if (bahData[index].hotel_description && bahData[index].hotel_description != "") tempHotelHTML = tempHotelHTML+"<span class='hotelDesc'>"+bahData[index].hotel_description+"</span>";

lastHotelName = bahData[index].hotel_name;
lastHotelBoard = bahData[index].hotel_board;
lastHotelNights = bahData[index].hotel_nights;
}
}
if (bahData[index].deal_type == "Car") {
if(carData == 1){
tempMoreDates2 = "<div class='moreDates'>";
tempMoreDates2 += "<span class='price'><a href='"+bahData[index].url+"'>"+currency+bahData[index].price+"</a> </span>";
tempMoreDates2 += "<span class='dates'>"+bahData[index].outbound_travel_from+" - "+bahData[index].outbound_travel_to+"</span>";
tempMoreDates2 += "</div>";
tempCarHTML += tempMoreDates2;
} else {
tempCarHTML = tempCarHTML+"<div class='car'>";
tempCarHTML = tempCarHTML+"<span class='fare'><span style='display:none;'>"+currency+bahData[index].price+"</span><img src='/cms/global/assets/images/promotion/uk/may_campaign_2009/flight_car_icon.gif' alt='Flight + Car' /><a href='"+bahData[index].url+"'>"+currency+bahData[index].price+"</a></span>"+" <span class='offerHeading'>Flight + "+bahData[index].car_days+" days car rental</span><div class='clearBoth'>&nbsp;</div>"+bahData[index].outbound_travel_from+" - "+bahData[index].outbound_travel_to+"<br/>";

carData = 1;
}
}
}
}

if (tempHotelHTML == "" && tempCarHTML == "") tempHotelHTML = "<a class='tertiaryLink' href='/travel/fx/public/en_gb'>Search for holidays</a>";
else tempCarHTML = tempCarHTML+"</div>";

tempBAHHTML = tempHotelHTML + tempCarHTML;

if (price != undefined) {	
returnValue = "<td style='border-left: 0px none;' class='destination' id='destination-1'><a href='" + itemLpfURL + "' class='city'>" + arrivalAirportDisplay + "</a><p>"+txtFrom+" "+fromDisplay+"</p>";
returnValue += "<div class='locationThumbnail'><img height='93' width='160' alt='" + arrivalAirportDisplay + ".' src='/cms/global/assets/images/promotion/uk/novsale2009/thumbnails/thumbnail_" + arrivalAirport + ".jpg'/></div></td>";
returnValue += "<td class='fare' id='fare-1'><span style='display:none;'>" + currency + price + "</span><img src='/cms/global/assets/images/promotion/uk/may_campaign_2009/flight_icon.gif' alt='Flight' /><a href='" + itemLpfURL + "'>" +
currency + price + "</a></span><span class='type'>&nbsp;" + waynessTxt + "<a href='" + itemLpfURL + "' class='tertiaryLink'>" + txtFindThisPrice + "</a></td>";

returnValue += "<td class='holiday' style='border-right: 0px none;'>" + tempBAHHTML + "</td>";




tableData.push("<tr id="+to+">"+returnValue+"</tr>");
tableDataDepartures.push(from);
tableDataFares.push(price);

} else {

}
});
} else {
displayErrMsg();
}
}
});
return false;
}
function filterResults(selectedDeparture, maxPrice){
$("#fares-summary table tbody").html("");
var currentFare = 0;
for(var index = 0; index < tableDataDepartures.length; index++) {
currentFare = parseFloat(tableDataFares[index]);
if ((selectedDeparture == "ALL") && (currentFare < maxPrice)) {
$(tableData[index]).appendTo("#fares-summary table tbody");
} else {
if (selectedDeparture == "LON") {

if (((tableDataDepartures[index] == "LHR") || (tableDataDepartures[index] == "LGW") || (tableDataDepartures[index] == "LCY")) && (currentFare < maxPrice)) {
$(tableData[index]).appendTo("#fares-summary table tbody");
}

} else {
if ((tableDataDepartures[index] == selectedDeparture) && (currentFare < maxPrice)) {
$(tableData[index]).appendTo("#fares-summary table tbody");
}
}
}
}
}
function resetSession() {
var resetUrl = "/main/low-price-finder/itemid-zzall?eId=113001";

$.ajax({
type: "GET",
cache: true,
async: false,
url: resetUrl,
success: function (xml, status) {}
});
}
function getPicForStarRating(sStars)
{
for(var index = 0; index < IMG_STAR.length; ++index) {
if (sStars == IMG_STAR[index][0]||sStars == IMG_STAR[index][1])
{
return IMG_STAR[index][2];
}
}
return "<p>" + sStars + "<p>";
}
function tableStyle() {
$("#fares-summary table thead th:first").css("border-left-width", "0px");
$("#fares-summary table tbody tr td:first-child").css("border-left", "0px");
$("#fares-summary table tbody tr td:last-child").css("border-right", "0px");
$("#fares-summary table tbody tr td:has(div.button)").css("border-left", "0px");

$("#fares").tablesorter();

var sorting = [[1,0]];

$("#fares").trigger("sorton",[sorting]);
}
function displayErrMsg() {
$("#pleaseWait").hide();
$("#fares-summary").slideUp(500, function () {

var retVar = "<div style='text-align:center; padding:10px;'><h3 style='color:#DD0000;'>" + txtSystemsNotResponding + "</h3></div>";
$(this).html(retVar).slideDown(500);
});
}
