/* ======================================================================
 * ObjectMarker, klasa główna
 * ====================================================================== */
function ObjectMarker(type,color){
    this.surface_ = new RysownikSurface(40,40);
    this.path_ = new RysownikPath(this.surface_);
    this.setColor(color);
	this.setMarker(type);
}
ObjectMarker.prototype = new GOverlay();

ObjectMarker.prototype.initialize = function(map){
	this.surface_.attach(map.getPane(G_MAP_MARKER_PANE));
    GEvent.addDomListener(this.surface_.getNode(),'dblclick',this.zoomOnClick.bind(this)); //TODO dblclick??
}

/* ----------------------------------------------------------------------
 * @spec 
 * ---------------------------------------------------------------------- */
ObjectMarker.prototype.zoomOnClick = function(e){
    Event.stop(e);
    var map = Wiking.getMap();
    map.setCenter(this.position_,map.getZoom()+1);
};

/* ----------------------------------------------------------------------
 * @spec 
 * ---------------------------------------------------------------------- */
ObjectMarker.prototype.setColor = function(color){
    color = color.replace(" ","","g");
    var
      inc = function(c){
          c = parseInt(c) - 180;
          return c<0 ? "0" : String(c);
      },
      g = color.split(/[\(\,\)]/),
      grad = "rgb("+inc(g[1])+","+inc(g[2])+","+inc(g[3])+")";
    this.path_.setFill('linear',{
        color:color,
        direction:"V",
        arr:[
          [color,0],
          [grad,100]
        ]
    });
    this.path_.setStroke({color:color});
};

/* ----------------------------------------------------------------------
 * @spec 
 * ---------------------------------------------------------------------- */
ObjectMarker.prototype.setMarker = function(id){
    this.path_.setD(Wiking.PojazdMarkers.get(id,"cmd")); //TODO!
};

/* ----------------------------------------------------------------------
 * @spec 
 * ---------------------------------------------------------------------- */
ObjectMarker.prototype.move_ = function(glatlng){
    this.position_ = glatlng;
    if (!this.drawn_){
        Wiking.getMap().addOverlay(this);
        this.drawn_=true;
    }
    this.redraw(true);
};

ObjectMarker.prototype.hide = function(){
    this.surface_.hide();
}

ObjectMarker.prototype.show = function(){
    this.surface_.show();
}

ObjectMarker.prototype.redraw = function(force){
    if (!force) return;
    var point = Wiking.getMap().fromLatLngToDivPixel(this.position_);

	this.surface_.moveTo(point.x - 20, point.y - 38);
}

ObjectMarker.prototype.getPosition = function(){
    return this.position_;
}

ObjectMarker.prototype.remove = function(){
    var node = this.surface_.getNode();
    node.parentNode.removeChild(node);
}

/* ----------------------------------------------------------------------
 * @doc zwraca node surface'a czyli główny node tego obiektu (w DOM)
 * @spec 
 * ---------------------------------------------------------------------- */
ObjectMarker.prototype.getNode = function(){
    return this.surface_.getNode();
};

ObjectMarker.prototype.cloneNode = function(){
    return this.surface_.getNode().cloneNode(true);
};

/* ----------------------------------------------------------------------
 * @doc zwraca node patha, czyli sam marker
 * @spec 
 * ---------------------------------------------------------------------- */
ObjectMarker.prototype.getMarkerNode = function(){
    return this.path_.getNode();
};
