/*
**
** 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 parseLines(article_id,code,show,minzoom,maxzoom,the_title){
   
   var color = undefined;
   var polygon = undefined;
   var polyline = undefined;
   polygons[article_id]=new Array();
   var attrs = {};
   //alert(code);
   var lines = code.split("\n");
   //alert(lines);
   //alert('parsing '+lines.length+' lines');
   for (var l=0; l < lines.length; l++) {

		// if we match a polygon declaration..
	if (lines[l].match(/^(#[0-9a-fA-F]{6}) polygon/)) {

       		// if polygon is set then we were making a polygon which is now finished
		// in that case dump polygon (the path we were making) and reset polygon
	   if(polygon != undefined) {
	   	addPoly(polygon, color, 'polygon',article_id,show);
	   }
 	 	// if polyline is set then we were making a polyline which is now finished
		// in that case dump polyline (the path we were making) and set polyline to null
           if(polyline != undefined) {
           	addPoly(polyline, color, 'polyline');
           	polyline = undefined;
           }
           polygon = new Array();
           color = lines[l].substring(0, 7);
	
		// if we match a polyline declaration..
   	} else if (lines[l].match(/^(#[0-9a-fA-F]{6})/)) {

       		// if polyline is set then we were making a polyline which is now finished
        	// in that case dump polyline (the path we were making) and reset polyline
           if(polyline != undefined) {
           	addPoly(polyline, color, 'polyline');
           }
        	// if polygon is set then we were making a polygon which is now finished
        	// in that case dump polygon (the path we were making) and set polygon to null
           if(polygon != undefined) {
           	addPoly(polygon, color, 'polygon',article_id,show);
           	polygon = undefined;
           }
           polyline = new Array();
           color = lines[l].substring(0, 7);
		
	} else {
           lines[l].match(/^(?:\((.*?)\) *)?([^, ]+), *([^ ,]+)(?:, *(.+))?/);
           var icon = RegExp.$1;
           var lat = parseFloat(RegExp.$2);
           var lon = parseFloat(RegExp.$3);
           var caption = RegExp.$4;

           if(lat && lon){

                	// is this is just a point ?

        	if (polygon == undefined && polyline == undefined && lat && lon) {
                   
		   var marker = new GMarker(new GLatLng(lat, lon));
  		   var marker_caption='';
                   if(caption)marker_caption+=caption;

                   var link='link: <a href="./index.php?title='+the_title+'">'+the_title+'</a>';
    		   marker_caption+="<br/>"+link;

		   marker.caption=marker_caption;

		   if(maxzoom>17)maxzoom=17;
                   markerManager.addMarker(marker,minzoom,maxzoom);

               		// else, we are making either a polygon or a polyline
           	} else {
   		   //var tmp = new Array();
                   //tmp['lat']=lat;
                   //tmp['lon']=lon;
                   //tmp['icon']=icon;
                   //tmp['caption']=caption;
		   var point=new GLatLng(lat,lon);
                   if(polygon != undefined){
                      polygon.push(point);
                      //alert('added point to polygon');
                   }
                   if(polyline != undefined){
                      polyline.push(point);
                      //alert('added point to polyline');
                   }

                }
	   }// end of "if(lat && lon)"
	} // end of "it must be a marker"
   }  // end of loop

   if(polyline != undefined){
   	addPoly(polyline,color,'polyline');
   }
   //else {alert('polyline is undefined');}
   if(polygon != undefined){
   	addPoly(polygon,color,'polygon',article_id,show);
   }
   //else {alert('polyline is undefined');}
   return;
}

function addPoly(points,color,type,article_id,show){
   //alert('adding poly of type '+type+' with color: '+color);
   var str="";
   for(keyVar in points){
	str+=points[keyVar]+"\n";
   }
   for(i=0;i<points.length;i++){
	str+=points[i]+"\n";
   }	 
   //alert(str);
   var firstelm=points[0];
   points.push(firstelm);
   var opacity1=0.0;
   var opacity2=0.4;
   var opacity_stroke=0.8;
   var stroke=2;
   //var tempcolor='#FFFFFF';color='tempcolor';
   var highlightcolor='#FFFF00';
   var polygon1=new GPolygon(points,color,stroke,opacity_stroke,color,opacity1);
   var polygon2=new GPolygon(points,highlightcolor,stroke,opacity_stroke,color,opacity2);
   var polygonpair=new Array();
   polygonpair['normal']=polygon1;
   polygonpair['highlighted']=polygon2;
   //var str_article_id=''+article_id+'';
   polygons[article_id].push(polygonpair);
   if(show){
 	visible[article_id]=true;
 	map.addOverlay(polygon1);
   }

}



