﻿window.IsSilverlightMap = true;

NetGis = new Object();

NetGis.createObject = function (type) {
    return document.getElementById("NetGisClient").Content.services.createObject(type);
}


//ScriptCoordinate proxy
NetGis.Coordinate = function (x, y) {
    this.X = x;
    this.Y = y;
    this.getObject = function () {
        var source = NetGis.createObject("CoordinateType");
        source.X = this.X;
        source.Y = this.Y;
        return source;
    };
};

var NetGisQueryType = {
    Within: 1,
    Contains: 2,
    Intersects: 3,
    BBox: 4
};

//ScriptMap proxy
NetGis.Map = function () {
    //ScriptingFacade tarafından oluşturulur
    this.source = document.getElementById("NetGisClient").Content.scriptMap;
    this.setCenter = function (coordinate, zoom) {
        this.source.SetCenter(coordinate.getObject(), zoom);
    } //returns void
    this.GoToXY = function (x, y) {
        this.setCenter(new NetGis.Coordinate(x, y), this.getZoom());
    }
    this.getCenter = function () {
        var c = this.source.GetCenter();
        return new NetGis.Coordinate(c.X, c.Y);
    } //returns NetGis.Coordinate

    this.getZoom = function () {
        return this.source.GetZoom();
    } //returns Integer

    this.setZoom = function (zoom) {
        this.source.SetZoom(zoom);
    } //returns void

    this.getCoordinate = function (onEnded) {
        this.source.GetCoordinate(onEnded);
    } //returns void

    this.getPolygon = function (onEnded) {
        this.source.GetPolygon(onEnded);
    } //return void

    this.getLineString = function (onEnded) {
        this.source.GetLineString(onEnded);
    } //return void

    this.query = function (queryType, fad, coordinate, onEnded) {
        if (queryType == NetGisQueryType.Contains)
            this.source.QueryContains(fad, coordinate.getObject(), onEnded);
    } //returns void

    this.addMarker = function (coordinate, layerName, image, angle) {
        this.source.AddMarker(coordinate.getObject(), layerName, image, angle);
    } //returns void

    this.clearMarkers = function (layerName) {
        this.source.ClearMarkers(layerName);
    } //returns void

    this.getWM = function () {
        return this.source.GetWM();
    } // return integer

    this.getHM = function () {
        return this.source.GetHM();
    } // returns integer

    this.getBufferBBox = function (coordinate) {
        var WM = this.getWM();
        var Cllx = coordinate.X - (WM * 11);
        var Clly = coordinate.Y;
        var CurX = coordinate.X;
        var CurY = coordinate.Y + (WM * 14);
        return Cllx + ',' + Clly + ',' + CurX + ',' + CurY;
    }

    this.CallBackNow = function (cmd, arg, usecache) {
        if (window.DefaultMapCallBack)
            window.DefaultMapCallBack(cmd, arg, usecache);
    }

    this.Height = function () {
        return this.source.getMapHeight();
    }
    this.Width = function () {
        return this.source.getMapWidth();
    }

    this.SRID = function () {
        return this.source.GetSRID();
    }

    this.StopInvoke = function () { }
    this.UpdateAppLayers = function () { }

    this.WorldToPoint = function (x, y) {
        var temp = new NetGis.Coordinate(x, y);
        var c = this.source.WorldToPoint(temp.getObject());
        return new NetGis.Coordinate(c.X, c.Y);

    }
    this.GetCanvasLayer = function (LayerId) {
        if (!LayerId) LayerId = "Default";
        LayerId = "Canvas_" + LayerId;
        if (_fo(LayerId))
            return _fo(LayerId)._CTX;
        var l = document.createElement("canvas");
        l.id = LayerId;
        l.setAttribute("NCParent", "InsNCWebMap1");
        l.setAttribute("NCType", "Canvas");
        l.style.cssText = "position:absolute;left:0px;top:0px;border:none;padding:0px;margin:0px;z-index:70;";
        //this.GetCanvasAppLayer().appendChild(l);
        if (window.G_vmlCanvasManager)
            G_vmlCanvasManager.initElement(l); //ie fix
        this._SetCanvasSize(l.id);
        l = _fo(LayerId);
        if (!l.getContext) return null;
        l._CTX = l.getContext('2d');
        if (!l._CTX) return null;
        l._CTX.lineWidth = "1";

        //var radgrad = l._CTX.createRadialGradient(45,45,10,52,50,30);
        //radgrad.addColorStop(0, 'rgba(159,98,0)');
        //radgrad.addColorStop(1, "rgb(255, 202, 206)");
        l._CTX.strokeStyle = "rgb(255, 0, 18)";
        l._CTX.fillStyle = "rgb(255, 202, 206)"; //radgrad; 

        return l._CTX;
    }
    this._SetCanvasSize = function (LayerId) {
        _fo(LayerId).width = this.getWM();
        _fo(LayerId).height = this.getHM();
        _fo(LayerId).style.width = this.getWM() + "px";
        _fo(LayerId).style.height = this.getHM() + "px";
    }
}



