
var G = {screenWidth: 0,
         screenHeight: 0,
			map_grabbed_at: {x:0, y:0},
			center: {x:0, y:0}};
         
var isCSS;
var isW3C;
var isIE4;
var isNN4;
var menucur_cat, menucur_main, menucur_alpha;
var redarrowtimer = '',redarrow_speed = 300,redarrow_n=10;

var full2navScale = 10;
var numLevels = 1;

function setScreenSize(x,y)
{
	G.screenWidth = x;
	if (y>600) {
		G.screenHeight = y;
	}
	else {
		G.screenHeight = 600;
	}
	G.panelWidth = 360;
	G.center.x = G.screenWidth/2 + G.panelWidth/2;
	G.center.y = G.screenHeight/2;
}
//function setNumCategories(n) {G.numCategories = n;}

function setCatG(n) {G['C' + n] = SID('C' + n);makeMenuOnScreen(G['C' + n]);}

function setABCG(n) {G['A' + n] = SID('A' + n);makeMenuOnScreen(G['A' + n]);}

function makeMenuOnScreen(g)
{
	if ((g.offsetHeight + g.offsetTop) > G.screenHeight)
	{
		//alert('1.' + n);
		if (g.offsetHeight > G.screenHeight)
		{
			g.style.top = "0px";
			//alert('2.' + n);
		}
		else
		{
			newtop = (G.screenHeight-g.offsetHeight);
			oldtop = parseInt(g.style.top);
			if (newtop<oldtop)
				g.style.top = newtop + 'px';
			else
				g.style.top = oldtop + 'px';
			//alert('3.' + n);
		}
	}
}

function SID(x)
{
	if (typeof x == "string") return document.getElementById(x);
	return x;
}


function init()
{
    if (document.images) {
        isCSS = (document.body && document.body.style) ? true : false;
        isW3C = (isCSS && document.getElementById) ? true : false;
        isIE4 = (isCSS && document.all) ? true : false;
        isNN4 = (document.layers) ? true : false;
        isIE6CSS = (document.compatMode && document.compatMode.indexOf("CSS1") >= 0) ? true : false;
    }
}

function postinit()
{
	//G.redarrow = 
	//G.screenWidth = document.body.parentNode.scrollWidth;
	//G.screenHeight = document.body.parentNode.scrollHeight;
	G.Clear_Button = SID('Clear_Button');
	G.Copy_Button = SID('Copy_Button');
	G.Descriptions_Element = SID('Descriptions_Element');
	G.Logo_Element = SID('Logo_Element');
	G.Categories_Element = SID('Categories_Element');
	G.Alphabet_Element = SID('Alphabet_Element');
	G.Nav_Element = SID('Nav_Element');
	G.Copy_Button = SID('Copy_Button');
	G.Clear_Button = SID('Clear_Button');
	G.Help_Button = SID('Help_Button');
	G.Categories_Main_Menu = SID('Categories_Main_Menu');
	G.Alphabet_Main_Menu = SID('Alphabet_Main_Menu');
	G.Navigate_Main_Menu = SID('Navigate_Main_Menu');
	G.Help_Main_Menu = SID('Help_Main_Menu');
	G.holdtext = SID('holdtext');
	G.redarrow = SID('redarrow');
	position_redarrow();
	G.Big_Element = SID('Big_Element');
	G.Big_Element.style.backgroundColor = '#fffeff';
	G.Hold_Button = SID('Hold_Button');
	G.holdState = 0;
	/*
	#Descriptions_Element, #Logo_Element, #Categories_Element, #Alphabet_Element,
	#Nav_Element, #Copy_Button, #Clear_Button, #Help_Button,
	#EOL_Element, 
	#Categories_Main_Menu, #Alphabet_Main_Menu, #Navigate_Main_Menu, #Help_Main_Menu,
	.Categories_Menu, .Alphabet_Menu,
	#Dump_Element
	*/
	for ( var c = 1; c<=numLevels; c++)
	{
		G['townmap' + c] = SID('townmap' + c);
		G['Map_Level_' + c] = SID('Map_Level_' + c);
		G['NavMap' + c] = SID('NavMap' + c);
		//G['townmap' + c].style.left = 0;
		//G['townmap' + c].style.top = 0;
	}
	/*
	if (!isIE4)
	{
		G.Copy_Button.innerHTML = "";
		G.Copy_Button.onclick = "";
		G.Copy_Button.style.backgroundColor = G.Copy_Button.style.borderColor;
	}
	*/
	/*
	for ( var c = 1; c <= G.numCategories; c++)
	{
		G['C' + c] = SID('C' + c);
	}
	*/
}


