/*
**
** Manage areas on a google
**
** Copyright Sonologic, http://www.sonologic.nl/
** Arnold Obdeijn, aobdeijn@sonologic.nl
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License along
** with this program; if not, write to the Free Software Foundation, Inc.,
** 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
** http://www.gnu.org/copyleft/gpl.html
**
*/

function hover_listener(latlng){
    
   var o=0;
   var lat,lng,area_borders;
   var str="";
   try{
      for(o=0;o<ordered.length;o++){
      
      	i=ordered[o];
      	if(visible[i]){
	        //str+="considering "+links[i]+" for highlight<br/>\n";	
         	lat=latlng.lat();
  	 	lng=latlng.lng();
 	 	area_borders=borders[i];
         	if(lat>area_borders['latmin'] &&
            	lat<area_borders['latmax'] &&
            	lng>area_borders['lonmin'] &&
            	lng<area_borders['lonmax']){
			if(!hovering[i]){
	        		//log(links[i]+" should be highlighted<br/>\n");	
                   		//log("hovering over polygon(s) "+i);
		   		highlight(i);
			}
			return;
         	} else {
			if(hovering[i]){
		   		try{lowlight(i);}catch(err){}
			}
         	}
        }
      } // end of for loop
   } catch(err){
	//log(str);
	//alert(err);
   }
   //alert(str);


}
function lowlightAllBut(index){
   for(pageId in polygons){
	if(pageId==index)continue;
        //if(shouldBeVisible(zoomlevel,zoomlevels[pageId]))lowlight(pageId);
	try{
           if(visible[pageId])lowlight(pageId);
	}catch(err){
	   //alert(err);
	}
   }
}	
function highlight(index){
   //log('highlighting '+index);
   try{
   //if(shouldBeVisible(zoomlevel,zoomlevels[index])){
      	for(j=0;j<polygons[index].length;j++){
		//log('highlighting '+index+':'+j);
       		map.removeOverlay(polygons[index][j]['normal']);
      		map.addOverlay(polygons[index][j]['highlighted']);
      	}
        hovering[index]=true;
   //}
   } catch(err){
	//alert(err);
   }
   if(index>0){
      try{
         document.getElementById('link'+index).style.color='red';
      } catch(err) {
	//alert('failed to make link '+index+' blue');
      }	
   }
   if(highlighted != index) lowlight(highlighted);
   highlighted=index;
}
function lowlight(index){
   try{
   //if(shouldBeVisible(zoomlevel,zoomlevels[index])){
   	for(j=0;j<polygons[index].length;j++){
     		map.removeOverlay(polygons[index][j]['highlighted']);
      		map.addOverlay(polygons[index][j]['normal']);
   	}
   	hovering[index]=false;
   //}
   } catch(err){
	//alert(err);
   }
   if(index>0){
      try{
	document.getElementById('link'+index).style.color='blue';
      } catch(err) {
	//alert('failed to make link '+index+' blue');
      }	
   }
}
function orderPolygons(){
   
   ordered = new Array();
   copyofarea= new Array();
   var i=0;
   var count=0;
   var verylarge=999999999999999999;
   var str="";
   //showPolygons();
   
   for(keyVar in area){
	copyofarea[keyVar]=area[keyVar];
	count++;
   }
   for(i=0;i<count;i++){
       		// get the (next) smallest area
	minarea=verylarge;
       	for(pageId in copyofarea){
	   if(copyofarea[pageId]<minarea){
		minarea_index=pageId;
		minarea=copyofarea[pageId];
	   }
       	}
		// store the index of the smallest found area in ordered 
       	ordered.push(minarea_index);
		// reset the value of the smallest found area in copyofarea
       	copyofarea[minarea_index]=verylarge;
   }
   //showOrdered();
}
function showPolygons(){
   var str="showing content of array 'polygons':\n";
   str+="length:"+polygons.length+"\n";
   var i;
   for(keyVar in polygons){
	for(i=0;i < polygons[keyVar].length;i++){
		str+=links[keyVar]+":"+i+"[normal] contains "+ typeof(polygons[keyVar][i]['normal'])+"\n";
		str+=links[keyVar]+":"+i+"[highlighted] contains "+ typeof(polygons[keyVar][i]['highlighted'])+"\n";
 	}
   }
   alert(str);
}
function showOrdered(){
   var str="showing content of array 'ordered'\n";
   for(keyVar in ordered){
	str+=keyVar+': '+ordered[keyVar];
	try{
	   str+=" "+links[ordered[keyVar]];
	} catch (err){}
	try{
	   str+=" "+area[ordered[keyVar]];
	} catch (err){}
	str+="\n";
   }
   alert(str);
}
function showArray(array){
   var str="";
   for(keyVar in array){
	str+=keyVar+': '+array[keyVar]+"\n";
   }
   alert(str);
}
function updateMap(oldLevel,newLevel){
   for(pageId in zoomlevels){
     try{
        if(!shouldBeVisible(oldLevel,zoomlevels[pageId]) && shouldBeVisible(newLevel,zoomlevels[pageId])){
	   //str='should add polygons '+links[pageId];
 	   //log(str);
           for(j=0;j<polygons[pageId].length;j++)
	      visible[pageId]=true;
	      map.addOverlay(polygons[pageId][j]['normal']);
        }
        if(shouldBeVisible(oldLevel,zoomlevels[pageId]) && !shouldBeVisible(newLevel,zoomlevels[pageId])){
	   //str='should remove polygons '+links[pageId];
 	   //log(str);
           for(j=0;j<polygons[pageId].length;j++)
              map.removeOverlay(polygons[pageId][j]['normal']);
	      visible[pageId]=false;
              //map.removeOverlay(polygons[pageId][j]['highlighted']);
   	}
      }catch(err){
	//var str_err='polygons['+pageId+']['+j+']';
	//alert(err+"\n"+str_err);
      }	
   }
}
function log(message){
   document.getElementById('info').innerHTML=message+"<br/>"+document.getElementById('info').innerHTML;
   //alert(message);
}
function shouldBeVisible(zoomlevel,levels){
   try{maxi=levels['max']}catch(err){return false;} 
   if(zoomlevel>levels['max'])return false;
   if(zoomlevel<levels['min'])return false;
   return true;
}

