﻿/* RGMan.js*/

function _RGMan() {	
	var RGMan = Class.create( {
		initialize: function(parent, container) {
			this.container = container;
			this.parent = parent;
			this.rpBaseLayer = WSpec.getBaseLayer(29, 'RPBaseLayer');//z-index 번호 다시 설정 필요(29는 PoiLayer 밑에 번호임)
			this.rpLayer = WSpec.getBaseLayer(60, 'RPLayer');
			
			this.renderer = null;
			this.level = null;	//현재 레벨
			this.width = null;	//레이어 넓이
			this.height = null;	//레이어 높이
			this.shape = null;	//경로이미지개체
			
			this.rpBaseLayer.appendChild(this.rpLayer);

			this.setInnerEvent();
			this.initRGMan(WSpec.initlevel);		//layer 사이즈 설정
			
			this.rpState = WSpec.rpready;		//경로 검색 상태 설정
			this.rpData = null;					//경로 데이터 초기화
			
			this.rpLeftTop = null;
			this.rpRightBottom = null;
			
			this.rgPoiIndex = 10;					//RG POI Index
			this.rgCoordIndex = 0;
			this.rpObject = new Array();
			
			this.rpCoords = new Array();
			this.rpOption = {};
		},
		initRenderer: function (layer) {
			//function 렌더러 설정
			//append 된 element를 가지고 초기화해야 한다.
			if (Graphic.rendererSupported("VML")) {
				this.renderer = new Graphic.VMLRenderer(layer);
			}
			else if (Graphic.rendererSupported("SVG")) {
				this.renderer = new Graphic.SVGRenderer(layer);
			}
			else if (Graphic.rendererSupported("Canvas")) {
				this.renderer = new Graphic.CanvasRenderer(layer);
			}
		},
		setInnerEvent: function() {			
			EventMan.addListener(this.parent, 'zoomTileLayer', this.initRGMan.bindAsEventListener(this));
			EventMan.addListener(this.parent, 'moveTileLayer', this.drawRPRG.bindAsEventListener(this));
			
			EventMan.addListener(this.parent, 'onCreate', this.layerAppendEvent.bindAsEventListener(this));
		},
		initRGMan: function(level) {
			//lon,lat좌표로 pixel구하기 위해서 사전작업 필요
//			var xtilecount = WSpec.getXTileCount(level); //이거 일단 주석처리. 잘 될지 돌려보고.
//			var ytilecount = WSpec.getYTileCount(level);
//			
//			this.width = xtilecount * WSpec.tilesize;
//			this.height = ytilecount * WSpec.tilesize;
//			
//			this.rpBaseLayer.style.width = this.width;
//			this.rpBaseLayer.style.height = this.height;

//			this.rpLayer.style.width = this.width;
//			this.rpLayer.style.height = this.height;

			this.level = level;

			this.layerAppendEvent();
			
			if(this.rpData != null) {
				//레벨이 바뀌었을 때, 경로 데이터가 있다면 다시 그린다.
				this.calRPObject(this.rpData);
				this.drawRPRG();
			}
		},
		layerAppendEvent: function() {
			EventMan.trigger(this, 'drawRG', {"level":this.level, "layer":this.rpBaseLayer});
		},
		clearRPRG : function() {
			for(var i = this.rpLayer.childNodes.length - 1; i >= 0; i--) {
				this.rpLayer.removeChild(this.rpLayer.childNodes[i]);
			}
			this.parent.removeGroupPoi(WSpec.rggroupid);
			this.rpData = null;
			this.initCoords();
		},
		getRGState: function() {
			//function 클래스 상태 체크(요청 가능(결과 없음), 현재 검색중, 검색 완료(결과 있음))
			return this.rpState;
		},
		setRGState: function(stateValue) {
			//function 클래스 상태 수정
			this.rpState = stateValue;
		},
		requestRPRG: function(coords, option) {
			var posCoords = "";
			for(var i = 0; i < coords.length; i++) {
				if(i > 0) {
					posCoords += ",";
				}
				posCoords += coords[i].lon + "," + coords[i].lat;
			}
			
			if(option == undefined) {
				option = {"method":WSpec.rpshort, "free":WSpec.rpfreeno, "express":WSpec.rpexpressno, "beginner":WSpec.rpbeginnerno};
			}

			this.rpState = WSpec.rpsearchstart;
			
			new Ajax.Request(WSpec.rgserviceurl,
								{ "method":"POST",
									"parameters":"coords=" + posCoords + "&method=" + option.method + "&free=" + option.free + "&express=" + option.express + "&beginner=" + option.beginner,
									"onSuccess":this.responseRPRG.bind(this)
								});
		},
		responseRPRG: function(response) {
			if(response.responseText.substr(0, 3) != "200") {
				EventMan.trigger(this, 'failRPRG', response.responseText);
			}
			else {
				eval("this.rpData=" + response.responseText.substring(4, response.responseText.length));
				this.rpState = WSpec.rpsearchend;
				this.calRPObject(this.rpData);
				this.setCenterAndZoom();
				this.drawRPRG();
				EventMan.trigger(this, 'successRPRG');
			}
		},
		calRPObject: function(rpData) {
			var points = new Array();
			var point, nowCoord, nodeCount = 0;
			
			if(this.rpObject != undefined) {
				for(var i = 0; i < this.rpObject.length; i++) {
					this.rpObject[i] = null;
				}
				this.rpObject.length = 0;
			}
			this.rgPoiIndex = 10;
			
			//존재하던 경로 객체를 제거한다.
			for(var i = this.rpLayer.childNodes.length - 1; i >= 0; i--) {
				this.rpLayer.removeChild(this.rpLayer.childNodes[i]);
			}
			
			//출발지, 경유지, 도착지를 다시 표시한다.
			this.parent.removeGroupPoi(WSpec.rggroupid);
			
			for(var i = 0; i < this.rpCoords.length; i++) {
				if(this.rpCoords[i].kind == "start") {
					this.parent.addPoi(WSpec.rggroupid, i,WSpec.rgimagestart,"",{"lon":parseInt(this.rpCoords[i].lon), "lat":parseInt(this.rpCoords[i].lat)},6,26,29,"","");
					this.rpCoords[i].index = i;
					this.rpLeftTop = {"lon": parseInt(this.rpCoords[i].lon), "lat": parseInt(this.rpCoords[i].lat)};
				}
				else if (this.rpCoords[i].kind == "end") {
					this.parent.addPoi(WSpec.rggroupid, i,WSpec.rgimageend,"",{"lon":parseInt(this.rpCoords[i].lon), "lat":parseInt(this.rpCoords[i].lat)},6,26,29,"","");
					this.rpCoords[i].index = i;
					this.rpRightBottom = {"lon": parseInt(this.rpCoords[i].lon), "lat": parseInt(this.rpCoords[i].lat)};
				}
				else {
					this.parent.addPoi(WSpec.rggroupid, i,WSpec.rgimagevia,"",{"lon":parseInt(this.rpCoords[i].lon), "lat":parseInt(this.rpCoords[i].lat)},6,26,29,"","");
					this.rpCoords[i].index = i;
				}
			}

			nowCoord = {"lon":rpData.DataRows.tbt_coord_x * 10, "lat":rpData.DataRows.tbt_coord_y * 10};
			
			point = WSpec.coordToPixel(nowCoord, this.level);
			
			coord = WSpec.pixelToLonLat(this.level, point.x, point.y);
		
			points[0] = new Array(5);
			points[0][0] = point.x;
			points[0][1] = this.height - point.y;
			points[0][2] = nowCoord.lon;
			points[0][3] = nowCoord.lat;
			
			var j = 1;
			
			var pxLeftTop = {"x":points[0][0], "y":points[0][1]}, pxRightBottom = {"x":points[0][0], "y":points[0][1]};
			var lefttop = {"lon": nowCoord.lon, "lat": nowCoord.lat}, rightbottom = {"lon": nowCoord.lon, "lat":nowCoord.lat};

			//전체 영역 좌표를 구한다.
			if(this.rpLeftTop.lon >= nowCoord.lon) this.rpLeftTop.lon = nowCoord.lon;
			if(this.rpLeftTop.lat <= nowCoord.lat) this.rpLeftTop.lat = nowCoord.lat;
			if(this.rpRightBottom.lon <= nowCoord.lon) this.rpRightBottom.lon = nowCoord.lon;
			if(this.rpRightBottom.lat >= nowCoord.lat) this.rpRightBottom.lat = nowCoord.lat;
			
			for(var i = 0; i < rpData.DataRows.tbt_offset.length; i++) {
				nowCoord.lon += rpData.DataRows.tbt_offset[i].x * 10;
				nowCoord.lat += rpData.DataRows.tbt_offset[i].y * 10;

				point = WSpec.coordToPixel(nowCoord, this.level);
				
				if((points[j - 1][0] != point.x && points[j - 1][1] != point.y) || parseInt(rpData.DataRows.tbt_node[nodeCount].vertex_id) - 2 == i) {
					points[j] = new Array(5);
					
					points[j][0] = point.x;
					points[j][1] = this.height - point.y;
					points[j][2] = nowCoord.lon;
					points[j][3] = nowCoord.lat;
					
					//경로 정보가 존재하는 좌표인가?
					if(parseInt(rpData.DataRows.tbt_node[nodeCount].vertex_id) - 2 == i) {
						if(rpData.DataRows.tbt_node[nodeCount].tbt_text != undefined) {
							points[j][4] = rpData.DataRows.tbt_node[nodeCount].rg_code;
						}
						nodeCount++;
					}
					
					//rpOject의 픽셀 영역 좌표를 구한다.
					if(pxLeftTop.x >= points[j][0]) pxLeftTop.x = points[j][0];
					if(pxLeftTop.y >= points[j][1]) pxLeftTop.y = points[j][1];
					if(pxRightBottom.x <= points[j][0]) pxRightBottom.x = points[j][0];
					if(pxRightBottom.y <= points[j][1]) pxRightBottom.y = points[j][1];
					
					//rpObject의 영역 좌표를 구한다.
					if(lefttop.lon >= nowCoord.lon) lefttop.lon = nowCoord.lon;
					if(lefttop.lat <= nowCoord.lat) lefttop.lat = nowCoord.lat;
					if(rightbottom.lon <= nowCoord.lon) rightbottom.lon = nowCoord.lon;
					if(rightbottom.lat >= nowCoord.lat) rightbottom.lat = nowCoord.lat;
					
					//전체 영역 좌표를 구한다.
					if(this.rpLeftTop.lon >= nowCoord.lon) this.rpLeftTop.lon = nowCoord.lon;
					if(this.rpLeftTop.lat <= nowCoord.lat) this.rpLeftTop.lat = nowCoord.lat;
					if(this.rpRightBottom.lon <= nowCoord.lon) this.rpRightBottom.lon = nowCoord.lon;
					if(this.rpRightBottom.lat >= nowCoord.lat) this.rpRightBottom.lat = nowCoord.lat;
					
					//WSpec.rpcount개씩 포인트 좌표를 rpObject로 담는다.
					if((j % WSpec.rpcount) == 0) {
						var rpIndexCount = j / WSpec.rpcount - 1;
						this.rpObject[rpIndexCount] = {};
						this.rpObject[rpIndexCount].points = new Array();
						
						for(var o = (j - WSpec.rpcount); o <= j; o++) {
							var rpIndex = this.rpObject[rpIndexCount].points.length;
							this.rpObject[rpIndexCount].points[rpIndex] = new Array(4);
							this.rpObject[rpIndexCount].points[rpIndex][0] = points[o][0];
							this.rpObject[rpIndexCount].points[rpIndex][1] = points[o][1];
							this.rpObject[rpIndexCount].points[rpIndex][2] = points[o][2];
							this.rpObject[rpIndexCount].points[rpIndex][3] = points[o][3];
							if(points[o][4] != undefined) {
								this.rpObject[rpIndexCount].points[rpIndex][4] = points[o][4];
							}
						}

						//this.rpObject[rpIndexCount].points = points.slice(j - WSpec.rpcount, j);
						this.rpObject[rpIndexCount].draw = false;
						this.rpObject[rpIndexCount].leftTop = lefttop;
						this.rpObject[rpIndexCount].rightBottom = rightbottom;
						this.rpObject[rpIndexCount].pxlefttop = pxLeftTop;
						this.rpObject[rpIndexCount].pxrightbottom = pxRightBottom;
						
						lefttop = {"lon": nowCoord.lon, "lat": nowCoord.lat};
						rightbottom = {"lon": nowCoord.lon, "lat": nowCoord.lat};
						pxLeftTop = {"x":points[j][0], "y":points[j][1]};
						pxRightBottom = {"x":points[j][0], "y":points[j][1]};
					}
					j++;
				}
			}
			

			if((j % WSpec.rpcount) > 0) {
				var lastIndex = parseInt(j / WSpec.rpcount);
				this.rpObject[lastIndex] = {};
				this.rpObject[lastIndex].points = new Array();

				var forIndex = 0;
				for(var o = (lastIndex * WSpec.rpcount) - 1; o < points.length; o++, forIndex++) {
					this.rpObject[lastIndex].points[forIndex] = new Array(4);
					this.rpObject[lastIndex].points[forIndex][0] = points[o][0];
					this.rpObject[lastIndex].points[forIndex][1] = points[o][1];
					this.rpObject[lastIndex].points[forIndex][2] = points[o][2];
					this.rpObject[lastIndex].points[forIndex][3] = points[o][3];
					if(points[o][4] != undefined) {
						this.rpObject[lastIndex].points[forIndex][4] = points[o][4];
					}
				}

				this.rpObject[lastIndex].draw = false;
				this.rpObject[lastIndex].leftTop = lefttop;
				this.rpObject[lastIndex].rightBottom = rightbottom;
				this.rpObject[lastIndex].pxlefttop = pxLeftTop;
				this.rpObject[lastIndex].pxrightbottom = pxRightBottom;
			}
			
			for(var i = 0; i < points.length; i++) {
				points[i][0] = null;
				points[i][1] = null;
				points[i][2] = null;
				points[i][3] = null;
				points[i][4] = null;
				points[i] = null;
			}
			points = null;
			pxLeftTop = null;
			pxRightBottom = null;
			lefttop = null;
			rightbottom = null;			
		},
		setCenterAndZoom: function() {
			//좌표와 레벨을 바꾼다.
			var rpCenterCoord = {"lon":this.rpLeftTop.lon + ((this.rpRightBottom.lon - this.rpLeftTop.lon) / 2), "lat":this.rpRightBottom.lat + ((this.rpLeftTop.lat - this.rpRightBottom.lat) / 2)};
			var rpLeftTopPixel = WSpec.coordToPixel(this.rpLeftTop, this.level);
			var rpRightBottomPixel = WSpec.coordToPixel(this.rpRightBottom, this.level);
			
			
			var nowWidth = Math.abs(rpRightBottomPixel.x - rpLeftTopPixel.x);
			var nowHeight = Math.abs(rpRightBottomPixel.y - rpLeftTopPixel.y);
			var viewPortWidth = WSpec.viewportwidth;
			var viewPortHeight = WSpec.viewportheight;
			
			if(nowWidth > nowHeight) {
				if(viewPortWidth > nowWidth) {
					while(viewPortWidth / 2 >= nowWidth && this.level <= WSpec.maxlevel) {
						this.level = this.level - 1;
						viewPortWidth = viewPortWidth / 2;
					} 
				}
				else {
					do {
						this.level = this.level + 1;
						viewPortWidth = viewPortWidth * 2;
					} while(viewPortWidth <= nowWidth && this.level >= WSpec.minlevel)
				}
			}
			else {
				if(viewPortHeight > nowHeight) {
					while(viewPortHeight / 2 >= nowHeight && this.level <= WSpec.maxlevel) {
						this.level = this.level - 1;
						viewPortHeight = viewPortHeight / 2;
					} 
				}
				else {
					do {
						this.level = this.level + 1;
						viewPortHeight = viewPortHeight * 2;
					} while(viewPortHeight <= nowHeight && this.level >= WSpec.minlevel)
				}
			}
			
			if(this.level > WSpec.maxlevel) this.level = WSpec.maxlevel;
			if(this.level < WSpec.minlevel) this.level = WSpec.minlevel;

			EventMan.trigger(this, 'drawRG', {"level":this.level, "layer":this.rpBaseLayer});
			
			this.parent.setCenterAndZoom(rpCenterCoord, this.level);
			
			rpCenterCoord = null;
			rpLeftTopPixel = null;
			rpRightBottomPixel = null;
		},
		drawRPRG: function() {
			if(this.rpObject == null || this.rpObject.length == 0) {
				return;
			}
			//function 경로 검색 결과 그리기
			bound = this.parent.getBound();
			for(var i = 0; i < this.rpObject.length; i++) {
				if(this.rpObject[i].draw == false) {
					var chkDraw = true;

					if(this.rpObject[i].rightBottom.lat > bound[1].lat) chkDraw = false;
					if(chkDraw && this.rpObject[i].rightBottom.lon < bound[1].lon) chkDraw = false;
					if(chkDraw && this.rpObject[i].leftTop.lat < bound[3].lat) chkDraw = false;
					if(chkDraw && this.rpObject[i].leftTop.lon > bound[3].lon) chkDraw = false;
					/*
					if(!chkDraw) {
						var debugString = "";
						debugString = "leftTop(" + this.rpObject[i].leftTop.lon + "," + this.rpObject[i].leftTop.lat + ")";
						debugString = debugString + " rightBottom(" + this.rpObject[i].rightBottom.lon + "," + this.rpObject[i].rightBottom.lat + ")";
						debugString = debugString + " bound[1](" + parseInt(bound[1].lon) + "," + parseInt(bound[1].lat) + ") bound[3](" + parseInt(bound[3].lon) + "," + parseInt(bound[3].lat) + ")";
						debugString = debugString + " function: " + gradient + "x + " + yconst + " = y";

						$('e').value = $('e').value + "DRAW: " + chkDraw + " " + debugString + "|";
					}
					*/
					if(chkDraw) {
						var lineLayer = WSpec.getBaseLayer(60);
						
						lineLayer.setStyle({left:this.rpObject[i].pxlefttop.x - 10,
										top:this.rpObject[i].pxlefttop.y - 10,
										width:Math.abs(this.rpObject[i].pxlefttop.x - this.rpObject[i].pxrightbottom.x) + 20,
										height:Math.abs(this.rpObject[i].pxlefttop.y - this.rpObject[i].pxrightbottom.y) + 20});

						lineLayer = this.rpLayer.appendChild(lineLayer);
						
						this.initRenderer(lineLayer);
					
						//경로 라인 그리기
						var shape = new Graphic.Polyline(this.renderer);

						for(var k = 0; k < this.rpObject[i].points.length; k++) {
							if(Prototype.Browser.IE) {
								this.rpObject[i].points[k][0] = this.rpObject[i].points[k][0] - this.rpObject[i].pxlefttop.x + 3;
								this.rpObject[i].points[k][1] = this.rpObject[i].points[k][1] - this.rpObject[i].pxlefttop.y + 9;
							}
							else {
								this.rpObject[i].points[k][0] = this.rpObject[i].points[k][0] - this.rpObject[i].pxlefttop.x + 10;
								this.rpObject[i].points[k][1] = this.rpObject[i].points[k][1] - this.rpObject[i].pxlefttop.y + 10;
							}
						}
						
						shape.addPoints(this.rpObject[i].points);
						
						shape.setStroke(this.getStroke());
						shape.setAntialiasing(true);

						this.renderer.add(shape);
						
						//경로 박스 보여주기
						/*
						var box = new Graphic.Polyline(this.renderer);
						var boxpoints = new Array(4);;
						boxpoints[0] = new Array(2);
						boxpoints[1] = new Array(2);
						boxpoints[2] = new Array(2);
						boxpoints[3] = new Array(2);
						boxpoints[4] = new Array(2);
						var pluswidth, plusheight;
						if(Prototype.Browser.IE) {
							pluswidth = 6;
							plusheight = 18;	
						}
						else {
							pluswidth = 20;
							plusheight = 20;
						}
						boxpoints[0][0] = 0;
						boxpoints[0][1] = 0;
						boxpoints[1][0] = Math.abs(this.rpObject[i].pxlefttop.x - this.rpObject[i].pxrightbottom.x) + pluswidth;
						boxpoints[1][1] = 0;
						boxpoints[2][0] = Math.abs(this.rpObject[i].pxlefttop.x - this.rpObject[i].pxrightbottom.x) + pluswidth;
						boxpoints[2][1] = Math.abs(this.rpObject[i].pxlefttop.y - this.rpObject[i].pxrightbottom.y) + plusheight;
						boxpoints[3][0] = 0;
						boxpoints[3][1] = Math.abs(this.rpObject[i].pxlefttop.y - this.rpObject[i].pxrightbottom.y) + plusheight;
						boxpoints[4][0] = 0;
						boxpoints[4][1] = 0;
						
						box.addPoints(boxpoints);
						box.setStroke({r:0, g:113, b:226, a:1000, w:1});
						box.setAntialiasing(true);
						this.renderer.add(box);
						*/

						//경로 정보 보여주기
						for(var k = 0; k < this.rpObject[i].points.length; k++) {
							if(this.rpObject[i].points[k][4] != undefined) {
								var coord = new WCoord();
								coord.lon = this.rpObject[i].points[k][2];
								coord.lat = this.rpObject[i].points[k][3];

								for(var l in WSpec.rgimage) {
									if(WSpec.rgimage[l].rgcode == parseInt(this.rpObject[i].points[k][4])) {
										this.parent.addPoi(WSpec.rggroupid, this.rgPoiIndex, WSpec.rgimage[l].src,"",coord,4,23,23,"","");
										this.rgPoiIndex++;
										break;
									}
								}

							}
						}

						this.rpObject[i].draw = true;
						this.renderer.draw();
						
						lineLayer = null;
					}
				}
			}
			this.parent.stickPoi();
		},
		getRPRGData: function() {
			//function 경로 검색 결과 반환
			if(this.rpObject == null) {
				return;
			}
			
			var rpObjLength = this.rpObject.length - 1;
			var pointsLength = this.rpObject[rpObjLength].points.length - 1;
			
			var rpData = {};
			
			rpData.totDist = this.rpData.DataRows.tot_dist;
			rpData.totTime = this.rpData.DataRows.tot_time;
			rpData.errorMsg = this.rpData.DataRows.err_msg;
			rpData.startCoord = {"lon":this.rpObject[0].points[0][2], "lat":this.rpObject[0].points[0][3]};
			rpData.endCoord = {"lon":this.rpObject[rpObjLength].points[pointsLength][2], "lat":this.rpObject[rpObjLength].points[pointsLength][3]};
			rpData.passCoord = {};
			rpData.sumText = this.rpData.DataRows.tbt_sum_text;
			rpData.node = this.rpData.DataRows.tbt_node;
			//rpData.startAddr = this.rpData.DataRows.start_name;
			//rpdata.goalAddr = this.rpData.DataRows.goal_name;
			
			return rpData;
		},
		getStroke: function() {
			return  {r:0, g:113, b:226, a:200, w:3};//parseInt(this.level / 3) + 2};
		},
		chkCoord: function(kind) {
			for(var i = 0; i < this.rpCoords.length; i++) {
				if(this.rpCoords[i].kind == kind) {
					return this.delPassCoord(this.rpCoords[i].index);
				}
			}
		},
		setStartCoord: function(coord) {
			//출발지 설정
			var index = this.chkCoord("start");
			
			if(index == undefined) {
				index = this.rgCoordIndex;
				
				this.rgCoordIndex++;
			}
				
			this.parent.addPoi(WSpec.rggroupid, index, WSpec.rgimagestart,"",coord,6,26,29,"","");
			this.parent.stickPoi();
			
			this.rpCoords[this.rpCoords.length] = {"lon":coord.lon, "lat":coord.lat, "kind":"start", "index":index};
			
			return index;

		},
		setEndCoord: function(coord) {
			//도착지 설정
			var index = this.chkCoord("end");
			
			if(index == undefined) {
				index = this.rgCoordIndex;
				
				this.rgCoordIndex++;
			}
			
			this.parent.addPoi(WSpec.rggroupid, index, WSpec.rgimageend,"",coord,6,26,29,"","");
			this.parent.stickPoi();
			
			this.rpCoords[this.rpCoords.length] = {"lon":coord.lon, "lat":coord.lat, "kind":"end", "index":index};
			
			return index;
		},
		setPassCoord: function(coord) {
			//경유지 설정
			this.parent.addPoi(WSpec.rggroupid, this.rgCoordIndex, WSpec.rgimagevia,"",coord,6,26,29,"","");
			this.parent.stickPoi();
			
			this.rpCoords[this.rpCoords.length] = {"lon":coord.lon, "lat":coord.lat, "kind":"pass", "index": this.rgCoordIndex};
			
			this.rgCoordIndex++;
			
			return this.rgCoordIndex - 1;
		},
		delPassCoord: function(index) {
			//특정 경유지 삭제
			this.parent.removePoi(WSpec.rggroupid, index);
			
			var i = 0;
			for(; i < this.rpCoords.length; i++) {
				if(this.rpCoords[i].index == index) {
					break;
				}
			}
			
			if(i == 0) {
				this.rpCoords = this.rpCoords.slice(1, this.rpCoords.length);
			}
			else {
				this.rpCoords = this.rpCoords.slice(0, i);
				this.rpCoords.concat(this.rpCoords.slice(i + 1, this.rpCoords.length));
			}
			
			return index;
		},
		initCoords: function() {
			this.rpCoords.length = 0;
		},
		setRPOption: function(option) {
			this.rpOption = option;
		},
		getRPOption: function() {
			if(this.rpOption.method == undefined) {
				this.rpOption = {"method":WSpec.rpstaticopti, "free":WSpec.rpfreeno, "express":WSpec.rpexpressno, "beginner":WSpec.rpbeginnerno}
			}
			return this.rpOption;
		},
		setRPCoords: function(coords) {
			this.rpCoords = coords;
		},
		getRPCoords: function() {
			return this.rpCoords;
		},
		callRPRG: function() {
			//설정된 데이터로 경로 탐색
			if(this.rpOption.method == undefined) {
				this.rpOption = {"method":WSpec.rpstaticopti, "free":WSpec.rpfreeno, "express":WSpec.rpexpressno, "beginner":WSpec.rpbeginnerno}
			}
			if(this.rpCoords.length < 2) {
				return;
			}
			var coords = new Array(this.rpCoords.length);
			
			var index = 1;
			for(var i = 0; i < this.rpCoords.length; i++) {
				if(this.rpCoords[i].kind == "start") {
					coords[0] = {"lon":parseInt(this.rpCoords[i].lon), "lat":parseInt(this.rpCoords[i].lat)};
				}
				else if (this.rpCoords[i].kind == "end") {
					coords[coords.length - 1] = {"lon":parseInt(this.rpCoords[i].lon), "lat":parseInt(this.rpCoords[i].lat)};
				}
				else {
					coords[index] = {"lon":parseInt(this.rpCoords[i].lon), "lat":parseInt(this.rpCoords[i].lat)};
					index++;
				}
			}

			this.requestRPRG(coords, this.rpOption);
			
			for(var i = 0; i < coords.length; i++) {
				coords[i] = null;
			}
			coords = null;
		}
	});
	window.RGMan = RGMan;
};
_RGMan();