/**
	 * Klasa do obsługi tras dojazdu
	 * 
	 * @param map	GMap2
	 * @param place	document	Element w dokumencie, w którym umieszczą się wskazówki dojazdowe
	 * @return	bool
	 */
var GMapsDirections = function(map, place)
{
    if(!map || !place)
        return false;
		
    /**
		 * Przechowuje obiekt GDirections
		 * @type GDirections
		 */
    var gdirections = new GDirections(map, place);
		
    /**
		 * Przechowuje węzły
		 * @type Array
		 */
    var directions = Array();
		
    /**
		 * Pobiera węzeł wg indeksu
		 * @param 	index	mixed
		 * @return 	waypoint	Konkretny punkt określony na mapie	
		 */
    this.getDirection = function(index)
    {
        return directions[index];
    };
		
    /**
		 * Pobiera tablicę wszystkich węzłów
		 */
    this.getDirections = function()
    {
        return directions;
    };
		
    /**
		 * Ustawia węzeł
		 * @param 	waypoint	string/GLatLng	Zapytanie geokodujące lub współrzędne punktu
		 * @param	index	W przypadku gdy nie zostanie podany jest wybierany automatycznie
		 * 
		 * @return	index	Aktualny indeks w tablicy węzłów
		 */	
    this.setDirection = function(waypoint, index)
    {
        directions[!index ? directions.length : index] = waypoint;
        return !index ? directions.length-1 : index;
    };
		
    /**
		 * Usuwa węzeł z trasy(po indeksie lub po waypoincie)
		 * @param	index
		 * @param	waypoint
		 */
    this.deleteDirection = function(index, waypoint)
    {
        if(!index)
            for(var i in directions)
                if(directions[i] == waypoint) index = i;
        
        directions.splice(index, 1);

    };
		
    /**
		 * Czyści trasę
		 */
    this.clear = function()
    {
        directions = Array();
        return true;
    };
		
    /**
		 * Sortuje węzły wg podanej kolejności
		 * 
		 * @param	sortorder	Array	Tablica indeksów przypisanych w odpowiedniej kolejności
		 */
    this.sort = function(sortorder)
    {
        if(sortorder.length > 0)
        {
            var new_directions = Array();
				
            for(var i in sortorder)
            {
                new_directions[new_directions.length] = directions[sortorder[i]];
            }
				
            directions = new_directions;
        }
    };
		
    /**
		 * Oblicza i wyświetla trasę na mapie
		 */
    this.calculateRoute = function(travelMode)
    {
        if(travelMode == 1)
          travelMode = G_TRAVEL_MODE_DRIVING;
        else
          travelMode = G_TRAVEL_MODE_WALKING;
          
        gdirections.loadFromWaypoints(directions, { travelMode: travelMode });
    };
    
    this.getObject = function()
    {
        return gdirections;
    };
};
