/*
 * Class MMPolyline
 */
function MMPolyline(points) {	
	this.map = null;
	this.polyline = new GPolyline(points);
	
	this.dash = false;
	this.color = "#0000ff";
	this.opacity = 0.45;
	this.weight = 5;

	
	//make a unique DOM id
	var current = new Date();
    this.domid = "MMPolylineId" + current.getTime();
}
MMPolyline.prototype = new GOverlay();

/**
 * sets a new polyline path
 */
MMPolyline.prototype.setPoints = function(points){
	this.polyline.remove();
	this.polyline = new GPolyline(points);
	this.polyline.initialize(this.map);
	this.redraw(true);
}

/**
 * sets a new polyline path by encoded JSON string
 */
MMPolyline.prototype.setEncodedPolyline = function(encodedPolyline){
	this.polyline.remove();
	
	this.polyline = GPolyline.fromEncoded(encodedPolyline);
	this.color = encodedPolyline.color;
	this.opacity = encodedPolyline.opacity;
	this.weight = encodedPolyline.weight;
	
	this.polyline.initialize(this.map);
	this.redraw(true);
}

/**
 * Called by the map after the overlay is added to the map using GMap2.addOverlay(). The overlay object 
 * can draw itself into the different panes of the map that can be obtained using GMap2.getPane().
 */
MMPolyline.prototype.initialize = function(map){
	this.map = map;
	this.polyline.initialize(map);
}


/**
 * Called by the map after the overlay is removed from the map using GMap2.removeOverlay() or GMap2.clearOverlays(). 
 * The overlay must remove itself from the map panes here.
 */
MMPolyline.prototype.remove = function(){
	this.map = null;
	this.polyline.remove();
}


/**
 * Returns an uninitialized copy of itself that can be added to the map.
 */
MMPolyline.prototype.copy = function(map){
	return new MMPolyline();
}

/**
 * Called by the map when the map display has changed. The argument force will be true if the zoom level or 
 * the pixel offset of the map view has changed, so that the pixel coordinates need to be recomputed.
 */
MMPolyline.prototype.redraw = function(force){   	
	
	this.polyline.redraw(force);	
	
	var dom;	
	try
	{
		if(navigator.userAgent.indexOf("MSIE") != -1)
		{
			var shps = document.getElementsByTagName("shape");
			dom = shps[shps.length-1];//assume ours is the most recently added by the superclass redraw
			if(dom.getAttribute("id")) throw("element not found!");
			dom.id = this.domid;//assign unique DOM id so we can modify attributes later
		}
		else
		{
			var shps = document.getElementsByTagName("path");
			dom = shps[shps.length-1];//assume ours is the most recently added by the superclass redraw
			if(dom.getAttribute("id")) throw("element not found!");
			dom.setAttribute("id",this.domid);//assign unique DOM id so we can modify attributes later
		}
	
		//to show mouseover
		dom.style.cursor = "pointer";
		
		//set up the appearance of our polyline
		this.SetColor(this.color);	
		this.SetOpacity(this.opacity);
		this.SetWeight(this.weight);
		this.SetDash(this.dash);
	   
		//set up event handlers
		GEvent.clearInstanceListeners(dom);
		GEvent.bindDom(dom, "mouseover", this, this.OnMouseOver);
		GEvent.bindDom(dom, "mousemove", this, this.OnMouseMove);
		GEvent.bindDom(dom, "mouseout", this, this.OnMouseOut);
		GEvent.bindDom(dom, "click", this, this.OnClick);
		GEvent.bindDom(dom, "mousedown", this, this.OnMouseDown);
	}
	catch(e){}
}


MMPolyline.prototype.SetColor = function(color)
{
	this.color = color;
    var dom = document.getElementById(this.domid); 

    if(dom)
    {
	    if(navigator.userAgent.indexOf("MSIE") != -1)dom.stroke.color = this.color;
	    else dom.setAttribute("stroke",this.color);
    }
}

MMPolyline.prototype.SetWeight = function(weight)
{
	this.weight = weight;
    var dom = document.getElementById(this.domid); 

    if(dom)
    {
	    if(navigator.userAgent.indexOf("MSIE") != -1)dom.stroke.weight = this.weight.toString()+"px"; 
	    else dom.setAttribute("stroke-width",this.weight.toString()+"px");
    }
}

MMPolyline.prototype.SetOpacity = function(opacity)
{
    this.opacity = opacity;
	var dom = document.getElementById(this.domid); 

	if(dom)
    {
		if(navigator.userAgent.indexOf("MSIE") != -1)dom.stroke.opacity = this.opacity;
	    else dom.setAttribute("stroke-opacity",this.opacity);
    }
}

MMPolyline.prototype.SetDash = function(dash)
{
    this.dash = dash;
    var dom = document.getElementById(this.domid); 
    
    if(dom)
    {
	    if(navigator.userAgent.indexOf("MSIE") != -1)
	    {
	        if(this.dash)dom.stroke.dashstyle = "dash";
	        else dom.stroke.dashstyle = "";
	    }
	    else
	    {
	        if(this.dash) dom.setAttribute("stroke-dasharray","10,10");
	        else dom.setAttribute("stroke-dasharray","");
	    }
    }
}

/**
 * Returns the vertex with the given index in the polyline
 */
MMPolyline.prototype.GetPoint = function(index)
{
	return this.polyline.getVertex(index);
}



MMPolyline.prototype.OnClick = function(e){
	var gMapDiv = this.map.getContainer();
	var pxPoint = new GPoint(e.clientX, e.clientY);
	pxPoint.x -= gMapDiv.offsetLeft;
    pxPoint.y -= gMapDiv.offsetTop;
    
	var latlng = this.map.fromContainerPixelToLatLng(pxPoint);
	GEvent.trigger(this, "click", latlng);
}

MMPolyline.prototype.OnMouseOut = function(){
	GEvent.trigger(this, "mouseout");
}

MMPolyline.prototype.OnMouseDown = function(e){
	if(e.button == 2) // right click
	{	
		var pxPoint = new GPoint(e.clientX, e.clientY);
		GEvent.trigger(this, "rightclick", pxPoint);
	}
}


MMPolyline.prototype.OnMouseOver = function(e){

	var pxPoint = new GPoint(e.clientX, e.clientY);
	GEvent.trigger(this, "mouseover", pxPoint);
}

MMPolyline.prototype.OnMouseMove = function(e){

	var pxPoint = new GPoint(e.clientX, e.clientY);
	GEvent.trigger(this, "mousemove", pxPoint);
}

/**
 * Returns the length (in meters) of the polyline along the surface of a spherical Earth.
 */
MMPolyline.prototype.getLength = function(){
	return this.polyline.getLength();
}

/**
 * Returns the bounds for this Polyline
 */
MMPolyline.prototype.getBounds = function()
{
	return this.polyline.getBounds();
}

/**
 * Returns the number of vertices in the polyline.
 */
MMPolyline.prototype.getVertexCount = function()
{
	return this.polyline.getVertexCount();
}
