// all packages need to dojo.provide() _something_, and only one thing
dojo.provide("xenomorph.InputAndPrompt");

// our declared class
dojo.declare("xenomorph.InputAndPrompt",
  [dijit._Widget, dijit._Templated],
  // class properties:
  {
    templatePath: null,
    // Necessary to keep Dijit from using templateString in AccordionPane
    templateString: 
      [ '<div class="Xenomorph_InputAndPrompt BorderRadius05"',
              'dojoAttachPoint="containerNode">',
            '<div class="Xenomorph_InputAndPrompt_Background"',
              'dojoAttachPoint="promptNode"',
              'dojoAttachEvent="onfocus:_Background_onFocus"',
              '>&nbsp;</div>',
            '<input type="text"',
              'dojoAttachPoint="inputNode"',
              'dojoAttachEvent="onblur:_Input_onBlur, onchange:_Input_onChange, onfocus:_Input_onFocus"',
            '/>',
        '</div>'
      ].join(' '),
    
    // src: String
    src: null,
    
    'id':      '',
    'message': '',
    'name':    '',
    'tabindex': '101',
    'type':    'text',
    
    'onChangeHandler': function(){},
    
    //This is called before the widget is created
    //Be warned cant set type to password in IE !!!
    buildRendering: function(){
      
      //Plug in the message - if set
      if( this.message.length > 0 ){
        
        this.templateString = this.templateString.replace( '&nbsp;', this.message );
      }
      
      //Be warned cant set type to password in IE !!!
      if( this.type == 'password' ){
        
        this.templateString = this.templateString.replace( '<input type="text"', '<input type="password"' );
      }
      
      this.inherited("buildRendering",arguments);
    },
    
    //Start the widget.
    'startup': function(){
     
     this.inherited("startup",arguments);
     
     this.startUpLoadEngine();
    },
    
    getValue: function(){
      
      return this.inputNode.value;
    },
    setDisabled: function(){
      dojo.attr( this.inputNode, 'disabled', true );
    },
    setEnabled: function(){
      dojo.attr( this.inputNode, 'disabled', false );
    },
    setOnChangeHandler: function( arg ){
      this.onChangeHandler = arg;
    },
    setValue: function( to ){
      
      this.inputNode.value = to;
      
      this.prompt_toggle( 'input' );
    },
    
    prompt_toggle: function( flag ){
      
      //state : prompt
      if( flag == 'prompt' ){
        
        this.promptNode.innerHTML = this.message;
      }
      
      //state : input
      if( flag == 'input' ){
        
        this.promptNode.innerHTML = '';
      }
    },
    
    startUpLoadEngine: function(){
      
      dojo.attr( this.inputNode, 'tabindex', this.tabindex );
      
      //Wire things up
      if( this.id.length > 0 )
        this.inputNode.id = this.id;
      
      if( this.name.length > 0 )
        this.inputNode.name = this.name;
      
      //this.prompt_toggle('prompt');
    },
    
    //Events
    _Background_onFocus: function(/*Event*/ e){
      
      dojo.stopEvent(e);
      
      //this.prompt_toggle( 'input' );
      this.inputNode.select();
    },
    
    _Input_onBlur: function(/*Event*/ e){
      
      dojo.stopEvent(e);
      
      if( this.inputNode.value.length > 0 ){
        this.prompt_toggle( 'input' );
      }else{
        this.prompt_toggle( 'prompt' );
      }
    },
    
    _Input_onChange: function(/*Event*/ e){
      
      dojo.stopEvent(e);
      if( this.onChangeHandler )
        this.onChangeHandler();
    },
    
    _Input_onFocus: function(/*Event*/ e){
      
      dojo.stopEvent(e);
      
      this.prompt_toggle( 'input' );
    },
    
    statics: { 'counter': 0 }
  }
);
