﻿var LP;
if (LP == undefined) { LP = {}; }

LP.initMap = function() {

    FA.initialBounds = [2.96562, 42.28899, 3.23959, 42.08650];
    FA.wmsLayer = { url: 'http://wms.aeridigitals.com/project/Palmeras 2008', layer: '340' };
    FA.initMap();

    FA.loadKml([
		{ name: 'camping', url: 'camping.kml', minZoom: 0, maxZoom: 21, visible:true }
		]);
	LP.init();
};

LP.gdir = null;
LP.clearedField = null;
LP.route = null;

LP.init = function() {
	LP.gdir = new GDirections(null,document.getElementById("drivingDirections"));
	GEvent.addListener(LP.gdir, "load", LP.onGDirectionsLoad);
	GEvent.addListener(LP.gdir, "error", LP.handleErrors);
};  
LP.getDirections = function(fromAddress) {
	LP.gdir.load("from: " + fromAddress + " to: " + "Camping Las Palmeras @42.18816,3.10258", { "locale" : LP.locale, "getPolyline" : true });
};

LP.handleErrors = function(){
	var statusCode = LP.gdir.getStatus().code;
	
	switch(statusCode)
	{
		case G_GEO_UNKNOWN_ADDRESS: alert(LP.GMAP_UNKNOWN_ADDRESS);	break;
		case G_GEO_SERVER_ERROR: alert(LP.GMAP_SERVER_ERROR); break;
		case G_GEO_MISSING_QUERY: alert(LP.GMAP_MISSING_QUERY); break;
		case G_GEO_BAD_KEY: alert(LP.GMAP_GEO_BAD_KEY); break;
		case G_GEO_BAD_REQUEST: alert(LP.GMAP_BAD_REQUEST); break;
		case G_GEO_UNKNOWN_DIRECTIONS: alert(LP.GMAP_UNKNOWN_DIRECTIONS); break;
		case G_GEO_UNKNOWN_ADDRESS: alert(LP.GMAP_UNKNOWN_ADDRESS); break;
		default: alert(LP.GMAP_UNKNOWN_ERROR + statusCode);
	}
};
LP.onGDirectionsLoad = function(){
	
	var polyline = this.getPolyline(),
		bounds = this.getBounds(),
		map = FA.map,
		points = [];

	var mapBounds = new OpenLayers.Bounds();
	mapBounds.extend(new OpenLayers.LonLat(bounds.getSouthWest().x, bounds.getSouthWest().y));
	mapBounds.extend(new OpenLayers.LonLat(bounds.getNorthEast().x, bounds.getNorthEast().y));
	
	map.zoomToExtent(FA.reproject(mapBounds), true);
	map.zoomOut(1);
	
	if (LP.route == null) {
		LP.route = new OpenLayers.Layer.Vector("Google directions",
		{
			alwaysInRange: true
		});

		map.addLayer(LP.route);
		LP.route.setVisibility(true);
	}
	// add feature
	
	for(var index = 0, limit = polyline.getVertexCount(); index < limit; index++){
		var vertex = polyline.getVertex(index)
		points.push(new OpenLayers.Geometry.Point(vertex.x, vertex.y));
	}
	var geometry = new OpenLayers.Geometry.LineString(points),
		feature = new OpenLayers.Feature.Vector(
			FA.reproject(geometry),
			null,
		  {
				strokeColor : '#992E20',
				strokeWidth : 3
		  });

	LP.route.removeFeatures(LP.route.features);
	LP.route.addFeatures(feature);

	jQuery("#printLink").show();
};

LP.clearField = function(input){
	if (input == LP.clearedField)
		return;
	
	LP.clearedField = input;
	input.value = "";
};

LP.popup = null;
LP.openPrintWindow = function(){

	LP.popup = window.open("directions_print.html","_blank","location=no,status=no,toolbar=no,width=500,height=500");
	
	window.setTimeout('LP.setPopupText()', 1500);
						
}

LP.setPopupText = function(){
	jQuery("#directions", LP.popup.document).html(jQuery("#drivingDirections").html());
	LP.popup.print();
}

jQuery(document).ready(LP.initMap);

