var iMaxFare = 0;
var iMinFare = 100000;
var maxFareDisplay = 290;
var mediumFareDisplay = 200;
var minFareDisplay = 115;
var smallRangeMin = 3;
var smallRangeDivider = 10;
var largeRangeDivider = 2;
var increment = 1;
$(document).ready(function () {
setAutoComplete("to", "toAutoComplete", "/dwr/exec/locationHelper.getMatchedRedemptionLocations.dwr?callCount=1&c0-scriptName=locationHelper&c0-methodName=getMatchedRedemptionLocations&xml=true&c0-param0=string:", DWRdestsResponseParser, updateCabinDropDown);
setAutoComplete("from", "fromAutoComplete", "/dwr/exec/locationHelper.getMatchedRedemptionLocations.dwr?callCount=1&c0-scriptName=locationHelper&c0-methodName=getMatchedRedemptionLocations&xml=true&c0-param0=string:", DWRdestsResponseParser, updateCabinDropDown);
setResetCabins($("#to"));
setResetCabins($("#from"));
var count = 0;
var tallestBar = 0;
var topMargin = "";
$("table#fareGraph td  .price .amount").each(function (i) {
var iThisFare = $(this).html();
getMaxFare(Math.round(iThisFare));
getMinFare(Math.round(iThisFare));
});
var iFareRange = iMaxFare - iMinFare;
if (iFareRange > 0) {
var fareRangePercentage = iFareRange / iMaxFare * 100;
var fareRangeDisplay = maxFareDisplay - minFareDisplay;
if (fareRangePercentage > smallRangeMin) {
increment = fareRangeDisplay / iFareRange / largeRangeDivider;
}
else {
increment = fareRangeDisplay / iFareRange / smallRangeDivider;
minFareDisplay = mediumFareDisplay;
}
}
else {
minFareDisplay = mediumFareDisplay;
}
$("table#fareGraph td  .price .amount").each(function (i) {
var iThisFare = $(this).html();
var sBestFare = Math.round((iThisFare - iMinFare) * increment) + minFareDisplay;
if (sBestFare > tallestBar) {
tallestBar = sBestFare;
}
sBestFare = sBestFare + "px";
$(this).parents("div.wrap").css("height", sBestFare);
count++;
});
topMargin = maxFareDisplay - tallestBar + "px";
$("table#fareGraph").css("margin-top", topMargin);
});
function getMaxFare(iThisFare) {
if (iThisFare > iMaxFare) {
iMaxFare = iThisFare;
}
}
function getMinFare(iThisFare) {
if (iThisFare < iMinFare) {
iMinFare = iThisFare;
}
}
function setOneWay() {
var oneway = $("#oneway").val();
if (oneway == 'true') {
$("#oneway").val('false');
} else {
$("#oneway").val('true');
}
document.dealFinderGraph.submit();
}
function hideCal() {
}
function updateCabinDropDown(){
var originCode = getLocationCodeFromAjaxSelection($("#from").val());
var destinationCode = getLocationCodeFromAjaxSelection($("#to").val());
if(originCode==''){
originCode=$("#origin").val();
}
if(destinationCode==''){
destinationCode=$("#destination").val();
}
if(originCode!='' && destinationCode!='' ){
updateCabin(jQuery.trim(originCode), jQuery.trim(destinationCode), "en");
}
}
function getLocationCodeFromAjaxSelection(location){
var locationCode='';
var locationString=location.split(',');
if(locationString.length==4){
if(locationString[2].length==4){
locationCode = locationString[2];
}
}
return locationCode;
}
function updateCabin(dep, dest, lang) {
var queryString = "";
var fullURL = "";
queryString = '?callCount=1&c0-scriptName=cabinHelper&c0-methodName=getCabinsForRoute&c0-param0=string:'+dep+'&c0-param1=string:'+dest+'&c0-param2=string:'+lang+'&xml=true';
fullURL = '/dwr/exec/cabinHelper.getCabinsForRoute.dwr' + queryString;
$.get(fullURL, function(data){
var ansLength = data.length;
if(ansLength > 0){
var splitData = data.split("\n");
eval(splitData[0]);
if (s0) {
$("#cabinHolder").empty();
var optionsString='<select class="withLink" id="cabin" name="cabin" onfocus="hideCal();">';
for (var i in s0) {
optionsString+='<option value="'+i+'">'+s0[i]+'</option>';
}
optionsString+='</select>';
$("#cabinHolder").append(optionsString);
}
}
});
}

