


var gmaps_class;
if(!gmaps_class) gmaps_class = {};

gmaps_class.maps = {};

gmaps_class.maps.set = function(j_element,options)
{
	var map_name = jQuery(j_element).attr("id");
	var this_map = new GMap2(j_element);
	gmaps_class.maps[map_name] = this_map;
	gmaps_class.maps[map_name].options = options;
	return gmaps_class.maps[map_name];
}

gmaps_class.maps.get = function(j_element)
{
	return gmaps_class.maps[jQuery(j_element).attr("id")];
}
gmaps_class.maps.append = function ( j_element, description, appending ) 
{
	var map_name = jQuery(j_element).attr('id');
	gmaps_class.maps[map_name][description] = appending;
};

gmaps_class.get_map_type = function(map_type)
{
	switch(map_type) 
	{
		case 'map':
			map_type = G_NORMAL_MAP;
			break;
		case 'sat':
			map_type = G_SATELLITE_MAP;
			break;
		case 'hybrid':
			map_type = G_HYBRID_MAP;
			break;
	};
	return map_type;
}



gmaps_class.init = function(j_element, options, callback )
{
	
	function defaults()
	{
		return {
			"language" : "en",
			"map_type" : "map",
			"map_center" : [0,0],
			"map_zoom" : 12,
			"map_control" : "large",
			"map_enable_type" : true,
			"map_enable_overview" : false,
			"map_enable_dragging" : true,
			"map_enable_info_windows": true,
			"map_enable_2click_zoom" : true,
			"map_enable_scroll_zoom" : false,
			"map_enable_smooth_zoom" : false,
			"map_enable_scall_control" : false
		};
	};
	
	options = jQuery.extend(defaults(),options);
	
	if (GBrowserIsCompatible()) 
	{
		
		var this_map = gmaps_class.maps.set(j_element,options);
		var map_type = gmaps_class.get_map_type(options.map_type);
		this_map.setCenter(new GLatLng(options.map_center[0], options.map_center[1]), options.map_zoom*1,map_type);
		
		switch (options.map_control) 
		{
			case "small":
				this_map.addControl(new GSmallMapControl());
				break;
			case "large":
				this_map.addControl(new GLargeMapControl());
				break;
		};
		
		if (options.map_enable_type) this_map.addControl(new GMapTypeControl()); // Off by default
		
		if (options.map_enable_overview) this_map.addControl(new GOverviewMapControl());// Off by default
	
		if (!options.map_enable_dragging) this_map.disableDragging();
		if (!options.map_enable_info_windows) this_map.disableInfoWindow();
		if (options.map_enable_2click_zoom) this_map.enableDoubleClickZoom();
		if (options.map_enable_scroll_zoom) this_map.enableScrollWheelZoom();
		if (options.map_enable_smooth_zoom) this_map.enableContinuousZoom();
		if (options.map_enable_scale_control) this_map.addControl(new GScaleControl());
		
		if (typeof callback == 'function') 
			return callback(this_map, j_element, options);
	}
	else
	{
		jQuery(j_element).text('Your browser does not support Google Maps.');
		return false;
	}
	return;
}

gmaps_class.remove_marker = function ( j_element, marker, callback ) 
{
	var this_map = gmaps_class.maps.get(j_element);
	this_map.removeOverlay(marker);
	if (typeof callback === 'function') return callback(marker);
	return;
}
gmaps_class.clear_overlays = function (j_element,callback)
{
	var this_map = gmaps_class.maps.get(j_element);
	this_map.clearOverlays();
	if (typeof callback === 'function') return callback(marker);
	return;
}

