//
//为了减少客户端开销，不给每个DOM对象attachEvent,而是统一在document的事件里面处理。
//

var _UltraPageDebug=true;

_UltraPageDebug=true;
//全局的日志记录对象，先简单的用Array，实际应该用Logger对象完成；
var _UltraPageLog=new Array();
window.addLogger=function(msg){
	if(_UltraPageDebug)
		_UltraPageLog[_UltraPageLog.length]=msg;
}

window.__debug=false;
function switchDebug(){
	window.__debug=!window.__debug;
}
function AssertMsg(errorObj){
	if(typeof(errorObj)=="string"){
		window.status=errorObj;}
	else{
		window.status=errorObj.message;}
}

var _webVarGlobalKey=function(){};

_webVarGlobalKey.keys=0;

_webVarGlobalKey.getKeys=function(){
	return (_webVarGlobalKey.keys++);
}

function _webVarReDefine(obj/*object:window域下对象*/,varName/*string:对象id*/,prefix/*string:对象编码前缀*/){
	var vprefix=prefix || "_webVar_";
	obj.name=varName || vprefix+_webVarGlobalKey.getKeys();
	obj.id=obj.name +prefix;
	//为varName定义
	var _rtn=typeof(window[obj.name]);
	if(_rtn!="undefined"){
		if(window.__debug==true){
			alert("变量window."+obj.name+"重复定义了！");
		}
	}
	window[obj.name]=obj;
}

var _CityTypeEnums={
	"normal"	:0,	//普通城市
	"normal-img"	:"ncity.png",
	"provincial":1,//省会城市
	"provincial-img"	:"pcity.png",
	"captial":2,	//首都 
	"captial-img"	:"ccity.png",
	"getIamge":	function(tp){
		for(var v in _CityTypeEnums){
			if(_CityTypeEnums[v]==tp) return _CityTypeEnums[v+"-img"];
		}
		return "";
	}
}

//定义全局辅助变量
window["_gblCurrentShowedWebCityObj"]=null;
window["_gblWebCityObj"]=null;
window["_gblActionMode"]="";

function isOneWayType(){
	return window['_gblJournalType']=='OW';
}

function _CityDef(cityName/*string:城市名字*/,citysPosx/*int:座标x*/,citysPosy/*int:座标y*/,
	cityType/*_CityTypeEnums:城市类型*/,threeZCode){
	
	this.cityName=cityName;
	//处理是否有英文字符的情况
	if(cityName.substr(1,1)==" "){
		this.cityName=cityName.substr(2);
	}
	this._ori_citysPosx=citysPosx||10;
	this._ori_citysPosy=citysPosy||10;
	this.sposx=this._ori_citysPosx-5;
	this.sposy=this._ori_citysPosy-5;
	this.eposx=this._ori_citysPosx+5;
	this.eposy=this._ori_citysPosy+5;
	this.cityType=cityType || _CityTypeEnums["normal"];
	this.threeZCode=threeZCode || "未定义";
}

function _WebCityObject(varName/*string:对象名称id*/,cityDef/*_CityDef:城市定义*/,zIndex/*int:Z-Index*/,
	parElement/*DOM Object:web对象*/){
	_webVarReDefine(this,varName,"_webCity_");
	this.cityDef=cityDef;
	this.par=parElement ||document.body;
	this.zIndex=zIndex || 9;
}



_WebCityObject.prototype.create = function () {
	var str="<span id='"+this.name+"_cntr' class='city-ctnr' "+
		//"title='"+this.cityDef["cityName"]+"("+this.cityDef["threeZCode"]+")' "+
		">";
	str+="<img style='width:10px;height:10px;' id='"+this.name+"_img' src='"+_CityTypeEnums.getIamge(this.cityDef["cityType"])+"'>";
	str+="<span id='"+this.name+"_lbl' style='display:none'>"+this.cityDef["cityName"]+"</span>";
	str+="</span>\r\n";
	this.par.insertAdjacentHTML('afterBegin', str);
	this.oObject=document.getElementById(this.name+"_cntr");
	this.oObject.style.left=this.cityDef.sposx+"px";
	this.oObject.style.top=this.cityDef.sposy+"px";
	this.oObject.style.zIndex=this.zIndex;
	this.imgObject=document.getElementById(this.name+"_img");
	this.lblObject=document.getElementById(this.name+"_lbl");
	this.oObject._isWebCityObj="yes";//
	this.oObject._varId=this.name;
	this.oObject_threeZCode=this.cityDef.threeZCode;
}

