/*
main.js - this handles displaying the map and so on...
*/

if (GBrowserIsCompatible()) {
var debug='off';
var map = null;

var onebyone='no';

var ovmap = null;
var ovcontrol = null;
var gmarkers=[];
var mp=new Object();
mp.html=[];
mp.dbid=[];
mp.icon=[];
mp.title=[];
mp.savstat=[];
mp.ishome=[];
mp.type=[];
mp.opclo=[];
mp.mob=[];
mp.id=[];
mp.lat=[];
mp.lng=[];
mp.message=[];
mp.edstat=[];
mp.ictype=[];

var tic_pk=new Object();
tic_pk.train='1-n-n';
var lab_part='l_';

var stinfo=new Object();
stinfo.lat=0;
stinfo.lng=0;
stinfo.zoom='';

var blessed= new Object();
blessed.id='';
blessed.title='';

var infoL = [];
var onsc=[];
var onscedit=[];
var onsccl=[];
var madelabel=[];
var madelabelm=[];
var collectsbhtml = [];


/* used by the new transporttaion layer */
var trans=[];
var madetrans=[];
var deftrainstog='on';

/* cookie test */
$.cookie('c_stat','on', {expires: 365, path: '/',domain: ('.' + domsnip)});
if ($.cookie('c_stat')=='on'){var c_stat='on';} else {var c_stat='off';}

/* set the icons to mono, with trains on, on first load */
var urlcolstatus='m';
var coltogopp='on';
var colchval='';
var trainchval=' checked';
var layersfunc='layersoff';

/*  then overide them if the cookie is set...*/
if ($.cookie('coltog')=='on'){coltogopp='off';colchval=' checked';urlcolstatus='c';}
if ($.cookie('trainstog') =='off') {trainchval='';layersfunc='layerson'; tic_pk.train='';} 



var dbid='';
var ishome='';
var i = 0;
var ix = 0;
var fullid=0;
/* edit variables */ 

/* settings - inline optional */
var tic=2; /* initial tictoc settings for labels */
var bufr=1;
var opacity=100;

var xmlgrab='';

var icon_def="shadow50.png";
var emaillink='';
var welcome='<div id="welc">Quick Help<ul><li><b>Search for addresses in Japan<br/> <span class="welchi">in English!</span></b></li><li>On-screen checkboxes to toggle ~120,000 English labels and ~9000 trains and subways. (Use the "details" checkbox to show train labels)</li><li>Double-click to create markers<br/> - save them to your computer.</li><li>Mouse scroll-wheel or slider to zoom </li><li>Click close (above) to toggle this sidebar.</li></ul>[<a href="javascript:welcoff()">don\'t show again (requires cookies)</a> ]</div>';
/* debugging variables */
var ponscreen=0;
var poffscreen=0;
var newlabelcounter=0;
var ponscreenm=0;
var poffscreenm=0;
var nlcountm=0;
var nlcount=0;
var nmcount=0;
var nmlcount=0;
var mar_only='n';
var lab_only='n';

/* global variable to set whether we are myclicking */
var myclick_f=0;
/*a couple of globals */
var tout1g='';
var tout2g='';
var timoutval=1000;


/* set up the ticflag */
$('#labelticker').empty().append('labels: <input id="lticker" type="checkbox" name="checkbox" value="checkbox" onclick="labelsoff()" style="height:13px;width:13px;background:#ff0000;color:#0000ff;border: 1px solid #880000;" checked> <span class="labon">on</span>');
$('#didcopy').empty().append('english labels &#169; 2007 diddlefinger.');
$('#trainticker').empty().append('trains: <input id="tticker" type="checkbox" name="checkbox" value="checkbox" onclick="'+layersfunc+'(\'train\')" style="height:13px;width:13px;background:#ff0000;color:#0000ff;border: 1px solid #880000;" ' + trainchval + '>');
/* only display the col icon if the trainsticker is on */
if (trainchval==' checked'){$('#trainticker').append('&nbsp;detail: <input id="tticker" type="checkbox" name="checkbox" value="checkbox" onclick="tog_col_mon(coltogopp)" style="height:10px;width:10px;background:#ff0000;color:#0000ff;border: 1px solid #880000;"'  + colchval + '>');};






/* A function to create markermanaged markers and set up the event window */ 
function cr_Mar(point,name,html,icon,dbidh) {


var url='/gm/marker_lookup/m_lookup.php?id=' + dbidh;
var html ='<div id="mar_win_cont" style="height:200px;width:370px"></div>';
        var marker = new GMarker(point, {draggable: false, icon:icon});
                
        /* === store the name so that the tooltip function can use it === */
		marker.tooltip = '<div class="tooltip">'+ name +'</div>';
        GEvent.addListener(marker, "click", function() {
        marker.openInfoWindowHtml(html, {onOpenFn: getBubCont(url)});
        });
        
        /* ======The new marker "mouseover" and "mouseout" listeners======  */
		GEvent.addListener(marker,"mouseover", function() {showTooltip(marker);
		});
		GEvent.addListener(marker,"mouseout", function() {
		tooltip.style.visibility="hidden";

		});
				
                
        return marker;
      }


function createAjaxMarker(point, icon, aurl)
{
var html ='<div id="mar_win_cont" style="height:200px;width:370px"></div>';
       var marker = new GMarker(point, {draggable: false, icon:icon});
	   map.addOverlay(marker);
       marker.openInfoWindowHtml(html, {onOpenFn:getBubCont(aurl)});
       return marker;
       }






/* A function to create the marker and set up the event window */ 
function createMarker(point,label,html,icons,mob,dbid,opclo,type,id,savstat,ishome,edstat,ictype) {

/* set the icon size, etc...*/

if (ictype=='ar'){var icon = new GIcon(arIcon);}
else {var icon = new GIcon(baseIcon);}

icon.image = icons;
var name=label;

if (status == 'static')
{
 var marker = new GMarker(point,{draggable: false, icon:icon});
 }
else
{
var marker = new GMarker(point,{draggable: true, icon:icon});
}
 
var cent = marker.getPoint();
/* === store the name so that the tooltip function can use it === */
marker.tooltip = '<div class="tooltip">'+ name +'</div>';


GEvent.addListener(marker, "click", function()
	{
	
	var cent= marker.getPoint();
	/*
	var centlat =marker.getPoint().lat().toFixed(7);
	var centlng =marker.getPoint().lng().toFixed(7);
 */
	var centlat =roundNumber(marker.getPoint().lat().toString(),6);
	var centlng =roundNumber(marker.getPoint().lng().toString(),6);



	/* we should assign this a temporary id of "noid" */
	if (dbid == null) {dbid='noid'};

	if (mode !='b')
		{
	/* ====== EDIT MODE ONLY */
	/* prepare to make pulldowns in js. */
	var pd_sel= map.getZoom();
	pulldown1=makePulldown(pd_name1,pd_data,pd_sel);
	pulldown2=makePulldown(pd_name2,pd_data,pd_sel);
	pulldown3=makePulldown_SC(pd_name3,pd_data3,supercatid,pd_sel);
	pulldown4=makePulldown_SC(pd_name3,pd_data4,supercatid,pd_sel);
	pulldown5=makePulldown_loc(pd_name3,pd_data4,supercatid,pd_sel);
	
	/*====== end edit mode only */
	
	var tab1= makelab(centlat,centlng,pulldown1,pulldown2,pulldown3,html);
	var infoL = makeinfoL_edit(tab1);
		}
	
	else
	{
		if (parseInt(blessed.id) >= 0)
		{
		var mfromhome= cent.distanceFrom(blessed.point);
		};

		var tab1 = getdist(mfromhome,blessed.title,centlat,centlng);
		var tab2 = makepupEmail(mob,dbid,centlat,centlng,id);
		if(ictype=='ar'){var tab2 = makepupEmailAr(mob,dbid,centlat,centlng,id);}
			else{var tab2 = makepupEmail(mob,dbid,centlat,centlng,id);}
		
		
		var tab4 = makemaintab(id);
		var tab5 = makeexporttab(id);	
		var infoL = makeinfoL(tab4,tab2,tab5,tab1);
	}

 /* open window */
 
	 marker.openInfoWindowTabsHtml(infoL);

	
	
	mp.lat[id]=centlat;
	mp.lng[id]=centlng;

/* display */
	tout1g=id;
	setTimeout("dsp_fld(tout1g,mp.edstat[tout1g])", timoutval);


});
/* -=============== end listener....======================== */

 
 
 GEvent.addListener(marker, "dragend", function()
 
 	{

 	/* this next must stay inside the listener. */
 	/* it makes sure that when we click on a marker, it has the right ll. */

	var cent = marker.getPoint();

 	/* make a variable to be referenced by the php variable, below */
	/*
	var centlat =marker.getPoint().lat().toFixed(7);
	var centlng =marker.getPoint().lng().toFixed(7); 
	*/
	
	var centlat =roundNumber(marker.getPoint().lat().toString(),6);
	var centlng =roundNumber(marker.getPoint().lng().toString(),6);
	
	
	var tab1='';var tab2='';var tab4='';	


	if (mode !='b')
		{
		
	/*
	================================================
	 EDIT MODE ONLY
	 set up the variable
	 */
	var pd_sel= map.getZoom();
	pulldown1=makePulldown(pd_name1,pd_data,pd_sel);
	pulldown2=makePulldown(pd_name2,pd_data,pd_sel);
	pulldown3=makePulldown_SC(pd_name3,pd_data3,supercatid,pd_sel);
	pulldown4=makePulldown('scat_id',pd_data4,'3');
	pulldown5=makePulldown_SC('pref_cid',pd_data3,supercatid,pd_sel);
	/* === end edit mode only ========================	*/
		var tab1 = 'Edit ' + dbid + makelab(centlat,centlng,pulldown1,pulldown2,pulldown3,html);
	var infoL = makeinfoL_edit(tab1);
	}
	
	else 
	{
	if (parseInt(blessed.id) >= 0)
		{
		var mfromhome= cent.distanceFrom(blessed.point);
		}
	var tab1 = getdist(mfromhome,blessed.title,centlat,centlng);
	if(ictype=='ar'){var tab2 = makepupEmailAr(mob,dbid,centlat,centlng,id);}
	else{var tab2 = makepupEmail(mob,dbid,centlat,centlng,id);}
	var tab4 = makemaintab(id);
	var tab5 = makeexporttab(id);	
	var infoL = makeinfoL(tab4,tab2,tab5,tab1);
	}
	
	marker.openInfoWindowTabsHtml(infoL);
 
	mp.lat[id]=centlat;
	mp.lng[id]=centlng;

/* display */
	tout1g=id;
	setTimeout("dsp_fld(tout1g,mp.edstat[tout1g])", timoutval);
	
  
 });
/* ======== end listener... ================================ */

 

gmarkers[id] = marker;
mp.html[id]=html;
mp.title[id]=label;
mp.icon[id]=icons;
mp.mob[id]=mob;
mp.dbid[id]=dbid;
mp.opclo[id]=opclo;
mp.type[id]=type;
mp.id[id]=id;
mp.savstat[id]=savstat;
mp.ishome[id]=ishome;
mp.edstat[id]=edstat;
mp.ictype[id]=ictype;

/* $('#log').append(id); */

map.addOverlay(marker);

/*
do we need to open this window?
 we don't do it for MSIE browsers as they have problems...
*/

if (opclo=='o')
{

/*
	var lat =marker.getPoint().lat().toFixed(7);
	var lng =marker.getPoint().lng().toFixed(7);
*/
	var lat =roundNumber(marker.getPoint().lat().toString(),6);
	var lng =roundNumber(marker.getPoint().lng().toString(),6);

	
	if (mode != 'b' && dbid == 'noid')
	{
	
	/*================================================
	 EDIT MODE ONLY
	 set up the variable */
	var pd_sel= map.getZoom();
	pulldown1=makePulldown(pd_name1,pd_data,pd_sel);
	pulldown2=makePulldown(pd_name2,pd_data,pd_sel);
	pulldown3=makePulldown_SC(pd_name3,pd_data3,supercatid,pd_sel);
	pulldown4=makePulldown('scat_id',pd_data4,'3');
	pulldown5=makePulldown_SC('pref_cid',pd_data3,supercatid,pd_sel);

	var tab1 = makelab(lat,lng,pulldown1,pulldown2,pulldown3,html);
	var infoL = makeinfoL_edit(tab1);	

	/* === end edit mode only ========================	*/	
	}
	else
	{
		if (parseInt(blessed.id) >= 0)
		{
		var mfromhome= cent.distanceFrom(blessed.point)
		}
		
	var tab1 = getdist(mfromhome,blessed.title,lat,lng);
	
	if(ictype=='ar'){var tab2 = makepupEmailAr(mob,dbid,lat,lng,id);}
	else{var tab2 = makepupEmail(mob,dbid,lat,lng,id);}
	
	var tab4 = makemaintab(id);
	var tab5 = makeexporttab(id);	
	var infoL = makeinfoL(tab4,tab2,tab5,tab1);		
	}

	to_marker=marker;
	to_infoL=infoL;
	setTimeout("to_marker.openInfoWindowTabsHtml(to_infoL,{suppressMapPan:true})",1);

/* display */
 	to_id=id;to_mp=mp.edstat[id];
 	tout1g=id;tout2g=mp.edstat[id];
	setTimeout("dsp_fld(tout1g,tout2g)", timoutval);

	mp.lat[id]=lat;
	mp.lng[id]=lng;

}

/* ======The new marker "mouseover" and "mouseout" listeners======  */
GEvent.addListener(marker,"mouseover", function() {showTooltip(marker);
});
GEvent.addListener(marker,"mouseout", function() {
tooltip.style.visibility="hidden"
});

/* store information and return */
var sbpack=[];
sbpack['html']='<a href="javascript:myclick(' + id + ')" onmouseover="mymouseover('+id+')" onmouseout="mymouseout()">' + name + '</a>';
/* sbpack['index']=id; */
sbpack['type']=type;



/* i++; */



return sbpack;
}
/* ================================================end of createmarker======== */






function makemaintab(i)
{

var ret='<div class="outerbubcont" style="height:190px;"><div class="bf_tcont"><div id="bf_subl_'+i+'"></div><div id="bf_subr_'+i+'"></div></div><div class="bubcont"><div id="bf_cnt1_'+i+'"></div><div id="bf_cnt2_'+i+'"></div><div id="bf_cnt3_'+i+'"></div></div><div class="bf_bcont"><div id="bf_status_'+i+'"></div><div id="bf_del_'+i+'"></div></div></div>';
return ret;
}

function makeexporttab(i)
{
var ret='<div class="outerbubcont" style="height:190px;"><div id="bf_kml_'+ i +'"></div><div id="bf_gpx_'+ i +'"></div><div id="bf_kmlv_'+ i +'"></div></div>';
return ret;
}



				

/* ==========================
 This function picks up the side_bar click and opens the corresponding info window */

function myclick(i) {
	
	myclick_f=1;
	var gpo=gmarkers[i].getPoint();
	var lat =gmarkers[i].getPoint().lat().toString();
	var lng =gmarkers[i].getPoint().lng().toString();
	lat=roundNumber(lat,6);
    lng=roundNumber(lng,6);
	

if (mode !='b')
{

	/* ====== EDIT MODE ONLY
	prepare to make pulldowns in js.
	*/
	var pd_sel= map.getZoom();
	pulldown1=makePulldown(pd_name1,pd_data,pd_sel);
	pulldown2=makePulldown(pd_name2,pd_data,pd_sel);
	pulldown3=makePulldown_SC(pd_name3,pd_data3,supercatid,pd_sel);
	pulldown4=makePulldown('scat_id',pd_data4,'3');
	pulldown5=makePulldown_SC('pref_cid',pd_data3,supercatid,pd_sel);

	/* ====== end edit mode only */
	
var lab = makelab(lat,lng,pulldown1,pulldown2,pulldown3,html);
var tab1 = '<a href="' + mp.dbid[i] + '">Edit ' + mp.dbid[i] + '</a>' + lab;
}

else 
{
	if (parseInt(blessed.id) >= 0)
	{
		var mfromhome= gpo.distanceFrom(blessed.point);
	}
	var tab1 = getdist(mfromhome,blessed.title,lat,lng);
	
	if (mp.ictype[i]=='ar'){var tab2 = makepupEmailAr('static',mp.dbid[i],lat,lng,i);}else{
	var tab2 = makepupEmail('static',mp.dbid[i],lat,lng,i);}
	var tab4 = makemaintab(i);	
	var tab5 = makeexporttab(i);
} 


/* set the lat */
mp.lat[i]=roundNumber(gmarkers[i].getPoint().lat().toString(),6);
mp.lng[i]=roundNumber(gmarkers[i].getPoint().lng().toString(),6);

/* make the tabs */



/* make the infoL for the popup */
if (mode=='b'){infoL = makeinfoL(tab4,tab2,tab5,tab1);}
	else {var infoL = makeinfoL_edit(tab1);}

/* open window */
gmarkers[i].openInfoWindowTabsHtml(infoL);

/* display */

 	tout1g=i;tout2g=mp.edstat[i];
 	
	setTimeout("dsp_fld(tout1g,tout2g)", timoutval);
	
/*  dsp_fld(i,mp.edstat[i]);  */

/* end of function.... */
}
 

 
/* =========================================================================== */ 

/* basic map setup and creation */

var map = new GMap2(document.getElementById("map"));
map.addMapType(G_PHYSICAL_MAP);
map.addControl(new GLargeMapControl(),
new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(2, 20)));
map.addControl(new GMapTypeControl(),
new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(2, 2))
);

 

