// The Coords object

function Coords()
{
  this.COOKIE_COORDS = "bicing";
  this.COOKIE_EXPIRY = 30; 
  this.COOKIE_DEFAULT= "bicing_default";
  this.ELEM_SEP= ",";
  this.LINE_SEP= "|";
  this.LINE_ELEMENTS = 3;   // Number of elemenets per line 

  this.coords = new Array();
  this.defecto= 0;

  // Read the coordinates from the cookie
  var cookie_string=readCookie(this.COOKIE_COORDS);
  if ( ( cookie_string != null ) && ( cookie_string != "" ) )
  {
     var lines = cookie_string.split(this.LINE_SEP);
     for ( var x in lines )
     {
	var values = lines[x].split(this.ELEM_SEP);
	
	if ( values.length != this.LINE_ELEMENTS )
	{
           eraseCookie(this.COOKIE_COORDS);
	} else {
	   values[0] = this._clean_name( values[0] );
	   if ( this._check_lat( values[1] ) && this._check_lng( values[2] ) )
	   {
	      this.coords.push(values);
	   }
	} 
     }   
  } 

  // Read the default value
  cookie_string=readCookie(this.COOKIE_DEFAULT);
  if ( ( cookie_string != null ) && cookie_string.match(/^\d+$/) ) 
  {
	if ( ( cookie_string >= 0 ) && ( cookie_string < this.coords.length ) )
	{
		this.defecto = cookie_string;
	}
	else {
		this.defecto = 0;
	}
  }

  // Save the values read to cookie 
  this._save_to_cookies();
} // constructor
 
Coords.prototype._save_to_cookies = function()    // Define Method
{
  // Write out the values of the various coordinates
  var cookie_string="";
  var null_values = 0;

  for ( var x in this.coords )
  { 
    if ( this.coords[x] != null ) 
    {
      var line = this.coords[x].join(this.ELEM_SEP);
      if ( cookie_string != "" )
      {
         cookie_string += this.LINE_SEP + line;
      } 
      else {
  	cookie_string = line;
      }
   }
   else {
	// To reduce the default value correctly
	if ( x < this.defecto )
	{
		null_values++;
	}
   }
  }

  //debug_msg("Stored COORDS:"+cookie_string);
  createCookie(this.COOKIE_COORDS, cookie_string , this.COOKIE_EXPIRY );

  //debug_msg("Stored DEFAULT:"+this.default);
  createCookie(this.COOKIE_DEFAULT, (this.defecto - null_values) , this.COOKIE_EXPIRY );
  
} // _save_to_cookies
 
Coords.prototype.remove = function(i)
{
  debug_msg("Deleting "+i);
  if ( i < this.coords.length && i >= 0 ) 
  {
       this.coords[i] = null;
       if ( i == this.defecto )
       {
	  var j=0;
	  // set default equal to first non-null value
	  while ( j < this.coords.length && this.coords[j] == null )
	  {
		j++;
	  }
	
	  if ( this.coords[j] != null )
	  {
		this.defecto = j;
	  }
	  else {
	  	this.defecto = 0;
	  }
       }
  }
  this._save_to_cookies();
} // remove

Coords.prototype.add = function(name,lat,lng)
{  
  var newname = this._clean_name(name);
  if ( this._check_lat(lat) && this._check_lng(lng) ) 
  {
    var values = new Array( newname, lat, lng );
    this.coords.push(values);
    debug_msg("Added "+(this.coords.length-1));
    this._save_to_cookies();
    return (this.coords.length - 1);
  }
  else {
        debug_msg("Bad values");
	return -1;
  }
} // add
 
Coords.prototype._clean_name = function(newname)
{
  return ( newname.replace(/[^\w]/) ); 
} // _clean_name

Coords.prototype._check_lat = function(lat)
{
   return ( (lat != null) && lat.match(/^41\.\d{1,6}$/) );
} // _check_lat

Coords.prototype._check_lng = function(lng)
{
  return ( (lng != null) && lng.match(/^2\.\d{1,6}$/) );
} // _check_lng

Coords.prototype.delete_all = function()
{
  eraseCookie(this.COOKIE_COORDS);
} // delete_all

Coords.prototype.get_elements = function(i)
{
   return this.coords[i];
}

Coords.prototype.get_length = function()
{
   var count=0;
   for ( var x in this.coords )
   {
	if ( this.coords[x] != null )
	{
	   count++;
	}
   }
   return count;
} 

Coords.prototype.get_default = function()
{
   return this.defecto;
}

Coords.prototype.set_default = function(i)
{
   this.defecto=i;
   this._save_to_cookies(); 
}