function _AirLine(fromCity,toCity,lineType){
	this.fromCity=fromCity;
	this.toCity=toCity;
	this.lineType=lineType || "OW";
}

//_ori_citysPosx
function _graphicsLineByText(sCity,eCity,lineType) {
	var str="";
	str+="<vml:line id='_line_"+sCity.cityDef.threeZCode+"_"+eCity.cityDef.threeZCode+"' "+
	" style='cursor:move;position:absolute;display:none;z-Index:999;' "+
	" from='"+sCity.cityDef._ori_citysPosx+"px,"+sCity.cityDef._ori_citysPosy+"px"+"' ";
	str+=" to='"+eCity.cityDef._ori_citysPosx+"px,"+eCity.cityDef._ori_citysPosy+"px"+"' strokeweight='1pt' ";
	str+=" strokecolor='"+((lineType=="OW")?"blue":"red")+"' >\r\n";
	str+="</vml:line>";
	var airLine = document.createElement(str);
	var tmpStroke=document.createElement("<vml:Stroke dashstyle='solid' endarrow='classic' />");
	airLine.insertBefore(tmpStroke);
	airLine._fromCity=sCity;
	airLine._toCity=eCity;
	airLine._lineType=lineType;
	if(lineType=="RT") airLine.style.zIndex=1000;
	//airLine.title=sCity.cityDef.threeZCode+"-"+eCity.cityDef.threeZCode+"("+lineType+")";
	if(lineType=="RT")
	{
		airLine.title=sCity.cityDef.cityName+"-"+eCity.cityDef.cityName+"("+"往返"+")";
	}
	else
	{
		airLine.title=sCity.cityDef.cityName+"-"+eCity.cityDef.cityName+"("+"单程"+")";
	}
	document.getElementById("paintarea").appendChild(airLine);
	this.objLine=airLine;
	
}


function _gblGetActiveWebCityObjFromSubNode(pobj){
	var obj=pobj;
	if(obj._isWebCityObj=="yes") return obj;
	try{
		while(obj.parentNode!=null){
			obj=obj.parentNode;
			if(obj._isWebCityObj=="yes") return obj;
		}
	}catch(E){
			AssertMsg(E);
	}
	return null;
}	

window["_gblWebAirLineObj"]=null;
function _ProcessClick(){
	var obj=event.srcElement;
	assertDomObj(obj)
	window["_tmpObj"]=obj;
	if(obj.tagName=="line"){
		if(window["_gblWebAirLineObj"]!=null){
			window["_gblWebAirLineObj"].childNodes[0].strokeweight='2pt';
			window["_gblWebAirLineObj"].childNodes[0].dashstyle='solid';
		}
		obj.childNodes[0].strokeweight='3pt';
		obj.childNodes[0].dashstyle='shortdot';
		window["_gblWebAirLineObj"]=obj;
		_setCurrentChoosedAirline(obj);
	}
}
function _setCurrentChoosedAirline(obj){
	document.getElementById("lbl_currentChoosedAirline").innerHTML=obj.title;
	document.getElementById("orgCity").value=obj._fromCity.cityDef.threeZCode;
	document.getElementById("dstCity").value=obj._toCity.cityDef.threeZCode;
}

window["_tmpLineObject"]=null;
function _getTmpLineObj(){
	if(_tmpLineObject!=null) return _tmpLineObject;
	else{
		var str="";
		str+="<vml:line id=_tmpLine style='position:absolute;display:none;z-Index:1000;' from='0px,0px' ";
		str+=" to='100px,100px' strokeweight='2pt' strokecolor='green' >";
		str+="</vml:line>";
		var airLine = document.createElement(str);
		airLine.style.zIndex=1000
		var tmpStroke=document.createElement("<vml:Stroke dashstyle='solid' endarrow='classic' />");
		airLine.insertBefore(tmpStroke);
		document.getElementById("paintarea").appendChild(airLine);
		_tmpLineObject=airLine;
	}
	return _tmpLineObject;
}
function _ProcessMouseUp(){
	
}