/* v large icon size... */
var vlmIcon = new GIcon();
vlmIcon.iconSize = new GSize(32, 32);
vlmIcon.iconAnchor = new GPoint(5, 5);
vlmIcon.infoWindowAnchor = new GPoint(11, 5);
vlmIcon.infoShadowAnchor = new GPoint(9, 12);


/* transfer large icon size */
var lmtIcon = new GIcon();
lmtIcon.iconSize = new GSize(20, 20);
lmtIcon.iconAnchor = new GPoint(8, 8);
lmtIcon.infoWindowAnchor = new GPoint(16, 8);
lmtIcon.infoShadowAnchor = new GPoint(18, 25);

/* transfer small icon size */
var smtIcon = new GIcon();
smtIcon.iconSize = new GSize(14, 18);
smtIcon.iconAnchor = new GPoint(7, 9);
smtIcon.infoWindowAnchor = new GPoint(16, 8);
smtIcon.infoShadowAnchor = new GPoint(18, 25);



/* large icon size */
var lmIcon = new GIcon();
lmIcon.iconSize = new GSize(16, 16);
lmIcon.iconAnchor = new GPoint(8, 8);
lmIcon.infoWindowAnchor = new GPoint(16, 8);
lmIcon.infoShadowAnchor = new GPoint(18, 25);

