﻿Type.registerNamespace("Controller");

Controller.DataController = function (){
    this._aData = new Array();
}

Controller.DataController.prototype = {

    /* Set server data */
    setServerData: function(requestId, aParams, partId, onReturn, onError, passThrough){
        CV.mBOSS.DAL.DataLayerWebService.SetData(
            requestId, 
            aParams, 
            partId,
            Global.getConfigId(),
            onReturn , 
            onError, 
            passThrough);
    },
    
    /* Get data */
    getData: function(id){
        for (var i=0;i<this._aData.length;i++){
            if (this._aData[i][0]==id){
              return this._aData[i][1];
            }
        }
        return null;
    },
    
    getServerData: function(requestId, aParams, partId, onReturn, onError, passThrough){
       
         CV.mBOSS.DAL.DataLayerWebService.GetData(
            requestId, 
            aParams, 
            partId,
            Global.getConfigId(),
            onReturn , 
            onError, 
            passThrough);
    },
    
    getLatestUpdate: function(id){/* When was data updated */
        for (var i=0;i<this._aData.length;i++){
            if (this._aData[i][0]==id){
              return this._aData[i][3];
            }
        }
        return null;
    },
    
    /* Who updated data the last time */
    getLastSender: function(id){
        for (var i=0;i<this._aData.length;i++){
            if (this._aData[i][0]==id){
              return this._aData[i][2];
            }
        }
        return null;
    },
    
    setData: function(id, data, sender){
        
        
        //create event array
        var a = new Array(4);
        a[0] = id;
        a[1] = data; 
        a[2] = sender;
        a[3] = new Date();
       
        //check for exsisting data
        var bFound = false;
        for(var i=0; i<this._aData.length;i++){
           if (this._aData[i][0]==id){
              this._aData[i] = a;
              bFound = true;
            } 
        }
        
        //add event to subscribers table
        if(!bFound){
            this._aData.push(a);
        }
    }
}

Controller.DataController.registerClass('Controller.DataController', null, Sys.IDisposable);



Controller.EventController = function(){
    this._aSubscribers = new Array();
}

Controller.EventController.prototype = {
    //fire event to all subscribers
    fireThisEvent: function (eventId, sender, context){
    
        //output debugging message in VS.Net output window
        if(sender && sender.getClassName){
            //debug.trace("Firing event: " +eventId +" from the part \"" + sender.getClassName() + "\"");
        }
        else{
            //debug.trace("Firing event: " +eventId );
        }
     
    
        if(context!=null){
            Global.DataController().setData(eventId, context, sender);
        }
    
        for (var i=0;i<this._aSubscribers.length;i++){
            if (this._aSubscribers[i][0]==eventId){
              var func = this._aSubscribers[i][1];
              func(sender, context);  
            }
        }
    },
    
    //add subscriber to event
    subscribe: function (eventId, methodToCall){
        
        //output debugging message in VS.Net output window
       //debug.trace("Subscribing to event: " + eventId) 
        
        //create event array
        var a = new Array(2);
        a[0] = eventId;
        a[1] = methodToCall; 

        //add event to subscribers table
        this._aSubscribers.push(a);
    },
    
    getRegisteredEvents: function(){
        var res="";
        for (var i=0;i<this._aSubscribers.length;i++){
            res += this._aSubscribers[i][0] + "\r\n";
        }
        return res;
    }
}

Controller.EventController.registerClass('Controller.EventController', null, Sys.IDisposable);

/*
* Declare state controller
*/

/*
* Declare state controller
*/

var inty = 0;
Controller.ComponentsController = function(){
    Controller.ComponentsController.initializeBase(this);
} 
    
Controller.ComponentsController.prototype={ 
    
    /*
    * Create the class for the given part. 
    * Method is used by parts in iframe to initialize the class when the gui is loaded.
    */
    initPart: function(id, partWindow, controlEnum){
        map_id = id;
        map_partWindow = partWindow;
        map_controlEnum = controlEnum;
    
        mapLoaded = true;
        if(pageLoaded){
            loadMap();
        }
    },
   
    /*
    * returns the class for the given part
    */
    getPartById: function(id){
       return false;
    }
}

Controller.ComponentsController.registerClass('Controller.ComponentsController');


/*
* Define class for global reference to objects
*/
var i=0;

ClassGlobal = function(){
    ClassGlobal.initializeBase(this);
    
    this._mapApi = null;
    this._oComponentsController = new Controller.ComponentsController ();
    this._oEventController = new Controller.EventController();
    this._oDataController = new Controller.DataController();
}

ClassGlobal.prototype={

    ComponentsController: function() {return this._oComponentsController;},
    EventController: function(){ return this._oEventController;},
    DataController: function(){ return this._oDataController;},
    Utils: function(){ return this._oUtils;},
  
    getMapApi: function(){
        return this._mapApi;
    },
    
    setMapApi:function(mapApi){
       this._mapApi=mapApi;
    },
    
    getConfigId: function(){
        return this._configId;
    },
    
    setConfigId:function(configId){
       this._configId=configId;
    },
    
    setLoading: function(bLoading){
        if(bLoading){
            window.status="Loading...........";
        }
        else{
            window.status="";
        }
    }
}
ClassGlobal.registerClass('ClassGlobal');

var Global = new ClassGlobal();





// JScript File
Type.registerNamespace("Parts");

//Class
Parts.Part = function(id){
    this._id = id;
}

//return Part ID
Parts.Part.prototype.getId = function(){
    return this._id;
}

//only works if you have set the string with setClientData() method.
Parts.Part.prototype.getResourceString = function(key){
    return Global.DataController().getData(this._id + "." + key);
}
Parts.Part.prototype.elm= function(id){
    var enumCtl =  Global.DataController().getData(this._id + "enumControls");
    if( ! enumCtl ) enumCtl = eval(this._id + "enumControls");
    //var o = $get(eval("enumCtl." + id));
    if(eval("enumCtl." + id))
    {
        return $get(eval("enumCtl." + id));
    }
    else{return $get(id);}
    
}
Parts.Part.registerClass('Parts.Part', null, Sys.IDisposable);