function _ProcessMouseDown(){
	if(_gblActionMode==""){
		var obj=event.srcElement;
		assertDomObj(obj)
		var webCityObj=_gblGetActiveWebCityObjFromSubNode(obj);
		var lastWebCityObj=_gblCurrentShowedWebCityObj;
		if(webCityObj==null) {
			return;
		}else{
			_gblCurrentShowedWebCityObj=webCityObj;
			_HideWebCityLines(lastWebCityObj);
			_ShowWebCityLines(_gblCurrentShowedWebCityObj);
		}
		return;
	}
}

function _ProcessMouseMove(){
	/*if(_gblActionMode==""){
		var obj=event.srcElement;
		assertDomObj(obj)
		var webCityObj=_gblGetActiveWebCityObjFromSubNode(obj);
		var lastWebCityObj=_gblCurrentShowedWebCityObj;
		if(webCityObj==null) {
			return;
		}else{
			_gblCurrentShowedWebCityObj=webCityObj;
			_HideWebCityLines(lastWebCityObj);
			_ShowWebCityLines(_gblCurrentShowedWebCityObj);
		}
		return;
	}*/
	if(_gblActionMode=="AddLine"){	
		var tmpLineObj=_getTmpLineObj();
		var obj=window[_gblWebCityObj._varId];
		var _HoverWebCityObj=_gblGetActiveWebCityObjFromSubNode(event.srcElement);
		if(_HoverWebCityObj!=null && _HoverWebCityObj._isWebCityObj=="yes"){
			var objdest=window[_HoverWebCityObj._varId];
			tmpLineObj.from= obj["cityDef"].sposx+"px,"+ obj["cityDef"].sposy+"px";
			tmpLineObj.to= objdest["cityDef"].sposx+"px,"+ objdest["cityDef"].sposy+"px";
			tmpLineObj.style.display='';
		}else{
			window.status="moving.."
			tmpLineObj.from= obj["cityDef"].sposx+"px,"+ obj["cityDef"].sposy+"px";
			tmpLineObj.to= event.x+"px,"+ event.y+"px";
			tmpLineObj.style.display='';
		}
		event.returnValue=false;
		event.cancelBubble=true;
	}
}
/*
* 隐藏某个城市的出发的航线
*/
function _HideWebCityLines(vWebCityObj){
	if(window["_gblWebAirLineObj"]!=null){
		window["_gblWebAirLineObj"].childNodes[0].strokeweight='2pt';
		window["_gblWebAirLineObj"].childNodes[0].dashstyle='solid';
	}
	if(vWebCityObj==null) return;
	vWebCityObj.style.zIndex=9;
	vWebCityObj.className="city-ctnr";
	document.getElementById(vWebCityObj._varId+"_lbl").style.display='none';
	var obj=window[vWebCityObj._varId];
	var maps=null;
	if(isOneWayType()){
		maps=_gblMapCity_OW_Lines[obj.cityDef.threeZCode];
	}else{
		maps=_gblMapCity_RT_Lines[obj.cityDef.threeZCode];
	}
	if(maps!=null){
		for(var i=0;i<maps.length;i++){
			maps[i].objLine.style.display='none';
		}
	}
}
/*显示某个城市出发的航线*/
function _ShowWebCityLines(vWebCityObj){
	vWebCityObj.style.zIndex=10
	vWebCityObj.className="city-ctnr-active";
	document.getElementById(vWebCityObj._varId+"_lbl").style.display='';
	var obj=window[vWebCityObj._varId];
	var maps=null;
	if(isOneWayType()){
		maps=_gblMapCity_OW_Lines[obj.cityDef.threeZCode];
	}else{
		maps=_gblMapCity_RT_Lines[obj.cityDef.threeZCode];
	}
	if(maps!=null){
		for(var i=0;i<maps.length;i++){
			maps[i].objLine.style.display='';
		}
		window.status=obj.cityDef.cityName+"出发的"+window['_gblJournalType']+"航线有："+maps.length+"条.";
	}
}
/*
* 设置全局的航线类型
* 需要隐藏当前如果已经显示的航线
*/
function _setGblJournalType(tp){
	if(tp=="ONEWAY")
	{
		tp="OW";
	}
	else
	{
		tp="RT";
	}
	_HideWebCityLines(_gblCurrentShowedWebCityObj);
	_gblJournalType=tp;
}