/* small icon size... */
var smIcon = new GIcon();
smIcon.iconSize = new GSize(12, 12);
smIcon.iconAnchor = new GPoint(5, 5);
smIcon.infoWindowAnchor = new GPoint(11, 6);
smIcon.infoShadowAnchor = new GPoint(9, 12);


/* v small icon size... */
var vsmIcon = new GIcon();
vsmIcon.iconSize = new GSize(1, 1);
vsmIcon.iconAnchor = new GPoint(5, 5);
vsmIcon.infoWindowAnchor = new GPoint(11, 5);
vsmIcon.infoShadowAnchor = new GPoint(9, 12);

/* arrow size... */
var arIcon = new GIcon();
arIcon.iconSize = new GSize(23, 34);
arIcon.iconAnchor = new GPoint(11,34);
arIcon.infoWindowAnchor = new GPoint(12, 0);
arIcon.infoShadowAnchor = new GPoint(0, 0);
arIcon.shadow = pathtomarkers + 'arr_shad.png';
arIcon.shadowSize = new GSize(39, 34);




/* first set of options is for the visual overlay.*/
  var boxStyleOpts = {
    opacity:.2,
    border:"2px solid #ff6600"
  };

/* second set of options is for everything else */
  var otherOpts = {
    buttonHTML:"<img src='/gm/im/zoom_off.gif' />",
    buttonZoomingHTML:"<img src='/gm/im/zoom_on.gif' />",
    buttonStartingStyle:{width:'16px',height:'16px',border:'1px solid #333333'}
  };

