﻿	/**2009-04-21 tilo add *************************************************************************************************/
	var CongRouting = function() {
		this.vehicleType = null;	//[bus, subway, mixed]
		this.walkDist = 0;			//unit:m
		this.totalDist = 0;			//unit:m
		this.totalTime = 0;			//unit:second
		this.totalFare = 0;			//unit:won
		this.sectionRoutings = null;
		this.startPoint = null;
		this.startAddr = null;
		this.endPoint = null;
		this.endAddr = null;
		this.toString = function() {
			var output = "";
			for (var property in this) {
				if(property != 'toString') {
	                output += property + " = " + this[property] + "\n";
				}
			}
			return output;
		}
	}

	var CongSectionRouting = function() {
		this.id = null;
		this.type = null;
		this.subType = null;
		this.name = null;
		this.dist = 0;				//unit:m
		this.time = 0;				//unit:second
		this.nodes = null;
		this.graphicPaths = null;
		this.toString = function() {
			var output = "";
			for (var property in this) {
				if(property != 'toString') {
					output += property + " = " + this[property] + "\n";
				}
			}
			return output;
		}
	}

	var CongRoutingNode = function() {
		this.id = null;
		this.type = null;		//[busStop, subwayStation]
		this.subType = null;
		this.name = null;
		this.x = 0;
		this.y = 0;
		this.toString = function() {
			var output = "";
			for (var property in this) {
				if(property != 'toString') {
	                output += property + " = " + this[property] + "\n";
				}
			}
			return output;
		}
	}

	var CongRoutingService = function() {
		this.proxy = null;
		this.inputCoordSystem = "WCONGNAMUL";
		this.outputCoordSystem = "WCONGNAMUL";
		this.points = new Array();

		this.startPoint = null;
		this.endPoint = null;
		this.startAddr = null;
		this.endAddr = null;

		this.clientid = null;
		this.mapframe = null;

		this.serviceURL = "/";
	}

	CongClass.extend(CongRoutingService, CongBasicService);
	CongClass.extend(CongRoutingService, CongAjaxClass);

	/** route method ****************************************************************************** */

	CongRoutingService.prototype.setPoint = function(type, x, y) {	
		if(type == "startPoint") {
			this.startPoint = {'x':x, 'y':y};
		}
		else if(type == "endPoint") {
			this.endPoint = {'x':x, 'y':y};
		} 
	}
	CongRoutingService.prototype.setAddrName = function(type, addr) {	
		if(type == "startAddr") {
			this.startAddr = addr;
		}
		else if(type == "endAddr") {
			this.endAddr = addr;
		} 
	}
	CongRoutingService.prototype.setClientid = function(clientid) {	
		this.clientid = clientid;
	}
	CongRoutingService.prototype.setMapframe = function(mapframe) {	
		this.mapframe = mapframe;
	}
	CongRoutingService.prototype.getPublicRouteData = function(course) {
		if(this.startPoint == null || this.startPoint.x == undefined || this.startPoint.x == "" ||  this.startPoint.y == undefined || this.startPoint.y == "" ) {
			alert("출발지를 설정해 주십시오.");
			return false;
		}
		else if(this.endPoint == null || this.endPoint.x == undefined || this.endPoint.y == undefined || this.endPoint.x == "" || this.endPoint.y == "") {
			alert("도착지를 설정해 주십시오.");
			return false;
		}

		if(course == 'come') {
			var tmpX = this.startPoint;
			
			this.startPoint = this.endPoint;
			this.endPoint = tmpX;
			
		}
		
		return this.doRoute(true, this.startPoint.x, this.startPoint.y, this.endPoint.x, this.endPoint.y, this.clientid, this.mapframe);
	}

	CongRoutingService.prototype.route = function(startX, startY, endX, endY, clientid, mapframe) {
		return this.doRoute(true, startX, startY, endX, endY, clientid, mapframe);
	}
	CongRoutingService.prototype.asynchRoute = function(startX, startY, endX, endY, clientid, mapframe) {
		return this.doRoute(false, startX, startY, endX, endY, resultCount, clientid, mapframe);
	}
	CongRoutingService.prototype.doRoute = function(synch, startX, startY, endX, endY, clientid, mapframe) {
		var query = new CongQueryString(false);
		var urlInfo = "http://bus.congnamul.com/congsoa/routing/route.service?inputCoordSystem="+this.inputCoordSystem+"&outputCoordSystem="+this.outputCoordSystem+"&startX="+startX+"&startY="+startY+"&endX="+endX+"&endY="+endY+"&resultCount=20&sortOption=0&output=XML";
		query.add("ClientID", clientid);
		query.add("MAPFRAME", mapframe);
		query.add("QUERY", encodeURIComponent(urlInfo));
		if (synch==true) {
			var dom = this.getDom("/Web/Map/MapProxy.aspx", query.toString());
			return this.parseRoutings(dom, this.startPoint, this.startAddr, this.endPoint, this.endAddr);
		} else {
			this.request("/Web/Map/MapProxy.aspx", query.toString(), this.callbackRouteXML);
		}
	}

	CongRoutingService.prototype.onRoute = null;
	CongRoutingService.prototype.callbackRouteJSON = function(httprequest, data) {
		if (this.onRoute!=null) {
		    this.onRoute(data);
		} else {
			alert("callback function is null");
		}
	}
	CongRoutingService.prototype.callbackRouteXML = function(httprequest, dom) {
		var resultSet = this.parseRoutings(dom);
		if (this.onRoute!=null) {
		    this.onRoute(resultSet);
		} else {
			alert("callback function is null");
		}
	}

	CongRoutingService.prototype.parseRoutings = function(dom, startPoint, startAddr, endPoint, endAddr) {
		var routings = null;
		var parser = new CongDomParserClass(dom);
		var handler = {
			process : function(pObj, depth, nodeName, node) {
				var resObj = null;
				if (depth == 0) {
					routings = new Array();
					resObj = routings;
				} else if(depth == 1) {
					var routing = new CongRouting()
					routing.vehicleType = node.getAttribute("vehicleType");
					routing.totalDist = node.getAttribute("totalDist");
					routing.totalFare = node.getAttribute("totalFare");
					routing.totalTime = node.getAttribute("totalTime");
					routing.walkDist = node.getAttribute("walkDist");
					// tilo add 2009-04-28
					routing.startPoint = startPoint;
					routing.startAddr = startAddr;
					routing.endPoint = endPoint;
					routing.endAddr = endAddr;
					routing.sectionRoutings = new Array();
					pObj.push(routing);
					resObj = routing;
				} else if(depth == 2) {
					var sectionRouting = new CongSectionRouting();
					sectionRouting.id = node.getAttribute("id");
					sectionRouting.type = node.getAttribute("type");
					sectionRouting.subType = node.getAttribute("subType");
					sectionRouting.name = node.getAttribute("name");
					sectionRouting.dist = node.getAttribute("dist");
					sectionRouting.time = node.getAttribute("time");
					var graphicPathsString = node.getAttribute("graphicPaths")
					if(graphicPathsString != null) {
						//tilo modify
						//sectionRouting.graphicPaths = node.getAttribute("graphicPaths").split(",");
						sectionRouting.graphicPaths = [];
						var points = node.getAttribute("graphicPaths").split("|");
						var xpoints = [];
						var ypoints = [];
						if (points.length % 2 == 0) {
							for (var i = 0 ; i < points.length;i ++) {
								if (i % 2 == 0) {
									xpoints.push(points[i]);
								} else {
									ypoints.push(points[i]);
								}
							}
						}
						for (i = 0 ; i < xpoints.length ; i ++) {
							sectionRouting.graphicPaths.push({'x':xpoints[i], 'y':ypoints[i]});
						}
					}
					sectionRouting.nodes = new Array();
					pObj.sectionRoutings.push(sectionRouting);
					resObj = sectionRouting;
				} else if(depth == 3) {
					var routingNode = new CongRoutingNode();
					routingNode.id = node.getAttribute("id");
					routingNode.type = node.getAttribute("type");
					routingNode.subType = node.getAttribute("subType");
					routingNode.x = node.getAttribute("x");
					routingNode.y = node.getAttribute("y");
					routingNode.name = node.getAttribute("name");
					pObj.nodes.push(routingNode);
					resObj = routingNode;
				}
				return resObj;
			}
		}
		parser.parse(handler)
		return routings;
	}



	