// Subclassed VML/SVG Polyline
//
// Bill Chadwick May 2007
//
// Free for any use
//
// Adds 
//  click, mouseover and mouseout events
//  tooltip
//  dot or dash styling
//  dynamic setting of colour, opacity, weight and dash style  

var BDCCPolylineId = null;//counter for unique DOM Ids

// Constructor params exactly as GPolyline then a tooltip and dash which must be one of "dot" or "dash" or "solid"

function BDCCPolyline(points, color, weight, opacity, tooltip, dash) {	
    
    this.tooltip = tooltip;
    this.dash = (dash != null) ? dash : "solid";
    this.color = color;
    this.weight = weight;
    this.opacity = opacity;
    
    //make a unique DOM id for this polyline
    if(BDCCPolylineId == null)
        BDCCPolylineId = 0;
    else
        BDCCPolylineId += 1;
    this.domid = "BDCCPolylineId" + BDCCPolylineId.toString();

    this.usesVml = (navigator.userAgent.indexOf("MSIE") != -1);
    this.safari = (navigator.userAgent.indexOf("Safari") != -1);

    GPolyline.call(this,points,color,weight,opacity);//call super class constructor 
}
BDCCPolyline.prototype = new GPolyline(new Array(new GLatLng(0,0)));//subclass from GPolyline

// According to the GMap docs, GPolyline implements the GOverlay interface
// That is it implements the functions initialize, remove, copy and redraw
// Here we add to GPolyline's own implementation of these functions
//

BDCCPolyline.prototype.initialize = function(map) {
    GPolyline.prototype.initialize.call(this,map); //super class
    //Initialise cant be used to cache the SVG path node or its parent svg node as both are recreated in redraw
    //For VML the shape node is recreated in redraw and all shapes have a common parent        
}

BDCCPolyline.prototype.remove = function() {
    GPolyline.prototype.remove.call(this); //super class
}

BDCCPolyline.prototype.copy = function(map) {
    return new BDCCPolyline(this.points,this.color,this.weight,this.opacity,this.tooltip,this.dash);
}

//a method for returning all the vertexes of a polygon so it can be recreated with a different color
BDCCPolyline.prototype.Redraw = function() {
	var vertices = new Array();
	  for (var i=0; i < this.getVertexCount(); i++) {
	  	vertices[i] = this.getVertex(i);
	  	}
	 return vertices;
}

//note original redraw function
BDCCPolyline.prototype.redraw = function(force) {
   
   GPolyline.prototype.redraw.call(this,force); //super class
   if(!this.safari){
   var dom;
   //alert(this.usesVml);
   if(this.usesVml){
        try{        
            var shps = document.getElementsByTagName("shape");           	
            dom = (typeof(shps[shps.length-1]) == "undefined")?null:shps[shps.length-1];//assume ours is the most recently added by the superclass redraw
            if(dom != null){
	            if(this.tooltip != null){
	                dom.style.cursor = "pointer";//to show mouseover 
	                dom.title = this.tooltip;
	            }
	            dom.id = this.domid;//assign unique DOM id so we can modify attributes later   
	        }
        }
        catch (ex)
        {
		    if(BDCCPolylineId == 0){
			    //Problem: the lines are still created, but this error is thrown!!!
			    //alert("The designer of this Google Maps web page has attempted to use VML graphics for lines without including the necessary header material.");
			}
        }
   }
  // The following lines are commented by Ramesh on 05-05-2009 due to rectify the problem while drawing polygon
   
   /*else{
        var shps = document.getElementsByTagName("path"); 
		dom = shps[shps.length-1];//assume ours is the most recently added by the superclass redraw
        if(this.tooltip != null){
            dom.style.cursor = "pointer";//to show mouseover 
            dom.setAttribute("title",this.tooltip);
        }
        dom.setAttribute("id",this.domid);//assign unique DOM id so we can modify attributes later
        dom.setAttribute("pointer-events","stroke");//only click on the line, not its bounding rectangle
   }*/

   //set up the appearance of our polyline
	if(dom != null){
		this.setColor(this.color);
		this.setDash(this.dash);
		this.setOpacity(this.opacity);
		this.setWeight(this.weight);
		
		//set up event handlers
		//var cclick = GEvent.callback(this,this.onClick);
		var cover = GEvent.callback(this,this.onOver);
		var cout = GEvent.callback(this,this.onOut);
   
		GEvent.clearInstanceListeners(dom);//safety 
		//GEvent.addDomListener(dom,"click",function(event){cclick();});
		GEvent.addDomListener(dom,"mouseover",function(){cover();});
		GEvent.addDomListener(dom,"mouseout",function(){cout();});
	}
  }
}

//event handlers -- now handled by api v.89
/*BDCCPolyline.prototype.onClick = function(){
    GEvent.trigger(this,"click");
}*/
BDCCPolyline.prototype.onOver = function(){
    GEvent.trigger(this,"mouseover");
}
BDCCPolyline.prototype.onOut = function(){
    GEvent.trigger(this,"mouseout");
}

//once the shape has been drawn, we can modify it with these setX functions;

BDCCPolyline.prototype.setColor = function(color) {
    this.color = color;
    var dom = document.getElementById(this.domid); 
    if(this.usesVml){
        dom.stroke.color = this.color;
    }
    else{
        dom.setAttribute("stroke",this.color);
    }
}
BDCCPolyline.prototype.getColor = function() {
    return this.color;
}
BDCCPolyline.prototype.setDash = function(dash) {
    this.dash = dash;
    var dom = document.getElementById(this.domid); 
    if(this.usesVml){
        if(this.dash == "dash")
            dom.stroke.dashstyle = "dash";       
        else if (this.dash == "dot")
            dom.stroke.dashstyle = "dot";    
        else 
            dom.stroke.dashstyle = "";    
    }
    else{
        if(this.dash == "dash")
            dom.setAttribute("stroke-dasharray","10,10");
        else if (this.dash == "dot")
            dom.setAttribute("stroke-dasharray","3,17");
        else
            dom.setAttribute("stroke-dasharray","");
    }
}
BDCCPolyline.prototype.getDash = function() {
    return this.dash;
}
BDCCPolyline.prototype.setWeight = function(weight) {
    this.weight = weight;
    var dom = document.getElementById(this.domid); 
    if(this.usesVml){
        dom.stroke.weight = this.weight.toString()+"px";   
    }
    else{
        dom.setAttribute("stroke-width",this.weight.toString()+"px");
    }
}
BDCCPolyline.prototype.getWeight = function() {
    return this.weight;
}
BDCCPolyline.prototype.setOpacity = function(opacity) {
    this.opacity = opacity;
    var dom = document.getElementById(this.domid); 
    if(this.usesVml){
        dom.stroke.opacity = this.opacity;
    }
    else{
        dom.setAttribute("stroke-opacity",this.opacity);
    }
}
BDCCPolyline.prototype.getOpacity = function() {
    return this.opacity;
}