/* third set of options specifies callbacks */
var callbacks = {
   
  };
  
  map.addControl(new DragZoomControl(boxStyleOpts, otherOpts, callbacks),new GControlPosition(G_ANCHOR_TOP_LEFT,new GSize(548,2)));























/* this handles the scrollwheel scrolling */
map.enableScrollWheelZoom();

/* this handles the inlay map */ 
ovcontrol = new GOverviewMapControl(new GSize(200,200));

map.addControl(ovcontrol);
/* ovcontrol.hide(true); */

/* ovmap = ovcontrol.getOverviewMap(); */
/* setTimeout("ovmap.setMapType(ovmaptype);",1); */

 /* set centre */
map.setCenter(new GLatLng(latc,lngc), zoom, maptype);
 map.addControl(new PanoMapTypeControl());
 
/* add adsmanager */ 

var adsManager = new GAdsManager(map, "pub-3837424725182189");
adsManager.enable();

 
 

/* set base icon */
var baseIcon = new GIcon();
baseIcon.shadow = pathtomarkers + icon_def;
baseIcon.iconSize = new GSize(20, 34);
baseIcon.shadowSize = new GSize(37, 34);
baseIcon.iconAnchor = new GPoint(9, 34);
baseIcon.infoWindowAnchor = new GPoint(9, 2);
baseIcon.infoShadowAnchor = new GPoint(18, 25);



/* add button */
var pos = new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(282,2));
pos.apply(document.getElementById("infobox"));
map.getContainer().appendChild(document.getElementById("infobox"));

