﻿// Subclassed VML/SVG Polyline
//
// Bill Chadwick May 2007
// Modified by John Shepherd Nov 2007 to add support for polyline options
//
// 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;//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, options) {
    
    this.tooltip = tooltip;
    this.dash = (dash != null) ? dash : "solid";
    this.color = color;
    this.weight = weight;
    this.opacity = opacity;
	this.options = options;
    
    //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);

    GPolyline.call(this,points,color,weight,opacity,options);//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,this.options);
}

BDCCPolyline.prototype.redraw = function(force) {

   GPolyline.prototype.redraw.call(this,force); //super class
   try {
   var dom;
   if(this.usesVml){
        try{
            var shps = document.getElementsByTagName("shape");
            dom = shps[shps.length-1];//assume ours is the most recently added by the superclass redraw
            if(this.tooltip != null){
                dom.style.cursor = "help";//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)
			   alert("The designer of this Google Maps web page has attempted to use VML graphics without including the necessary header material.");*/
        }
   }
   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 = "help";//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
   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();});
   }
   catch(e) {}
}

//event handlers
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;
}