/*
//function changelevel(event,level)
function changelevel(level)
{
	document.images['townmap' + level].style.visibility = 'hidden';
	if (level>2)
		level = 1;
	else
		level = level + 1;
	document.images['townmap' + level].style.visibility = 'visible';
}
function changelevel_out(level)
{
	//if (!event) event = window.event;
	if (level<3)
	{
		var t = document.images['townmap' + level];
		t.style.visibility = 'hidden';
		level = level + 1;
		t = document.images['townmap' + level];
		t.style.visibility = 'visible';
	}
}
function changelevel_in(level)
{
	//if (!event) event = window.event;
	if (level>1)
	{
		var t = document.images['townmap' + level];
		t.style.visibility = 'hidden';
		level = level - 1;
		t = document.images['townmap' + level];
		t.style.visibility = 'visible';
	}
}
*/
function changelevel_to(level)
{
	//if (!event) event = window.event;
	make_redarrow_stop();
	if (level<1)
		level = 1;
	if (level>numLevels)
		level = numLevels;
	for( var c = 1; c <= numLevels; c++)
	{
		if (c==level)
		{
			//var t = document.images['townmap' + c];
			//t.style.visibility = 'visible';
			//document.images['townmap' + c].style.visibility = 'visible';
			G['townmap' + c].style.visibility = 'visible';
			G.currentMap = G['townmap' + c];
			G['Map_Level_' + c].style.backgroundColor = '#999999';
			//G['NavMap' + c].style.visibility = 'visible';
			G.currentNavMap = G['NavMap' + c];
		}
		else
		{
			G['townmap' + c].style.visibility = 'hidden';
			G['Map_Level_' + c].style.backgroundColor = '#EEEEEE';
			G['NavMap' + c].style.visibility = 'hidden';
		}
	}
}








function clear_description_element()
{
	//var s = document.all.Descriptions_Element;
	//s.innerHTML = "";
	G.Descriptions_Element.innerHTML = "";
}

function copy_description_element()
{
	//moz_copy(G.Descriptions_Element);
	
	//if (!isIE4)
		//moz_copy(G.Descriptions_Element.innerText);
		
	if (isIE4)
	{
		G.holdtext.innerText = G.Descriptions_Element.innerText;
		var Copied = G.holdtext.createTextRange();
		Copied.execCommand("RemoveFormat");
		Copied.execCommand("Copy");
	}	
}
/*
function moz_copy(inElement)
{
	if (inElement.createTextRange) {
    var range = inElement.createTextRange();
    if (range)
     range.execCommand('Copy');
  } else {
    var flashcopier = 'flashcopier';
    if(!document.getElementById(flashcopier)) {
      var divholder = document.createElement('div');
      divholder.id = flashcopier;
      document.body.appendChild(divholder);
    }
    document.getElementById(flashcopier).innerHTML = '';
    var divinfo = '<embed src="_clipboard.swf" FlashVars="clipboard='+escape(inElement.value)+'" width="0" height="0" type="application/x-shockwave-flash"></embed>';
    document.getElementById(flashcopier).innerHTML = divinfo;
  }
}

function moz_copy(copytext)
{
	var str   = Components.classes["@mozilla.org/supports-string;1"].
                       createInstance(Components.interfaces.nsISupportsString);
	if (!str) return false;
	
	str.data  = copytext;
	
	var trans = Components.classes["@mozilla.org/widget/transferable;1"].
	                       createInstance(Components.interfaces.nsITransferable);
	if (!trans) return false;
	
	trans.addDataFlavor("text/unicode");
	trans.setTransferData("text/unicode", str, copytext.length * 2);
	
	var clipid = Components.interfaces.nsIClipboard;
	var clip   = Components.classes["@mozilla.org/widget/clipboard;1"].getService(clipid);
	if (!clip) return false;
	
	clip.setData(trans, null, clipid.kGlobalClipboard);
}
*/
/*
function display_help_text()
{
	var s = document.all.Descriptions_Element;
	s.innerHTML = "Some helpful text";
}
*/


