var App = Class.create();

  App.prototype = 
  {
    initialize : function() 
    {
      this.debug("App initialize() - start"); 

      this.callback = null;
    
      var scriptSrc = document.getElementsByTagName("script")[0].src;
      var scriptPath = /.*scripts\//i.exec(scriptSrc)[0];
      this.imgPath = scriptPath.replace(/scripts\//, "images/global/");
                      
      this.debug("App initialize() - done");       
    },

    log : function()
    {
      if (this.logger != null)
      {
        this.logger.log.apply(this.logger, arguments);
      }
    },
    
    info : function(message)
    {
      //this.log(message, "info");
    },

    error : function(message)
    {
      this.log(message, "error");
    },
            
    debug : function(message)
    {
      //this.log(message, "debug");
    },
    
    onError : function(err)
    {
      var msg = "Error Occured";
      msg += "\n Message: " + err.message;
      msg += "\n Filename: " + err.fileName;
      msg += "\n Line: " + err.lineNumber;
      msg += "\n Name: " + err.name;
      //msg += "\n Stack: " + err.stack;
      
      this.error(msg);
    },

    _loadXML : function(url, params)
    {
      this.debug("App _loadXML() - start"); 
      
      var ajax = new Ajax.Request(url, { 
        method: 'get', 
        parameters: params, 
        onComplete: this._onComplete.bind(this)
      });  
      
      this.debug("App _loadXML() - done"); 
    },
                   
    _onComplete : function(request)
    {
      this.debug("App _onLoadComplete() - start"); 
            
      var xmlContent = request.responseText + "\n"; 
                
      try
      {
        var xmlParser = new DOMImplementation();        
        var xmlDoc = xmlParser.loadXML(xmlContent);    
      
        if(xmlDoc == null) 
        {
          this.error("xmlDoc == null");
          return;
        }
      
        var node = xmlDoc.getDocumentElement();   

        this.data = new Object();  
        var parent = this.data;
        
        this._processNode(node, parent);        
      }
      catch (err)
      {
        this.status = 0;
        this.message = "XML Parsing error";

        this.onError(err);
      }
      
      if (this.callback != null || typeof this.callback == 'function')
      {
        try
        {
          this.callback.apply(this);
        }catch (err) 
        {
          this.onError(err);
        }
      }
      
      this.debug("App _onLoadComplete() - done"); 
    },
    
    _processNode : function(node, data)
    {
      this.debug("App _processNode() - start"); 
      
      if (node.getNodeType() != DOMNode.ELEMENT_NODE)
      {
        this.warn("node.getNodeType() != DOMNode.ELEMENT_NODE"); 
        return; 
      }
      var name = node.getNodeName()
      this.info(name);
            
      if (node.hasAttributes() == true)
      {
        var attrmap = node.getAttributes();
        for( var i = 0; i < attrmap.getLength(); i++)
        {
           var attr = attrmap.item(i);
           var key = attr.getNodeName();
           var val = attr.getNodeValue();
           
           this.info(" " + key + ": " + val); 
           data[key] = val.toString();
        }        
      }     
      
      if (node.hasChildNodes() == true)
      {
        var childmap = node.getChildNodes();
        var collection = false;
        
        if (childmap.getLength() > 1)
        {
           collection = true;
           var nodetype = null;
           var name = null;

           for( var i = 0; i < childmap.getLength(); i++)
           {
             var child = childmap.item(i);
           
             if (i == 0)
             {
               nodetype = child.getNodeType();
               name = child.getNodeName();
             }else {
               if (nodetype != child.getNodeType() || name != child.getNodeName())
               {
                 collection = false;
                 break;
               }
             }
           }
        }        
        
        for( var i = 0; i < childmap.getLength(); i++)
        {
           var child = childmap.item(i);
           
           if(child.getNodeType() == DOMNode.TEXT_NODE || child.getNodeType() == DOMNode.CDATA_SECTION_NODE)
           {
              var text = child.getNodeValue();
              text = text.toString();
              text = text.replace(/'/g, "&#39;");
              text = text.replace(/'/g, "&#34;");
              this.info(" text: " + text);
              
              data.content = text.toString();

           }else {
             
             var parent = null;
             var name = child.getNodeName();
             
             if (collection == true)
             {
               if (i == 0)
               {
                 data[name] = new Array(childmap.getLength());
               }
               data[name][i] = new Object();
               parent = data[name][i];
               
             }else {
               data[name] = new Object();
               parent = data[name];
             }
             
             this._processNode(child, parent);
           }           
        }        
      }
      
      this.debug("App _processNode() - done");       
    },
    
    sendRequest : function(params, callback)
    {
       this.callback = callback;                            
       var hash = $H(params);
              
       this._loadXML(this.url, hash.toQueryString());
    },
    
    getStatus : function ()
    {
      var val = "";
      try
      {
         val = this.data.status.code; 
      }catch (e) { }
      return val;        
    },
    
    isStatusOK : function()
    {
      return this.getStatus() == 200;
    },

    getMessage : function ()
    {
      var val = "";
      try
      {
         val = this.data.status.message.toString(); 
      }catch (e) { }
      return val;        
    },
        
    getUserUID : function ()
    {
      var val = "";
      try
      {
         val = this.data.userinfo.uid; 
      }catch (e) { }
      return val;              
    },

    getUserEmail : function ()
    {
      var val = "";
      try
      {
         val = this.data.userinfo.email.content.toString(); 
      }catch (e) { }
      return val;              
    },
    
    getUsername : function ()
    {
      var val = "";
      try
      {
         val = this.data.userinfo.username.content.toString(); 
      }catch (e) { }
      return val;              
    },
        
    isUserLoggedIn : function()
    {
      if (this.getUserUID() == "")
        return false;
        
      return true;
    },

    getLoginURL : function ()
    {
      var val = "";
      try
      {
        val = this.data.logininfo.login.path.toString();
        if (val != "")
        {
          val += "?back=" + escape(document.location.pathname); 
        } 
      }catch (e) { }
      return val;              
    },
    
    getRegisterURL : function ()
    {
      var val = "";
      try
      {
         val = this.data.logininfo.register.path.toString(); 
      }catch (e) { }
      return val;              
    }
	
  };