/**
  * 用来动态调用DWR显示热点城市及航线及价格
  * create by huofei 2008-11-12
  */
var C_HOTFLIGHT_DAYS = 6;	//热点航线价格天数  
var C_HOTFLIGHT_NUM = 5;	//热点航线数
var C_HOTCITY_NUM = 8;		//热点城市数
var flightDates = [];

$(document).ready(function () {
	//1、初始化热点航线表的表头
	$("#trHotFlightTitle").empty();
	$("#trHotFlightTitle").append("<td  width='73'>抵达</td>");
	//从数据库调用热点日期
	hotFlightService.getHotDate(C_HOTFLIGHT_DAYS, function(data) {
		flightDates = data;
		for (var i = 0; i < C_HOTFLIGHT_DAYS; i ++) {
			$("#trHotFlightTitle").append("<td width='66'>" + (flightDates[i].getMonth() + 1) + "-" + flightDates[i].getDate() + "</td>");
		}	
	});
	
	//2、初始化热点城市列表
	hotFlightService.getHotCity(C_HOTCITY_NUM, function(data) {
		if (!data || data.length <= 0) return;
		$("#flightTab").empty();
		for (var i = 0; i < data.length; i ++) {
			hotCity = data[i];
			$("#flightTab").append("<li onclick='getHotFlight(this, " + hotCity.hotCityId + ", \"" + hotCity.cityCode + "\", \"" + hotCity.cityCn + "\")'>" + hotCity.cityCn + "</li>");
		}
		$("li", "#flightTab").each(function() {
			$(this).bind("click",function() {
				for (var i = 0; i < $("li", "#flightTab").size(); i ++) {
					var li = $("li", "#flightTab").get(i);
					if (li == this) {
						li.className = "active";
					} else {
						li.className = "normal";
					}
				}
			});
		});
		//取第一个热点城市的热点航线
		$("li", "#flightTab").eq(0).click();
		//getHotFlight($("#hotFlight").get(0), data[0].hotCityId, data[0].cityCode, data[0].cityCn);
	});
});

function getFlightDates() {
	var now = new Date();
	var d;
	for (var i = 0; i < C_HOTFLIGHT_DAYS; i ++) {
		d = new Date();
		d.setTime(now.getTime() + (1000*60*60*24*i));
		flightDates.push(d);
	}
}

function getHotFlight(which, hotcityid, citycode, citycn) {
	$("tr.fltHotPrice").remove();
	$("form[@name=hotFlightSearch] :hidden[@name=depDisplay1]").val(citycn);
	$("form[@name=hotFlightSearch] :hidden[@name=depCity1]").val(citycode);
	$("form[@name=hotFlightSearch] :hidden[@name=ticketCitycn]").val(citycn);
	hotFlightService.getHotFlight(hotcityid, C_HOTFLIGHT_NUM, C_HOTFLIGHT_DAYS, function(data) {
		if (!data || data.length <= 0) return;
		for (var i = 0; i < data.length; i ++) {
			var hotflight = data[i];
			var tr = $("<tr align='center' class='fltHotPrice'></tr>");
			tr.append("<td> -&gt;" + hotflight.arrCityCn + "</td>");
			var hotprices = hotflight.datePrice;
			if (!hotprices || hotprices.length <= 0) {
				for (var j = 0; j < C_HOTFLIGHT_DAYS; j ++) {
					tr.append("<td>-</td>");
				}
			} else {
				var tdnum = 0;
				for (var j = 0; j < hotprices.length; j ++) {
					var hotprice = hotprices[j];
					var priceIndex = indexOfFlightDates(hotprice.flightDate);
					if (priceIndex < 0) continue;
					if (priceIndex > tdnum) {
						var size = priceIndex - tdnum;
						for (var n = 0; n < size; n ++) {
							tr.append("<td>-</td>");
							tdnum ++;
						}
					}
					
					if (hotprice.hotPrice > 0) {
						var fd = (hotprice.flightDate.getYear() < 2008 ? hotprice.flightDate.getYear() + 1900 : hotprice.flightDate.getYear()) + "-" + (hotprice.flightDate.getMonth() + 1) + "-" + hotprice.flightDate.getDate();
						tr.append("<td><a href='javascript:getAllPrice(\"" + hotflight.arrCity + "\", \"" + hotflight.arrCityCn + "\", \"" + fd + "\")'>￥" + hotprice.hotPrice + "</a></td>");
					} else {
						tr.append("<td>-</td>");
					}
					tdnum ++;
				}
				//如果后面没了，则用'-'补齐
				if (tdnum < C_HOTFLIGHT_DAYS) {
					var size = C_HOTFLIGHT_DAYS - tdnum;
					for (var n = 0; n < size; n ++) {
						tr.append("<td>-</td>");
						tdnum ++;
					}
				}
			}
			$("#hotFlight").append(tr);
		}
	});
	
	//$("#hotFlight").each(function() {
	//	if (this == which) {
	//		$(this).css({"color": "#000000", "text-decoration": "none","background":"url(../images/indexmain2l3.jpg) repeat-x"});
	//	} else {
	//		$(this).css({"color": "#FFFFFF", "text-decoration": "none"});
	//	}
	//});
}

function indexOfFlightDates(pricedate) {
	var pd = (pricedate.getMonth() + 1) + "-" + pricedate.getDate();
	for (var i = 0; i < flightDates.length; i ++) {
		var fd = (flightDates[i].getMonth() + 1) + "-" + flightDates[i].getDate();
		if (pd == fd) {
			return i;
		}
	}
	return -1;
}

function getAllPrice(arrcity, arrcitycn, flightdate) {
	$("form[@name=hotFlightSearch] :hidden[@name=arrDisplay1]").val(arrcitycn);
	$("form[@name=hotFlightSearch] :hidden[@name=arrCity1]").val(arrcity);
	$("form[@name=hotFlightSearch] :hidden[@name=fltDate1]").val(flightdate);
	$("form[@name=hotFlightSearch]").submit();
}