function button_down(event)
{
	//evt = (evt) ? evt : event;
	if (!event) event = window.event;
	if (isIE4)
		event.srcElement.style.backgroundColor = '#999999';
	else
		event.target.style.backgroundColor = '#999999';
	
	/*
	try
	{
		event.srcElement.style.backgroundColor = '#999999';
	}
	catch (e)
	{
		try
		{
			event.target.style.backgroundColor = '#999999';
		}
		catch (e)
		{
		}
	}
	*/
}

function button_up(event,clr)
{
	//evt = (evt) ? evt : event;
	if (!event) event = window.event;
	if (!(clr))
		clr = '#EEEEEE';
	if (isIE4)
		event.srcElement.style.backgroundColor = clr;
	else
		event.target.style.backgroundColor = clr;
	/*
	try
	{
		event.srcElement.style.backgroundColor = clr;
	}
	catch (e)
	{
		//alert('failed');
	}
	*/
}

function toggle_category_menu(d)
{
	if (menucur_cat)
		menucur_cat.style.visibility = 'hidden';
	if (d.style.visibility == 'visible')
		d.style.visibility = 'hidden';
	else
		d.style.visibility = 'visible';
	menucur_cat = d;
}

function toggle_main_menu(d)
{
/*	if (menucur_main==d)
	{
		menucur_main.style.visibility = 'hidden';
	}
	else*/
		
	if (d.style.visibility == 'visible')
	{
		d.style.visibility = 'hidden';
		/*
		if (menucur_cat)
			menucur_cat.style.visibility = 'hidden';
		if (menucur_alpha)
			menucur_alpha.style.visibility = 'hidden';
		if (menucur_main)
			menucur_main.style.visibility = 'hidden';
			*/
		hide_all_menus()
		menucur_main = '';
	}
	else
	{
		d.style.visibility = 'visible';
		/*
		if (menucur_cat)
			menucur_cat.style.visibility = 'hidden';
		if (menucur_alpha)
			menucur_alpha.style.visibility = 'hidden';
		if (menucur_main)
			menucur_main.style.visibility = 'hidden';
			*/
		hide_all_menus()
		menucur_main = d;
	}
	//menucur_main = d;
}

function hide_all_menus()
{
	G.currentNavMap.style.visibility = 'hidden';
	if (menucur_cat)
	{
		menucur_cat.style.visibility = 'hidden';
		menucur_cat = '';
	}
	if (menucur_alpha)
	{
		menucur_alpha.style.visibility = 'hidden';
		menucur_alpha = '';
	}
	if (menucur_main)
	{
		menucur_main.style.visibility = 'hidden';
		menucur_main = '';
	}
}

/*
function set_height_BG_Element()
{
	alert('1: ' + document.all.Descriptions_Element.style.height);
	document.all.BG_Element.style.height = 112 + 4 * (22+6) + 6 + document.all.Descriptions_Element.style.height;
	alert('2');
}
*/

