/*--------------------------------------------------------------------------- 
*    javascript library to overlay SVG file with google maps v2
*--------------------------------------------------------------------------- 
* Author: Thibault HILAIRE 
* Filename: overlaySVG.js 
* Date : 4 july 2009
* $Id$
*--------------------------------------------------------------------------- 
* Purpose:
*  This library allows to overlay a SVG file on a google map (v2)
*  You can use it exactly as you use the GGroundOverlay, except that
*  your SVG file MAY BE ALREADY PROJECTED according to the Mercator projection
*
* Ex:
*   var boundaries = new GLatLngBounds( new GLatLng(48.2, 1.9), new GLatLng(48.2, 2.0));
*   map.addOverlay( new overlaySVG( "myfile.svg", boundaries ));
*
* Warning:
*  Your SVG file MUST have a group with "mainGroup" as 'id'. Only this group will be considerd
*  if it is not the case, you can put your SVG items between <g id="mainGroup"> and <g/>
*
*--------------------------------------------------------------------------- 
* 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 3 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, see <http://www.gnu.org/licenses/>.
*---------------------------------------------------------------------------*/


// create the object
function overlaySVG( svgUrl, bounds)
{
	this.svgUrl_ = svgUrl;
	this.bounds_ = bounds;
}


// prototype
overlaySVG.prototype = new GOverlay();


// initialize
overlaySVG.prototype.initialize = function( map)
{
	//create new div node 
	var svgDiv = document.createElement("div");
	svgDiv.setAttribute( "id", "svgDivison");
	//svgDiv.setAttribute( "style", "position:absolute");
	svgDiv.style.position = "absolute";
	svgDiv.style.top = 0;
	svgDiv.style.left = 0;
	svgDiv.style.height = 0;
	svgDiv.style.width = 0;
	map.getPane(G_MAP_MAP_PANE).appendChild(svgDiv);
	
	// create new svg element and set attributes
	var svgRoot = document.createElementNS( "http://www.w3.org/2000/svg", "svg");
	svgRoot.setAttribute( "id", "svgRoot");
	svgRoot.setAttribute( "width", "100%");
	svgRoot.setAttribute( "height","100%");
	svgDiv.appendChild( svgRoot);
	
	// load the SVG file
	GDownloadUrl( this.svgUrl_, function( data, responseCode)
	{
		var xml = GXml.parse(data);
		// specify the svg attributes
		svgRoot.setAttribute("viewBox", xml.documentElement.getAttribute("viewBox"));
		// append the defs
		var def = xml.documentElement.getElementsByTagName("defs");
		//for( var int=0; i<def.length; i++)
		//	svgRoot.appendChild(def[0].cloneNode(true));
		//append the main group
		var nodes = xml.documentElement.getElementsByTagName("g");
		for (var i = 0; i < nodes.length; i++)
			if (nodes[i].id=="mainGroup")
				svgRoot.appendChild(nodes[i].cloneNode(true));
	});
	
	// keep interesting datas
	this.svgDiv_ = svgDiv;
	this.map_ = map;

	// set position and zoom
	this.redraw(true);
}



// remove from the map pane
overlaySVG.prototype.remove = function()
{
	this.div_.parentNode.removeChild( this.div_);
}


// Copy our data to a new overlaySVG...
overlaySVG.prototype.copy = function()
{
	return new overlaySVG( this.url_, this.bounds_, this.center_);
}


// Redraw based on the current projection and zoom level...
overlaySVG.prototype.redraw = function( force)
{
	// We only need to redraw if the coordinate system has changed
	if (!force) return;
	// get the position in pixels of the bound
	posNE = map.fromLatLngToDivPixel(this.bounds_.getNorthEast());		
	posSW = map.fromLatLngToDivPixel(this.bounds_.getSouthWest());
	// compute the absolute position (in pixels) of the div ...
	this.svgDiv_.style.left = Math.min(posNE.x,posSW.x) + "px";
	this.svgDiv_.style.top = Math.min(posSW.y,posNE.y) + "px";
	// ... and its size
	this.svgDiv_.style.width = Math.abs(posSW.x - posNE.x) + "px";
	this.svgDiv_.style.height = Math.abs(posSW.y - posNE.y) + "px";
}