$(function(){
  var _inputTypeSwapper = function(opts){
    var obj      = this;
    var defaults = {
      usernameDefaultText: "username",
      passwordDefaultText: "password"
    }; 
    
    // merge options with defaults
    obj.opts = $.extend({}, defaults, opts);       
    
    this.user_toggle = function(node){
      if(node.attr('value').length > 0 && node.attr('value') == obj.opts.usernameDefaultText) node.attr('value', '');
      else if(node.attr('value').length == 0) node.attr('value', obj.opts.usernameDefaultText);
    }
    
    this.init_type_swap = function(userNode, passNode){
      this.swap = function(s_node, h_node){
      
        h_node.hide();
        s_node.show();    
      }
      var id       = passNode.attr('id');
      var txtNode = $('#' + id + '_text');
  
      if(txtNode.length == 0){
        if(!$.browser.msie){
          txtNode = passNode.clone()
                            .attr("type", "text")
                            .attr("id", passNode.attr('id') + "_text")
                            .attr("name", passNode.attr('name') + "_text")
                            .val(obj.opts.passwordDefaultText).insertAfter(passNode);
        
        // workaround for ie only-set-it-once rule for input type attribute
        }else{
          txtNode = $(document.createElement("input")).attr({
            "type": 'text',
            "id": passNode.attr('id') + "_text",
            "name": passNode.attr('name') + "_text",
            "class": passNode.attr('class'),
            "value": obj.opts.passwordDefaultText
          });
          
          txtNode = txtNode.insertBefore(passNode);
        }
      }
      
      passNode.hide();
      txtNode.show();
      
      txtNode.focus(function(){
        obj.swap(passNode, $(this));
        passNode.focus();
      });
      
      passNode.blur(function(){
        // don't swap if there's a value
        if($(this).attr('value').length > 0) return false;
        obj.swap(txtNode, $(this));
      });
      
      // bind execution events
      userNode.focusin( function(e){ obj.user_toggle($(e.target)); });    
      userNode.focusout( function(e){ obj.user_toggle($(e.target)); }); 
  
      // fixes asp.net SFA mess, so when pressing enter the form autosubmits
      var loginToolSubmit = function(e) {
        var user = $("input.login_username").val();
        var pass = $("input.login_password").val();
        
        if (e.keyCode == 13) {
          if (user.length > 0 && pass.length > 0) {
            e.preventDefault();
            e.stopPropagation();
            $("input.login_submit").click();
          } else {
            alert("Devi inserire le credenziali per accedere all'area riservata.");
          }
          
          return false;
        }
        
        return true;
      };
      
      /* search form */
      $("input.login_username").keypress(loginToolSubmit);
      $("input.login_password").keypress(loginToolSubmit);   
    }

    // automatize first swap
    obj.init_type_swap($("input.login_username"), $("input.login_password"));
    
  };
  
  // extends jquery
  $.extend({
    inputTypeSwapper: function(opts){
      var o = new _inputTypeSwapper(opts);
    }
  });
});

