

function generateGraph(minPricesStr, maxPricesStr, xLegend, maxMinPrice) {
	if (maxMinPrice == 0) {
		// this avoids flash err in firefox
		minPricesStr = "";
		maxPricesStr = "";
	}

	var so = new SWFObject("/swf/open-flash-chart.swf", "yapchart", "600", "120", "8", "#FFFFFF");
	so.addVariable("variables","true");
	so.addVariable("bg_colour","#ffffff");
	so.addVariable("tool_tip", "$#val#<br>#x_label#");
	so.addVariable("x_offset", false)
	so.addVariable("y_label_style","none")
	so.addVariable("y_axis_colour", "#FFFFFF");
	so.addVariable("y_grid_colour","#EEEEEE");
	so.addVariable("y_ticks","5,10,2");

	so.addVariable("area_hollow","3,0,75,#0071C9,,,#E6F2FA");

	so.addVariable("values",minPricesStr);

	so.addVariable("x_labels",xLegend);
	so.addVariable("x_axis_colour", "#DDDDDD");
	so.addVariable("x_label_style","10,#222222,0,7")
	so.addVariable("x_axis_steps","7");
	so.addVariable("x_grid_colour","#EEEEEE");
	so.addVariable("x_tick_size", "1");

	so.addVariable("y_max",maxMinPrice * 1.3);

	so.write("priceGraph");
}


function updateBrowseGraph() {
	var datePulldown = document.getElementById("datePulldown");
	var datePulldownVal = datePulldown.value;
	var xLegendAll = document.getElementById("xLegendAll");
	var xLegendAllVal = xLegendAll.value;
	var maxPricesAll = document.getElementById("maxPricesAll");
	var maxPricesAllVal = maxPricesAll.value;
	var minPricesAll = document.getElementById("minPricesAll");
	var minPricesAllVal = minPricesAll.value;
	var totalPricesAll = document.getElementById("totalPricesAll");
	var totalPricesAllVal = totalPricesAll.value;
	var numPricesAll = document.getElementById("numPricesAll");
	var numPricesAllVal = numPricesAll.value;

	var datePulldownSplit = datePulldownVal.split("-");
	var xLegendAllSplit = xLegendAllVal.split(",");
	var maxPricesSplit = maxPricesAllVal.split(",");
	var minPricesSplit = minPricesAllVal.split(",");
	var totalPricesSplit = totalPricesAllVal.split(",");
	var numPricesSplit = numPricesAllVal.split(",");

	var datePulldownBeginDate = convertPulldownDate(datePulldownSplit[0]);
	var datePulldownEndDate = convertPulldownDate(datePulldownSplit[1]);

	var xLegendNew = "";
	var maxPricesNew = "";
	var minPricesNew = "";
	var maxMinPrice = 0;
	var minMinPrice = 30000;
	var maxMaxPrice = 0;
	var totalPrice = 0;
	var numPrices = 0;

	for (var i = 0; i < xLegendAllSplit.length; i++) {
		var ithXLegend = xLegendAllSplit[i];
		var converted = Date.parse(ithXLegend);
		var ithXLegendDate = new Date(converted);

		if (dateInRange(ithXLegendDate, datePulldownBeginDate, 
				datePulldownEndDate)) {
			xLegendNew += ithXLegend + ",";
			maxPricesNew += maxPricesSplit[i] + ",";
			minPricesNew += minPricesSplit[i] + ",";

			if (minPricesSplit[i] != "null") {
				maxMinPrice = Math.max(maxMinPrice, 
					minPricesSplit[i]);
				minMinPrice = Math.min(minMinPrice, 
					minPricesSplit[i]);
			}

			if (maxPricesSplit[i] != "null") {
				maxMaxPrice = Math.max(maxMaxPrice, 
					maxPricesSplit[i]);
			}

			if (totalPricesSplit[i] != "null") {
				totalPrice += parseFloat(totalPricesSplit[i]);
				numPrices += parseInt(numPricesSplit[i]);
			}
		}
	}

	xLegendNew = xLegendNew.slice(0, -1);
	minPricesNew = minPricesNew.slice(0, -1);
	maxPricesNew = maxPricesNew.slice(0, -1);
	generateGraph(minPricesNew, maxPricesNew, xLegendNew, maxMinPrice);
	if (minMinPrice == 30000) {
		minMinPrice = 0;
	}
	var avgPrice = 0;
	if (numPrices > 0) {
		avgPrice = totalPrice / numPrices;
	}

	$("#minMinPrice").html("$" + numberFormat(minMinPrice));
	$("#maxMaxPrice").html("$" + numberFormat(maxMaxPrice));
	$("#avgPrice").html("$" + numberFormat(avgPrice));

	updatePagination(datePulldownSplit[2]);
	updateAvgPrices(datePulldownSplit[2]);
}


