// Subclassed VML/SVG Polygon
// 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 BDCCPolygonId;//counter for unique DOM Ids

// Constructor params exactly as GPolygon then a tooltip and dash which must be one of "dot" or "dash" or "solid"

function BDCCPolygon(points, color, weight, opacity, fillColor, fillOpacity,tooltip, dash) {	
    
    this.tooltip = tooltip;
    this.dash = (dash != null) ? dash : "solid";
    this.color = color;
    this.weight = weight;
    this.opacity = opacity;
    this.fillColor = fillColor;
    this.fillOpacity = fillOpacity;
    
    //make a unique DOM id for this Polygon
    if(BDCCPolygonId == null)
        BDCCPolygonId = 0;
    else
        BDCCPolygonId += 1;
    this.domid = "BDCCPolygonId" + BDCCPolygonId.toString();
    
    GPolygon.call(this,points,color,weight,opacity,fillColor, fillOpacity);//call super class constructor 
}
BDCCPolygon.prototype = new GPolygon(new Array(new GLatLng(0,0)));//subclass from GPolygon

// According to the GMap docs, GPolygon implements the GOverlay interface
// That is it implements the functions initialize, remove, copy and redraw
// Here we add to GPolygon's own implementation of these functions
//

BDCCPolygon.prototype.initialize = function(map) {
    GPolygon.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        
}

BDCCPolygon.prototype.remove = function() {
    GPolygon.prototype.remove.call(this); //super class
}

BDCCPolygon.prototype.copy = function(map) {
    return new BDCCPolygon(this.points,this.color,this.weight,this.opacity,this.tooltip,this.dash);
}

BDCCPolygon.prototype.redraw = function(force) {
   
   GPolygon.prototype.redraw.call(this,force); //super class
   var dom;
   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(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
   }
   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
   }

   //set up the appearance of our Polygon
   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
BDCCPolygon.prototype.onClick = function(){
    GEvent.trigger(this,"click");
}
BDCCPolygon.prototype.onOver = function(){
    GEvent.trigger(this,"mouseover");
}
BDCCPolygon.prototype.onOut = function(){
    GEvent.trigger(this,"mouseout");
}

//once the shape has been drawn, we can modify it with these setX functions;

BDCCPolygon.prototype.setColor = function(color) {
    this.color = color;
    var dom = document.getElementById(this.domid); 
    if(navigator.userAgent.indexOf("MSIE") != -1){
        dom.stroke.color = this.color;
    }
    else{
        dom.setAttribute("stroke",this.color);
    }
}
BDCCPolygon.prototype.getColor = function() {
    return this.color;
}
BDCCPolygon.prototype.setDash = function(dash) {
    this.dash = dash;
    var dom = document.getElementById(this.domid); 
    if(navigator.userAgent.indexOf("MSIE") != -1){
        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","");
    }
}
BDCCPolygon.prototype.getDash = function() {
    return this.dash;
}
BDCCPolygon.prototype.setWeight = function(weight) {
    this.weight = weight;
    var dom = document.getElementById(this.domid); 
    if(navigator.userAgent.indexOf("MSIE") != -1){
        dom.stroke.weight = this.weight.toString()+"px";   
    }
    else{
        dom.setAttribute("stroke-width",this.weight.toString()+"px");
    }
}
BDCCPolygon.prototype.getWeight = function() {
    return this.weight;
}
BDCCPolygon.prototype.setOpacity = function(opacity) {
    this.opacity = opacity;
    var dom = document.getElementById(this.domid); 
    if(navigator.userAgent.indexOf("MSIE") != -1){
        dom.stroke.opacity = this.opacity;
    }
    else{
        dom.setAttribute("stroke-opacity",this.opacity);
    }
}
BDCCPolygon.prototype.getOpacity = function() {
    return this.opacity;
}



//end of subclassed Polygon 