/*
* jquery.mapabc  ver0.1a
* MapABC的 jQuery 插件
*
* Copyright (c) 2008 Martin Lu (http://www.zh51home.com)
* Dual licensed under the MIT (MIT-LICENSE.txt)
* and GPL (GPL-LICENSE.txt) licenses.
*
* $Date: 2008-10-24 $
*/

jQuery.fn.mapabc = function(obj) {
    //初始化参数值
    if (obj == undefined) {
        alert("错误，没有找到初始化对象");
        return undefined;
    }
    else {
        if (obj.compassScale == undefined) obj.compassScale = 70;
        if (obj.zoomLevel == undefined) obj.zoomLevel = 13;
        if (obj.lat == undefined || obj.lng == undefined) {
            obj.lat = "KJGQTTMRLLHDL";
            obj.lng = "JILMUPQSJOLLHH";
        }
    }

    if (this.length > 1) {
        alert("错误：id编码重复");
        return undefined;
    }
    else if (this.length == 0) {
        alert("错误：id不匹配");
        return undefined;
    }
    var map_id = this[0].id

    //创建一个MmapOptions对象
    var mapOptions = new MMapOptions();
    //设置地图组件的Id
    mapOptions.mapId = "fmp_show";
    //设置地图的初始Zoom值
    mapOptions.zoomLevel = obj.zoomLevel;
    //设置地图的中心，注意：MLatLng("纬度","经度")方法参数的顺序，纬度在前，经度在后
    mapOptions.center = new MLatLng(obj.lat, obj.lng);

    //创建地图对象
    var mapObj = new MMap(map_id, mapOptions);

    if (obj.tagPoint != undefined) {
        if (obj.tagPoint.contentText == undefined) {
            alert("错误:对象tagPoint不存在contentText属性");
            return undefined;
        }

        if (obj.tagPoint.imgUrl == undefined) {
            alert("错误：对象tagPoint不存在imgUrl属性");
            return undefined;
        }

        if (obj.tagPoint.lat == undefined) {
            alert("错误：对象tagPoint不存在imgUrl属性");
            return undefined;
        }

        if (obj.tagPoint.lng == undefined) {
            alert("错误：对象tagPoint不存在imgUrl属性");
            return undefined;
        }

        var pointStyle = new MStyle();
        pointStyle.lineColor = 0x666666;
        pointStyle.lineSize = 1;
        pointStyle.fillColor = 0xe5e5e5;
        pointStyle.fillOpacity = 50;
        pointStyle.labelColor = 0x00ff00;
        pointStyle.textContent = obj.tagPoint.contentText;
        var customPoint = new MCustomPointOverlay(new MLatLng(obj.tagPoint.lat, obj.tagPoint.lng), obj.tagPoint.imgUrl, pointStyle);
        mapObj.addOverlay(customPoint, false); //true 为自动调整视野
    }
    mapObj.setCompassScale(obj.compassScale);

    //完善jquery插件的行为
    this._mapObj = mapObj;
    this.getCenterByLatLng = function() {
        return this._mapObj.getCenterByLatLng();
    }

    //添加事件处理
    if (obj.onMapMoved != undefined) {
        mapObj.addEventListener(MMap.EVENT_MAP_MOVED, obj.onMapMoved);
    }
    return this;
};
