System.status.pageAbbr = "HZ";

QunarHisControl = new function(){
	document.domain="qunar.com";
	var listens = [];

	this.write =function(){
		document.write('<iframe src="http://history.qunar.com/history/history.html" onload="jx05CFEventFC(this)" style="width:1px;height:1px;visibility:hidden;position:absolute"></iframe>');
	}

	window.jx05CFEventFC=function(element){
		var adaptor = null;
		try{
			adaptor = element.contentWindow.QunarHistory;
		}catch(e){SystemLog.add(e);}
		if(!adaptor)
			return ;
		for(var i=0;i<listens.length;i++){
			try{
				listens[i](adaptor);
			}catch(e){SystemLog.add(e);}
		}
	}
	this.addListener=function(func){
		listens.push(func);
	}
}

var _eventCache;

function system_onload() {

	window.coolselectMapObj = new CoolSelect("hselHotelPriceMap",
		templates.coolselectmap, 
		"hdivHotelPriceMap", 
		{options:System.language._SORT_RANGE_MAP_})

	Event.observe(coolselectMapObj, "change", (function()
		{listMap.sort(this.value)}).bind(coolselectMapObj));
		
	Event.observe(coolselectMapObj, "change", (function()
		{trackSort(this.value)}).bind(coolselectMapObj));


	var _controls = new ControlGroup(controlDef );
	//trackAction(System.status.pageAbbr+"|search|"+(new Date().getTime() - CLIENT_TIME.getTime()) / 1000, null, true);
	

	Event.observe("hselHotelRoomNumber","change",function() {
		window.setTimeout(function(_v) {
			trackAction("HZ|CS|Rm|"+_v);
		}.bind(this, $("hselHotelRoomNumber").value), 200);
		if(parseInt($("hselHotelRoomNumber").value) > 5) {
			this._msgbox = new MsgBox(templates.lightbox);
			this._msgbox.show(1,"OK",System.language._MORETHAN5);
			$("hselHotelRoomNumber").setValue(5, true);
		}
	});
	Event.observe("hinpNewSearch","click",startNewSearch.bind(this,_controls));

	var roomNum = 1;
	var rIndex = window.location.href.indexOf("roomNum");
	if (rIndex >= 0) {
		roomNum = parseInt(window.location.href.substring(rIndex).substring("roomNum".length + 1, "roomNum".length + 2));
	}
	if (isNaN(roomNum)) {
		roomNum = 1;
	}
	$("hselHotelRoomNumber").setValue(roomNum);
	
	if (typeof SERVER_MESSAGE != "undefined" && SERVER_MESSAGE == "toDateOverflow14DayAndFixed") {
		this._msgbox = new MsgBox(templates.lightbox);
		this._msgbox.show(1,"OK",System.language._ONLY_14DAYS);
		$jsex.$('hinpToDate').datepickerObj.setValue(Date.addDay(Date.parseDate($jsex.$('hinpFromDate').value), 14));
	}

	QunarHisControl.addListener(initSearchHistory);
	QunarHisControl.write();
	trackKeyword();
	//Event.observe("jxCancelf", "click", function() {
	//	trackAction("HZ|F|Clr");
	//});

}

function trackKeyword() {
	return;
	ipt=$jsex.$('jxKeyFilter');
	var _TIME_OUT = 5000;
	var _trackHandler = function(){
		if(ipt.value.strip() != "") {
			trackAction("HZ|F|Kw|"+encodeURIComponent(ipt.value));
		}
		window.clearTimeout(System.status.__changeHandler);
		System.status.__changeHandler = null;
	};
	Event.observe(ipt, "keydown", function(evt) {
		if(System.status.__changeHandler) {
			if(System.status.__oldKeyword != ipt.value) {
				System.status.__oldKeyword == ipt.value;
				window.clearTimeout(System.status.__changeHandler);
				System.status.__changeHandler = window.setTimeout(_trackHandler, _TIME_OUT);
			}
		} else {
			System.status.__changeHandler = window.setTimeout(_trackHandler, _TIME_OUT);
		}
	}.bind(this));
}

function startNewSearch(_controls) {
	_eventCache = doSearch.bind(this,_controls);
	var city = $("hinpToCity").value.strip();
	if (city == "" || city == System.language._CITYINPUT_LANG.blankInput) {
		Element.setValue($("hinpToCity"), System.language._CAPTIAL);
		_eventCache();
		return;
	}
	
	Event.observe(_controls,"checkpassed",_eventCache);
	Event.observe(_controls,"checkfailed",null);
	_controls.checkAll();
	trackAction("HZ|SCH");
}

function doSearch(_controls) {
	Event.stopObserving(_controls,"checkpassed",_eventCache)
	window.setTimeout(function(){	// 解决浏览器aborted问题
		location.href = "http://hotel.qunar.com/search.jsp" + "?_VTYPE=map&" + _controls.serialize();
	},10);
}

function changeFilterPriceValue(_user) {
	var filterValue = Form.serializeElements($("hdivPriceFilter").getElementsBySelector("input[valueelement='true']"),true).filterValue;
	var min = parseFloat(filterValue[0]);
	var max = parseFloat(filterValue[1]);
	filterFace.filter('price',min,max);
	if(_user) {
		trackAction("HZ|F|Pr|"+min+"-"+max);
	}
}

function initSearchHistory(adaptor) {
	adaptor.addHotelLists({toCity:cityName,fromDate: new Date(fromTime),toDate: new Date(toTime)});

	var _historyList = $A(adaptor.getHotelLists());
	$("hinpToCity_CI").hotcityObj.hotCityList.cityHistory=[];
	var keys={};
	_historyList.each(function(_record) {
		if(!keys[_record.toCity]){
			keys[_record.toCity] = true;
			$("hinpToCity_CI").hotcityObj.hotCityList.cityHistory.push(_record.toCity)
		}
	});

}

Date.addDay = function(now, days) {
	return new Date(now.getTime() + days*24*60*60*1000);
}

function trackSort(_v) {
	var arr = _v.split(",");
	var _t = arr[0];
	var _s = arr[1];
	var _type = "", _order = "";
	switch(_t) {
		case "price":
			_type = "Pr";
			break;
		case "star":
			_type = "St";
			break;
		default:
			trackAction("HZ|S|Df");
			return;	
	}
	if(_s == "0") _order = "asc";
	if(_s == "1") _order = "desc";
	trackAction("HZ|S|"+_type+"|"+_order);
}