//function map_dblclick(evt)
function map_dblclick(event,d)
{
	//evt = (evt) ? evt : event;
	if (!event) event = window.event;
	//var d = G[idstr];
	//var d = document.all.Map_Image;
	if (d.style.left)
		center_map(d,event.clientX - parseInt(d.style.left),event.clientY - parseInt(d.style.top));
	else
		center_map(d,event.clientX,event.clientY);
	
}

function center_map(d,x,y)
{
	//var d = document.all.Map_Image;
	
	//d.style.left = (-x + parseInt(parseInt(G.screenWidth)/2) + G.panelWidth/2) + 'px';
	//d.style.top = (-y + parseInt(parseInt(G.screenHeight)/2)) + 'px';
	d.style.left = (-x + G.center.x) + 'px';
	d.style.top = (-y + G.center.y) + 'px';
}


function grab_map(event,d)
{
	//alert('1');
	make_redarrow_stop();
	if (!event) event = window.event;
	//var d = document.all.Map_Image;
	if (d.style.left)
	{
		G.map_grabbed_at.x = event.x - parseInt(d.style.left);
		G.map_grabbed_at.y = event.y - parseInt(d.style.top);
	}
	else
	{
		G.map_grabbed_at.x = event.x;
		G.map_grabbed_at.y = event.y;
	}
	//alert('x:' + parseInt(G.map_grabbed_at.x));
	//alert('3');
}

function drag_map(event,d)
{
	if (!event) event = window.event;
	
	//Xmv = event.x - G.screenWidth/2 - G.panelWidth/2;
	//Ymv = event.y - G.screenHeight/2;
	Xmv = event.x - G.center.x;
	Ymv = event.y - G.center.y;

	center_map(d,-Xmv+G.map_grabbed_at.x,-Ymv+G.map_grabbed_at.y);
}
/*
function nav_map_move_onto(evt)
{
	
}
*/
function nav_map_center(event)
{
	//evt = (evt) ? evt : event;
	if (!event) event = window.event;
	//var navmap = G.Navigate_Main_Menu;

	Xcoord = (event.clientX - parseInt(G.Navigate_Main_Menu.style.left) - 5)*full2navScale;
	Ycoord = (event.clientY - parseInt(G.Navigate_Main_Menu.style.top) - 5)*full2navScale;
	
	//document.all.Navigate_Main_Menu.style.left='500px';
	//alert(parseInt(document.all.Navigate_Main_Menu.style.left));
	
	center_map(G.currentMap,Xcoord,Ycoord);
	
}

function nav_map_click(event)
{
	//evt = (evt) ? evt : event;
	if (!event) event = window.event;
	//var navmap = document.all.Navigate_Main_Menu;
	
	Xcoord = (event.clientX - parseInt(G.Navigate_Main_Menu.style.left) - 5)*full2navScale;
	Ycoord = (event.clientY - parseInt(G.Navigate_Main_Menu.style.top) - 5)*full2navScale;
	center_map(G.currentMap,Xcoord,Ycoord);
	hide_all_menus();
}

function nav_map_init_position(event)
{
	//evt = (evt) ? evt : event;
	
	if (!event) event = window.event;
	//var d = document.all.Map_Image;
	//var navmap = document.all.Navigate_Main_Menu;
	G.currentNavMap.style.visibility = 'visible';

	var kluge = {x:(event.clientX - (-parseInt(G.currentMap.style.left) + G.center.x)/full2navScale - 5) + 'px',
	             y:(event.clientY - (-parseInt(G.currentMap.style.top) + G.center.y)/full2navScale - 5) + 'px'};
	G.Navigate_Main_Menu.style.left = (kluge.x>event.clientX) ? event.clientX : kluge.x;
	G.Navigate_Main_Menu.style.top = (kluge.y>event.clientY) ? event.clientY : kluge.y;
	/*
	if (kluge.x>event.clientX)
		G.Navigate_Main_Menu.style.left = event.clientX;
	else
		G.Navigate_Main_Menu.style.left = kluge.x;
	*/
}

