function RHRN_Map( id, centerLatLng, config ){
	this._Id = id;

	this._Config = config;

	this._Options = {
    zoom: 13,
    center: centerLatLng,
    mapTypeId: google.maps.MapTypeId.ROADMAP
  }

 	this._Map = new google.maps.Map(document.getElementById( this._Id ), this._Options );

}

function OpenPassengersMap( config ){

	var client_latlng = new google.maps.LatLng( config.account_location.lat, config.account_location.lng );
	var event_latlng = new google.maps.LatLng( config.event_location.lat, config.event_location.lng );

	var rhrn_map = new RHRN_Map( 'map', client_latlng, config );

	var bounds = new google.maps.LatLngBounds();
	bounds.extend( client_latlng );
	bounds.extend( event_latlng );

	rhrn_map._Map.fitBounds( bounds );

	var event_marker = new google.maps.Marker({
      position: event_latlng,
      title:"Evenement"
  });

	event_marker.setMap( rhrn_map._Map );

	var client_marker = new google.maps.Marker({
      position: client_latlng,
      title:"Jouw positie"
  });

 	client_marker.setMap( rhrn_map._Map );

	google.maps.event.addListener(rhrn_map._Map, 'tilesloaded', function () {
		PassengersMap_UpdateMap( rhrn_map );
	} );

	google.maps.event.addListener(rhrn_map._Map, 'projection_changed', function(event) {
		PassengersMap_UpdateMap( rhrn_map );
	} );
}

String.prototype.trim = function() {
	return this.replace(/(^ \ s+|( \ s+$))/g,'');
}
function find_address_lat_lng( values, func_succes, func_failed ){
	var address = [];

	for( var i=0; i< values.length; i ++ ) {
		if ( values[ i ].trim() != '' ){
			address.push( values[ i ].trim() );
		}
	}

	if ( address.length >= 1 ){
		var geocoder = new google.maps.Geocoder();

		geocoder.geocode( {
	    'address': address.join( ', ' ) 
		}, 
		function(results, status){
    if (status == google.maps.GeocoderStatus.OK) {

				func_succes( results[0].geometry.location );

	    } else {
				if ( func_failed ){
					func_failed();
    		}
    	}
 		});
		
	} else {
		if ( func_failed ){
			func_failed();
		}
	}

}

var myDirectionsRenderer = null;
function find_route( latlngFrom, latlngTo, func_succes, func_failed ){

	var dir = new google.maps.DirectionsService();

 	var request = {
		origin: latlngFrom,
		destination: latlngTo,
		travelMode: google.maps.DirectionsTravelMode.DRIVING,
		unitSystem : google.maps.DirectionsUnitSystem.METRIC
	}

	dir.route( request, function( rRoute, rStatus ){
		if (rStatus == 'OK') {
			func_succes( rRoute );
		} else {
			if ( func_failed ) func_failed();
		}		
	} ) ;

}

function PassengersMap_UpdateMap( rhrn_map ){
	var bounds = rhrn_map._Map.getBounds();

	jQuery.getJSON( rhrn_callback, { 
			task: 'update_passenger_map',
			event_id: rhrn_map._Config.event_id,
			b_sw: bounds.getSouthWest().toUrlValue(),
			b_ne: bounds.getNorthEast().toUrlValue()
		} 
	);

}

var to_event_location = { lat: null, lng: null };
function SetupEventDistance( config ){
	to_event_location = config.event;

	determine_need_for_rider_route(  );

	$('#estimated_km').change(function(){
		$(this).addClass( 'changed' );
		$('#message_estimated_km').hide();
	});

	$('.Vanaf input').change(function(){
		if ( $('#estimated_km').hasClass( 'changed' ) == false ){
			calculate_rider_route();
		}
	});
}

function determine_need_for_rider_route(){
	var distance_set = parseInt( $('#estimated_km').val() );
	if ( ! distance_set ){
		calculate_rider_route();
	}
}

function calculate_rider_route(){
	var city = $('#from_city').val();
	var zipcode = $('#from_zipcode').val();
	var street = $('#from_street').val();

	find_address_lat_lng( [ city, zipcode, street ], 
		function( lat_lng ){
			$('#departure_error_message').hide();
			find_route( 
				new google.maps.LatLng( to_event_location.lat, to_event_location.lng ), 
				lat_lng, 
				function( rRoute ){
					$('input[name=estimated_km]').val( Math.floor( rRoute.routes[0].legs[0].distance.value / 1000 ) );
					$('#message_estimated_km').show();
				}
			);
		}, 
		function(){
			$('#departure_error_message').show();
			// alert( '! ok' );
		}
	);
}

//if ( window.name ){
//	if ( window.opener ){
		//window.opener.location.href = location.href;
		//window.close();
//	}
//}

jQuery(document).ready(function(){
	if( typeof jQuery().fancybox != 'function' ){
		var script = document.createElement( 'script' );
		script.src = base_url + 'plugins/content/i2s_image_resizer_popup/fancybox/jquery.fancybox.pack.js';
		jQuery('head').append( script );
		var css = document.createElement( 'link' );
		css.type = 'text/css';
		css.rel = 'stylesheet';
		css.href = base_url + 'plugins/content/i2s_image_resizer_popup/fancybox/jquery.fancybox.css';
		jQuery('head').append( css );
	}

	jQuery('a.inline').each(function(){
		this.href += ( this.href.indexOf( '?' ) != -1 ? '&amp;' : '?' ) + 'inline=true';
	});
	
	jQuery('a.inline').click(function(){
		jQuery.get( this.href, function( data ){
			jQuery.fancybox({
				'hideOnContentClick': false,
				'content' : data
			});
		} );
		return false;
	});

});