function updateAvgPrices(futureDaysEnd) {
	updateAvgPricesHelper(futureDaysEnd, "Lo", "Lowest");
	updateAvgPricesHelper(futureDaysEnd, "Hi", "Highest");
}


function updateAvgPricesHelper(futureDaysEnd, labelShort, labelLong) {
	var airlineNamesLabel = "airlineNames" + labelShort + futureDaysEnd;
	var airlineNames = document.getElementById(airlineNamesLabel);
	var airlineNamesVal = airlineNames.value;
	var avgPricesLabel = "avgPrices" + labelShort + futureDaysEnd;
	var avgPrices = document.getElementById(avgPricesLabel);
	var avgPricesVal = avgPrices.value;

	var airlineNamesSplit = airlineNamesVal.split(",");
	//var avgPricesSplit = avgPricesVal.split(",");
	var avgPricesSplit = avgPricesVal.split(";");

	for (var i = 0; i < 3; i++) {
		var avgPriceTrLabel = "#avgPriceTr" + labelLong + i;

		//if (airlineNamesSplit.length <= i) {
		if (airlineNamesSplit.length <= i || 
				airlineNamesSplit[i].length == 0) {
			$(avgPriceTrLabel).hide();
		} else {
			var airlineNameLabel = "#airlineName" + labelLong + i;
			var avgPriceLabel = "#avgPrice" + labelLong + i;
			$(airlineNameLabel).html(airlineNamesSplit[i]);
			$(avgPriceLabel).html(avgPricesSplit[i]);
			$(avgPriceTrLabel).show();
		}
	}
}


function updatePagination(futureDaysEnd) {
	var i = 0;
	var numNullsInRow = 0;
	while (true) {
		var pageAnchor = document.getElementById("page" + i);
		if (pageAnchor == null) {
			numNullsInRow++;
			if (numNullsInRow > 3) {
				break;
			}
		} else {
			numNullsInRow = 0;
			updatePaginationCore(pageAnchor, futureDaysEnd);
		}
		i++;
	}

	pageAnchor = document.getElementById("pageNext");
	if (pageAnchor != null) {
		updatePaginationCore(pageAnchor, futureDaysEnd);
	}
}


function updatePaginationCore(pageAnchor, futureDaysEnd) {
	/*var hrefSplit = pageAnchor.href.split("?");
	if (hrefSplit[1].substring(0, 13) == "futureDaysEnd") {
		pageAnchor.href = hrefSplit[0] + "?futureDaysEnd=" + 
			futureDaysEnd + "&" + hrefSplit[1].substring(17);
	} else {
		pageAnchor.href = hrefSplit[0] + "?futureDaysEnd=" + 
			futureDaysEnd + "&" + hrefSplit[1];
	}*/

	var urlPostfix;
	if (futureDaysEnd == "30") {
		urlPostfix = "";
	} else if (futureDaysEnd == "60") {
		urlPostfix = futureDaysEnd + "/";
	}

	var pageAnchorHref = pageAnchor.href;
	var pageAnchorL3 = pageAnchorHref.slice(pageAnchorHref.length - 3);
	if (pageAnchorL3 == "60/") {  // will never be 30
		var pageAnchorAllButL3 = pageAnchorHref.slice(0, -3);
		pageAnchor.href = pageAnchorAllButL3 + urlPostfix;
	} else {
		pageAnchor.href = pageAnchor.href + urlPostfix;
	}
}


function numberFormat(numberFloat) {
	var numberFloatRounded = Math.round(numberFloat);
	var numberStr = new String(numberFloatRounded);

	// from publicV2/js/common.js
	numberStr = addCommas(numberStr);

	return numberStr;
}


function dateInRange(ithXLegendDate, datePulldownBeginDate, 
		datePulldownEndDate) {
	var beginDiff = datePulldownBeginDate - ithXLegendDate;
	var endDiff = datePulldownEndDate - ithXLegendDate;
	return (beginDiff <= 0 && endDiff > 0);
}


function convertPulldownDate(datePulldown) {
	var datePulldownDashed = datePulldown.substring(0, 2) + "/";
	datePulldownDashed += datePulldown.substring(2, 4) + "/";
	datePulldownDashed += datePulldown.substring(4);

	var converted = Date.parse(datePulldownDashed);
	var result = new Date(converted);

	return result;
}
