﻿function MyMap(map) {
    this.map = map;
    this.setCenter = setCenter;
    this.addMarker2 = addMarker2;
    this.showBalloon = showBalloon;
    this.clearMarker = clearMarker;
    this.closeBalloon = closeBalloon;
    this.addPin = addPin;
    this.hideAddPin = hideAddPin;
    this.setBalloonSize = setBalloonSize;
    this.addMarker = addMarker;
    this.hideSmallMap = hideSmallMap;

    var balloonSize = [{ width: 180, height: 185 }, { width: 260, height: 310 }, { width: 350, height: 400 }, { width: 500, height: 600}];
    var size = 3;

    function hideSmallMap() {
        try {
            map.hideSmallMap();
        }
        catch (e) { }
    }

    function setCenter(ll, z) {
        try {
            map.setCenter(ll, z);
        } catch (e) {
            var gll = new GLatLng(ll.lat, ll.lng);
            if (!z) {
                z = 8;
            }
            map.setCenter(gll, getZoomLv(z));
        }
    }


    function addMarker(ll, pin) {
        try {
            map.addMarker(ll, pin);
        }
        catch (e) {
            var gll = new GLatLng(ll.lat, ll.lng);
            var marker = new GMarker(gll);
            var macthPin = pin.match(/http:+/);
            if (macthPin == null) {
                marker = new GMarker(gll, getIcon(pin));
            }
            map.addOverlay(marker);
            map.setCenter(gll);

        }
    }

    function addMarker2(name, ll, pin) {
        try {
            map.addMarker2(name, ll, pin);
        }
        catch (e) {
            var gll = new GLatLng(ll.lat, ll.lng);
            var marker = new GMarker(gll);
            var macthPin = pin.match(/http:+/);
            if (macthPin != null) {
                marker = new GMarker(gll, getIcon(pin));
            }
            var html = getHtmlIframe(ll.url, size);


            map.addOverlay(marker);

            if (ll.url != '' && ll.url) {
                GEvent.addListener(marker, "click", function() { marker.openInfoWindowHtml(html, getBalloonSize(size)); });
            }
            map.setCenter(gll);
        }
    }

    function showBalloon(p1, url, ll, p2, s) {
        try {
            map.showBalloon(p1, url, ll, p2, s);
        }
        catch (e) {
            window.setTimeout(function() {
                var gll = new GLatLng(ll.lat, ll.lng);

                var html = getHtmlIframe(url, size);
                var balloonSize = getBalloonSize(size);
                if (s) {
                    html = getHtmlIframe(url, s);
                    balloonSize = getBalloonSize(s);
                }

                var gll = new GLatLng(ll.lat, ll.lng);

                map.openInfoWindowHtml(gll, html, balloonSize);
            }, 500);
        }
    }

    function clearMarker() {
        try {
            map.clearMarker();
        } catch (e) {
            map.clearOverlays();
        }
    }

    function closeBalloon() {
        try {
            map.closeBalloon();
        } catch (e) {
            map.closeInfoWindow();
        }
    }

    var markerAddPin;
    var hasAddPin = false;
    function addPin(url, icon, s, title, bp1) {
        try {
            map.addPin(url, icon, s, title, bp1);
        } catch (e) {
            window.setTimeout(function() {
                if (!hasAddPin) {
                    hasAddPin = true;

                    var center = map.getCenter();
                    var gll = center;
                    var marker = new GMarker(gll);
                    var macthPin = icon.match(/http:+/);
                    if (macthPin != null) {
                        marker = new GMarker(gll, getIcon(icon));
                    }
                    map.addOverlay(marker);
                    marker.enableDragging();
                    markerAddPin = marker;

                    var balloonSize = getBalloonSize(size);
                    var html = getHtmlIframe(url, size);
                    if (s) {
                        html = getHtmlIframe(url, s);
                        balloonSize = getBalloonSize(s);
                    }
                    GEvent.addListener(marker, "dragstart", function() { map.closeInfoWindow(); });
                    GEvent.addListener(marker, "drag", function() { });
                    GEvent.addListener(marker, "dragend", function() {
                        var src = url;
                        var s = size;
                        var result = url.match(/\?/);

                        var query = "&";
                        if (result == null) {
                            query = "?";
                        }
                        query += "lat=" + marker.getLatLng().y + "&lon=" + marker.getLatLng().x + "&zoom=" + getOldZoom(map.getZoom());

                        marker.openInfoWindowHtml(getHtmlIframe(src, s, query), balloonSize);
                    });
                }
            }, 1500);
        }
    }

    function hideAddPin() {
        try {
            map.hideAddPin();
        } catch (e) {
        if (markerAddPin) {
            markerAddPin.closeInfoWindow();
            map.removeOverlay(markerAddPin);
            markerAddPin = null;
            hasAddPin = false;
        }


        }
    }

    function setBalloonSize(s) {
        try {
            map.setBalloonSize(s);
        } catch (e) {
            size = s;
            var width = balloonSize[size - 1].width;
            var height = balloonSize[size - 1].height;
        }
    }

    function getHtmlIframe(url, s, query) {
        var width = 320;
        var height = 370;

        if (s) {
            width = balloonSize[s - 1].width - 30;
            height = balloonSize[s - 1].height - 30;
        }
        if (!query) {
            query = '';
        }
        var html = '<iframe src=' + url + query + ' frameborder="0" height="' + height + '" width="' + width + '" > </iframe>';
        return html;
    }

    function getBalloonSize(s) {
        return balloonSize[s - 1];
    }

    function getZoomLv(z) {
        var zs = [17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7];
        return zs[z - 1];
    }

    function getOldZoom(z) {
        var oldZs = [11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1];
        return oldZs[z - 6];

    }

    function getIcon(iconurl) {
        var gicon = new GIcon();
        gicon.image = iconurl;
        gicon.iconSize = new GSize(32, 32);
        gicon.iconAnchor = new GPoint(15, 15);
        return { icon: gicon };
    }

}

LatLng = function(lat, lng, title, url, r) {
    // if only one argument is specified, use it as a base64 string
    // and decode the arguments value from the string instead.
    if (arguments.length == 1) {
        // base64 encoded values mode: use the first arg as the
        // base64 string and then decode values from there
        var argsString = Util.base64Encode(arguments[0]).split(',');

        this.lat = Math.round(parseFloat(argsString[0]), 5);
        this.lng = Math.round(parseFloat(argsString[1]), 5);
        this.title = argsString[2] ? eval(argsString[2]) : undefined;
        this.url = argsString[3] ? eval(argsString[3]) : undefined;
        this.r = argsString[4] ? eval(argsString[4]) : undefined;

    } else {
        // normal construction mode
        this.lat = parseFloat(lat);
        this.lng = parseFloat(lng);
        this.title = title;
        this.url = url;
        this.r = r;
    }
};

LatLng.prototype = {
    isValid: function() {
        return this.lat && this.lng;
    },
    toString: function() {
        var s = this.lat + "," + this.lng;
        if (this.title) s += ",'" + this.title + "'";
        else s += ",null";
        if (this.url) s += ",'" + this.url + "'";
        else s += ",null";
        if (this.r) s += "," + this.r;
        else s += ",null";
        return s;
    }
};
