// This file can be include in any HTML page.  
// Any links that end with extension of .h2o will be changed 
// if Water is not available.

var current_water_version="0.3";  // **WATER_VERSION**

// Cross-platform way to add an onload event to the page
function add_onload_event (a_function) {

//setup onload function
if(typeof window.addEventListener != 'undefined')
{
	//.. gecko, safari, konqueror and standard
	window.addEventListener('load', a_function, false);
}
else if(typeof document.addEventListener != 'undefined')
{
	//.. opera 7
	document.addEventListener('load', a_function, false);
}
else if(typeof window.attachEvent != 'undefined')
{
	//.. win/ie
	window.attachEvent('onload', a_function);
}

//** remove this condition to degrade older browsers
else
{
	//.. mac/ie5 and anything else that gets this far
	
	//if there's an existing onload function
	if(typeof window.onload == 'function')
	{
		//store it
		var existing = onload;
		
		//add new onload handler
		window.onload = function()
		{
			//call existing onload function
			//existing();
			
			//call generic onload function
			a_function();
		};
	}
	else
	{
		//setup onload function
		window.onload = a_function;
	}
}

}


function change_links_to_water_programs () {
// Needs to support different browsers other than IE.
 
 if (!water_is_current_version) {  // 
   var a_links=document.getElementsByTagName("A");
   var a_link=null; var extension=null;
   for(var i=0; i < a_links.length; i++) {
     //alert(i);
     //../install/auto_install_water.htm#http://waterlanguage.org/examples/
     a_link = a_links[i];
     extension=a_link.href.substring(a_link.href.length-4);  // examples: .h2o, .html
     //alert(extension);
     if (a_link.href && extension==".h2o")
       //a_link.target="_blank";
       a_link.href=create_water_program_link(a_link.href);
       //alert("a water link");
   }
 }
}

function create_water_program_link (water_program_url) {
 // Given a URL of a Water program, return a URL that will launch the program or install Water, then launch the program
 // If water_program_url = http://www.waterlanguage.org/examples/cash_register.h2o
 // Returns: http://waterlanguage.org/install/auto_install_water.htm#http%3A%2F%2Fwww.waterlanguage.org%2Fexamples%2Fcash_register.h2o
 return "http://waterlanguage.org/install/auto_install_water.htm#" + encodeURIComponent(water_program_url); 
}

function water_is_current_version () {
  return (window.water_version && window.water_version==current_water_version);
}

add_onload_event (change_links_to_water_programs);