addLoadListener(RouteModuleInit);

function RouteModuleInit() {
	//RouteModuleClass.Constructor(RouetData);
	if (typeof(RouteModuleClass) == 'undefined') return;
	var Timer = (RouetData.ActiveTimer) ? RouetData.ActiveTimer : 500;
	RouteModuleClass.ActiveValue = (RouetData.ActiveValue) ? RouetData.ActiveValue : 'Smart 導航';
	setTimeout(function() { RouteModuleClass.Constructor(RouetData); }, Timer);
}

var RouteModuleClass = {
	id: 'RouteModuleClass',
	MapSection: '',
	AddrType: [{OptionSet:'地址', ValueSet:'addr'}, {OptionSet:'地點', ValueSet:'point'}],
	WinSmartMidHeight: 300,
	WinSmartFlag: true,
	SetSPoint: '',
	SetEPoint: '',
	getWinWarn: '',
	getRLS: '',
	getRLSO: '',
	getContainer: '',
	getRSSEns: '',
	getTrigger: '',
	getWinSmart: '',
	BlockAction: false,
	IntervalID: '',
	TimerIntervalID: '',
	Refer: GetRefer(),
	Browser: identifyBrowser(),
	BrowserPNGfix: 'ie5,ie5mac,ie5.5,ie6',
	//method
	IconSetInit: function() {
		//Start
		this.StartPoint = new UIcon();
		this.StartPoint.image = 'http://map.pchome.com.tw/img/IconSP.gif';
		this.StartPoint.iconWidth = 21;
		this.StartPoint.iconHeight = 24;
		this.StartPoint.iconAnchor = new UPoint(11, 24);
		//End
		this.EndPoint = new UIcon();
		this.EndPoint.image = 'http://map.pchome.com.tw/img/IconEP.gif';
		this.EndPoint.iconWidth = 21;
		this.EndPoint.iconHeight = 24;
		this.EndPoint.iconAnchor = new UPoint(11, 24);
	},
	MkSEMarker: function(Type, Latlng) {
		var IconSet = (Type == 'S') ?  RouteModuleClass.StartPoint : RouteModuleClass.EndPoint;
		var marker = new UMarker(Latlng, IconSet);
		marker.type = Type;
		MapStatus.Map.addOverlay(marker);
		(Type == 'S') ? this.SetSPoint = marker : this.SetEPoint = marker;
	},
	GetKeyCode: function(e) {
		var KeyCode;
		if (window.event && window.event.srcElement) KeyCode = window.event.keyCode;
		else if (e && e.target)	KeyCode = e.keyCode;
		return KeyCode;
	},
	ShowWinWarn: function(ElementPosArray) {
		$('WinWarn').style.left = ElementPosArray[0] + 'px';
		$('WinWarn').style.top = ElementPosArray[1] + 'px';
		$('WarnTxt').update(this.WarnTxt);
		Element.show('WinWarn');
		setTimeout(function() { Element.hide('WinWarn'); }, 2000);
	},
	RollEventAction: function(Mode) {
		if (Mode == 'Over') {
			if (this.BlockAction) return;
			clearTimeout(this.TimerIntervalID);
		} else {
			clearTimeout(this.TimerIntervalID);
			this.TimerIntervalID = setTimeout('RouteModuleClass.VaryAction()', 500);
		}//end if
	},
	VaryAction: function() {
		if (this.BlockAction || !$('RollingSection')) return;
		clearTimeout(this.TimerIntervalID);
		var ElementPosArray = getPosition($('RollingSection'));
		this.getContainer.style.left = ElementPosArray[0] + 'px';
		this.getContainer.style.top = ElementPosArray[1] + 'px';
		
		this.getRSSEns.MoveFlagY = false;
		this.getRSSEns.GoalY = (this.getRSSEns.CurrentPosY == 0) ? this.getRSSEns.StartY : 0;
		var speed = 5;
		this.BlockAction = true;
		clearInterval(this.IntervalID);
		this.IntervalID = setInterval('RouteModuleClass.realMove('+speed+')', 15);
	},
	realMove: function(speed) {
		var ObjY = this.getRSSEns.CurrentPosY;
		var b = this.getRSSEns.GoalY - ObjY;
		var speedy = b/speed;
		var SignY = (speedy < 0) ? '-' : '+';
		speedy = Number(SignY + ((Math.abs(speedy) <= 1) ? 1 : Math.abs(speedy)));
		//check y position
		if (!this.getRSSEns.MoveFlagY) {
			if (Math.abs(ObjY-this.getRSSEns.GoalY) <= Math.abs(speedy)) { this.getRSSEns.CurrentPosY = this.getRSSEns.GoalY; this.getRSSEns.MoveFlagY = true; }
			else this.getRSSEns.CurrentPosY = ObjY + speedy;
		}
		this.getRSSEns.style.top = this.getRSSEns.CurrentPosY + 'px';
		
		if (this.getRSSEns.MoveFlagY) {
			clearInterval(this.IntervalID);
			this.BlockAction = false;
			if (this.getRSSEns.GoalY == 0) {
				clearTimeout(this.TimerIntervalID);
				this.TimerIntervalID = setTimeout('RouteModuleClass.VaryAction()', 1000);
			} else this.getContainer.style.left = '-1000px';
		}//end if
	},
	ClearDefault: function(funcid, content) {
		Element.hide('WinWarn');
		if ($F(funcid) == content) $(funcid).value = '';
		$(funcid).style.color = '#444444'; 
	},
	RecoverDefault: function (funcid, content) {
		if ($F(funcid) == '') { $(funcid).value = content; $(funcid).style.color = '#b2b2b2'; }
	},
	InputFix: function (FieldID) {
		$(FieldID).value = $F(FieldID).replace(/^\s*|\s*$/g, ''); //trim
		$(FieldID).value = $F(FieldID).replace(/'/g, '');
		$(FieldID).value = $F(FieldID).stripScripts();
		$(FieldID).value = $F(FieldID).stripTags();
	},
	SectionClear: function(SectionID) {
		while ($(SectionID).childNodes.length > 0) $(SectionID).removeChild($(SectionID).firstChild);
	},
	LoadingStatus: function(Mode) {
		if (Mode == 'show') {
			//show
			if (this.getWinSmart) Element.hide(this.getWinSmart);
			if (this.Polyline) MapStatus.Map.removeOverlay(this.Polyline);
			if ($('Overlay')) $('Overlay').onclick = '';
			this.SearchBFlag = true;
			var ElementPosArray = getPosition(this.MapSection);
			this.getRLS.style.left = ElementPosArray[0] + 'px';
			this.getRLS.style.top = ElementPosArray[1] + 'px';
			this.getRLS.style.width = this.MapSection.getWidth() + 'px';
			this.getRLS.style.height = this.MapSection.getHeight() + 'px';
			this.getRLSO.style.width = this.MapSection.getWidth() + 'px';
			this.getRLSO.style.height = this.MapSection.getHeight() + 'px';
			Element.show(this.getRLS);
			$('SearchRoute').disabled = true;
			if ($('SearchLocate')) $('SearchLocate').disabled = true;
			var SelectTag = document.getElementsByTagName('select');
			for (var i=0; i<SelectTag.length; SelectTag[i++].style.visibility = 'hidden');
		} else {
			//hide
			this.SearchBFlag = false;
			if ($('Overlay')) $('Overlay').onclick = function() { MapStatus.HideOWin(); };
			Element.hide(this.getRLS);
			$('SearchRoute').disabled = false;
			if ($('SearchLocate')) $('SearchLocate').disabled = false;
			var SelectTag = document.getElementsByTagName('select');
			for (var i=0; i<SelectTag.length; SelectTag[i++].style.visibility = 'visible');
		}//end if
	},
	SearchCheck: function(Type) {
		if (this.SearchBFlag) return;
		Element.hide('WinWarn');
		var hash = $H();
		hash['ObjID'] = this.id;
		hash['Refer'] = this.Refer;
		hash['Encode'] = 'big5';
		switch (Type) {
			case 'locating':
				if ($F('SearchLocate').length == 0 || $F('SearchLocate') == '請輸入搜尋點') { 
					var ElementPosArray = getPosition($('LFSearchSection'));
					ElementPosArray[0] += 0;
					ElementPosArray[1] += 35;
					this.WarnTxt = '請輸入搜尋點';
					this.ShowWinWarn(ElementPosArray);
					return;
				}
				this.InputFix('SearchLocate');
				this.LoadingStatus('show');
				
				if ($F('LFAddrTypeSelect') == 'addr') {
					hash['action'] = 'addressing';
					hash['addrs'] = $F('SearchLocate');
				} else {
					this.ListPOIsPageNum = 1;
					this.ListPOIsType = 0;
					this.Keyword = $F('SearchLocate');
					hash['action'] = 'ListPOIs';
					hash['keyword'] = this.Keyword;
					hash['Type'] = 'P21';
					hash['MaxRows'] = this.ListPOIsMaxRows;
					hash['PageNum'] = this.ListPOIsPageNum;
				}//end if
				this.DataListQuery(hash);
				break;
			case 'routing':
				if ($F('SearchRoute').length == 0 || $F('SearchRoute') == '請輸入出發點') { 
					var ElementPosArray = getPosition($('SearchRoute'));
					ElementPosArray[0] += 100;
					ElementPosArray[1] -= 18;
					this.WarnTxt = '請輸入出發點';
					this.ShowWinWarn(ElementPosArray);
					return;
				}
				this.InputFix('SearchRoute');
				this.LoadingStatus('show');
				
				this.SearchBFlag = true;
				hash['action'] = Type + 'V2';
				hash['RouteSP'] = $F('SearchRoute');
				hash['SPType'] = $F('AddrTypeSelect');
				var getPoint = MapStatus.Marker.getPoint();
				hash['destlat'] = getPoint.lat();
				hash['destlng'] = getPoint.lng();
				this.DataListQuery(hash);
				break;
		};//end switch
	},
	TunnelSwitch: function (Mode, ObjID) {
		var GetHead = document.getElementsByTagName('head')[0];
		switch (Mode) {
			case 'Open':
				if ($(ObjID+'MapTunnel')) GetHead.removeChild($(ObjID+'MapTunnel'));
				var MapTunnel =document.createElement('script');
				MapTunnel.id = ObjID + 'MapTunnel';
				MapTunnel.setAttribute('type', 'text/javascript'); 
				document.getElementsByTagName('head')[0].appendChild(MapTunnel);
				break;
			default:
				GetHead.removeChild($(ObjID+'MapTunnel'));
		}//end switch
	},
	DataListQuery: function(Vars) {
		var Path = 'http://map.pchome.com.tw/api/Api_Channels.php?' + Vars.toQueryString();
		this.TunnelSwitch('Open', Vars['ObjID']);
		$(Vars['ObjID']+'MapTunnel').src = Path;
	},
	QueryDone: function(JSON) {
		this.SearchBFlag = false;
		this.LoadingStatus('hide');
		switch (JSON.info) {
			case 'routingSuccess':
				this.WinSmartInit();
				//this.VaryAction();
				this.ResetRSSEns();
				Element.hide(this.getWinWarn);
				
				var DataArray = JSON.Data;
				this.SetSPoint = '';
				this.SetEPoint = '';
				var WinSmartMain = $(this.id+'WinSmartMain');
				
				var TitleSet = DataArray.pop().DataElement.split('^');
				var PointSet = DataArray.pop().DataElement.split(',');
				var PonitAmt = PointSet.length;
				for (var i=0; i<PonitAmt; i++) PointSet[i] = Number(PointSet[i]);
				var TotalLength = '全程:' + DataArray.pop().DataElement + 'km';
				this.SectionClear(this.id+'WinSmartMain');
				this.RouteInfo = [];
				this.RouteInfo.push(TotalLength);
				
				var StartLatLng = new ULatLng(PointSet[1], PointSet[0]);
				var EndLatLng = new ULatLng(PointSet[PonitAmt-1], PointSet[PonitAmt-2]);
				var SouthWast = [Math.min(StartLatLng.lat(), EndLatLng.lat()), Math.max(StartLatLng.lng(), EndLatLng.lng())];
				var NouthEast = [Math.max(StartLatLng.lat(), EndLatLng.lat()), Math.min(StartLatLng.lng(), EndLatLng.lng())];
				var Bounds = new UBounds(new ULatLng(SouthWast[0], SouthWast[1]), new ULatLng(NouthEast[0], NouthEast[1]));
				Bounds.extend(StartLatLng);
				Bounds.extend(EndLatLng);
				this.ViewORoute = [MapStatus.Map.getBoundsZoomLevel(Bounds), Bounds.getCenter()];
				this.MkSEMarker('S', StartLatLng);
				this.MkSEMarker('E', EndLatLng);
				
				//Polyline
				var PointArray = [];
				while (PointSet.length > 0) {
					var LatLng = PointSet.splice(0, 2);
					PointArray.push(new ULatLng(LatLng[1], LatLng[0]));
				}//end while
				this.Polyline = new UPolyline(PointArray);
				MapStatus.Map.addOverlay(this.Polyline);
				MapStatus.ZoomClickAction(this.ViewORoute[0], this.ViewORoute[1]);
				
				//info
				this.RouteLatLngSet = [];
				var RouteElement = document.createElement('div');
				RouteElement.className = 'RouteElement';
				RouteElement.innerHTML = '<span class="RouteInfoSP"></span>';
				WinSmartMain.appendChild(RouteElement);
				var TitleElement = document.createElement('span');
				TitleElement.className = 'RouteInfoBB';
				TitleElement.style.height = Math.ceil(CalculateTxt(TitleSet[0])/18)*25 + 'px';
				TitleElement.innerHTML = TitleSet[0];
				TitleElement.ObjID = this.id;
				TitleElement.onmouseover = function() { this.style.textDecoration = 'underline'; };
				TitleElement.onmouseout = function() { this.style.textDecoration = 'none'; };
				TitleElement.onclick = function() {	RouteModuleClass.RouteClickAction(this); };
				RouteElement.appendChild(TitleElement);
				this.RouteLatLngSet.push(StartLatLng);
				
				for (var i=0; i<DataArray.length; i++) {
					var RouteInfo = DataArray[i].DataElement.split('^');
					var Order = i + 1;
					var RouteElement = document.createElement('div');
					RouteElement.className = 'RouteElement';
					RouteElement.style.height = Math.ceil(CalculateTxt(RouteInfo[0])/18)*25 + 'px';
					RouteElement.innerHTML = '<span class="RouteInfoA">'+Order+'.</span>';
					WinSmartMain.appendChild(RouteElement);
					var TitleElement = document.createElement('span');
					TitleElement.className = 'RouteInfoB';
					TitleElement.innerHTML = RouteInfo[0];
					TitleElement.ObjID = this.id;
					TitleElement.onmouseover = function() { this.style.textDecoration = 'underline'; };
					TitleElement.onmouseout = function() { this.style.textDecoration = 'none'; };
					TitleElement.onclick = function() {	RouteModuleClass.RouteClickAction(this); };
					RouteElement.appendChild(TitleElement);
					this.RouteLatLngSet.push(new ULatLng(RouteInfo[1], RouteInfo[2]));
					this.RouteInfo.push(RouteInfo[0]);
				}//end for
				
				var RouteElement = document.createElement('div');
				RouteElement.className = 'RouteElement';
				RouteElement.innerHTML = '<span class="RouteInfoEP"></span>';
				WinSmartMain.appendChild(RouteElement);
				var TitleElement = document.createElement('span');
				TitleElement.className = 'RouteInfoBB';
				TitleElement.innerHTML = '我的目的地';
				TitleElement.ObjID = this.id;
				TitleElement.onmouseover = function() { this.style.textDecoration = 'underline'; };
				TitleElement.onmouseout = function() { this.style.textDecoration = 'none'; };
				TitleElement.onclick = function() {	RouteModuleClass.RouteClickAction(this); };
				RouteElement.appendChild(TitleElement);
				this.RouteLatLngSet.push(EndLatLng);
				
				var RouteElement = document.createElement('div');
				RouteElement.className = 'RouteElement';
				WinSmartMain.appendChild(RouteElement);
				var TitleElement = document.createElement('span');
				TitleElement.className = 'RouteInfoF';
				TitleElement.innerHTML = '全程路線地圖';
				TitleElement.ObjID = this.id;
				TitleElement.onmouseover = function() { this.style.textDecoration = 'underline'; };
				TitleElement.onmouseout = function() { this.style.textDecoration = 'none'; };
				TitleElement.onclick = function() {	RouteModuleClass.RouteClickAction(this); };
				RouteElement.appendChild(TitleElement);
				
				var RouteElement = document.createElement('div');
				RouteElement.className = 'RouteElement';
				WinSmartMain.appendChild(RouteElement);
				var TLElement = document.createElement('span');
				TLElement.className = 'RouteInfoFL';
				TLElement.innerHTML = TotalLength;
				RouteElement.appendChild(TLElement);
				this.RouteInfo.push(TitleSet[1]);
				
				//Show WinSmart
				if (this.WinSmartFlag) Element.show(this.id+'WinSmart');
				
				//OImgPreviewer.ImgPreviewerPicker.ClearWin
				if (typeof(OImgPreviewer) != 'undefined') OImgPreviewer.ImgPreviewerPicker.ClearWin();
				break;
			case 'routingV2ErrorNone':
				var ElementPosArray = getPosition($('SearchRoute'));
				ElementPosArray[0] += 100;
				ElementPosArray[1] -= 18;
				this.WarnTxt = '很抱歉，您輸入的地點無法找到，請重新輸入。';
				this.ShowWinWarn(ElementPosArray);
				break;
			case 'routingV2ErrorRouteNone':
				var ElementPosArray = getPosition($('SearchRoute'));
				ElementPosArray[0] += 100;
				ElementPosArray[1] -= 18;
				this.WarnTxt = '很抱歉，無法規劃您的路線。';
				this.ShowWinWarn(ElementPosArray);
				break;
			case 'addressingSuccess':
				this.ClearO();
				this.DataSet = JSON.Data[0].DataElement;
				var MyDataArray = this.DataSet.split('^');
				if (typeof(MapStatus) != 'undefined') MapStatus.ZoomClickAction(9, new ULatLng(MyDataArray[0], MyDataArray[1]));
				break;
			case 'addressingErrorNone':
				this.ClearO();
				var ElementPosArray = getPosition($('LFSearchSection'));
				ElementPosArray[0] += 0;
				ElementPosArray[1] += 35;
				this.WarnTxt = '很抱歉，您輸入的地點無法找到，請重新輸入。';
				this.ShowWinWarn(ElementPosArray);
				break;
			case 'ListPOIsSuccess':
				this.ClearO();
				this.DataSet = JSON.Data[0].DataElement;
				var MyDataArray = this.DataSet.split('^');
				if (typeof(MapStatus) != 'undefined') MapStatus.ZoomClickAction(9, new ULatLng(MyDataArray[0], MyDataArray[1]));
				break;
			case 'ListPOIsErrorNone':
				this.ClearO();
				var ElementPosArray = getPosition($('LFSearchSection'));
				ElementPosArray[0] += 0
				ElementPosArray[1] += 35;
				this.WarnTxt = '很抱歉，您輸入的地點無法找到，請重新輸入。';
				this.ShowWinWarn(ElementPosArray);
				break;
			default:
				//alert(JSON.info);
		}//end switch
	},
	RouteClickAction: function(FuncID){
		Element.hide(this.getWinWarn);
		var GetFather = FuncID.parentNode;
		var GetSection = $(FuncID.ObjID+'WinSmartMain').getElementsByTagName('div');
		for (var i=0; i<GetSection.length; i++) {
			if (GetFather == GetSection[i]) {
				GetSection[i].className = 'RouteElementFunc';
				MapStatus.Map.refreshMap();
				(i == (GetSection.length-2)) ? MapStatus.ZoomClickAction(this.ViewORoute[0], this.ViewORoute[1]) : MapStatus.ZoomClickAction(9, this.RouteLatLngSet[i]);
			} else GetSection[i].className = 'RouteElement';
		}//end for
	},
	WinSmartInit: function() {
		if (this.getWinSmart) return;
		var WinSmart = document.createElement('div');
		WinSmart.id = this.id + 'WinSmart';
		WinSmart.className = 'WinSmart';
		var ElementPosArray = getPosition(this.MapSection);
		WinSmart.style.left = ElementPosArray[0] + 5 + 'px';
		WinSmart.style.top = ElementPosArray[1] + 5 + 'px';
		WinSmart.style.zIndex = Number($('WinInfoB').getStyle('z-index')) + 10;;
		document.getElementsByTagName('body')[0].appendChild(WinSmart);
		this.getWinSmart = WinSmart;
		Element.hide(WinSmart);
		
		//WinSmartTop
		var WinSmartTop = document.createElement('div');
		WinSmartTop.id = this.id + 'WinSmartTop';
		WinSmartTop.className = 'WinSmartTop';
		WinSmartTop.style.cursor = 'move';
		WinSmartTop.onmousedown = RouteModuleClass.WinDragStart;
		WinSmart.appendChild(WinSmartTop);
		//WinSmartTop - WinSmartBtn
		var WinSmartBtn = document.createElement('div');
		WinSmartBtn.className = 'WinSmartBtn';
		WinSmartBtn.ObjID = this.id;
		WinSmartTop.appendChild(WinSmartBtn);
		//WinSmartMid
		var WinSmartMid = document.createElement('div');
		WinSmartMid.className = 'WinSmartMid';
		WinSmartMid.style.height = this.WinSmartMidHeight + 'px';
		WinSmart.appendChild(WinSmartMid);
		//WinSmartMid - WinSmartMain
		var WinSmartMain = document.createElement('div');
		WinSmartMain.id = this.id + 'WinSmartMain';
		WinSmartMain.className = 'WinSmartMain';
		WinSmartMain.style.height = this.WinSmartMidHeight + 'px';
		WinSmartMid.appendChild(WinSmartMain);
		//WinSmartBottom
		var WinSmartBottom = document.createElement('div');
		WinSmartBottom.className = 'WinSmartBottom';
		WinSmart.appendChild(WinSmartBottom);
		
		WinSmartBtn.onmouseover = function() { this.style.backgroundPosition = '-13px 0px'; };
		WinSmartBtn.onmouseout = function() { this.style.backgroundPosition = '0px 0px'; };
		WinSmartBtn.onclick = function() { RouteModuleClass.ClearO(); };
	},
	ClearO: function() {
		if (this.getWinSmart) Element.hide(this.getWinSmart);
		if (this.getWinWarn) Element.hide(this.getWinWarn);
		if (this.Polyline) MapStatus.Map.removeOverlay(this.Polyline);
		if (this.SetSPoint) MapStatus.Map.removeOverlay(this.SetSPoint);
		if (this.SetEPoint) MapStatus.Map.removeOverlay(this.SetEPoint);
	},
	ResetRSSEns: function() {
		clearTimeout(this.TimerIntervalID);
		this.getContainer.style.left = '-1000px';
		this.getRSSEns.style.top = '-39px';
		this.getRSSEns.CurrentPosY = -39;
		this.getRSSEns.MoveFlagY = false;
		this.getRSSEns.StartY = -39;
		this.getRSSEns.GoalY = 0;
		Element.hide(this.getWinWarn);
	},
	DragFuncSetInit: function() {
		var GetHtml = document.getElementsByTagName('html')[0];
		//GetHtml.onmousemove = RouteModuleClass.TotalMoveEffect;
		//GetHtml.onmouseup = RouteModuleClass.TotalMoveEnd;
		GetHtml.onmousemove = function(e) {
			if (typeof(TotalMoveEffect) == 'function') TotalMoveEffect(e);
			RouteModuleClass.TotalMoveEffect(e);
		}
		GetHtml.onmouseup = function(e) {
			if (typeof(TotalMoveEnd) == 'function') TotalMoveEnd(e);
			RouteModuleClass.TotalMoveEnd(e);
		}
		this.DragFuncSetFlag = true;
	},
	SelectionSwitch : function(Mode) {
		var GetBody = document.getElementsByTagName('body')[0];
		switch (Mode) {
			case 'Disable':
				GetBody.onselectstart = function() { return false; }; //IE route
				GetBody.style.MozUserSelect = "none"; //Firefox route
				break;
			default:
				GetBody.onselectstart = function() { return true; };  //IE route
				GetBody.style.MozUserSelect = "block"; //Firefox route
		}//end switch
	},
	WinDragStart: function(e) {
		if (!RouteModuleClass.DragFuncSetFlag) return;
		var MouseStatus;
		if (window.event && window.event.srcElement) MouseStatus = window.event.button;
		else if (e && e.target) MouseStatus = e.button;
		var target = getEventTarget(e);
		var FuncID = target.id.replace('WinSmartTop', '');
		//var ObjMapping = eval('MapCreator'+FuncID);
		if (!RouteModuleClass.WinSmartDrag) return;
		if (MouseStatus == 0 || MouseStatus == 1) {
			if (RouteModuleClass.Browser.indexOf('ie') != -1 && MouseStatus != 1) return;
			var ElementPosArray = getPosition($(FuncID+'WinSmart'));
			var PageSizeArray = getPageSize();
			var PageScroll = getPageScroll();
			var MouseX = (RouteModuleClass.Browser.indexOf('ie') != -1) ? window.event.x : e.pageX;
			var MouseY;
			if (RouteModuleClass.Browser.indexOf('ie') != -1) {
				MouseY = window.event.y + PageScroll[1];
			} else MouseY = e.pageY;
			
			//if (MouseX > PageSizeArray[0] || MouseY > PageSizeArray[1]) return;
			RouteModuleClass.DragFlag = true;
			RouteModuleClass.WinPosX = ElementPosArray[0];
			RouteModuleClass.WinPosY = ElementPosArray[1];
			RouteModuleClass.MoveDistX = MouseX - ElementPosArray[0];
			RouteModuleClass.MoveDistY = MouseY - ElementPosArray[1];
			RouteModuleClass.FuncWinDrag = FuncID;
			RouteModuleClass.SelectionSwitch('Disable');
			
			$(FuncID+'WinSmart').className = 'WinSmartDrag';
		}//end if
	},
	TotalMoveEffect: function(e) {
		var FuncID = RouteModuleClass.FuncWinDrag;
		if (!RouteModuleClass.DragFlag) return;
		var PageScroll = getPageScroll();
		var MouseX = (RouteModuleClass.Browser.indexOf('ie') != -1) ? window.event.x : e.pageX;
		var MouseY;
		if (RouteModuleClass.Browser.indexOf('ie') != -1) {
			MouseY = window.event.y + PageScroll[1];
		} else MouseY = e.pageY;
		
		RouteModuleClass.WinPosX = MouseX - RouteModuleClass.MoveDistX;
		RouteModuleClass.WinPosY = MouseY - RouteModuleClass.MoveDistY;
		
		$(FuncID+'WinSmart').style.left = RouteModuleClass.WinPosX + 'px';
		$(FuncID+'WinSmart').style.top = RouteModuleClass.WinPosY + 'px';
	},
	TotalMoveEnd: function(e) {
		var FuncID = RouteModuleClass.FuncWinDrag;
		if (!RouteModuleClass.DragFlag) return;
		var MouseStatus;
		if (window.event && window.event.srcElement) MouseStatus = window.event.button;
		else if (e && e.target) MouseStatus = e.button;
		if (MouseStatus == 0 || MouseStatus == 1) {
			if (RouteModuleClass.Browser.indexOf('ie') != -1 && MouseStatus != 1) return;
			RouteModuleClass.MoveDistX = 0;
			RouteModuleClass.MoveDistY = 0;
			RouteModuleClass.DragFlag = false;
			RouteModuleClass.FuncWinDrag = '';
			RouteModuleClass.SelectionSwitch('Enable');
			$(FuncID+'WinSmart').className = 'WinSmart';
		}//end if
	},
	Constructor: function(DataObj) {
		if (!$('WinInfoB') || !$(DataObj.MapSection)) return;
		this.MapSection = $(DataObj.MapSection);
		var getBody = document.getElementsByTagName('body')[0];
		
		//WinWarn
		var WinWarn = document.createElement('div');
		WinWarn.id = 'WinWarn';
		getBody.appendChild(WinWarn);
		this.getWinWarn = WinWarn;
		Element.hide(WinWarn);
		//WinWarn - WarnTxt
		var WarnTxt = document.createElement('span');
		WarnTxt.id = 'WarnTxt';
		WarnTxt.innerHTML = '很抱歉，您輸入的地點無法找到，請重新輸入。';
		WinWarn.appendChild(WarnTxt);
		this.getWinWarn.Msg = WarnTxt;
		//WinWarn - WinWarnBtn
		var WinWarnBtn = document.createElement('span');
		WinWarnBtn.id = 'WinWarnBtn';
		WinWarnBtn.onmouseover = function() { this.style.backgroundPosition = '-13px 0px'; };
		WinWarnBtn.onmouseout = function() { this.style.backgroundPosition = '0px 0px'; };
		WinWarnBtn.onclick = function() { Element.hide('WinWarn'); };
		WinWarn.appendChild(WinWarnBtn);
		
		//RouteLoadingSection
		var RouteLoadingSection = document.createElement('div');
		RouteLoadingSection.id = 'RouteLoadingSection';
		RouteLoadingSection.style.left = '250px';
		RouteLoadingSection.style.top = '250px';
		RouteLoadingSection.style.zIndex = Number($('WinInfoB').getStyle('z-index')) + 10;
		getBody.appendChild(RouteLoadingSection);
		this.getRLS = RouteLoadingSection;
		Element.hide(this.getRLS);
		//RouteLoadingSection - OverlaySet
		var OverlaySet = document.createElement('div');
		OverlaySet.className = 'OverlaySet';
		RouteLoadingSection.appendChild(OverlaySet);
		this.getRLSO = OverlaySet;
		//RouteLoadingSection - LoadingSection
		var LoadingSection = document.createElement('div');
		LoadingSection.id = 'LoadingSection';
		LoadingSection.style.left = '5px';
		LoadingSection.style.top = '5px';
		if (this.BrowserPNGfix.indexOf(this.Browser) != -1) {
			LoadingSection.style.background = 'url(/img/transparent.gif)';;
			LoadingSection.style.filter = 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src=/img/Map/BackLoading.png, sizingMethod=scale)';
		}//end if
		RouteLoadingSection.appendChild(LoadingSection);
		
		//RSSContainer
		var RouetSetContainer = document.createElement('div');
		RouetSetContainer.className = 'RouetSetContainer';
		//RouetSetContainer.style.background = '#ff0000';
		RouetSetContainer.style.left = '-1000px';
		RouetSetContainer.style.zIndex = Number($('WinInfoB').getStyle('z-index')) + 5; 
		RouetSetContainer.style.clip = 'rect(0px, ' + 202 + 'px, ' + 39 + 'px, 0px)';
		getBody.appendChild(RouetSetContainer);
		this.getContainer = RouetSetContainer;
		
		//RouteSearchSection
		var RouteSearchSection = document.createElement('div');
		RouteSearchSection.id = 'RouteSearchSection';
		RouteSearchSection.style.top = '-39px';
		RouteSearchSection.CurrentPosY = -39;
		RouteSearchSection.MoveFlagY = false;
		RouteSearchSection.StartY = -39;
		RouteSearchSection.GoalY = 0;
		RouteSearchSection.onmouseover = function() { RouteModuleClass.RollEventAction('Over');	};
		//RouteSearchSection.onmouseout = function() { RouteModuleClass.RollEventAction('Out'); };
		RouetSetContainer.appendChild(RouteSearchSection);
		this.getRSSEns = RouteSearchSection;
		//RouteSearchSection - AddrTypeSelect
		var AddrTypeSelect = document.createElement('select');
		AddrTypeSelect.id = 'AddrTypeSelect';
		AddrTypeSelect.className = 'AddrTypeSelectSet';
		RouteSearchSection.appendChild(AddrTypeSelect);
		for (var i=0; i<this.AddrType.length; i++) {
			var OptionElement = document.createElement('option');
			OptionElement.value = this.AddrType[i].ValueSet;
			AddrTypeSelect.appendChild(OptionElement);
			var TextNode = document.createTextNode(this.AddrType[i].OptionSet);
			OptionElement.appendChild(TextNode);
		}//end for
		//RouteSearchSection - SearchRoute
		var SearchRoute = document.createElement('input');
		SearchRoute.id = 'SearchRoute';
		SearchRoute.name = 'SearchRoute';
		SearchRoute.className = 'SearchRouteInputSet';

		SearchRoute.type = 'text';
		SearchRoute.value = '請輸入出發點';
		SearchRoute.onfocus = function() {
			RouteModuleClass.RollEventAction('Over');
			RouteModuleClass.ClearDefault(this.id, '請輸入出發點');
		};
		SearchRoute.onblur = function() { RouteModuleClass.RecoverDefault(this.id, '請輸入出發點'); };
		SearchRoute.onkeydown = function(e) {
			if ($('WinWarn')) Element.hide('WinWarn');
			if (RouteModuleClass.GetKeyCode(e) != 13) return;
			RouteModuleClass.SearchCheck('routing');
		}
		RouteSearchSection.appendChild(SearchRoute);
		//RouteSearchSection - BtnRoute
		var BtnRoute = document.createElement('div');
		BtnRoute.id = 'BtnRoute';
		BtnRoute.onclick = function() { RouteModuleClass.SearchCheck('routing'); };
		RouteSearchSection.appendChild(BtnRoute);
		
		//WinInfoB
		$('WinInfoBTR').style.top = '2px';
		if (!$('WinInfoFuncSection')) {
			var WinInfoFuncSection = document.createElement('div');
			WinInfoFuncSection.id = 'WinInfoFuncSection';
			$('WinInfoB').appendChild(WinInfoFuncSection);
		} else WinInfoFuncSection = $('WinInfoFuncSection');
		
		var RouteTrigger = document.createElement('div');
		RouteTrigger.id = 'RouteTrigger';
		RouteTrigger.className = 'RouteTriggerOut';
		RouteTrigger.innerHTML = this.ActiveValue;
		RouteTrigger.onmouseover = function() { this.className = 'RouteTriggerOver'; };
		RouteTrigger.onmouseout = function() { this.className = 'RouteTriggerOut'; };
		RouteTrigger.onclick = function() { RouteModuleClass.VaryAction() };
		WinInfoFuncSection.appendChild(RouteTrigger);
		this.getTrigger = RouteTrigger;
		
		this.WinSmartMidHeight = (!DataObj.WinSmartHeight) ? 300 : DataObj.WinSmartHeight;
		this.WinSmartDrag = (DataObj.WinSmartDrag == undefined) ? true : DataObj.WinSmartDrag;
		if (this.WinSmartDrag) this.DragFuncSetInit();
		
		this.IconSetInit();
		
		//extend OImgPreviewer.ImgPreviewerPicker.ClearWin
		if (typeof(OImgPreviewer) != 'undefined') {
			var OriginalFunc = OImgPreviewer.ImgPreviewerPicker.ClearWin;
			Object.extend(OImgPreviewer.ImgPreviewerPicker, {
							ClearWin: function() {
								OriginalFunc();	
								//Route
								RouteModuleClass.ResetRSSEns();
							}
			});
		}//end if
		
		//MapStatus.ClearWin
		if (typeof(MapStatus.ClearWin) == 'function') {
			var ClearWinFunc = MapStatus.ClearWin;
			Object.extend(MapStatus, {
							ClearWin: function() {
								ClearWinFunc();	
								//Route
								RouteModuleClass.ResetRSSEns();
							}
			});
		}//end if
		
		//MapStatus.HideOWin
		if (typeof(MapStatus.HideOWin) == 'function') {
			var HideOWinFunc = MapStatus.HideOWin;
			Object.extend(MapStatus, {
							HideOWin: function() {
								HideOWinFunc();	
								//Route
								RouteModuleClass.ClearO();
							}
			});
		}//end if*/
		
		//extend MapStatus.IconClickAction
		/*if (typeof(MapStatus) != 'undefined') {
			var OriginalFunc = MapStatus.IconClickAction;
			Object.extend(MapStatus, {
							IconClickAction: function(Lat, Lng) {
								OriginalFunc();	
								//Route
								RouteModuleClass.ResetRSSEns();
							}
			});
		}//end if*/
		
		//LocateFunc
		this.LocateFunc = (DataObj.LocateFunc != undefined) ? DataObj.LocateFunc : false;
		if (!this.LocateFunc || !$('LFSection')) return;
		//LFSearchSection
		//var ElementPosArray = getPosition(this.MapSection);
		var LFSearchSection = document.createElement('div');
		LFSearchSection.id = 'LFSearchSection';
		//LFSearchSection.style.left = ElementPosArray[0] + 38 + 'px';
		//LFSearchSection.style.top = ElementPosArray[1] + 5 + 'px';
		LFSearchSection.style.zIndex = Number($('WinInfoB').getStyle('z-index')) + 5; 
		//getBody.appendChild(LFSearchSection);
		$('LFSection').appendChild(LFSearchSection);
		//LFSearchSection - SearchLocate
		var SearchLocate = document.createElement('input');
		SearchLocate.id = 'SearchLocate';
		SearchLocate.name = 'SearchLocate';
		SearchLocate.type = 'text';
		SearchLocate.value = '請輸入搜尋點';
		SearchLocate.onfocus = function() {
			RouteModuleClass.RollEventAction('Over');
			RouteModuleClass.ClearDefault(this.id, '請輸入搜尋點');
		}
		SearchLocate.onblur = function() { RouteModuleClass.RecoverDefault(this.id, '請輸入搜尋點'); };
		SearchLocate.onkeydown = function(e) {
			if ($('WinWarn')) Element.hide('WinWarn');
			if (RouteModuleClass.GetKeyCode(e) != 13) return;
			RouteModuleClass.SearchCheck('locating');
		}
		LFSearchSection.appendChild(SearchLocate);
		//LFSearchSection - LFAddrTypeSelect
		var LFAddrTypeSelect = document.createElement('select');
		LFAddrTypeSelect.id = 'LFAddrTypeSelect';
		LFAddrTypeSelect.style.marginLeft = '5px';
		LFAddrTypeSelect.className = 'AddrTypeSelectSet';
		LFSearchSection.appendChild(LFAddrTypeSelect);
		for (var i=0; i<this.AddrType.length; i++) {
			var OptionElement = document.createElement('option');
			OptionElement.value = this.AddrType[i].ValueSet;
			LFAddrTypeSelect.appendChild(OptionElement);
			var TextNode = document.createTextNode(this.AddrType[i].OptionSet);
			OptionElement.appendChild(TextNode);
		}//end for
		//LFSearchSection - SubmitLocate
		var SubmitLocate = document.createElement('div');
		SubmitLocate.id = 'SubmitLocate';
		SubmitLocate.onmouseover = function() { this.style.backgroundPosition = '-52px 0px'; };
		SubmitLocate.onmouseout = function() { this.style.backgroundPosition = '0px 0px'; };
		SubmitLocate.onclick = function() { RouteModuleClass.SearchCheck('locating'); };
		LFSearchSection.appendChild(SubmitLocate);
	}
};

var MapCommonClass = {
	PreQueryDone: function(JSON) {
		//this.TunnelSwitch('Close', JSON.ObjID);
		if (JSON.info.indexOf('ErrorRefer') != -1 || JSON.info == 'QueryError') window.location = 'http://photo.pchome.com.tw/';
		RouteModuleClass.QueryDone(JSON);
	}
};
