// Dynamic Link builder script
// Copyright 2007 Gennadiy Shvets
// The program is distributed under the terms of the GNU General
// Public License 3.0
//
// To use this script:
// 1. [Optional] Set URL prefix - this prefix will be pre-pended to URLs 
//    of all links that this script will create:
//
//    var L_urlPrefix = 'http://allmyscripts.com';
//
// 2. Create new array containing link id and link URL in format:
//
//    var L_allLinks = new Array ('widgets', '/widgets/index.html',
//      'gadgets', '/gadgets/index.html');
//
// 3. Add javascript to your HTML page
//
//    <script type="text/javascript" src="gs_links.js"></script>
//
// See http://www.allmyscripts.com/Dynamic_Links/index.html for more information.

var L_text2Link = new Array ();

function L_findLinks ()
{

	// Copy all data to associative array
	var len = L_allLinks.length;
	for (var i = 0; i < L_allLinks.length; i += 2)
	{
		L_text2Link[L_allLinks[i]] = L_allLinks[i + 1];
	}
	// Loop through all DIV and SPAN tags on the page
	L__processElements ('DIV');
	L__processElements ('SPAN');
	// Free some memory
	L_text2Link = null;

	if	(window.onload_gsl_saved)
		window.onload_gsl_saved();
}

function L__processElements (p_type)
{

	var array = document.getElementsByTagName(p_type);
	if	(array == null)  return;
	var cl;
	var one_el;
	var text;
	var url;
	var prefix = (L_urlPrefix == null)? '': L_urlPrefix;
	for (var i = 0; i < array.length; i++)
	{
		one_el = array[i];
		cl = one_el.className;
		if	(!cl.match(/^_link(\s+|$)/))  continue;
		text = cl.replace(/^_link\s*/, '');
		text = text.replace(/^[^\s_]\S+\s*/, '');
		if	(text)
			text = text.replace(/^_/, '');
		else
		{
			text = one_el.innerHTML.replace(/<[^>]+>/g, '');
			text = text.replace(/&nbsp;/g, '');
		}
		url = L_text2Link[text];
		if	(url == null)  continue;
		one_el.innerHTML = '<a href="' + prefix + url + '" target="_blank">' + one_el.innerHTML + '</a>';
	}
}

if	(window.onload)
    window.onload_gsl_saved = window.onload;
window.onload = L_findLinks;

