var offsX=100;
var offsY=360;
var dots= new Array('bluedot.gif','yellowdot.gif','greendot.gif','reddot.gif');
var cols=new Array('000000','0000CC','CC9900','339900','CC0033');
var languages=new Array('Deutsch','Français','English');

var currentsection=1;
var currentlocation=-1;
var currentlanguage=0;
//-------------------------------------------------
// return value for a key
//-------------------------------------------------
function KeyValue(loc,key)
{
   for(var j=0;j<loc.length;j+=2)
     if(loc[j]==key)return loc[j+1];  
  return "";
}
//--------- sort array by criteria ---------------
function SortList(crit,rev,blanks)
{
  var list=new Array();

  for(var i =0;i<locations.length;i++)
    {
       var loc=locations[i];
       var txt=KeyValue(loc,crit);
       if(txt.length || blanks)list[list.length]=txt+"="+i;  
    } 
  list.sort();
  for(var i =0; i <list.length;i++)
    {
      var p=list[i].lastIndexOf("=");
      list[i]=list[i].substring(p+1);
    }  
  if(rev)list.reverse();  
  return list;
}
//----------------------------------------------
// crit is an array of remove criteria
// each entry is an array of 2-3 values:
// 1. the key (Latin,Sponsoring,Category...)
// 2. the criteria. 
// This can be: 'set': remove if key is set
//              'unset': remove if key is not set
//              '=': remove if key value is equal to value 3
//              '!='
//              '<' 
//              '>'
//              'in': remove if value 3 is in key value (string)
//              'not in'
//---------------------------------------------                
function ExcludeOfList(list,crit,exclus)
{
   var newlist=new Array();
   for(var i=0;i<list.length;i++)
     {
        var loc=locations[list[i]];
		var pub=KeyValue(loc,'Publish');
		if(pub!=1) continue;		
        var res=crit.length;        
        for(var j=0;j<crit.length;j++)
           {
              var acrit=crit[j];
              var val=KeyValue(loc,acrit[0]);
              if(acrit[1]=='set' && val.length) res--;
              if(acrit[1]=='unset' && !val.length) res--;              
              if(acrit[1]=='=' && val==acrit[2]) res--;              
              if(acrit[1]=='!=' && val!=acrit[2]) res--; 
              if(acrit[1]=='<' && val<acrit[2]) res--;               
              if(acrit[1]=='>' && val>acrit[2]) res--;               
              if(acrit[1]=='in' && val.indexOf(acrit[2])>=0) res--;               
              if(acrit[1]=='not in' && val.indexOf(acrit[2])<0) res--;    
           }
        if(res==crit.length||res>0&&!exclus)newlist[newlist.length]=list[i];   
     }  
   return newlist;   
}
//-----------------------------------------------
// include in list: reverse of ExcludeFromList
//------------------------------------------------
function IncludeInList(list,crit,all)
{
   var newlist=new Array();
   for(var i=0;i<list.length;i++)
     {
        var loc=locations[list[i]];
		var pub=KeyValue(loc,'Publish');
		if(pub!=1) continue;
        var res=0;        
        for(var j=0;j<crit.length;j++)
           {
              var acrit=crit[j];
              var val=KeyValue(loc,acrit[0]);
              if(acrit[1]=='set' && val.length) res++;
              if(acrit[1]=='unset' && !val.length) res++;              
              if(acrit[1]=='=' && val==acrit[2]) res++;              
              if(acrit[1]=='!=' && val!=acrit[2]) res++; 
              if(acrit[1]=='<' && val<acrit[2]) res++;               
              if(acrit[1]=='>' && val>acrit[2]) res++;               
              if(acrit[1]=='in' && val.indexOf(acrit[2])>=0) res++;               
              if(acrit[1]=='not in' && val.indexOf(acrit[2])<0) res++;    
           }
        if(res==crit.length||res>0&&!all)newlist[newlist.length]=list[i];   
     }  
   return newlist;   
}
//------------------------------------------------
// display location: menu, dot and info
//------------------------------------------------
function DisplayLocation(s,l)
{
  currentsection=s;
  currentlocation=l;
  var str="";
  var lng = languages[currentlanguage];
  var locsection=locsections[currentlanguage]; // Deutsch....
  var p=360;
  var h=22;

  for(var i=0;i<locsection.length;i++)
     {
       var sec="<img src='layout/"+dots[i]+"' onclick='DisplayLocation("+(i+1)+",-1)'><font face='Verdana, Arial, Helvetica, sans-serif' size='2' color="+cols[i+1]+"><b> "+locsection[i]+"</b></font>";
       str+="<div style='position:absolute; left:0px; top:"+p+"px; height:24px; z-index:5;'>"+sec+"</div>";
       p+=24;
       if(i+1==currentsection)
         {
           p+=4;
           var rem=new Array();
           rem[0]=new Array('Section','=',currentsection);
           var list=SortList(lng);
           list=IncludeInList(list,rem,1);
           for(var j=0;j<list.length;j++)
             {
               var aloc=locations[list[j]];
               var colindex=list[j]==currentlocation?0:i+1;
               var ln="<font face='Verdana, Arial, Helvetica, sans-serif' size='2' color="+cols[colindex]+">"+KeyValue(aloc,lng)+"</font>";
               str+="<div style='position:absolute; left:24px; top:"+p+"px; height:20px; z-index:5'onclick='DisplayLocation("+currentsection+","+list[j]+")'>"+ln+"</div>";
               p+=20;               
             }
             
         }
         
    }
  var txt = document.getElementById("Sections");
  txt.innerHTML=str;    
  
  for(var i=0;i<locations.length;i++)
   {
      var location = locations[i];
      var s=parseInt(KeyValue(location,'Section'));
      var cnt="";
      if(currentlocation==i){cnt="<img src='layout/"+dots[s-1]+"' onclick='DisplayLocation("+s+","+i+")'>";}
      var loc = document.getElementById("Loc"+i);
      loc.innerHTML=cnt;     
     } 
  var s="";   
  var pic=document.getElementById("Picture");  
  var txt = document.getElementById("Title");  
  var info = document.getElementById("Info"); 
  var infotext; 
  if(currentlocation>=0)
    {
     s=KeyValue(locations[currentlocation],lng);
     txt.innerHTML="<span class='maptitle'>"+s+"</span>";        
     s=KeyValue(locations[currentlocation],'Picture');
     if(s.length)s="<img src= 'media/mappics/"+s+"'>";
     pic.innerHTML=s;
     var li=lng.substr(0,1);
     li="Text_"+li.toLowerCase();
     infotext=KeyValue(locations[currentlocation],li);
     info.innerHTML="<font face='Verdana, Arial, Helvetica, sans-serif' size='2'>"+infotext+"</font>";          
    }
  if(pic.innerHTML=="")
    pic.style.width=0;
  else pic.style.width=160;   
  if(infotext=="")
    txt.style.top=(offsY-40)+"px";
  else txt.style.top=(offsY-110)+"px";    
  
       
}
//----------------------------------------------------------------
// show dot when mouse moves over location
//----------------------------------------------------------------
function MouseMove(e)
{
  var px=e.pageX-offsX;
  var py=e.pageY-offsY;
  var nl=-1;

  var tit = document.getElementById("Title");
  if(px>=0&&px<640&&py>=0&&py<480)
    {
      for(i=0;i<locations.length;i++)
        {
          var content ="";        
          var loc=locations[i];
		  var pub=KeyValue(loc,'Publish');
		  if(pub!=1) continue;
          var s=parseInt(KeyValue(loc,'Section'));
          var pos=KeyValue(loc,'Place');
          var p=pos.indexOf(",");
          var lx=px-(parseInt(pos.substr(0,p)));
          var ly=py-(parseInt(pos.substr(p+1)));
          if( lx>=0 && lx<20 && ly>=0 && ly<20)
             {
               content="<img src='layout/"+dots[s-1]+"' onclick='DisplayLocation("+s+","+i+")'>";
             } 
          var locel = document.getElementById("Loc"+i);             
          locel.innerHTML=content;   
        }
    }
}


