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

// 带文本的标记 *********************************************************************************************************
// baidu map marker with a text label
// markerOpts{map, latlng, title, image}
// labelOpts {latlng, text, map }
function LabelMarker(markerID, markerOpts, labelOpts)
{
    if (markerOpts.haveIcon)
    { 
        this._icon = new BMap.Icon(markerOpts.iconFile,markerOpts.iconSize);
        this.marker = new BMap.Marker(markerOpts.lnglat,
        {
            icon: this._icon,
            enableClicking : markerOpts.clickable,
            title : markerOpts.title,
            enableDragging: markerOpts.canMove,
            offset:markerOpts.offset
        }); 
    }
    else
    {
        this.marker = new BMap.Marker(markerOpts.lnglat,
        {            
            enableClicking: markerOpts.markerCanClickable,
            title: markerOpts.title,
            enableDragging: markerOpts.canMove
        });
        labelOpts.offset = new BMap.Size(8, 25);
    }
    this._opts = markerOpts;
    this._id = markerID; 
    this.label = null;    
    if (markerOpts.withLabel) {
 
        this.label = new BMap.Label(labelOpts.text, { position: labelOpts.lnglat,  offset: labelOpts.offset });
        this.label.setStyle(labelOpts.style);
        this.marker.setLabel(this.label);
    }
    
    this.canEdit = markerOpts.canEdit;
    this.Tag = null;
    this.parentID = null;
    this.markerType = markerOpts.markerType;
    markerOpts.map.addOverlay(this.marker);
}
// 获取该标注ID
LabelMarker.prototype.getID = function ()
{
    return this._id;
}
// 获取该标注Tag
LabelMarker.prototype.getTag = function ()
{
    return this.Tag;
}
// 设置该标注Tag
LabelMarker.prototype.setTag = function (tag)
{
    this.Tag = tag;
}
// 获取该标注宿主ID
LabelMarker.prototype.getParentID = function ()
{
    return this.parentID;
}
// 设置该标注宿主ID
LabelMarker.prototype.setParentID = function (pid)
{
    this.parentID = pid;
}
// 获取一个值，该值指示标记是否可以修改。
LabelMarker.prototype.getEditEnable = function ()
{
    return this.canEdit;
}

// 左键单击事件：供客户代码注册用
LabelMarker.prototype.addClickListener = function (listener)
{
    // 注：要用局部变量，否则无法将值传入 listener
    var eventArg = this; 
    //
    this.marker.addEventListener("click", function ()
    {
        listener(eventArg);
    });
}
// 右键单击事件：供客户代码注册用
LabelMarker.prototype.addRightclickListener = function (listener)
{
    // 注：要用局部变量，否则无法将值传入 listener
    var eventArg = this;
    //
    this.marker.addEventListener("rightclick", function ()
    {
        listener(eventArg);
    });
}
// 鼠标进入事件：供客户代码注册用
LabelMarker.prototype.addMouseOverListener = function (listener)
{
    // 注：要用局部变量，否则无法将值传入 listener
    var eventArg = this;
    //
    this.marker.addEventListener("mouseover", function ()
    {
        listener(eventArg); 
    });
}
// 鼠标离开事件：供客户代码注册用
LabelMarker.prototype.addMouseOutListener = function (listener)
{
    // 注：要用局部变量，否则无法将值传入 listener
    var eventArg = this;
    //
    this.marker.addEventListener("mouseout", function ()
    {
        listener(eventArg);
    });
}
// 当用户停止拖动标记时会触发此事件
LabelMarker.prototype.addDragendListener = function (listener)
{
    var eventArg = this;
    this.marker.addEventListener("dragend", function ()
    {     
        listener(eventArg);
    });
}

// 获取位置
LabelMarker.prototype.getPosition = function ()
{
    return this.marker.getPosition();
}
// 从地图上删除
LabelMarker.prototype.removeFromMap = function ()
{
    if (this.marker != null)
    {
        this._opts.map.removeOverlay(this.marker);
        this.marker = null;
    }
}
// 设置标记的地理位置
LabelMarker.prototype.setPosition = function (point)
{
    try
    {
        if (this.marker != null)
        {
            this.marker.setPosition(point);
        }
    }
    catch (err)
    {
    }
}
// 修改标记图片
LabelMarker.prototype.modifyImage = function (imageFile)
{
    if (this.marker == null) return;
    var imagePro = this.marker.getIcon();

    if (!this._opts.haveIcon || imagePro.imageUrl == imageFile) return;

    imagePro.setImageUrl(imageFile);
    this.marker.setIcon(imagePro);
}
// 修改标记标题
LabelMarker.prototype.modifyTitle = function (newTitle)
{
    if (this.marker == null) return; 
    this.marker.setTitle(newTitle); 
}
// 修改标记文本标签样式
LabelMarker.prototype.modifyLabelStyle = function (mStyle)
{
    if (this.label == null) return;
    this.label.setStyle(mStyle);
} 
// 修改标记文本标签内容
LabelMarker.prototype.modifylblTxt = function (text)
{
    if (this.label == null) return;
    this.label.setContent(text);
}
// 添加右键菜单
LabelMarker.prototype.addContextMenu = function (menu)
{
    if (this.marker != null && menu != null)
    {
        this.marker.addContextMenu(menu);        
    }
}

// 从地图上隐藏
LabelMarker.prototype.hideFromMap = function ()
{
    if (this.marker != null)
    {
        this._opts.map.removeOverlay(this.marker);
    }
}
// 在地图上显示
LabelMarker.prototype.showOnMap = function ()
{
    if (this.marker != null)
    {
        this._opts.map.addOverlay(this.marker);
    }
}

// 隐藏lbl
LabelMarker.prototype.hideLabel = function ()
{
    if (this.label != null && this.marker != null)
    {
        this.marker.setLabel(null);
    }
}
// 显示lbl
LabelMarker.prototype.showLabel = function (map)
{
    if (this.label != null && this.marker != null)
    {
        this.marker.setLabel(this.label);
    }
}
// 获取属性集合
LabelMarker.prototype.getOpts = function ()
{
    return this._opts;
}