var Util = {
  onload_events: new Array(),
  scroll_time: 800,
  hiding_messages: false,
  
  add_load_event: function(func) {
    Util.onload_events.push(func)
  },
  
  execute_load_events: function() {
    if (Util.onload_events.length == 0) { return }
    for (var i=0; i < Util.onload_events.length; i++) {
      Util.onload_events[i]();
    }    
  },
  
  eventually_hide_messages: function() {
    setTimeout(function() {
      Util.hide_messages()
    }, 5500)
  },
  
  click_body_to_close_messages: function() {
    $("body").click(Util.hide_messages)
  },
  
  hide_messages: function() {
    var messages = $('#messages')
    if (Util.messages_are_visible()) { 
      Util.hiding_messages = true
      messages.slideUp("normal", function() { Util.hiding_messages = false })
    }
  },
  
  messages_are_visible: function() {
    var messages = $('#messages')
    return (messages.is(':visible') && !Util.hiding_messages)
  },
  
  toggle_tooltip: function(name) {
    Util.close_tool_tips(name)
    $("#help_icon_" + name + " div.tool_tip").toggle()
  },
  
  close_tool_tips: function(name) {
    $("a.info:not('#help_icon_" + name + "') div.tool_tip").hide()
  },
  
  copy_common_role: function(row) {
    name = $.trim($(row).parent().find("label").text())
    description = $.trim($(row).parent().find("em").text())
    $("#project_role_name").val(name)
    $("#project_role_description").val(description)
  },
  
  watch_agreement_amount_fields: function() {
    $("input.amount").click(function(evt) {
      // select this radio button
      $(evt.target).parent().find("input[type=radio]").click()
    })
  },
  
  change_agreement_acceptance_arrows: function() {
    $("#accept_go").hide();
    $("#decline_go").hide();
    $("#accept_done").show();
    $("#decline_done").show();
  },
  
  accept_invitation: function() {
    Util.change_agreement_acceptance_arrows();
    $("#decline_invitation").hide();
    $("#accept_invitation").show();
  },
  
  decline_invitation: function() {
    Util.change_agreement_acceptance_arrows();
    $("#accept_invitation").hide();
    $("#decline_invitation").show();
  }

}
$(document).ready(function() {Util.execute_load_events()});

var Former = {
  init: function() {
    Util.add_load_event(Former.make_username_from_name);
    //Util.add_load_event(Former.make_email_from_username);
    
    var name = $("#user_name").val()
    if ($.string(name).blank()) {
      $("#user_login").val(Former.make_one_word(name))
    }
  },
  
  make_username_from_name: function() {
    if ($.string($("#user_login").val()).blank()) {
      Former.watch_and_translate("#user_name", "#user_login", Former.make_one_word)
    }
  },
  
  make_email_from_username: function() {
    if ($.string($("#user_email").val()).blank()) {
      Former.watch_and_translate("#user_login", "#user_email", Former.make_gmail_address)
    }    
  },
  
  watch_and_translate: function(from, to, processor) {
    $(from).keyup(function() {
      $(to).val(processor($(from).val()))
    })
  },
  
  make_one_word: function(text) {
    return text.toLowerCase().replace(/[\W\s]/g, '')
  },

  make_gmail_address: function(text) {
    if (text == '') {
      return ''
    } else {
      return text + "@gmail.com"
    }
  },
  
  submit_google_openid: function() {
    Former.submit_openid_form("https://www.google.com/accounts/o8/id")    
  },
  
  submit_yahoo_openid: function() {
    Former.submit_openid_form("yahoo.com")    
  },
  
  submit_flickr_openid: function() {
    Former.submit_openid_form("flickr.com")    
  },
  
  submit_openid_form: function(url) {
    $("#hide_if_using_other_service").hide()
    $("#openid_identifier").val(url)
    $("#openid_form").submit()
  }
  
  
}




