//This function requires that searchdatabase.js be already loaded
/*
Science & Engineering Encyclopaedia. Copyright Dirac Delta Consultants Ltd. 2001-2002

No part of this document may be reproduced or transmitted in any form or by any mean, electronic or mechanical, including printing, placing on the internet or by any information storage and retrieval system without permission from the Publisher in writing
*/
var D
var lnk;
var xps;
var yps;
var mapwindow;
var timer;
var bumpCounter;
var matrix_size = 0;

function  link_map(qs)
{
	
	bumpCounter=0;
	mapwindow=open("", "LinkMapWindow", "toolbar=no, scrollbars=no, status=no,width=500, height=450 ")
	mapwindow.focus();
	mapwindow.document.writeln('<title>Link Network - Science & Engineering Encyclopaedia</title>')
	mapwindow.document.writeln('<STYLE TYPE="text/css"><!--	A:link {color:"blue"; text-decoration:"none"; font-size:8pt;} A:visited {color:"blue"; text-decoration:"none"; font-size:8pt;} A:hover {color:"red"; text-decoration:"none"; font-size:10pt;} -->	</STYLE>')
	mapwindow.document.writeln('<body vlink=blue link=blue background="../../../images/target.jpg">')
	
	lnk=ematrix.slice(1);
	
	for (loop=0;loop<lnk.length;loop++)
	{

		str=escape(ematrix[0][loop]);
		if (str!="")
		{
			str=str.toLowerCase();
			mapwindow.document.writeln("<a href=\u0022../../../" + str.substr(0,1) + "/" + str.substr(1,1) + "/" + str + "/source.html\u0022 target='main' ID = 'x" + loop + "', STYLE = 'position: absolute;'>o " + ematrix[0][loop] + "</a>" );
			matrix_size++;
		}
	}


	D = new Array(matrix_size);
	for (j=0;j<matrix_size;j++)
	{
		D[j]=new Array(matrix_size);
	}

	xps = new Array(matrix_size);
	yps = new Array(matrix_size);

	var I;
	var V;
	var j;

	for(I=0; I<matrix_size; I++)
	{
		for(V=I+1; V<matrix_size; V++)
		{
			if(lnk[I][V]==1.0)
			{
				if (lnk[V][I]==1.0)
				{
					D[I][V]=25+10*(Math.random()-0.5);
					D[V][I]=D[I][V];
				}else
				{
					D[I][V]=75+10*(Math.random()-0.5);
					D[V][I]=D[I][V];
				}
			}else
			{
				D[I][V]=200+10*(Math.random()-0.5);
				D[V][I]=D[I][V];
			}
		}
	}


	xps[0] = 0;
	yps[0] = 0;
	for(I=1; I<matrix_size; I++)
	{
		xps[I] = 2*(Math.random()-0.5);
		yps[I] = 2*(Math.random()-0.5);
	}
	
	timer=setTimeout("iter()",10);
	mapwindow.document.close();
}


function iter()
{
		for(A=1; A<matrix_size; A++)
		{
			for(B=0; B<matrix_size; B++)
			{
				if (A!=B)
				{
					xdist=xps[A]-xps[B];
					abs_xdist=Math.abs(xdist);
					ydist=yps[A]-yps[B];
					abs_ydist=Math.abs(ydist);
					curr_dist=Math.sqrt(xdist*xdist + ydist*ydist);
					move_x = xdist/curr_dist;
					move_y = ydist/curr_dist;

					xps[A] += (curr_dist < D[A][B] ? move_x : -move_x);
					yps[A] += (curr_dist < D[A][B] ? move_y : -move_y);

					if (bumpCounter>50) //Bump routine
					{
						if ((abs_xdist < 60) || (abs_ydist < 25))
						{
							xps[A] += (xps[A]<xps[B] ? -0.5 : 0.5);
							yps[A] += (yps[A]<yps[B] ? -0.5 : 0.5);
						}
					}


				}
			}
		}
		
		
		
		for (loopy=0;loopy<matrix_size;loopy++)
		{
			if (mapwindow)
			{
				mapwindow.document.getElementById("x"+loopy).style.left = xps[loopy]+250;
				mapwindow.document.getElementById("x"+loopy).style.top = yps[loopy]+200;
				//eval("mapwindow.document.all.x"+loopy+".style.left = xps["+loopy+"]+250;")
				//eval("mapwindow.document.all.x"+loopy+".style.top = yps["+loopy+"]+200;")
			}
		}
	
	bumpCounter++;
	if (bumpCounter<300) timer=setTimeout("iter()",1);	

}