gmaps_class.add_marker = function(j_element,options,callback)
{
	function defaults() {
		return {
			'point_lat_lng': undefined,
			'point_html': undefined,
			'point_open_html_event': 'click',
			'point_is_draggable': false,
			'point_is_removable': false,
			'point_remove_event': 'dblclick',
			'point_min_zoom': 4,
			'point_max_zoom': 17,
			'point_icon': undefined,
			'center_map': false,
			'center_move_method':'normal',
			'auto_pan' : false
		};
	};
	var this_map = gmaps_class.maps.get(j_element);
	
	options = jQuery.extend({},defaults(),options);
	
	var marker_options = {};
	
	if (typeof options.point_icon == 'object') 
	{
		jQuery.extend(marker_options, {'icon': options.point_icon});
	}
	
	if (options.point_is_draggable) jQuery.extend(marker_options, {'draggable': options.point_is_draggable});
	if (options.auto_pan) jQuery.extend(marker_options, {'autoPan': options.auto_pan});
			
	if (options.center_map) {
		switch (options.center_move_method) 
		{
			case 'normal':
				this_map.setCenter(new GLatLng(options.point_lat_lng[0],options.point_lat_lng[1]));
				break;
			case 'pan':
				this_map.panTo(new GLatLng(options.point_lat_lng[0],options.point_lat_lng[1]));
				break;
		}
	}
		
	var marker = new GMarker(new GLatLng(options.point_lat_lng[0],options.point_lat_lng[1]), marker_options);
	//gmaps_class.debug(marker);
	if(options.point_html)
	{
		GEvent.addListener(marker, options.point_open_html_event, 
		function()
		{
			marker.openInfoWindowHtml(options.point_html, {maxContent: options.point_max_content, maxTitle: options.point_max_title});
		});
	}
	
	if(options.point_is_removable)
	{
		GEvent.addListener(marker, options.point_remove_event, 
		function()
		{
			this_map.removeOverlay(marker);
		});
	}
	
	this_map.addOverlay(marker);
	if (typeof callback == 'function') return callback(marker, options);
	return;
}

gmaps_class.create_icon = function(options)
{
	function defaults()
	{
		return {
			'icon_img' : undefined,
			'icon_shadow' : undefined,
			'icon_size' : undefined,
			'icon_shadow_size' : undefined,
			'icon_anchor' : undefined,
			'icon_info_window_anchor' : undefined
		};
	}
	options = jQuery.extend(defaults(),options);
	
	var icon = new GIcon(G_DEFAULT_ICON);
	
	if(options.icon_img) icon.image = options.icon_img;
	if(options.icon_shadow) icon.shadow = options.icon_shadow;
	if(options.icon_size) icon.iconSize = options.icon_size;
	if(options.icon_shadow_size) icon.shadowSize = options.icon_shadow_size;
	if(options.icon_anchor) icon.iconAnchor = options.icon_anchor;
	if(options.icon_info_window_anchor) icon.infoWindowAnchor = options.icon_info_window_anchor;
	return icon;
}

gmaps_class.search = function(j_element, options, callback) {
	function defaults() {
		return {
			'query': null,
			'return_type': 'getLatLng',
			'cache' : undefined
		};
	};
	var this_map = gmaps_class.maps.get(j_element);
	options = jQuery.extend(defaults(), options);
	
	if (typeof this_map.Geocoder === 'undefined') 
	{	
		if (typeof options.cache === 'undefined') {
		 	var geoCoder = new GClientGeocoder();
		} 
		else 
		{
			var geoCoder = new GClientGeocoder(cache);
		}
		gmaps_class.maps.append(j_element, 'Geocoder', geoCoder);
		this_map = gmaps_class.maps.get(j_element);
	}
	this_map.Geocoder[options.return_type](options.query, function(result){
		if (typeof callback === 'function') {
			return callback(result, options); 
		}
	});
	return;
};

gmaps_class.search_code = function ( code ) {
	switch (code) {
		case G_GEO_SUCCESS:
			return {'code':G_GEO_SUCCESS,'success':true,'message':'Success'};
		case G_GEO_UNKNOWN_ADDRESS:
			return {'code' : G_GEO_UNKNOWN_ADDRESS, 'success' : false, 'message' : 'No corresponding geographic location could be found for one of the specified addresses. This may be due to the fact that the address is relatively new, or it may be incorrect'};
			break;
		case G_GEO_SERVER_ERROR:
			return {'code' : G_GEO_UNKNOWN_ADDRESS, 'success' : false, 'message' : 'A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.'};
			break;
		case G_GEO_MISSING_QUERY:
			return {'code' : G_GEO_UNKNOWN_ADDRESS, 'success' : false, 'message' : 'The HTTP q parameter was either missing or had no value. For geocoder requests, this means that an empty address was specified as input. For directions requests, this means that no query was specified in the input.'};
			break;
		case G_GEO_BAD_KEY:
			return {'code' : G_GEO_UNKNOWN_ADDRESS, 'success' : false, 'message' : 'The given key is either invalid or does not match the domain for which it was given.'};
			break;
		case G_GEO_BAD_REQUEST:
			return {'code' : G_GEO_UNKNOWN_ADDRESS, 'success' : false, 'message' : 'A directions request could not be successfully parsed.'};
			break;
		default:
			return {
				'code': null,
				'success': false,
				'message': 'An unknown error occurred.'
			};
		break;
	};
}