var poscop = new GControlPosition(G_ANCHOR_BOTTOM_LEFT, new GSize(70,4));
poscop.apply(document.getElementById("didcopyline"));
map.getContainer().appendChild(document.getElementById("didcopyline"));

var gltib = new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(1,70));
gltib.apply(document.getElementById("gl_tib"));
map.getContainer().appendChild(document.getElementById("gl_tib"));

var glbox = new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(50,65));
glbox.apply(document.getElementById("gl_box"));
map.getContainer().appendChild(document.getElementById("gl_box"));

var goobox = new GControlPosition(G_ANCHOR_BOTTOM_LEFT, new GSize(2,32));
goobox.apply(document.getElementById("goo_box"));
map.getContainer().appendChild(document.getElementById("goo_box"));
$('#goo_box').css("display","inline");

var gooline = new GControlPosition(G_ANCHOR_BOTTOM_LEFT, new GSize(66,14));
gooline.apply(document.getElementById("goo_line"));
map.getContainer().appendChild(document.getElementById("goo_line"));
$('#goo_line').css("display","inline");


/* set label mm  */
var mml = new MarkerManager(map,{maxZoom:19,borderPadding:20});

/* set transportation mm */
var mmt = new MarkerManager(map,{maxZoom:19,borderPadding:1});

map.enableContinuousZoom();
map.disableDoubleClickZoom();

/*===== end of setup*/

/* =======================
 ====== set up marker mouseover tooltip div */
var tooltip = document.createElement("div");
map.getPane(G_MAP_FLOAT_PANE).appendChild(tooltip);
tooltip.style.visibility="hidden";


/*
==============================================================
we send this an array and it passes it to the createmarker code.
as a function, we can send it off to external JS.
*/



function create_from_array(om)
	{
	
blessed.id='';
blessed.title='';
blessed.point='';
	
	
while (ix < om.length) {

var ajtype='';
var ajurl='';

var lat = om[ix][0];
var lng = om[ix][1];
var point = new GLatLng(lat,lng);
var html= om[ix][2];
var label = om[ix][3];
var icons = om[ix][4];
var dbid = om[ix][5];
var mob = om[ix][6];
var opclo = om[ix][7];
var type= om[ix][8];
var id= om[ix][9];
var savstat= om[ix][10];
var ishome = om[ix][11];
var edstat = om[ix][12];
var ajtype = om[ix][13];
var ajurl = om[ix][14];
var ictype = om[ix][15];


/* no ajtype - so a standard display */

if (ajtype == 'ajurl' || ajtype =='ajadd')
{
/*
send it off to an ajax opener
we have a choice of two...
if we need different types, this is where we add them...
*/

if (ajtype=='ajurl')
{
	var aurl='/gm/marker_lookup/m_lookup.php?id=a' + ajurl;
	var ico = new GIcon(vsmIcon);
	ico.image = icons;

}
else if (ajtype == 'ajadd')
	{
	var aurl='/gm/marker_lookup/jpadd_lookup.php?q=' + ajurl;
	var ico = new GIcon(vsmIcon);
	ico.image = icons;


	}
else 
	{
	exit();
	}
	
var poo=createAjaxMarker( point ,ico, aurl);
}
else
{
/* normal, not ajax...*/
 	if (edstat==''){edstat='eoff';}

	if (ishome == 1)
	{
	blessed.id=ix;
	blessed.title=label;
	blessed.point=point;
	}

/*
 grab the html and id and mob and send to create marker.
 the marker creator returns an array. This becomes a subarray of collectsbhtml[id]
*/

collectsbhtml[id]= createMarker(point,label,html,icons,mob,dbid,opclo,type,id,savstat,ishome,edstat,ictype);



} /* finish type */

ix++;

} /* finish loop */
	
	
return collectsbhtml;

}


     function getBubCont(v)
	{
	var i=v.split('=');
	$.post(v,{id: i[1]},
	function(t)
  	 {
  	tout1g=t;
  	setTimeout("$('#mar_win_cont').empty().append(tout1g)", 1000);
	});
	}



/*
==============================================================
 used by the function that grabs from an array...
 splashes an html pack in the sidebar...
 we can use it if we want to delete something
*/

function splashsidebar(sbdata)
{
	var sentplaces='';
	var myplaces='';
	var newplaces='';

	var ox=0;
	while (ox < sbdata.length) {
	var blesspoint='';
	if (mp.id[ox] == blessed.id && blessed.id !=''){blesspoint=' &#171;';}
	
	if (sbdata[ox]['type']=='cookie')
	{
	myplaces += sbdata[ox]['html']+blesspoint + '<br/>';
		} 
	else if (sbdata[ox]['type']=='sent')
	{
	sentplaces += ''+sbdata[ox]['html'] + '<br/>';
		}
	else if (sbdata[ox]['type']=='new')
	{
	newplaces += ''+sbdata[ox]['html'] + '<br/>';
		}
		
	else
	{
	/*  newplaces += '<br/>'+sbdata[ox]['html']; */
	newplaces +=sbdata[ox]['type'];
	}	
		
	ox++;
	} /* end while */

/* empty them in all cases */
$('#side_bar_top').empty(); 
$('#side_bar_exp').empty();
$('#side_bar_new').empty();
/* $('#side_bar').empty(); */

if (sentplaces !=''){ $('#side_bar_exp').append('<div class="sb_ttl">This page (unsaved)</div>' + sentplaces);}
if (newplaces !=''){ $('#side_bar_new').append('<div class="sb_ttl">Unsaved markers</div>'+ newplaces);}
if (myplaces !=''){$('#side_bar_top').append('<div class="sb_ttl">My Places</div>'+ myplaces);}

} /* end of function */
/* ============================================================== */

