/**
 * @author Alexandr (Regio)
 * New routing component configuration/setup
 */
function InitRouting(div, mapcat, cfg) {
	var renderer = Regio.Route.CreateFlashTileRenderer(mapcat, cfg);
	var route = new Regio.Route(div, mapcat.listeners, {
		providers: {
			delfiRouting: Regio.Route.CreateDelfiProvider()
		},
		renderers: [renderer]
	});
	
	mapcat.addCallback("map.onMapParams", function(args) {
		with (args) {
			var width = max_e - min_e, height = max_n - min_n;
			route.updateForBBox([min_e - width/2, min_n - height/2, max_e + width/2, max_n + height/2], 100);
		}
	});
	
	route.listeners
	.add("requestStarted", function(type) {
		$('.route_message').hide();
		$('.route_message_wait').show();
	})
	.add("requestError", function(desc) {
		$('.route_message').hide();
		$('.route_message_error').show();
	})
	.add("requestComplete", function(desc) {
		$('.route_message').hide();
	})
	.add("drawDescription", function(data, overview) {
		mapcat.broadcast("routeDescriptionDraw");
		$('.route_length').text(overview.length);
		$('.route_time').text(overview.time);
		$('.only_visible_when_route').show();
	})
	.add("row", function(obj, pos, childs) {
		var objectID = obj.id, wayPointNum = pos[0];
		$('A', childs)
		.bind("mouseover", function() {
			renderer.listeners.broadcast("hiliteSegment", wayPointNum, objectID);
		})
		.bind("mouseout", function() {
			renderer.listeners.broadcast("deHiliteSegment", wayPointNum, objectID);
		})
		.bind("click", function() {
			renderer.listeners.broadcast("centerSegment", wayPointNum, objectID);
			return false;
		});
		$(childs)
		.each(function() {
			this.id = 'route_segment_'+obj.id;
		});
	});
	
	renderer.listeners
	.add("overSegment", function(w, s) {
		$('#route_segment_'+s).addClass("active");
	})
	.add("outSegment", function(w, s) {
		$('#route_segment_'+s).removeClass("active");
	});
	
	var routePoints = {
		savedStartTxt: $('#startPoint').val(),
		savedEndTxt: $('#endPoint').val(),
		points: {},
		symbols: { start: "A", end: "B" },
		styles: { start: { color: "0091D5", alpha: 80 }, end: { color: "ff3333", alpha: 80 }},
		getLayerName: function() {
			if (!this.layer) {
				this.layer = "ROUTE_POINTS";
				mapcat.broadcast("layers.addSystemLayer", this.layer);
			}
			return this.layer;
		},
		setPoint: function(which, e, n, txt, center) {
			this.points[which] = { e: e, n: n, txt: txt }
			if (!txt) txt = Regio.Utils.format("{0}:{1}", e, n);
			$('#'+which+'Point').val(txt);
			
			if (this.points.start && this.points.end) {
				this._startRoute([this.points.start, this.points.end]);
			}
			
			mapcat.broadcast("layers.addPointToSystemLayer", this.getLayerName(), {
				objectId: which,
				geo: [{e: e, n: n}],
				color: this.styles[which].color,
				alpha: this.styles[which].alpha,
				symbolId: this.symbols[which],
				tooltip: txt
			});
			
			if(center) {
				mapcat.broadcast("map.center", { e: e, n: n, zoom: 12 });
			}
		},
		clear: function() {
			$('#startPoint').val(this.savedStartTxt);
			$('#endPoint').val(this.savedEndTxt);
			this.points = [];
			route.stopRouting();
			route.clearPoints();
			mapcat.broadcast("layers.clearSystemLayer", this.getLayerName());
		},
		swap: function() {
			var s = this.points['start'];
			var e = this.points['end'];
			if (s && e) {
				this.setPoint('start', e.e, e.n, e.txt);
				this.setPoint('end', s.e, s.n, s.txt);
			}
		},
		_startRoute: function(arr) {
			route.stopRouting();
			route.clearPoints();
			route.addPoints(arr);
			route.startRouting();
		}
	};
	
	mapcat.addCallback("routing.startPointFixed", function(args) {
		routePoints.setPoint("start", args.e, args.n, args.locationName, args.center);
		mapcat.broadcast("layers.removeSystemLayer", "_ROUTE_POINTS"); // remove FT buildin routing doings
	});
	
	mapcat.addCallback("routing.endPointFixed", function(args) {
		routePoints.setPoint("end", args.e, args.n, args.locationName, args.center);
		mapcat.broadcast("layers.removeSystemLayer", "_ROUTE_POINTS"); // remove FT buildin routing doings
	});
	
	mapcat.addCallback("routing.clear", function() {
		routePoints.clear();
	});
	
	// html bindings
	$('.route_message').hide();
	
	(function() {
		$a = $(".routingAButton");
		$b = $(".routingBButton");
		var active = "active"; // class :hover
		
		$a.click(function() {
			if ($a.hasClass(active)) {
				$a.removeClass(active);
				mapcat.broadcast('mapcat.stopEditMode');
			} else {
				$a.addClass(active);
				$b.removeClass(active);
				mapcat.broadcast('routing.placeRouteStartPoint');
			}
			return false;
		});
		
		$b.click(function() {
			if ($b.hasClass(active)) {
				$b.removeClass(active);
				mapcat.broadcast('mapcat.stopEditMode');
			} else {
				$b.addClass(active);
				$a.removeClass(active);
				mapcat.broadcast('routing.placeRouteEndPoint');
			}
			return false;
		});
		
		var deselect = function() {
			$a.removeClass(active);
			$b.removeClass(active);
		}
		mapcat.addCallback("routing.endPointFixed", deselect);
		mapcat.addCallback("routing.startPointFixed", deselect);
	})();
	
	$('.routingSwapButton').click(function() {
		routePoints.swap();
		return false;
	});
	
	$('.generate_random_route_points').click(function() {
		route.addPoints([
			{ e: 679270, n: 6491249 }, 
			{ e: 642145, n: 6456186 }
		]);
		return false;
	}).attr('href', '#');
	
	$('.start_routing').click(function() {
		route.startRouting();
		return false;
	}).attr('href', '#');
	
	$('.stop_routing').click(function() {
		route.stopRouting();
		return false;
	}).attr('href', '#');
	
	$('.clear_route_points').click(function() {
		routePoints.clear();
		return false;
	}).attr('href', '#');

	return route;
}