gmaps_class.move_to = function ( j_element, options, callback ) {
	function defaults() {
		return {
			'center_method': 'normal',
			'map_type': 'map',
			'map_center': [],
			'map_zoom': null
		};
	};
	
	var this_map = gmaps_class.maps.get(j_element);
	options = jQuery.extend(defaults(),options);	
	
	if (options.map_type)
		var map_type = gmaps_class.get_map_type(options.map_type);
	var point = new GLatLng(options.map_center[0], options.map_center[1]);
	switch (options.center_method) {
		case 'normal':
			this_map.setCenter(point, options.map_zoom, map_type);
			break;
		case 'pan':
			this_map.panTo(point);
			break;
	}
	if (typeof callback == 'function') return callback(point, options);
};


gmaps_class.debug = function(object)
{
	var out="";
	if(typeof object == 'object')
	{
		for(i in object) 
		{
			out+="\n"+i;
		}
		document.write(out);
	}
}

var gmaps_class;
if(!gmaps_class) gmaps_class = {};
(function($)
{
	$.fn.vrs4_gmap = function(method, options, callback) {
		
		return this.each(
			function(){
				if (method == 'init' && typeof options == 'undefined') 
				{
					new gmaps_class.init(this, {}, null);
				} 
				else if (method == 'init' && typeof options == 'object') 
				{
					new gmaps_class.init(this, options, callback);
				} 
				else if (method == 'init' && typeof options == 'function') 
				{
					new gmaps_class.init(this, {}, options);
				} 
				else if (typeof method == 'object' || method == null) 
				{
					new gmaps_class.init(this, method, options);
				} 
				else 
				{
					try {
						new gmaps_class[method](this, options, callback);
					} 
					catch(err) 
					{
						//console.log(err);
						throw Error('gmaps function does not exist / method: '+method);
					}
				}
		});
	}
})(jQuery);



//var ico = new GIcon(G_DEFAULT_ICON);
//ico.image = site_domain+"public/images/gmap_marker.png";
//ico.shadow = site_domain+"public/images/gmap_marker_shadow.png";

function showAddress(address) {
      if (geocoder) {
        geocoder.getLatLng(
          address,
          function(point) {
            if (!point) {
              alert(address + " not found");
            } else {
              map.setCenter(point, 13);
              var marker = new GMarker(point);
              map.addOverlay(marker);
              marker.openInfoWindowHtml(address);
            }
          }
        );
      }
}


$(document).ready(
function(){
	$("#details_map").vrs4_gmap('init',
	{
		"map_center":[39.559985,19.955978],
		"map_zoom":10
	}
	);
	
	var map = gmaps_class.maps.get('#details_map');
	geocoder = new GClientGeocoder();
	var addressA = $('#from_location').val();
	var addressB = $("#to_location").val();
	var markerA;
	var markerB;
	
	geocoder.getLatLng(
          addressA+',Corfu Greece',
          function(point) 
          {
            if (!point) {
              
            } 
            else {
            	//pointA=point;
            	//map.setCenter(point, 13);
            	markerA = new GMarker(point);
            	map.addOverlay(markerA);
            	markerA.openInfoWindowHtml(addressA);
            }
          }
	);
	
	if($("#to_lat").val()=="" && $("#to_lat").val()=="")
	{
	geocoder.getLatLng(
          addressB+',Corfu Greece',
          function(point) 
          {
            if (!point) {
              
            } 
            else {
            	//map.setCenter(point, 13);
            	//pointB=point;
            	markerB = new GMarker(point);
            	map.addOverlay(markerB);
            	markerB.openInfoWindowHtml(addressB);
            }
          }
	);
	}else{
		markerB = new GMarker(new GLatLng($("#to_lat").val(),$("#to_lng").val()));
        map.addOverlay(markerB);
        markerB.openInfoWindowHtml(addressB);
	}

	
	
	/*
	var polyline = new GPolyline([
  		  markerA.getLatLng(),
  		  markerB.getLatLng()
	], "#ff0000", 5);
	map.addOverlay(polyline);*/


});