/*  splashes the database include... */
function splashdbsidebar(sbdata)
{
	var dbplaces='';
	var ox=0;
	while (ox < sbdata.length) {
	dbplaces += '<br/>'+sbdata[ox]['html'];
	ox++;
	} /* end while */
/* empty in all cases */
$('#side_bar').empty();
if (dbplaces !=''){ $('#side_bar').append('<b>From Database</b>'+dbplaces);}

} /*
end of function
*/
/*
============================================================== 
*/










/*=======================
 CASE - read markers from database...
 we are not bothering with this at present
*/
if (mode != 'al' && mode != 'el'){





/*
======================= 
CASE - now add anything from the session

first splash the array data... 
this is a mixture of anything from the cookie (we look for it with php) and
anything we have set to be displayed 'incoming';
splash the array of all the places we have copied from the cookie, plus anything that is the esult of a lookup. 
*/


/* send the array off for processing */
if (om)
{
	/*
	this creates/modifies a array called collectsbhtml
	*/
	create_from_array(om);

	/*
	which we can send off to be splashed.
	*/
	splashsidebar(collectsbhtml);
	}

}
 /*
 off if al or el mode...
 */





/*
=====================
 CASE - check for creation of new place...
*/

var markerthree=[];

GEvent.addListener(map, "dblclick", function(marker, point) {

if (marker != null) return;

if (point) {

/*
 reset the variable...
we reset this in edit mode....
*/

if (mode !='b'){maxid=0;}

var id = maxid;

mp.html[id]='Type some text here. No tags except <b>bold</b>.';
mp.title[id]='New&#160;Place';
mp.icon[id]= pathtomarkers + pAc.newicon;
mp.mob[id]= 'drag';
mp.dbid[id]= 'noid';
mp.opclo[id]= 'o';
mp.type[id]='new';
mp.id[id]= maxid;
mp.savstat[id]= 'n';
mp.ishome[id]= '0';
mp.edstat[id]= 'eon';
mp.ictype[id]= 'base';

/*
 this returns an array of values for the sidebar. 
*/
markerthree = createMarker(point,mp.title[id],mp.html[id],mp.icon[id],mp.mob[id],mp.dbid[id],mp.opclo[id],mp.type[id],mp.id[id],mp.savstat[id],mp.ishome[id],mp.edstat[id],mp.ictype[id]);

/*
 which we add to our sidebar collection 
*/
collectsbhtml[id]=markerthree;

collectsbhtml[id]['type']='new';

/*
  which we can send off to be splashed. 
*/
splashsidebar(collectsbhtml);

/*
 increment so that the next marker we make picks up the correct value
*/
maxid++;

i++;

}

});




/*
 =======================
 CASE - read localities from database... 
*/

