/*
    Document   : application
    Created on : 02-Aug-2009, 11:21:08
    Author     : Andreas Hadjigeorgiou
    Description: The gmapp application file. This will initialise the map
                 output process
        
*/



//Ext.BLANK_IMAGE_URL = './ext/resources/images/default/s.gif';
//Ext.state.Manager.setProvider(new Ext.state.CookieProvider());
Ext.ns('lbgm');

lbgm.options = {
    appname: "Geomatic Maps"
};

// create application
lbgm.app = {
    // do NOT access DOM from here; elements don't exist yet

    // private variables

    // private functions

    // public space
 //   return {
        // public properties, e.g. strings to translate

       loadSettings: function() {
            var getParams = document.URL.split("?");
            var key = '';
            //var point = '';
            
            if(getParams.length > 1 ) {
                this.urlparams = Ext.urlDecode(getParams[1]);
                key = this.urlparams['key'];

                if(key != undefined && key != '')
                    key = key.replace("#", "");
                //point = this.urlparams['point'];
                //Ext.MessageBox.alert(lbgm.options.appname, "No GUID specified");
                //return;
            }
            else {
                //Ext.MessageBox.alert(lbgm.options.appname, "No GUID specified");
                key = 'C41D6C35-2430-0001-82BE-1B0E2130B3B0';
            }
            
            //AJAX
            var conn = new Ext.data.Connection();
            conn.request({
                url: 'src/php/conn/gmappsettings.conn.php',
                method: 'POST',
                params: {"request": "loadSettings", "key": key},
                success: function(responseObject) {
                   obj = Ext.util.JSON.decode(responseObject.responseText);
                   if(obj.success) {
                       lbgm.layout.key = key;
                       lbgm.layout.layoutid = obj.results[0].layoutid;
                       lbgm.layout.xmlfile = obj.results[0].xmlfile;
                       lbgm.layout.appid = obj.results[0].appid;
                       lbgm.layout.mapboundstop = obj.results[0].mapboundstop;
                       lbgm.layout.mapboundsbottom = obj.results[0].mapboundsbottom;
                       lbgm.layout.mapboundsleft = obj.results[0].mapboundsleft;
                       lbgm.layout.mapboundsright = obj.results[0].mapboundsright;
                       lbgm.layout.enablenavigation = obj.results[0].enablenavigation;
                       lbgm.layout.minzoomlevel = obj.results[0].minzoomlevel;
                       lbgm.layout.initlongitude = obj.results[0].initlongitude;
                       lbgm.layout.initlatitude = obj.results[0].initlatitude;
                       lbgm.layout.languageid = obj.results[0].defaultlanguageid;
                       lbgm.layout.enableidentification = obj.results[0].enableidentification;
                       lbgm.layout.enablenearest = obj.results[0].enablenearest;
                       lbgm.layout.enableshare = obj.results[0].enableshare;

                       lbgm.layout.init();
                   }
                   else {
                       Ext.Msg.alert(lbgm.options.appname, obj.error);
                       return false;
                   }
                },
                failure: function() {
                   Ext.Msg.alert(lbgm.options.appname, 'Unable to load application settings.');
                },
                scope: this
            });
            
       },

/* TODO: communicate with appsettings.conn.php to load the company
 * and the application paremeters into a global json object */

        // public methods
        init: function() {
            Ext.QuickTips.init();

/* TODO: From the paremeters if showloading is true create and show
 * application loading */
            //Ext.get('loading').remove();
            
            //alert('Application successfully initialized');
            this.loadSettings();

/* TODO: If loading is true remove it after application initialization */
            //Ext.get('loading').remove();
            //Ext.get('loading-mask').fadeOut({remove:true});
        },

        setLanguage: function(languageid) {
            OpenLayers.Request.GET({
                url: 'src/php/conn/gmappsettings.conn.php',
                params: {
                    request: "setSession",
                    sessionParam: "lbgm_languageid",
                    sessionValue: languageid
                },
                success: function(responseObject) {
                   lbgm.layout.languageid = languageid;
                   //alert(responseObject.responseText);
                   lbgm.maplets.poitree.tree.reload();
                },
                failure: function() {
                   Ext.Msg.alert(lbgm.options.appname, 'Unable to set language.');
                }

            });
        },

        includeJS: function(jsFile) {
            var s = document.createElement('script');
            s.setAttribute('type','text/javascript');
            s.setAttribute('src',jsFile);
            document.getElementsByTagName("head")[0].appendChild(s);
        }
    };
//}(); // end of app
//
// end of file