function EOL_clicked(evt,eol_fname)
{
	evt = (evt) ? evt : event;
	eol_close();
	eol_window = window.open('EOL_Company_C.html','EOL','width=500,height=700,resizable=no,scrollbars=yes,toolbar=no,location=no,directories=no,status=no,menubar=no,copyhistory=no',true);
	eol_window.moveTo(300,178);
}
function EOL_open(eol_fname)
{
	//evt = (evt) ? evt : event;
	eol_close();
	eol_window = window.open(eol_fname,'EOL','width=700,height=700,resizable=no,scrollbars=yes,toolbar=no,location=no,directories=no,status=no,menubar=yes,copyhistory=no',true);
	//eol_window.moveTo(390,128);
	eol_window.moveTo(300,0);
}

function eol_close()
{
	if (eol_window)
	{
		try
		{
			eol_window.close();
		}
		catch (e)
		{
		}
	}
}

function make_redarrow_visible()
{
	G.redarrow.style.visibility = 'visible';
	setTimeout(make_redarrow_hidden,redarrow_speed/2);
}

function make_redarrow_hidden()
{
	G.redarrow.style.visibility = 'hidden';
	//setTimeout(make_redarrow_hidden,500);
}

function make_redarrow_start()
{
	make_redarrow_stop();
	redarrowtimer = setInterval(make_redarrow_visible,redarrow_speed);
	setTimeout(make_redarrow_stop,redarrow_n*redarrow_speed);
}

function make_redarrow_stop()
{
	try
	{
		clearInterval(redarrowtimer);
	}
	catch (e)
	{
	}
	make_redarrow_hidden();
}

function position_redarrow()
{
	//G.redarrow.style.left = (G.panelWidth + (G.screenWidth - G.panelWidth)/2-15) + 'px';
	//G.redarrow.style.top = (G.screenHeight/2-48) + 'px';
	G.redarrow.style.left = (G.center.x) + 'px';
	G.redarrow.style.top = (G.center.y-48) + 'px';
	
}







/*
function dump_object(src)
{
	dst_obj = document.all.Dump_Element;
	//dst_obj = document.getElementsById('Dump_Element');
	dst_fld = "innerHTML";
	if ((typeof src) == "object")
	{
		//src = document;
		var a;
		var i = 0;
		var b = new Array();
		//var something = document.body.parentElement.clientHeight;
		for(b[i++] in src);
		b.sort();
		for (a in b)
		{
			try
			{
				if ((b[a]!="innerHTML")&&(b[a]!="outerHTML"))
				{
					if (something[b[a]])
						dst_obj[dst_fld] += b[a] + ", " + typeof b[a] + ", " + src[b[a]] + "<br/>";
					else
						dst_obj[dst_fld] += b[a] + ", " + typeof b[a] + "<br/>";
				}
			}
			catch (e)
			{
				dst_obj[dst_fld] += b[a] + ", " + typeof b[a] + ", " + "<i>nope</i>" + "<br/>";
			}
		}
	}
	else
	{
		dst_obj[dst_fld] += src;
	}
}
*/

function getScreenWidth()
{
	return G.screenWidth;
}
function getScreenHeigth()
{
	return G.screenHeight;
}
function flipBigColor()
{
	//alert(G.Big_Element.style.backgroundColor);
	if (G.Big_Element.style.backgroundColor == '#fffffe')
	{
		G.Big_Element.style.backgroundColor = '#fffeff';
		//alert(2);
	}
	else
	{
		G.Big_Element.style.backgroundColor = '#fffffe';
		//alert(3);
	}
}

function toggleHoldButton()
{
	if (G.holdState)
	{
		G.Hold_Button.style.backgroundColor = '#EEEEEE';
	}
	else
	{
		G.Hold_Button.style.backgroundColor = '#999999';
	}
	G.holdState = ~G.holdState;
}