function read_labels()
{

var urltoget = pathtoxml + xmlgrab;

var request2 = GXmlHttp.create();
/*
NOTE - NO TRAILING SLASH 
*/ 
request2.open("POST", urltoget, true);
request2.onreadystatechange = function() {
if (request2.readyState == 4) {
var xmlDoc = request2.responseXML;

/*
 obtain the array of markers and loop through it
*/ 
var markers = xmlDoc.documentElement.getElementsByTagName("mk");
for (var i = 0; i < markers.length; i++) {
/*
 obtain the attributes of each marker
 NOTE: different marker types have different attributes...
*/
var mtype = markers[i].getAttribute("q");
var lat = parseFloat(markers[i].getAttribute("l"));
var lng = parseFloat(markers[i].getAttribute("m"));
var zfr = parseInt(markers[i].getAttribute("f"));
var zto = parseInt(markers[i].getAttribute("t"));

/*
these are marker specific...
*/
if (mtype=='l'){
var locality = markers[i].getAttribute("n");
var style = markers[i].getAttribute("s"); if (style==''){style='s1';}
var dbid = parseInt(markers[i].getAttribute("r"));
var dbidh='a' + dbid;
} 
else if (mtype=='m'){
var xm_name = markers[i].getAttribute("n");
var xm_type = markers[i].getAttribute("s");
var xm_spc = markers[i].getAttribute("v");

var xm_id = markers[i].getAttribute("r");
var xm_style = 'm' +  markers[i].getAttribute("b");


var dbidh='a' + xm_id + '_' + xm_spc;
}

var point = new GLatLng(lat,lng);

if (zfr < 0){zfr=0;};if (zto > 19) {zto=0;};

/* have we already made and stored this label? */

if (onsc[dbidh] != 1  && mtype =='l')
{
madelabel[dbidh]=createElabel(point, locality, style, opacity, "");
onsc[dbidh]=1;
mml.addMarker(madelabel[dbidh],zfr,zto);
nlcount++;

}
else if (trans[dbidh] != 1 && mtype=='m')
{
/*
this relates to additional overlays, that are added to new markermanagers
*/

if (xm_type =='tmulti') {var mxvl = 11;var myvl = 5;}
else {var mxvl = 7;var myvl = 5;};

/*
is the detail checkbox set? (this flag is inverted but safe)
*/
if (coltogopp == 'off')
{
madetrans[dbidh] = new ELabel(point, xm_name, xm_style, new GSize(mxvl,myvl), opacity, "");
trans[dbidh]=1;
zfr2=zfr + 2;

mmt.addMarker(madetrans[dbidh],zfr2,zto);
nmlcount++;

}

/*
we want to create two markers for each entity - a large and a small...
we may want to do this based on the marker type - so we COULD send something with the xml
to denote both preset icon size, how many we associate and the zoom levels to display the different sizes at...
HOWEVER: - since we have many marker types, this would be tricky...
*/


var sffz=2;
var html='<b>'+xm_name+'</b>';

if (xm_type =='tmulti') {var icon_sm = new GIcon(smtIcon);}
else {var icon_sm = new GIcon(smIcon);};

icon_sm.image = '/gm/im/' + xm_type + '_s.png';
var markerS = cr_Mar(point, xm_name, html, icon_sm, dbidh);


if(xm_type=='tmulti') {var icon_lm = new GIcon(lmtIcon);}
else {var icon_lm = new GIcon(lmIcon);};

icon_lm.image = '/gm/im/' + xm_type + '.png';
var markerL = cr_Mar(point, xm_name, html,icon_lm,dbidh);


/* is the detail checkbox set? (this flag is inverted but safe) 
 if so - only allow small markers...
 */
if (coltogopp == 'on')
{
markerL=markerS;
}

mmt.addMarker(markerL,(zfr + sffz),zto);
mmt.addMarker(markerS,zfr, (zfr + sffz - 1));
nmcount++;
}




/*
 ======= EDIT MODE ========
*/
 if (mode == 'el')
 {
   /* create a marker to edit it... */
 	var level_from = parseInt(markers[i].getAttribute("p"));
	var level_to = parseInt(markers[i].getAttribute("j"));
 	var scid = parseInt(markers[i].getAttribute("u"));
	var cid = parseInt(markers[i].getAttribute("w")); 
 	var supcid = parseInt(markers[i].getAttribute("v")); 
 
 if (onscedit[dbidh] != 1 && onscedit[dbidh] != 2)
{
var icon = new GIcon(baseIcon);
 icon.image = pathtomarkers + "loc.png";
 madelabelm[dbidh] = new GMarker(point,{draggable: true, icon:icon});
onscedit[dbidh] = 1;
/* for debugging */
nlcountm++;
}

/*
 do we have any removedmarkers to be added to the screen 
*/
if (onscedit[dbidh] == 1)
{
var mlo = cMEDIT(madelabelm[dbidh],point,level_from,level_to,locality,dbid,scid,cid,supcid,dbidh);
}

} 

/*
 ======= OFF EDIT MODE========
*/
}

/*
 this tells us how many markers we have justcreated...
 */
}

$('#l_loaded').empty().append('Labels: '+ nlcount + ' of ~120,000 |');
$('#m_loaded').empty().append('Markers: '+ nmcount + ' of ~9,000 |');
};
request2.send(null);
getarea();
/*
$('#l_loaded').empty().append('Labels2: '+ nlcount + ' of ~120,000 |');
$('#m_loaded').empty().append('Markers2: '+ nmcount + ' of ~9,000 |');
*/
};
/* 
 end read LABELS/Markers
*/
/* 
=================================================================================
*/



/*
 ==========================
 only for edit mode...
*/
function cMEDIT(marker,point,level_from,level_to,locality,dbid,scid,cid,supcid,dbidh)
{

/*
function to handle marker creation to edit label in edit mode... 
*/
map.addOverlay(marker); window["ponscreenm"]++;
onscedit[dbidh] = 2;

GEvent.addListener(map, "zoomend", function(ol,nl)
{
map.removeOverlay(marker);window["poffscreenm"]--; onscedit[dbidh]=1;
});

 GEvent.addListener(map, "moveend", function()
{
var fl=0;
var pkr=gtoffbnds(point,fl,'');
if (pkr == false && (onscedit[dbidh]==2))
{
map.removeOverlay(marker);window["poffscreenm"]--;onscedit[dbidh]=1;
} 
});

/*
  these are used in both the below 
*/
var pd_sel1 = level_from;
var pd_sel2 = level_to;
/*
  make pulldowns - some of the variables are global 
*/
var pulldown1=makePulldown(pd_name1,pd_data,pd_sel1);
var pulldown2=makePulldown(pd_name2,pd_data,pd_sel2);


GEvent.addListener(marker, "click", function() {
 /*
  click, so open with details for editing 
*/
/*
  this is variable, so has to be checked each time... 
*/
var centlat = marker.getPoint().lat().toFixed(7);
var centlng = marker.getPoint().lng().toFixed(7);

html=make_loc_edit(centlat,centlng,pulldown1,pulldown2,locality,dbid,scid,cid,supcid);
marker.openInfoWindow(html);
});


 GEvent.addListener(marker, "dragend", function() {
/*
  dragged, so open with details for editing 
*/
 /*
  this is variable, so has to be checked each time... 
*/

/*
var centlat = marker.getPoint().lat().toFixed(7);
var centlng = marker.getPoint().lng().toFixed(7);
*/
	var centlat =roundNumber(marker.getPoint().lat().toString(),6);
	var centlng =roundNumber(marker.getPoint().lng().toString(),6);

html=make_loc_edit(centlat,centlng,pulldown1,pulldown2,locality,dbid,scid,cid,supcid);
marker.openInfoWindow(html);
}); 



 /*
 cMaEDIT end
 */ 
} 


/*

	 Make the link the first time the page opens
	 Make the email the first time the page opens
	 Make the link again whenever the map changes
	 Make the email again whenever the map changes
	 grab the labels when the page opens...
*/




/*
we do this at the end of the template
*/

makepageEmail();

GEvent.addListener(map, 'moveend', makeLink);
GEvent.addListener(map, 'moveend', makepageEmail);


/* 
note that the pagepopup email is called from within the relevant listener...
*/

