// special codes for mapper calls
// 1111 = loadcomplete
// 1112 = authorization failed
// 1001 = snaplocator
// 
var its;

var debug = 0;

function itemSelectedFM(x) {
 
 if (debug) alert("itemSelected: "+x);
 eval("its = new Array("+x+");");
 if (debug) alert(its);
 itemSelected(its);
 }


var commPending = new Array();

// elements of commPending are comma separated lists idx0,comm0,arg0


   
var mapperReady = 0;

function toMapper(idx,c,a)
{
  if (mapperReady)
     {
     writecomm(idx + "," + c + "," + a);
 	 writedebug(c + " " + a);
	 mapperReady = 0;
	 }

  else
     {
	 var ccom = idx + "," + c + "," + a;
	 var ccoma = new Array(ccom);
     writedebug("push "+ c + " " + a);
	 commPending = ccoma.concat(commPending);
	 }
}


function toMapper2(idx,c,a,b)
{
  if (mapperReady)
     {
     writecomm(idx + "," + c + "," + a + "," + b);
	 writedebug(c + " " + a + " " + b);
	 mapperReady = 0;
	 }

  else
     {
	 var ccom = idx + "," + c + "," + a + "," + b;
	 var ccoma = new Array(ccom);
	 writedebug("push "+ c + " " + a + " " + b);
	 commPending = ccoma.concat(commPending);
	 }
}

function loadRDF(idx,m)
{
  if (debug) alert("loadrdf "+m);
  toMapper(idx,"loadrdf",m);
}


function showMap(idx,m)
{
  if (debug) alert("showmap "+m);
  toMapper(idx,"showmap",m);
}


var lastMapLoaded = "";

function loadMap(idx,m)
{
  if (debug) alert("loadmap "+m);
  if (m == lastMapLoaded) return;
  lastMapLoaded = m;
  toMapper(idx,"loadmap",m);
}


function loadItems(idx,m)
{
  if (debug) alert("loaditems "+m);
  toMapper(idx,"loaditems",m);
}


function showChannel(idx,m)
{
  if (debug) alert("showchannel "+m);
  toMapper(idx,"showchannel",m);
}



function showItems(idx)
{
  if (debug) alert("showitems "+idx);
  toMapper(idx,"showitems","dummy");
}



function setItemtag(idx,tg)
{
  if (debug) alert("setItemtag "+tg);
  toMapper(idx,"setitemtag",tg);
}





function authorize(idx,x,y)
{
//  if (debug) 
//  alert("authorize ");
  toMapper(idx,"authorize",x + "x" + y);
}




// layouts are only executed every layoutInterval miliseconds 


var layoutPending = false;
var layoutInterval = 1000;
function maplayout(idx)
{
  if (layoutPending) return;
  layoutPending = true;
  setTimeout("maplayout1()",layoutInterval);
}

function maplayout1()
{
  layoutPending = false;
  if (debug) alert("maplayout "+idx);
  toMapper(0,"layout","dummy");
}


function showLastMap(idx)
{
  if (debug) alert("showlastmap "+idx);
//  alert("showlastmap");
  toMapper(idx,"showlastmap","dummy");
}

// turn the abilitiy of the user to locate the hilighted item on and off
function enableLocate(idx)
{
//  alert('enableLocate');
  toMapper(idx,"enablelocate","dummy");
}

function disableLocate(idx)
{
//  alert('disableLocate');
  toMapper(idx,"disablelocate","dummy");
}

function enterLocatorMode()
{
//  alert("enterlocatormode");
  toMapper(0,"enterlocatormode",0);
}


function enterSelectMode()
{
  toMapper(0,"enterselectmode",0);
}
// 1001 is the code for snapLocator; the return value is the
// location text
function snapLocator(x)
{
//  alert('snaplocator||'+x+'||');
  toMapper(1001,"snaplocator","dummy");
}

//utm10n,419792,5112170

function undoLocate(idx)
{
//  alert('undoLocate');
  toMapper(idx,"undolocate","dummy");
}

function highlight(idx,its)
{
  var ln = its.length;
//  enableLocate(idx);
  if (ln != 1) alert("Only one highlight at a time is supported for now");
  else toMapper(idx,"highlight",its[0]);
}


function highlightOne(idx,itm)
{
//  enableLocate(idx);
  toMapper(idx,"highlight",itm);
}



// since pop only came along in IE 5.5
function mypop(x)
{
  var ln = x.length;
  if (ln > 0)
     {
	 var rs = new(Array);
	 for (i=0;i<ln-1;i++)
		 rs[i] = x[i];
	 return rs;
	 }
   return x;
}

// remember: mapperIsReady is called directly from the mapper just
// once, at startup. Thereafter it is called from fromMapper 

function mapperIsReady()
{
  var ln = commPending.length;
  if (ln > 0) 
    {
//	var ccom = commPending.pop();
    ccom = commPending[ln-1];
	commPending = mypop(commPending);
	writecomm(ccom);
	writedebug("_p_ "+ccom);
	}
 else
   mapperReady = 1;

}
  


var frommappercount = 0;
var authorizationHasFailed = 0;

 function fromMapper(idx,v)
 {
//alert("from mapper "+frommappercount);
frommappercount++;
if (authorizationHasFailed) return;
 writedebug2("fromMapper " + frommappercount+ " " + v);
 mapperIsReady();
 mapperReturn(idx,v);
if (idx == 1111) parent.loadComplete();
else
if (idx == 1112) 
   {
   authorizationHasFailed = 1;
   parent.authorizationFailed();
   }
if (idx == 1001) 
   {
   if (v == "wrongmode") alert("Please select a location on the map first");
   if (v == "nobuttons") alert("There are no items on the map yet");
   else locationSelected0(v);
   }
}


function locationSelected0(x) {
var sp;
//alert("locationSelected||"+x+"||");
if (debug) alert(x);
sp = x.split(",");

 //alert(x);
 //alert(parent.app);
// alert(parent.app.document);
 locationSelected(sp[1],sp[2],sp[0]);

//    reporter.document.write(x);
  
//   reporter.document.close();
 } 

function reloadData()
{
//alert(parent.dataUrl);
loadItems(0,parent.dataUrl);
//showItems(0);
}

function editDone(uri,rld)
{ 
// alert("editDone");
   disableLocate(1);
   if (rld) reloadData();
   toMapper(5,"highlight",uri);
}

