// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults
/* Ported from  http://api.rubyonrails.com/classes/ActionView/Helpers/TextHelper.html#M000514  */
function auto_link(s){   
  var hlink = /(<\w+.*?>|[^=!:'"\/]|^)((?:http[s]?:\/\/)|(?:www\.))(([\w]+:?[=?&\/.-]?)*\w+[\/]?(?:\#\w*)?)([[:punct:]]|\s|<|$)/
  
  return s.replace(hlink, function (all, a, b, c, not_used, d) {
    if(a.match(/<a\s/i)){
      return all
    }
    else{
      var text = b + c
      if(b == "www."){
        var host = "http://www."
      }
      else{
        var host = b
      }
      return a + "<a href=\"" + host + c + "\">" + text + "</a>" + d
    }
  })
}

function scroll_to_bottom(element){
  $(element).scrollTop = $(element).scrollHeight
}

function scroll_and_focus(){
  scroll_to_bottom('chat_scroll_div')
  $('message_body').focus()
}

function add_message(name){
  new Insertion.Bottom(
    'message_table',
    "<tr>" +
    " <td>" +
    "   <blockquote>" +
    "		  " + auto_link($('message_body').value) +
    "			<cite>" +
    "       " + name +
    "			  <span class='message_time'>" +
    "			    at " + new Date().toString() +
    "			  </span>" +
    "			</cite>" +
    "		</blockquote>" +
    "	</td>" +
    "</tr>"
  )
  scroll_to_bottom('chat_scroll_div')
}