xmlgrab = xmlg(stinfo);
setTimeout("read_labels()",1);

/*
 Make the labels again whenever the map pans or zooms 
*/


GEvent.addListener(map, 'movestart', function()
{
stinfo.lat = parseFloat(map.getCenter().lat().toFixed(7));
stinfo.lng = parseFloat(map.getCenter().lng().toFixed(7));
stinfo.zoom = parseInt(map.getZoom());
});



GEvent.addListener(map, 'moveend', function()
{
if (tic > 0 && lab_part =='l_' || tic_pk.train !='')
	{
	if (map.getZoom() != stinfo.zoom || myclick_f==1){stinfo.lat=0;stinfo.lng=0;myclick_f=0;}
	xmlgrab=xmlg(stinfo);
	setTimeout("read_labels()",1);
		
	}

	
}); 


/*
have we zoomed? if so force a complete reload 
if we have no lab part and no train part, we don't do this 
the myclick flag is to make sure that we only get the new screen if we pan from Tokyo to Fukuoka 
*/



function labelsoff(){
	if (tic > 0){tic--;} else {tic=0;};
	lab_part='n_';
	nlcount=0;
	var ticflag='<input id="lticker" type="checkbox" name="checkbox" value="checkbox" onclick="labelson()" style="height:13px;width:13px;"> <span class="laboff">off</span>';
	$('#labelticker').empty().append('labels: ' + ticflag);
	$('#l_loaded').empty().append('Labels: 0 of ~120,000 |');
	mml.clearMarkers();
	return true;
	}


function labelson()
{
	madelabel= [];onsc=[];nlcount=0;
	tic++;
	lab_part='l_';
	lab_only='y';
	stinfo.lat=0;stinfo.lng=0;
	xmlgrab=xmlg(stinfo);
	setTimeout("read_labels()",0);
	var ticflag='<input id="lticker" type="checkbox" name="checkbox" value="checkbox" onclick="labelsoff()" checked style="height:13px;width:13px;"> <span class="labon">on</span>';
	$('#labelticker').empty().append('labels: ' + ticflag);	
	return true;
}



function layerson(lay_tp)
{

	madetrans = [];trans=[];nmcount=0;nmlcount=0;

	tic++;
	/* switch on for the train layer */
	if (lay_tp=='train')
	{
	$.cookie('trainstog','on', {expires: 365, path: '/', domain: ('.' + domsnip)});
	nmcount=0;
	mar_only='y';
	var trainchval=' checked';
	tic_pk.train = '1-n-n';
	var ticflag='<input id="tticker" type="checkbox" name="checkbox" value="checkbox" onclick="layersoff(\'train\')" ' + trainchval + ' style="height:13px;width:13px;">&nbsp;detail: <input id="tticker" type="checkbox" name="checkbox" value="checkbox" onclick="tog_col_mon(coltogopp)" style="height:10px;width:10px;background:#ff0000;color:#0000ff;border: 1px solid #880000;" ' + colchval +'>';
	$('#trainticker').empty().append('trains: ' + ticflag);
	}
	
	stinfo.lat=0;stinfo.lng=0;
	xmlgrab=xmlg(stinfo);
	setTimeout("read_labels()",1);

	return true;
}

function layersoff(lay_tp){


	/*
	don't kill the labels layer
	*/
	if (tic > 0){tic--;} else {tic=0;};

	/*
	switch off the train
	*/
	if (lay_tp=='train'){
	$.cookie('trainstog','off', {expires: 365, path: '/',domain: ('.' + domsnip)});
	tic_pk.train='';
	var ticflag='<input id="tticker" type="checkbox" name="checkbox" value="checkbox" onclick="layerson(\'train\')" style="height:13px;width:13px;">';
	$('#trainticker').empty().append('trains: ' + ticflag);
	$('#m_loaded').empty().append('Markers: 0 of ~9,000 |');
	}
	mmt.clearMarkers();
	return true;
	}



/* 
toggles the icons between colour and mono...
*/
function tog_col_mon(mon_col){

	/*
	set global url element
	*/
	if (mon_col=='on'){urlcolstatus='c';} else {urlcolstatus='m';};
	
	/*
	toggle the value for the tog_col_mon function call in the checkbox
	*/
	if (coltogopp=='off'){coltogopp='on';colchval='';} else {coltogopp='off';colchval=' checked';}
		
	/*
	set current status to cookie
	*/
	$.cookie('coltog', mon_col, {expires: 365, path: '/', domain: ('.' + domsnip)});
	

	stinfo.lat=0;stinfo.lng=0;
	xmlgrab=xmlg(stinfo);
	setTimeout("read_labels()",0);
	madetrans = [];trans=[];nmcount=0;nmlcount=0;
	
	/*
	clear train markers
	*/
	mmt.clearMarkers();
	
	return true;
	}

 



function createElabel(point, locality, style, opacity, nani) {
       
      marker=new ELabel(point, locality, style, new GSize(0,0), opacity, nani);
       
      return marker;
     
}


/*
show the glossary tab
*/
showGlossTab();

$("#zoomlevel").empty().append('zoom:&#160;' + map.getZoom());


social();

/*
 edit mode stuff
 run this when this javascript is called...
*/
if (mode !='b'){makeEditLinks();}

/*
do we splash a  welcome message? check above for cookie setter
*/

if ($.cookie('welc') != 'off'){$('#side_bar').empty().append(welcome);}

$('#infobox').css("display","inline");
/*
================================================================================= 
*/


}  /* end of main */ 
else {
document.write('Sorry, your browser cannot display this website');
}

