// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults
function reply_to(comment_id) {
  var comment = jQuery("#comment_"+comment_id);
  var comment_reply = jQuery('.reply', comment);
  comment_reply.slideToggle();
}

function tag_me(tag_name)
{
   // current_tags = document.getElementById('tags');
   // if (current_tags.value.length == 0)
   // {
   //    current_tags.value += tag_name;
   // } else {
   //    if (current_tags.value.indexOf(tag_name) > -1)
   //    {
   //       pattern = new RegExp('([ ,;]*)'+tag_name+'[ ,;]*', 'gi');
   //       current_tags.value = current_tags.value.replace(pattern, '$1');
   //       //alert( current_tags.value.indexOf(tag_name) );
   //    } else {
   //       current_tags.value += ', ' + tag_name;
   //    }
   // }
   
   // function toggleCategory(category, target) {
      var category = tag_name;
      reg    = new RegExp(category);
      com    = new RegExp(category+',');

      // com    = new RegExp(/\,\s\,/);
      endcom = new RegExp(/\,\s$/);
      begcom = new RegExp(/^\,\s/);

      var found;
      var target = $('posting_tag');
      target.value = target.value.replace(',', '')
      var sliced = target.value.split(/\s/)
      sliced.each(function(e,i) {
        if (category == e ) {
        // Found the whole string
          found = true;
          found_string = e;
          sliced[i] = ''
        }
      });
      if (found) {
        replaced = target.value.replace(found_string, '');
        target.value = sliced.join(' ').replace(/\s+/, ' ');
      } else {
        if (target.value == '') {
          target.value = category;
        } else {
          sliced.push(category);
          target.value = sliced.join(' ').replace(/\s+/g, ' ');
        }
      }
    

   
}

//message
function async_message(m, d){message(m, d);}
function messages(m, d){message(m, d);}
function message(message, duration){
    if (duration == undefined){ duration = 3000;}
    if (jQuery.browser.msie) { jQuery("#message").css({position: 'absolute'}); }
    jQuery("#message").text(message).fadeIn(700);
    setTimeout('jQuery("#message").fadeOut(2000)',duration);
    return false;
}

function nuke_link(link, url) {
  if(confirm('Delete Link?')) {
    formatted_link_ajax(link, 'js', 'delete', {url: url}, {async: true, success: function(a){
       jQuery('#link_'+link).fadeOut();
       message('Link deleted');
     }});
  }  
}
function nuke_messages() {
  if ( (jQuery("input:checked").length > 0) && confirm("Delete messages?") ) 
       jQuery("form").submit();
  else
    alert("Nothing selected.");
}

var original_height;
/* Expands viewport on user page in Admin; Tools should be a prototype style $() collection, preferably an array */
function expand(expand_div, tools)
{
  original_height = $(expand_div).style.height;
  $(expand_div).style.height = 'auto';
  tools.each( function(x) { x.toggle(); } );
}

function collapse(collapse_div, tools)
{
  $(collapse_div).style.height = original_height;
  tools.each( function(x) { x.toggle(); } );
}

/* Change to this version number */
function view_version(number)
{
  pattern = /([?&])version=([0-9]+)*/i
  look_at = window.location.search
  match = look_at.match(pattern)
  if (match)
  {
    if (match[2] == number) { return false; }
    else { window.location.href = window.location.href.replace(pattern, "$1version="+number); }
  } else if (look_at.match(/\?/)) {
    window.location.href += '&version='+number;
  } else {
    window.location.href += '?version='+number;
  }
}

function MarkSiplesRequest(id, domain) {
 location.href = "mailto:" + id + "@" + domain;
}