﻿	var CongRouteListViewer = function() {
		this.rotaionImage0 = "/congsoa/images/route/direction01.gif";
		this.rotaionImage1 = "/congsoa/images/route/direction02.gif";
		this.rotaionImage2 = "/congsoa/images/route/direction04.gif";
		this.rotaionImage3 = "/congsoa/images/route/direction03.gif";
		this.rotaionImage4 = "/congsoa/images/route/direction05.gif";
	};
	
	CongRouteListViewer.prototype.getTemplateEngine = function(mode, pathdata, template, pulictype) {
		var te = new CongTemplateEngine(template);
		if(mode == 'road')	{
			return this.getRoadListTemplate(mode, pathdata, te);
		}
		else if (mode == 'publictraffic')	{
			return this.getPublicRoadListTemplate(mode, pathdata, te, pulictype);
		}
		else if (mode == 'pathdetail')	{
			return this.getPathInfoTemplate(mode, pathdata, te);
		}
		else {
			return this.getPathListTemplate(mode, pathdata, te);
		}
	}
	
	CongRouteListViewer.prototype.getPathListTemplate = function(mode, pathdata, te) {
		te.setParameter("mode",mode);
		te.setParameter("TotalDist",pathdata.dist/1000 + " km");		// 총 길이

		var time = pathdata.time;
		if(((time/60)/60) >= 1) {
			time =  parseInt(time/(60*60)) + "시간 " + parseInt((time%(60*60))/60) + "분";
		}
		else {
			time = Math.round(time/60) + "분";
		}
		
		te.setParameter("TotalTime",time);						// 총 소요 시간
		te.setParameter("PassCount",pathdata.sections.length <= 1 ? "none" : "");
		
		
		var linkinfo = new Array();
		var passinfo = new Array();
		var sections = null;
		var idx = 0;
		var startClass = ""
		var endClass = ""
		var nodeLength = 0;
		var startNodeLength = null;
		var endNodeLength = null;
		var nodeCnt = 0;
		var road_dist = null;
		var road_time = null;
		
		for(var i=0;i<pathdata.sections.length;i++) {
			sections = pathdata.sections[i];
			for(var j=0; j < sections.links.length; j++) {
				linkinfo[idx] = new Object();
				switch(sections.nodes[j].rotation) {
		    		case 0 : linkinfo[idx].rotation = this.rotaionImage0; break;
			    	case 1 : linkinfo[idx].rotation = this.rotaionImage1; break;
			    	case 2 : linkinfo[idx].rotation = this.rotaionImage2; break;
		    		case 3 : linkinfo[idx].rotation = this.rotaionImage3; break;
			    	case 4 : linkinfo[idx].rotation = this.rotaionImage4; break;
			    	default : linkinfo[idx].rotation = this.rotaionImage0; break;
			    }
				
				if(typeof(sections.nodes[j].name) == 'undefined' || sections.nodes[j].name == null || sections.nodes[j].name.length <= 0) {
					linkinfo[idx].start_node_name = linkinfo[idx-1].end_node_name;
					startClass = "thm7";
					startNodeLength = "(" + nodeLength + "m)";
				}
				else {
					linkinfo[idx].start_node_name = sections.nodes[j].name;
					startClass = "";
					nodeLength = 0;
					startNodeLength="";
				}
				
				if(typeof(sections.nodes[j+1].name) == 'undefined' || sections.nodes[j+1].name == null || sections.nodes[j+1].name.length <= 0) {
					linkinfo[idx].end_node_name = linkinfo[idx].start_node_name;
					endClass = "thm7";
					nodeLength = nodeLength + sections.links[j].dist;
					endNodeLength = "(" + nodeLength + "m)";
				}
				else {
					linkinfo[idx].end_node_name = sections.nodes[j+1].name;
					endClass = "";
					nodeLength = 0;
					endNodeLength = "";
				}
				
				linkinfo[idx].start_node_num = nodeCnt+1;			// 출발 노드 번호(노드 번호는 1부터 시작)
				linkinfo[idx].end_node_num = nodeCnt+2;				// 도착 노드 번호
				
				road_dist = sections.links[j].dist;			
				if((road_dist/1000) >= 1) road_dist = Math.round((road_dist/1000) * 10) / 10 + " km";
				else road_dist = road_dist + " m";
				linkinfo[idx].road_dist = road_dist;				// 노드와 노드 사이의 도로 길이
				linkinfo[idx].road_name = sections.links[j].name;	// 노드와 노드 사이의 도로 이름
				linkinfo[idx].start_class = startClass;				// 도로 이름이 없을 경우의  class 이름
				linkinfo[idx].start_node_length = startNodeLength;	// 시작 노드의 길이
				linkinfo[idx].end_class = endClass;					// 도로 이름이 없을 경우의  class 이름
				linkinfo[idx].end_node_length = endNodeLength;		// 도착 노드의 길이
				linkinfo[idx].start_node_id = j;
				linkinfo[idx].end_node_id = j+1;
				linkinfo[idx].section_id = i;
				idx++;
				nodeCnt++;
			}
			nodeCnt++;
			
			if(pathdata.sections.length > 1) {
				passinfo[i] = new Object();
				if(i == 0) {
					passinfo[i].start_name = "출발지";
					passinfo[i].end_name = "경유지"+(i+1);
				}
				else {
					passinfo[i].start_name = "경유지"+(i);
					if(i == (pathdata.sections.length - 1)) {
						passinfo[i].end_name = "도착지";
					}
					else {
						passinfo[i].end_name = "경유지"+(i+1);
					}	
				}
				road_dist = sections.dist;			
				if((road_dist/1000) >= 1) road_dist =  road_dist/1000 + " km";
				else road_dist = road_dist + " m";
				passinfo[i].road_dist = road_dist;
				road_time = sections.time;
				if(((road_time/60)/60) >= 1) {
					road_time =  parseInt(road_time/(60*60)) + "시간 " + parseInt((road_time%(60*60))/60) + "분";
				}
				else {
					road_time =  Math.round(road_time/60) + "분";
				}
				passinfo[i].road_time = road_time;
			}
			else if(i == 0) {
				passinfo[i] = new Object();
				passinfo[i].start_name = "";
				passinfo[i].end_name = "";
				passinfo[i].road_dist = "";
				passinfo[i].road_time = "";
			}
		}
		
		te.setParameter("LinkInfo",linkinfo);
		te.setParameter("PassInfo",passinfo);
		
		return te;
	}

	CongRouteListViewer.prototype.getPublicRoadListTemplate = function(mode, routedata, te, pubtraffictype) {

		te.setParameter("mode",mode);

		var resultInfo = new Object();
		var buslinkinfo = new Array();
		var subwaylinkinfo = new Array();
		var mixedlinkinfo = new Array();
		

		if (routedata != null && routedata.length > 0) {
			for (i = 0; i < routedata.length ; i++) {
				var pubinfo = {pubtype:"", path:"", pathimg:"", payamt:"", totdist:"", tottime:"", walkdist:"",stopinfo:"",idx:0, pathdetail:"", pathidx:0};
				var pathinfo = "";
				var pathimginfo = "";
				var busstopinfo = 0;
				var substopinfo = 0;
				var walkinfo = 0;
				var pathdetail = "";
				
				pubinfo.pubtype = routedata[i].vehicleType;
				pubinfo.pathidx = i;
				if (routedata[i].totalFare > 0) {
					pubinfo.payamt = routedata[i].totalFare + " 원";
				} else pubinfo.payamt = "";
				
				//alert('교통수단 : ' + routedata[i].vehicleType);
				if((routedata[i].totalDist/1000) >= 1) pubinfo.totdist =  routedata[i].totalDist/1000 + " km";
				else pubinfo.totdist = routedata[i].totalDist + " m";
				
				if(((routedata[i].totalTime/60)/60) >= 1) {
					pubinfo.tottime =  parseInt(routedata[i].totalTime/(60*60)) + "시간 " + parseInt((routedata[i].totalTime%(60*60))/60) + "분";
				}
				else {
					pubinfo.tottime =  Math.round(routedata[i].totalTime/60) + "분";
				}
				
				if (routedata[i].sectionRoutings != null && routedata[i].sectionRoutings.length > 0) {
					
					for (j = 0; j < routedata[i].sectionRoutings.length; j++) {
						
						if (routedata[i].sectionRoutings[j].type == '02') {
							//alert(routedata[i].sectionRoutings[j].name);
							if (pathinfo == "")
								pathinfo = routedata[i].sectionRoutings[j].name + "(" + routedata[i].sectionRoutings[j].nodes[0].name.replace(",","") + ")";
							else 
								pathinfo = pathinfo  + " → " + routedata[i].sectionRoutings[j].name + "(" + routedata[i].sectionRoutings[j].nodes[0].name.replace(",","") + ")";
							

							if (pathimginfo == "") {
								var subimg = this.getSubType(routedata[i].sectionRoutings[j].subType);
								pathimginfo = routedata[i].sectionRoutings[j].name + "(" + routedata[i].sectionRoutings[j].nodes[0].name.replace(",","") + ")," + subimg.img;
								//alert('1 subimg : ' + subimg + ", pathimginfo : " + pathimginfo);
							} else { 
								var subimg = this.getSubType(routedata[i].sectionRoutings[j].subType);
								pathimginfo = pathimginfo  + "|" + routedata[i].sectionRoutings[j].name + "(" + routedata[i].sectionRoutings[j].nodes[0].name.replace(",","") + ")," + subimg.img;
								//alert('2 subimg : ' + subimg + ", pathimginfo : " + pathimginfo);
							}
							
							busstopinfo = busstopinfo + (routedata[i].sectionRoutings[j].nodes.length - 1);
						} else if (routedata[i].sectionRoutings[j].type == '06') {
							if (pathinfo == "")
								pathinfo = routedata[i].sectionRoutings[j].name + "(" + routedata[i].sectionRoutings[j].nodes[0].name.replace(",","") + ")";
							else 
								pathinfo = pathinfo  + " → " + routedata[i].sectionRoutings[j].name + "(" + routedata[i].sectionRoutings[j].nodes[0].name.replace(",","") + ")";

							if (pathimginfo == "") {
								var subimg = this.getSubType(routedata[i].sectionRoutings[j].subType);
								pathimginfo = routedata[i].sectionRoutings[j].nodes[0].name.replace(",","") + "," + subimg.img;
							} else { 
								var subimg = this.getSubType(routedata[i].sectionRoutings[j].subType);
								pathimginfo = pathimginfo  + "|" + routedata[i].sectionRoutings[j].nodes[0].name.replace(",","") + "," + subimg.img;
							}
							
							substopinfo = substopinfo + (routedata[i].sectionRoutings[j].nodes.length - 1);
						} else if (routedata[i].sectionRoutings[j].type == '99') {
							walkinfo = walkinfo + parseInt(routedata[i].sectionRoutings[j].dist,10);
						}
						
					}
				}
				
				//alert('pathinfo : ' + pathinfo);
				pubinfo.path = pathinfo; 
				pubinfo.pathimg = pathimginfo; 
				if((walkinfo/1000) >= 1) pubinfo.walkdist =  walkinfo/1000 + " km";
				else pubinfo.walkdist = walkinfo + " m ";
				
				if (busstopinfo > 0) {
					pubinfo.stopinfo = pubinfo.stopinfo + "버스 " + busstopinfo + " 개 정류장  "; 
				}
				if (substopinfo > 0) {
					pubinfo.stopinfo = pubinfo.stopinfo + "지하철 " + substopinfo + " 개 정류장  "; 
				}
				
				//alert('pathdetail :' + pathdetail );
				
				if (routedata[i].vehicleType == '02') {
					pubinfo.idx = buslinkinfo.length;
					buslinkinfo.push({"pubinfo":pubinfo});
				} else if (routedata[i].vehicleType == '06') {
					pubinfo.idx = subwaylinkinfo.length;
					subwaylinkinfo.push({"pubinfo":pubinfo});
				} else {
					pubinfo.idx = mixedlinkinfo.length;
					mixedlinkinfo.push({"pubinfo":pubinfo});
				}
			}
		}
		
		if (pubtraffictype == '02') {
			te.setParameter("LinkInfo",buslinkinfo);
		} else if (pubtraffictype == '06') {
			te.setParameter("LinkInfo",subwaylinkinfo);
		} else if (pubtraffictype == '11') {
			te.setParameter("LinkInfo",mixedlinkinfo);
		}
		/*
		te.setParameter("BusLinkInfo",buslinkinfo);
		te.setParameter("SubwayLinkInfo",subwaylinkinfo);
		te.setParameter("MixedLinkInfo",mixedlinkinfo);
		*/
		
		resultInfo = {'buscnt':buslinkinfo.length, 'subwaycnt': subwaylinkinfo.length, 'mixedcnt' : mixedlinkinfo.length };
		te.setParameter("resultInfo",resultInfo);
		return te;
	}
	CongRouteListViewer.prototype.getPathInfoTemplate = function(mode, routedata, te) {

		te.setParameter("mode",mode);

		var pathDetailInfo = new Array();

		
		var pubinfo = {pubtype:"", x:"", y:"", code:"", routetype:"", stopinfoname:"", pathinfo:""};
		
		if (routedata.sectionRoutings != null && routedata.sectionRoutings.length > 0) {
			
			for (j = 0; j < routedata.sectionRoutings.length; j++) {
				var pathdetail = "";
				// 자세한 경로 처리.
				if(routedata.sectionRoutings[j].nodes != null && routedata.sectionRoutings[j].nodes.length > 0) {
					pubinfo = {pubtype:"", x:"", y:"", code:"", routetype:"", stopinfoname:"", pathinfo:""};
					if (j == 0) {
						pubinfo.routetype = "start";
						pubinfo.x = routedata.startPoint.x;
						pubinfo.y = routedata.startPoint.y;
						pubinfo.stopinfoname = routedata.startAddr;
						pathdetail = pathdetail + "도보 " + routedata.sectionRoutings[j].dist + " m 거리 " ;
						if (routedata.sectionRoutings[j].nodes[routedata.sectionRoutings[j].nodes.length -1].type == '02') {
							pathdetail = pathdetail + routedata.sectionRoutings[j].nodes[routedata.sectionRoutings[j].nodes.length -1].name + " 버스 정류장으로 이동";
						} else if (routedata.sectionRoutings[j].nodes[routedata.sectionRoutings[j].nodes.length -1].type == '06') {
							pathdetail = pathdetail + routedata.sectionRoutings[j].nodes[routedata.sectionRoutings[j].nodes.length -1].name + " 으로 이동";
						}
					} else if (j == (routedata.sectionRoutings.length -1)) {
						pubinfo.routetype = "stop";
						pubinfo.stopinfoname = routedata.sectionRoutings[j].nodes[0].name;
						pubinfo.x = routedata.sectionRoutings[j].nodes[0].x;
						pubinfo.y = routedata.sectionRoutings[j].nodes[0].y;
						pathdetail = pathdetail + routedata.sectionRoutings[j].nodes[0].name + " 하차 ";
					} else if (routedata.sectionRoutings[j].type == '02' || routedata.sectionRoutings[j].type == '06'){
						if (routedata.sectionRoutings[j].type == '02' || routedata.sectionRoutings[j].type == '06') {
							if (j == 1) {
								pubinfo.routetype = "ride";
							} else {
								pubinfo.routetype = "transfer";
							}
							pubinfo.pubtype = routedata.sectionRoutings[j].type;
							pubinfo.code = routedata.sectionRoutings[j].nodes[0].id;
							pubinfo.x = routedata.sectionRoutings[j].nodes[0].x;
							pubinfo.y = routedata.sectionRoutings[j].nodes[0].y;

							pubinfo.stopinfoname = routedata.sectionRoutings[j].nodes[0].name;

							//승차하여 7개 정류장 후 재동약국,안국역 버스정류장 하차후 도보 137m 거리 안국역으로 이동
							//을지로3가역 방향 3호선 승차하여 2개 정류장 후 을지로3가역 하차후 도보 180m 거리 을지로3가역으로 이동 

							if (routedata.sectionRoutings[j].type == '02' || routedata.sectionRoutings[j].type == '06') {
								var maintype = routedata.sectionRoutings[j].type;
								var mainname = "";
								if (maintype ==  '02')
									mainname = routedata.sectionRoutings[j].name;
								else if (maintype ==  '06')
									mainname = routedata.sectionRoutings[j].nodes[routedata.sectionRoutings[j].nodes.length -1].name;
								
								pathdetail = pathdetail + (maintype == '02' ? "버스 " + mainname : mainname + "방향 " )+ " 승차하여  " + (routedata.sectionRoutings[j].nodes.length - 1) + " 개 정류장  " + (maintype == '02' ? "정류장" : "") + " 후  " + routedata.sectionRoutings[j].nodes[routedata.sectionRoutings[j].nodes.length -1].name + (maintype == '02' ? "버스 정류장" : "") + "에 하차 ";
								if (routedata.sectionRoutings[j + 1] != null && routedata.sectionRoutings[j + 1].length > 0 ) {
									if (routedata.sectionRoutings[j + 1].nodes != null && routedata.sectionRoutings[j + 1].nodes.length > 0 && (routedata.sectionRoutings[j + 1].nodes[routedata.sectionRoutings[j + 1].nodes.length - 1].type == '02' && routedata.sectionRoutings[j + 1].nodes[routedata.sectionRoutings[j + 1].nodes.length - 1].type == '06')) {
										var vtype = routedata.sectionRoutings[j + 1].nodes[routedata.sectionRoutings[j + 1].nodes.length - 1].type;
										var vname = routedata.sectionRoutings[j + 1].nodes[routedata.sectionRoutings[j + 1].nodes.length - 1].name;
										pathdetail = pathdetail + " 후 도보 " + routedata.sectionRoutings[j + 1].dist + " m 거리 " + (vtype=='02' ? vname + " 정류장" : vname ) + " 으로 이동 ";
									}
								}
							}
						}
					}
					pubinfo.pathinfo = pathdetail;
					if (pubinfo.routetype != "")
						pathDetailInfo.push(pubinfo);
				}
			}
			// 도착 관련 부분 추가.
			pathDetailInfo.push({routetype:"end", x:routedata.endPoint.x, y:routedata.endPoint.y,stopinfoname:routedata.endAddr, pathinfo:""});
		}
		
		
		te.setParameter("PathDetailInfo",pathDetailInfo);
		
		return te;
	}

	CongRouteListViewer.prototype.getRoadListTemplate = function(mode, sections, te) {
		te.setParameter("mode", "none");
				
		var linkinfo = new Array();
		
		for(var j=0; j < sections.nodes.length; j++) {
			linkinfo[j] = new Object();
			linkinfo[j].node_name = sections.nodes[j].name;
			linkinfo[j].node_id = j;
		}
		
		te.setParameter("LinkInfo",linkinfo);
		
		return te;
	}
	
	CongRouteListViewer.prototype.getSubType = function(subtype) {
		  this.subtype=parseInt(subtype,10);
		  //var rv={};
		  
		  var sText="#399ebd,13,일반,bus_green";
		  switch(this.subtype){
			  case 101:  sText="#3979ef,13,간선,bus_blue";     break;//간선
			  case 102:  sText="#52be52,12,지선,bus_green";     break;//지선
			  case 103:  sText="#de2c10,11,광역,bus_red";     break;//광역
			  case 104:  sText="#ffbe00,14,순환,bus_yellow";     break;//순환
			  case 105:  sText="#7b4110,16,공항,bus_air";     break;//공항
			  case 106:  sText="#399ebd,13,일반,bus_green";     break;//일반
			  case 107:  sText="#4a59ad,11,좌석,bus_red";     break;//좌석
			  case 108:  sText="#e7417b,11,급행,bus_red";     break;//급행
			  case 109:  sText="#399ebd,15,마을,bus_maeul";     break;//마을버스
			  case 110:  sText="#E53000,11,직행,bus_red";     break;//직행
	
			  case 61101:  sText="#2934a5,1,1호선,subway_1";     break;//1호선
			  case 61102:  sText="#42ba39,2,2호선,subway_2";     break;
			  case 61103:  sText="#ff7518,3,3호선,subway_3";     break;
			  case 61104:  sText="#298af7,4,4호선,subway_4";     break;
			  case 61105:  sText="#8c4de7,5,5호선,subway_5";     break;
			  case 61106:  sText="#ce8229,6,6호선,subway_6";     break;
			  case 61107:  sText="#636d29,7,7호선,subway_7";     break;
			  case 61108:  sText="#ff2c73,8,8호선,subway_8";     break;
	
			  case 61151:  sText="#638ace,10,인천선,subway_incheon";   break;//인천선
			  case 61152:  sText="#ffc721,9,분당선,subway_bundang";   break;//분당선
			  case 61153:  sText="#216dce,0,공항선,subway";     break;//공항선
			  case 61154:  sText="#52cfde,0,중앙선,subway_0";     break;//중앙선
			  case 61155:  sText="#42ba39,0,2호선지선(신도림),subway_2";  break;//2호선지선(신도림)
			  case 61156:  sText="#42ba39,0,2호선지선(성수),subway_2";   break;//2호선지선(성수)
	
			  case 62101:  sText="#ff7518,1,부산1호선,subway";     break;//부산1호선
			  case 62102:  sText="#39b631,2,부산2호선,subway";     break;//
			  case 62103:  sText="#ce9a42,3,부산3호선,subway";     break;//
	
			  case 62201:  sText="#ff3410,1,대구1호선,subway";     break;//대구
			  case 62202:  sText="#39b631,2,대구2호선,subway";     break;//
	
			  case 62401:  sText="#39b631,1,광주1호선,subway";     break;//광주
			  case 62402:  sText="#2186f7,2,광주2호선,subway";     break;//
	
			  case 62501:  sText="#31b231,1,대전선,subway";     break;//대전1호선
		  }
		  var sub=sText.split(",");
		  
		  return {color:sub[0], id:sub[1], name:sub[2], img:sub[3]};
	}
	