﻿PAT.Ivault = Class.create({
    layerName: null,
    sessionId: null,
    giskeyCollection: null,
    timer: null,
    setLayerName: function(l)
    {
        this.layerName = l;
    },
    getLayerName: function()
    {
        return this.layerName;
    },
    setSession: function(id)
    {
        this.sessionId = id;
    },
    setGiskeyCollection: function(giskeys)
    {
        this.giskeyCollection = giskeys;
    },
    getGiskeyCollection: function()
    {
        return this.giskeyCollection;
    },
    getSelectionXML: function()
    {//alert(this.giskeyCollection.toJSON());
        var parms = { "mgSessionId": PAT.sessionId, "ivaultLayerName": this.layerName, "ivaultGiskeyCollection": this.giskeyCollection.toJSON() }
        new Ajax.Request(currLocation.getDirectory() + 'Mapguide/GetSelectionXMLByLayer.aspx', {
            method: 'post',
            parameters: parms,
            onSuccess: this.setMapSelection.bindAsEventListener(this),
            onFailure: function(e)
            {
                alert(getText("GetSelectionIvaultInterfaceFailed") + "   -3")
            },
            onComplete: function()
            {
                //alert("complete");
            }
        });
    },
    setMapSelection: function(transport)
    {
        var jObj = eval('(' + transport.responseText + ')');
        if (jObj.notFound == null)
        {
            var selectionXML = jObj.selectionXml;
            var layerName = jObj.layerName;
            var scale = jObj.minScale;

            if (selectionXML != "")
            {
                sleep(1000); //we need this delay to deal with the problem on IE for zooming inconsistencies
                try
                {
                    mapguide.map.SetSelectionXML(selectionXML);
                }
                catch (e)
{ }
                this.timer = setInterval(this.zoomToFeature.bind(this, scale), 500);
            }
            else
            { alert("selectionXML is empty"); }
        }
    },

    zoomToFeature: function()
    {
        var scale = arguments[0];

        if (mapguide == null)
        {
        }
        else
        {
            if (mapguide.map == null)
            {
            }
            else
            {
                if (mapguide.map.mapInit == null)
                {
                }
                else
                {
                    clearInterval(this.timer);
                    mapguide.zoomToFeature();

                    //alert(mapguide.map.GetSelectionXML());
                    mapguide.zoomToMinScale.delay(100, mapguide.map, scale);
                }
            }
        }
    },
    zoomToInterfaceParms: function()
    {
        var parms = { "action": "read", "sessionId": this.sessionId, "mgSessionId": PAT.sessionId }
        new Ajax.Request(currLocation.getDirectory() + 'Mapguide/GetInterfaceParms.aspx', {
            method: 'post',
            parameters: parms,
            onSuccess: this.setMapSelection.bindAsEventListener(this),
            onFailure: function(e)
            {
                alert(getText("GetSelectionIvaultInterfaceFailed") + "  2");
            },
            onComplete: function()
            {
                //alert("complete");
            }
        });
    },
    updateInterfaceTable: function(selectedLayer, selectionXML)
    {//alert(selectionXML);
        var parms = { "action": "write", "sessionId": this.sessionId, "mgSessionId": PAT.sessionId, "xml": escape(selectionXML), "selectedLayer": selectedLayer };
        new Ajax.Request(currLocation.getDirectory() + 'Mapguide/GetInterfaceParms.aspx', {
            method: 'post',
            parameters: parms,
            onSuccess: function(e) { },
            onFailure: function(e)
            {
                alert(getText("GetSelectionIvaultInterfaceFailed") + "\n" + e.responseText);
            }
        });
    }
});

function sleep(ms)
{
    var date = new Date();
    var curDate = null;
    do { curDate = new Date(); }
    while (curDate - date < ms);
}