function _addNewAirLine(cityFrom,cityTo,lineType){
	var c1=cityFrom.cityDef.threeZCode;
	var c2=cityTo.cityDef.threeZCode;
	var tmp=new _graphicsLineByText(_gblWebCities[c1],_gblWebCities[c2],lineType);
	if(_gblMapCity_OW_Lines[c1]==null || typeof(_gblMapCity_OW_Lines[c1])=="undefined") 
		_gblMapCity_OW_Lines[c1]=[];
	if(_gblMapCity_RT_Lines[c2]==null || typeof(_gblMapCity_RT_Lines[c2])=="undefined") 
		_gblMapCity_RT_Lines[c2]=[];	
	if(lineType=="OW"){
		_gblMapCity_OW_Lines[c1][_gblMapCity_OW_Lines[c1].length]=tmp;
	}else{
		_gblMapCity_RT_Lines[c1][_gblMapCity_RT_Lines[c1].length]=tmp;
	}
}
var _gblWebCities={};
function _initliazeLocation4City(){
	for(var v in Cities){
		_gblWebCities[v]=new _WebCityObject(v,Cities[v], document.getElementById("CityMapDef"));
		_gblWebCities[v].create();
	}
}

window["_gblMapCity_OW_Lines"]={};
window["_gblMapCity_RT_Lines"]={};
//为所有初始化航线定义信息
function _initliazeLines4City(cities){
	var c1,c2,tmp;
	//单程的处理
	for(var v in OnewayCityAirList){
		if(cities.contains(v)){
			addLogger("[ow]**1、"+v);
			for(var i=0;i<OnewayCityAirList[v].length;i++){
				c1=v
				c2=OnewayCityAirList[v][i][1];
				if(cities.contains(c1) && cities.contains(c2)){	
					addLogger("**2、add OW line="+v+","+c2);
					var tmp=new _graphicsLineByText(_gblWebCities[c1],_gblWebCities[c2],"OW");
					if(_gblMapCity_OW_Lines[c1]==null || typeof(_gblMapCity_OW_Lines[c1])=="undefined") 
						_gblMapCity_OW_Lines[c1]=[];
					_gblMapCity_OW_Lines[c1][_gblMapCity_OW_Lines[c1].length]=tmp;
				}
			}
		}
	}//ow
	//往返处理
	for(var v in ReturnCityAirList){
		if(cities.contains(v)){
			addLogger("[rt]**1、"+v);
			for(var i=0;i<ReturnCityAirList[v].length;i++){
				c1=v
				c2=ReturnCityAirList[v][i][1];
				if(cities.contains(c1) && cities.contains(c2)){
					addLogger("**2、add RT line="+v+","+c2);
					var tmp=new _graphicsLineByText(_gblWebCities[c1],_gblWebCities[c2],"RT");
					if(_gblMapCity_RT_Lines[c1]==null || typeof(_gblMapCity_RT_Lines[c1])=="undefined") 
						_gblMapCity_RT_Lines[c1]=[];
					_gblMapCity_RT_Lines[c1][_gblMapCity_RT_Lines[c1].length]=tmp;
				}
			}
		}
	}//RT
	
}
function _webWinFALSEFunction(){
	return false;
}
function _webWinTRUEFunction(){
	return true;
}
Array.prototype.indexOf = function(o){
	for(var i=0;i<this.length;i++)
		if(this[i]==o)return i;
	return-1;
}
Array.prototype.contains = function(o){
	return this.indexOf(o) != -1;
}
//window.document.attachEvent("onselect",_webWinFALSEFunction);
//window.document.attachEvent("onselectstart",_webWinFALSEFunction);
