﻿	
	var CongRoutePathData = function() {
		this.success = false;
		this.dist = -1;
		this.time = -1;
		this.mbr = null;
		this.points = null;
		this.sections = null;
	}
	
	
	var CongRouteSection = function() {
		this.seq = -1;
		this.success = false;
		this.dist  = -1;
		this.time   = -1;
		this.mbr = null;
		this.nodes = null;
		this.links = null;
	}
	
	var CongRouteMbr = function() {
		this.x = -1;
		this.y = -1;
		this.w = -1;
		this.h = -1;
	}

	var CongRouteLink = function() {
		this.seq = -1;
		this.name   = null;
		this.dist   = -1;
		this.time   = -1;
		this.grade  = -1;
		this.eangle = -1;
		this.roadnum  = -1;
		this.points = null;
		this.strpoints = null;
	}

	var CongRouteNode = function() {
		this.seq = -1;
		this.name   = null;
		this.x   = -1;
		this.y   = -1;
		this.rotation = -1;
		this.routeinfo = null;
	}

	var CongRoutePoint = function() {
		this.x   = -1;
		this.y   = -1;
	}
	

	var CongRouteService = function() {
		this.proxy = null;
		//this.setProxy("/congsoa/clientproxy.jsp", "http://localhost");
		//this.serviceURL = 'http://www.kt114.com:8080/RisClientWebApp/route/';		
		this.serviceURL = "http://"+document.domain+":8080/RisClientWebApp/route/";		
		this.points = new Array();
		this.startPoint = null;
		this.opts = new Array();		

		// 틸로에서 추가
		this.inputCoordSystem = "WCONGNAMUL";
		this.outputCoordSystem = "WCONGNAMUL";
		
		//this.points.length = 4; // 틸로에서 삭제
		// 틸로에서 추가
		this.endPoint = null;
		this.passPoints = new Array();	// 경유지
		this.maxPoints = 20;
	};
	
	CongClass.extend(CongRouteService,CongBasicService);
	CongClass.extend(CongRouteService,CongAjaxClass);
	
	CongRouteService.prototype.getSearchRoute = function(point, method, options) {
		if(typeof(options)=='undefined') options = null;
		var query = new CongQueryString(false);
		if(point != null) {
			for(var i=0; i < point.length ; i++) {
				if(typeof(point[i]) != 'undefined' && point[i] != null) {
					query.add("x", point[i][0]);
					query.add("y", point[i][1]);
				}
			}
		}
 	    query.add("method", method);
 	    query.add("inputCoordSystem", this.inputCoordSystem);
 	    query.add("outputCoordSystem", this.outputCoordSystem);
		if(options!=null) {		
		    for(var i=0; i < options.length ; i++) {
		    	query.add(options[i][0], options[i][1]);
		    }
		}
		var dom = this.getDom(this.serviceURL+"getSearchRoute.service",query.toString());
		var pathdata = this.parsePathDataResult(dom);
		return pathdata;			
	}
	
	CongRouteService.prototype.getSearchRoad = function(type,roadnum) {
		var query = new CongQueryString(true);
		query.add("type",type);
		query.add("roadnum",roadnum);
		var dom = this.getDom(this.serviceURL+"getSearchRoad.service",query.toString());
		var section = this.parseSectionResult(dom);
		return section;
	}
	
	CongRouteService.sectionPathParseHandler = function(no,startNodeNum,startLinkNum) {
			this.seq = no;
			this.nodeStart = startNodeNum;
			this.linkStart = startLinkNum;
			this.process = function(pObj,depth,nodeName,node) {
				var resObj = null;
				if(depth==0) {
					resObj = new CongRouteSection();
					resObj.seq = this.seq;
					//alert(node.getAttribute("success"));
					resObj.success = (node.getAttribute("success")=='true');
					resObj.dist   = parseInt(node.getAttribute("length"));
					resObj.time   = parseInt(node.getAttribute("time"));
					
				} else if(depth==1){
					if(nodeName=='rout:mbr') {
						resObj = new CongRouteMbr();
						resObj.x = parseInt(node.getAttribute("x"));
						resObj.y = parseInt(node.getAttribute("y"));
						resObj.w = parseInt(node.getAttribute("w"));
						resObj.h = parseInt(node.getAttribute("h"));
						pObj.mbr = resObj;
					} else if(nodeName=='rout:link') {
						if(pObj.links==null) {
							pObj.links = new Array();
						}
						resObj = new CongRouteLink();
						resObj.name = node.getAttribute("name");
						resObj.dist = parseInt(node.getAttribute("length"));
						resObj.time  = parseInt(node.getAttribute("time"));
						resObj.grade = parseInt(node.getAttribute("grade"));
						resObj.roadnum = parseInt(node.getAttribute("roadnum"));
						resObj.eangle = parseInt(node.getAttribute("eangle"));
						resObj.speed = parseFloat(node.getAttribute("speed"));
						resObj.seq = this.linkStart + pObj.links.length;
						pObj.links[pObj.links.length] = resObj;
					} else if(nodeName=='rout:node') {
					    
						if(pObj.nodes==null) {
							pObj.nodes = new Array();
						}
						resObj = new CongRouteNode();
						resObj.name = node.getAttribute("name");
						resObj.x = parseInt(node.getAttribute("x"));
						resObj.y = parseInt(node.getAttribute("y"));
						resObj.rotation = parseInt(node.getAttribute("rotation"));
						resObj.routeinfo = node.getAttribute("routeinfo");
						resObj.seq = this.nodeStart + pObj.nodes.length;
						pObj.nodes[pObj.nodes.length] = resObj;
					}
				} else if(depth==2) {
					if(nodeName=='rout:point') {
						if(pObj.points==null) {
							pObj.points = new Array();
						}
						resObj = new CongRoutePoint();
						resObj.x = parseInt(node.getAttribute("x"));
						resObj.y = parseInt(node.getAttribute("y"));	
						pObj.points[pObj.points.length] = resObj;
					} 
					else if(nodeName=='rout:points') {
						var nodes = node.childNodes;
						var arr = new Array();
						for(var i=0; i < nodes.length; i++) {
							arr[i] = nodes.item(i).nodeValue;
						}
						pObj.strpoints = arr.join('');
					}
				}
				return resObj;
			}
	}
	
	CongRouteService.prototype.parseSectionResult = function(dom) {
		var parser = new CongDomParserClass(dom);
		var result = null;
		var handler = new CongRouteService.sectionPathParseHandler(1,1,1);
		result = parser.parse(handler);
		return result;	
	}
	
	CongRouteService.prototype.parsePathDataResult = function(dom) {
		var parser = new CongDomParserClass(dom);
		var result = null;
		var handler = {
			rootCode : null,
			process : function(pObj,depth,nodeName,node) {
				var resObj = null;
				if(depth==0) {
					resObj = new CongRoutePathData();
					resObj.success = (node.getAttribute("success")=='true');
					resObj.dist   = parseInt(node.getAttribute("length"));
					resObj.time   = parseInt(node.getAttribute("time"));
				} else if(depth==1){
					if(nodeName=='rout:mbr') {
						resObj = new CongRouteMbr();
						resObj.x = parseInt(node.getAttribute("x"));
						resObj.y = parseInt(node.getAttribute("y"));
						resObj.w = parseInt(node.getAttribute("w"));
						resObj.h = parseInt(node.getAttribute("h"));
						pObj.mbr = resObj;
					}
					else if(nodeName=='rout:section') {
						if(pObj.sections==null) {
							pObj.sections = new Array();
						}
						var startNodeNum = 1;
						var startLinkNum = 1;
						if(pObj.sections.length>0) {
							var preSection = pObj.sections[pObj.sections.length-1];
							startNodeNum  = preSection.nodes[preSection.nodes.length-1].seq+1;
							startLinkNum  = preSection.links[preSection.links.length-1].seq+1;
						}
						resObj = CongDomParserClass.parse(null,0,node,new CongRouteService.sectionPathParseHandler(pObj.sections.length,startNodeNum,startLinkNum));
					    pObj.sections[pObj.sections.length] = resObj;
					}
				} 
				return resObj;
			}
		}
		result = parser.parse(handler);
		return result;	
	}
	
	
	CongRouteService.prototype.setPoint = function(type, x, y) {	
		if(type == "startPoint") {
			this.points[0] = [x, y];
			this.startPoint = [x, y];
		}
		else if(type == "endPoint") {
			this.points[this.points.length-1] = [x, y];
			this.endPoint = [x, y];
		} else if(type=="passPoint") {
			if(this.passPoints.length < this.maxPoints) {
				this.passPoints[this.passPoints.length] = [x, y];
			} else {
				alert("경유지는 최대 " + this.maxPoints + "개 까지만 지정하실  수 있습니다.");
			}
		}
	}
	
	CongRouteService.prototype.clearPoint = function(type) {
		if(this.points[0][0] != this.startPoint[0])
			this.points.reverse();

		if(type == "startPoint") {
			this.points[0] = null;
			this.startPoint = null;
		} else if(type == "endPoint") {
			this.points[this.points.length-1] = null;
			this.endPoint = null;
		} else if(type== "passPoint") {
			this.passPoints.length = 0;
			this.passPoints = new Array();
		}
	}
	
	CongRouteService.prototype.addOption = function(name, value) {
		this.opts[this.opts.length] = [name, value];
	}
	
	CongRouteService.prototype.getPathData = function(course, option) {
		this.points.length = 0;
		if(this.startPoint) {
			this.points[0] = this.startPoint; 
		}
		if(this.passPoints && this.passPoints.length > 0) {
			for(var i=0; i<this.passPoints.length; i++) {
				this.points[this.points.length] = this.passPoints[i];
			}
		}
		if(this.endPoint) {
			this.points[this.points.length] = this.endPoint; 
		}
		
		if(typeof(this.points[0]) == 'undefined' || this.points[0] == null) {
			alert("출발지를 설정해 주십시오.");
			return false;
		}
		else if(typeof(this.points[this.points.length-1]) == 'undefined' || this.points[this.points.length-1] == null) {
			alert("도착지를 설정해 주십시오.");
			return false;
		}
		
		if(course == 'go') {
			if(this.points[0][0] != this.startPoint[0]) {
				this.points.reverse();
			}
		}
		else if(course == 'come') {
			if(this.points[0][0] == this.startPoint[0]) {
				this.points.reverse();
			}
		}
		
		if(typeof(this.points[1]) == 'undefined' || this.points[1] == null) {
			if(typeof(this.points[2]) != 'undefined' && this.points[2] != null) {
				this.points[1] = this.points[2];
				this.points[2] = null;
			}
		}
		
		var method = "realtime";
		
		if(option == "dist") method = "dist";
		else if(option == "time") method = "time";
		else if(option == "realtime") method = "realtime";
		
		var pathdata = this.getSearchRoute(this.points, method, this.opts);
		
		return pathdata;
	}
	
	
