/**
 * Copyright (C) 2010 Drumbit - www.drumbit.com
 * 
 * This file is part of FL500
 *
 * FL500 is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * FL500 is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with FL500.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */


var DEFAULT_VALUES = {
  'fl_user' : {'txt' : 'username','style' : 'dimmed'},
  'fl_pass' : {'txt' : 'password','style' : 'dimmed','password': true}
};

$(document).ready(function() {

  setDefaultValues();
  $('#form_login').submit(function() {
    removeDefaultValues();
    return true;
  });

  
});

function setDefaultValues() {
  for ( var elem in DEFAULT_VALUES) {
    if (DEFAULT_VALUES[elem].password == true
        && !$('#' + elem + '_hint').get().length) {
      $(
          "<input type='text' id='" + elem + '_hint' + "' rel='"+ elem  + "' class='"+ DEFAULT_VALUES[elem].style + "' />").val(DEFAULT_VALUES[elem].txt)
          .hide().prependTo($('#' + elem).parent());
    }

    if ($('#' + elem).val() == ''
        || $('#' + elem).val() == DEFAULT_VALUES[elem].txt) {
      if (DEFAULT_VALUES[elem].password == true) {
        $('#' + elem).hide();
        $('#' + elem + '_hint').show();
      } else {
    	$('#' + elem).addClass(DEFAULT_VALUES[elem].style);
        $('#' + elem).val(DEFAULT_VALUES[elem].txt)
      }
    }

    $('#' + elem).blur(function() {
      if ($(this).val() == '') {
        if (DEFAULT_VALUES[$(this).attr('id')].password == true) {
          $(this).hide();
          $('#' + $(this).attr('id') + '_hint').show();
        } else {
          $(this).addClass(DEFAULT_VALUES[$(this).attr('id')].style);
          $(this).val(DEFAULT_VALUES[$(this).attr('id')].txt);
        }
      }
    })

    $('#' + elem).focus(function() {
      if ($(this).val() == DEFAULT_VALUES[$(this).attr('id')].txt) {
        $(this).val('');
        $(this).removeClass(DEFAULT_VALUES[$(this).attr('id')].style);
      }
    })

    $('#' + elem + '_hint').focus(function() {
      try{
    	  $('#' + $(this).attr('rel')).show().focus();
      }catch(e){alert(e)}
    	
      $(this).hide();
    })
  }
}


function removeDefaultValues() {
  for ( var elem in DEFAULT_VALUES) {
    if ($('#' + elem).val() == DEFAULT_VALUES[elem].txt)
      $('#' + elem).val('')
  }
}


