/*
  name：advance class for baidu map development
  desc：baidu map v1.3
  time：2012-05-09
author：xws
 email：xws2012@gmail.com
*/

// 折线 ***********************************************************************************************************
function Polyline(sGUID, opts)
{
    this.opts_ = opts;
    this.polyline_ = new BMap.Polyline(opts.path,
    {
        enableClicking: opts.clickable,
        strokeColor   : opts.strokeColor,
        strokeOpacity : opts.strokeOpacity,
        strokeWeight  : opts.strokeWeight
    }
    );
    this.id = sGUID;
    opts.map.addOverlay(this.polyline_);
    if (opts.withStartMar)
    {
        var marOpt = { map: opts.map, markerType: opts.markerType, haveIcon: true, iconFile: opts.startMarImg, iconSize: new BMap.Size(opts.startMarSizex, opts.startMarSizey),
            lnglat: new BMap.Point(0, 0), clickable: true,
            title: opts.text, canMove: false, withLabel: true, offset: new BMap.Size(0, 0), withTrack: false
        };
        var lblOpt = { text: opts.text, offset: new BMap.Size(opts.startMarSizex, 0), lnglat: new BMap.Point(0, 0), style: { color: "blue", borderColor: "blue", backgroundColor: "white"} };
        this.startMar = new LabelMarker("sdferqwerasdfad", marOpt, lblOpt);
    }
}
Polyline.prototype.removeFromMap = function ()
{
    if (this.polyline_ == null) return;
    this.opts_.map.removeOverlay(this.polyline_);
    if (this.startMar != null)
    {
        this.startMar.removeFromMap();
    }
    this.polyline_ = null;
}
Polyline.prototype.hideFromMap = function ()
{
    if (this.polyline_ == null) return;
    this.opts_.removeOverlay(this.polyline_);
        if (this.startMar != null)
    {
        this.startMar.hideFromMap();
}
}
Polyline.prototype.showOnMap = function (map)
{
    if (this.polyline_ == null) return ;
    opts.map.addOverlay(this.polyline_);
    if (this.startMar != null)
    {
        this.startMar.showOnMap();
    }  
}
Polyline.prototype.getPath = function ()
{
    if (this.polyline_ == null) return null;
    return this.polyline_.getPath();
}
Polyline.prototype.setPath = function (path)
{
    if (this.polyline_ == null) return false;

    this.polyline_.setPath(path);
        if (path.length > 0 && this.opts_.withStartMar)
    {
        this.startMar.setPosition(path.getAt(0));
            this.startMar.showOnMap();
    }
    return true;
}
Polyline.prototype.getPointCound = function ()
{
    if (this.polyline_ == null) return false;
    return this.polyline_.getPath().length;
}
// 取得折线地理长度(米)
Polyline.prototype.getLength = function ()
{
    var d = 0;
    var path = this.polyline_.getPath();
    var latlng;
    for (var i = 0; i < path.getLength() - 1; i++)
    {
        latlng = [path.getAt(i), path.getAt(i + 1)];
        d += this.opts_.map.getDistance(latlng[0], latlng[1]);
    }
    return d;
}

// 删除最后 num 个点
Polyline.prototype.removePoints = function (num)
{
    if (this.polyline_ == null) return;
    var points = this.polyline_.getPath();
    if (points == null) return;
    if (num >= points.getLength())
    {
        this.polyline_.setPath(new Array());
        if (this.startMar != null)
        {
            this.startMarker.hideFromMap();
        }  
        return;
    }
    for (var i = 0; i < num; i++)
    {
        points.pop();
    }
}
// 删除前面 num 个点
Polyline.prototype.removePointsFront = function (num)
{
    if (this.polyline_ == null) return;
    var path = this.polyline_.getPath();
    if (path == null) return;
    if (path.getLength() < 1) return;

    if (num >= path.getLength())
    {
        this.polyline_.setPath(new Array());
        if (this.startMar != null)
        {
            this.startMarker.hideFromMap();
        }
        return;
    }
    for (var i = 0; i < num; i++)
    {
        path.removeAt(0);
    }
    if (this.startMar != null)
    {
        this.startMarker.setPosition(path[0]);
    } 
}
// 添加一个点到末端
Polyline.prototype.addPoint = function (point)
{
    var pointArray = this.polyline_.getPath();
    if (pointArray == null)
    {
        this.polyline_.setPath([point]);
        if (this.startMar != null)
        {
               this.startMarker.showOnMap();
               this.startMarker.setPosition(point);
        } 
    }
    else
    {
        pointArray.push(point);
        this.polyline_.setPath(pointArray);  
        // 下面一句 <= 2 是为了使车辆画轨迹时能显示起点标记，
        // 因为车辆轨迹的第一个点在创建车辆标记时添加的，不是通过此函数添加。
         if (pointArray.length == 1 && this.startMar != null)
         {
             this.startMar.showOnMap();
             this.startMar.setPosition(point);
         }
    }
}
// 添加一个点到前端
Polyline.prototype.addPointFront = function (point)
{
    if (this.polyline_ == null) return;
    var pointArray = this.polyline_.getPath();
    if (pointArray == null)
    {
        this.polyline_.setPath([point]);
        if (this.startMar != null)
        {
            this.startMar.showOnMap();
        }
    }
    else
    {
        pointArray.insertAt(0, point);
    }
    if (this.startMar != null)
    {
        this.startMar.setPosition(pointArray[0]);
    } 
}
// 获取折线端点数
Polyline.prototype.getLinePointNum = function()
{
    return this.polyline_.getPath().getLength();
}

// 订阅左键点击事件
Polyline.prototype.addClickListener = function (listener)
{
    // 注：要用局部变量，否则无法将值传入 listener
    var eventArg = this;
    //
    this.polyline_.addEventListener("click", function ()
    {
        listener(eventArg);
    });
}
// 订阅左键点击标记事件
Polyline.prototype.addMarClickListener = function (listener)
{
//    // 注：要用局部变量，否则无法将值传入 listener
    var eventArg = this;
    //
    this.startMar.addClickListener(function ()
    {
        listener(eventArg);
    });
}
