//map_centres - this will be populated with the map centres 
var map_centres = get_centres_from_xml();
//pin_data - this will be populated with the pin data 
var pin_data = get_pins_from_xml();
//object_and_array - this will be populated with the layer data from the XML 
var object_and_array = get_layers_from_xml();
var layer_data= object_and_array[0];
var layer_data_array = object_and_array[1];
var layer_param;
var initiators=init_layer_param();
var defaults=initiators[0];
var initial_lp=initiators[1];

/**
* init_layer_param - This function is used to set the inital layers parameter so that the correct layers are 
* displayed by default
*@returns returnarray array containing the initial layers
*/
function init_layer_param()
{	matches = /\/([^/]+)(\/google)?\/?$/.exec( window.location.pathname );
	map_centre = map_centres[ matches[1] ];
	default_load_layer=map_centre.layer;

	if(default_load_layer)
	{ comma_default_load_layer=','+default_load_layer;
    }

	lp = gup( 'layer' );

    if (lp=="")
    {	if(default_load_layer)
		{	if(default_load_layer.indexOf("nodefault"))
			{	arr=new Array(default_load_layer);
				lp="" +default_load_layer;
			}
			else
			{	arr=new Array("default", default_load_layer);
				lp="default," +default_load_layer;
			}
		}
		else
		{	arr=new Array("default");
			lp="default";
		}
	}
	else
	{	var no_default=lp.indexOf("nodefault");
		if(no_default==-1)
		{	lp='default,' + lp;
			var multi=lp.indexOf(",");
			if(multi!=-1)
			{	arr=lp.split(",");
			}
			else
			{	arr=new Array(lp);
			}
			arr.push("default");
	}
	else
	{	lp=remove(lp,",nodefault");
		lp=remove(lp,"nodefault,");
		var multi=lp.indexOf(",");
		if(multi!=-1)
		{	arr=lp.split(",");
		}
		else
		{	arr=new Array(lp);
		}
	}
	initial_lp=lp;
	}
	layer_param=lp;
	lp=remove(lp,",default");
    lp=remove(lp,"default,");
    lp=remove(lp,"default");
	returnarray=new Array(arr, lp);
	return returnarray;
}

/**
* get_layer_param - this function is used to retrieve the layer_param variable
* @returns layer_param
*/
function get_layer_param()
{	return layer_param;
}


/**
* ShowLayers- this function is used set the layer param variable that controls which layers are shown
* @param ley - the layer(s) to display
*/
function ShowLayers( lay )
{   layer_param = lay
    if (layer_param=="")
    {    layer_param="default";
	}
	var no_default=layer_param.indexOf("nodefault");
    if ( no_default == -1)
	{    layer_param=layer_param+",default";
	}
}

/**
* This Function will retrieve a value from the URI string similar to php's _GET
* @param name - name of the parameter to read from the URI
* @returns - item 1 from the results array which will be the value specified for the parameter in the uri
*/
function gup( name )
{	name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	var regexS = "[\\?&]"+name+"=([^&#]*)";
	var regex = new RegExp( regexS );
	var results = regex.exec( window.location.href );
	if( results == null )
		return "";
	else
		return results[1];
}
/**
*updateChecked - this function is used to update the layer checkboxes when the user adds a layer to be displayed
*@param x - the extra checkbox to be set as checked
*/
function updateChecked(x)
{    if (x !="default")
	 {     y=$F('layerstring');
		  new_y="";
		  var mySplitResult = y.split(",")
          for (var i = 0, length = mySplitResult.length; i< length; i++)
          {    if (x!=mySplitResult[i])
  		 	   {    if(mySplitResult[i]!="")
  		 	   		{    new_y=new_y + mySplitResult[i]+',';
			   		}
			   }
  		  }

		  if (new_y !="")
		  {    Form.Element.setValue($('layerstring'), x +','+ new_y);
		  }
		  else
		  {    Form.Element.setValue($('layerstring'), x);
		  }
	 }
	 else
	 {   x="nodefault";
         y=$F('layerstring');
         var mySplitResult = y.split(",");
         new_y="";
         for (var i = 0, length = mySplitResult.length; i< length; i++)
         {    if (x!=mySplitResult[i])
  		 	  {   if(mySplitResult[i]!="")
  		 	   		{    new_y=new_y + mySplitResult[i]+',';
			   		}
			  }
  		 }
  		 Form.Element.setValue($('layerstring'), new_y);
	}
	ShowLayers($F('layerstring'));
}

/**
*updateUnchecked - this function is used to update the layer checkboxes when the user removes a layer from being displayed
*@param x - the extra checkbox to be set as unchecked
*/
function updateUnchecked(x)
{    if (x =="default")
	 {    x="nodefault";
		  y=$F('layerstring');
		  new_y="";
		  var mySplitResult = y.split(",")
          for (var i = 0, length = mySplitResult.length; i< length; i++)
          {    if (x!=mySplitResult[i])
  		 	   {    if(mySplitResult[i]!="")
  		 	   		{    new_y=new_y + mySplitResult[i]+',';
			   		}
			   }
  		  }
		  if (new_y !="")
		  {    Form.Element.setValue($('layerstring'), x +','+new_y);
		  }
		  else
		  {    Form.Element.setValue($('layerstring'), x );
		  }
	}
	else
	{   y=$F('layerstring');
        var mySplitResult = y.split(",");
        new_y="";
        for (var i = 0, length = mySplitResult.length; i< length; i++)
        {    if (x!=mySplitResult[i])
  		 	 {    if(mySplitResult[i]!="")
  		 	   		{    new_y=new_y + mySplitResult[i]+',';
			   		}
			 }
  		}
  		Form.Element.setValue($('layerstring'), new_y);
	}
    ShowLayers($F('layerstring'));
}

/**
*remove - this function removes a specific string from a variable
*@param s - the string in which to search
*@param t - the value to search for and remove
*@returns r - new string containing everything from s exept the specified value t
*/
function remove(s, t)
{    i = s.indexOf(t);
  	 r = "";
  	 if (i == -1) return s;
  	 r += s.substring(0,i) + remove(s.substring(i + t.length), t);
  	 return r;
}
