﻿PAT.Tempest = Class.create( {
    wiSessionId:null,
    layerName:null,
    //who:null,
    initialize:function()
    {
    },
    getTempestSessionid:function()
    {
        return wiSessionId;
    },
    setTempestSessionId:function(id)
    {
        this.wiSessionId = id;
    },
    getLayerName:function()
    {
        return this.layerName;
    },
    setLayerName:function(name)
    {
        this.layerName = name;
    },
    getSelectionXML:function()
    {   
        var parms = {"ACTION":"XML", "wiSessionId":this.wiSessionId, "mgSessionId":PAT.sessionId, "tempestLayerName":this.layerName}
        new Ajax.Request(currLocation.getDirectory() + 'Tempest/TempestControler.aspx',{    
            method:'post',     
            parameters:parms, 
            onSuccess: this.setTempestInfo.bindAsEventListener(this),
            onFailure: function(e){
                alert(getText("GetSelectionTempestInterfaceFailed")) 
            },
            onComplete:function()
            {
                //alert("complete");
            }
        }); 
    },
    setTempestInfo:function(transport){
        var tempestObj = eval('(' + transport.responseText + ')' );
                      
        if((tempestObj.layerName || null) == null)
        {
            return;
        }
        if((tempestObj.selectionXml || null) == null)
        {
            return;
        }
        
        this.setLayerName(tempestObj.layerName);
        if(tempestObj.selectionXml != ""){
            mapguide.map.SetSelectionXML(tempestObj.selectionXml);
            mapguide.zoomToFeature();
            mapguide.zoomToMinScale.delay(1, mapguide.map, scale); 
        }
    },
    clearTempest:function()
    {
        var parms = {"ACTION":"DELETE", "wiSessionId":this.wiSessionId, "mgSessionId":PAT.sessionId}
        new Ajax.Request(currLocation.getDirectory() + 'Tempest/TempestControler.aspx',{    
            method:'post',     
            parameters:parms, 
            onSuccess: function(transport){
                  //alert("hello");
                  //var tempestObj = eval('(' + transport.responseText + ')' );
                  
                
                  //alert(tempestObj.selectionXml);
                  //var dbObjectId = tempestObj.dbObjectId;
                  //var gisKey = tempestObj.gisKey;
                  //var jsIdList = tempestObj.gisKeyList;
                  //mapguide.domObj.fire("PAT.Mapguide:selectionByXML", {"dbObjectId":this.data.config.dbObjectId, "gisKey":name, "jsIdList":this.domObj.gisKeys});
                  //mapguide.map.SetSelectionXML(tempestObj.selectionXml);
            },
            onFailure: function(e){
                alert(getText("ClearTempestFailed")) 
            },
            onComplete:function()
            {
                //alert("complete");
            }
        });
    },
    updateTempest:function(selectionXML){
        //var workingLayer;
        //for(var i = 0; i < workingLayers.length; i++)
        //{
        //    workingLayer = workingLayers[i];
        //    var lName = this.getLayerName();
        //    if((lName || null) == null) 
        //   {
        //        continue;
        //    }
        //    if( lName.toUpperCase() == workingLayer.name.toUpperCase())
        //    {
        //        break;
        //    }
        //}
        
        var parms = {"ACTION":"UPDATE", "wiSessionId":this.wiSessionId, "mgSessionId":PAT.sessionId, "selectionXML":escape(selectionXML), "tempestLayerName":this.layerName}
        new Ajax.Request(currLocation.getDirectory() + 'Tempest/TempestControler.aspx',{    
            method:'post',     
            parameters:parms, 
            onSuccess: function(transport){
                  //alert("hello");
                  var tempestObj = eval('(' + transport.responseText + ')' );
                  
                
                  //alert(tempestObj.selectionXml);
                  //var dbObjectId = tempestObj.dbObjectId;
                  //var gisKey = tempestObj.gisKey;
                  //var jsIdList = tempestObj.gisKeyList;
                  //mapguide.domObj.fire("PAT.Mapguide:selectionByXML", {"dbObjectId":this.data.config.dbObjectId, "gisKey":name, "jsIdList":this.domObj.gisKeys});
                  //mapguide.map.SetSelectionXML(tempestObj.selectionXml);
            },
            onFailure: function(e){
                alert(getText("UpdateTempestFailed")) 
            },
            onComplete:function()
            {
                //alert("complete");
            }
        }); 
    }
});