function Init(i)
{
  currentlanguage=i;
  document.write("<div id='Map' style='position:absolute; left:"+offsX+"px; top:"+offsY+"px; width:640px; z-index:4; border-width: 1px;  border-style: solid; border-color: white'><img src='layout/plan.jpg'></div>");
  document.write("<div id='Sections' style='position:absolute; left:815px; top:"+20+"px; width:350px; z-index:4'></div>");  
  document.write("<div id='Picture' style='position:absolute; left:"+(offsX+640-160)+"px; top:"+(offsY-130)+"px; width:160px; z-index:4;  border-width: 1px;  border-style: solid; border-color: white'></div>");   
  document.write("<div id='Title' style='position:absolute; left:105px; top:"+(offsY+10)+"px; width:540px; z-index:4'></div>");  
  document.write("<div id='Info' style='position:absolute; left:"+(offsX+10)+"px; top:"+(offsY-75)+"px; width:450px; z-index:4'></div>");   
  for(var i=0;i<locations.length;i++)
     {
       var loc=locations[i];
       var pos=KeyValue(loc,'Place');
       var p=pos.indexOf(",");
       var px=parseInt(pos.substr(0,p))+offsX;
       var py=parseInt(pos.substr(p+1))+offsY;
       var d=KeyValue(loc,'Section');

       document.write("<div id='Loc"+i+"' style='position:absolute; left:"+px+"px; top:"+py+"px; width:200px; height:20px; z-index:"+(i+5)+"'></div>");       
     }
  DisplayLocation(1,-1);
    
  document.captureEvents(Event.MOUSEMOVE);
  document.onmousemove=MouseMove;
  
}
