// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  }
  else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}


// Perform first_visit_check() on each page load
// commented out by atmos at the request of steve on november 10th, 2007
// initial page load shit they don't want anymore
// addLoadEvent(first_visit_check());
////////////////////////////////////////////////

var layout_rules = {
  'a.message_dismisser' : function(e) {
    e.onclick = function() {
      $(e.getAttribute('div_id')).hide();
    }
  }
};

function queryParameters(string)
{

  var pairs = String(string || document.location).split(/\?/)[1].split(/\&/);

  var params = new Object();
  for (i=0; i<pairs.length; i++){
    [key, value] = pairs[i].split('=');
    params[decodeURIComponent(key)] = decodeURIComponent(value);
  }

  return params;
}

function first_visit_check()
{
  var cookie_id   = 'first_visit_on';
  var fv_seconds  = parseInt(get_cookie(cookie_id));
  var redirect_to = '/first_visit';
  var path        = '/';

  if(isNaN(fv_seconds)){ /* No cookie */

    fv_cookie    = parseInt((new Date()).getTime() / 1000);  /* getTime() returns MILLIseconds since the Epoch */
    life_in_days = 999999;                                   /* days, or 2737 years */

    set_cookie(cookie_id, fv_cookie, life_in_days, path);

    if (document.location.pathname == '/'){                  /* Only perform the redirect if we're on the front page */
      document.location = redirect_to;
    }
  }
}

function url_for(options)
{
  new_params = params;
  url_args = {};

  for (var key in options){
    new_params[key] = options[key];
  }

  for (var key in new_params){
    if(key != 'controller' && key != 'action' && new_params[key] != ''){
      url_args[key] = new_params[key];
    }
  }

  query_string = '/' + new_params['controller'] + '/' + new_params['action'];
  query_string += '?' + $H(url_args).toQueryString();

  return query_string;
}



document.getElementsByAttribute = function(attribute, value, tagName, parentElement) {
  var children = ($(parentElement) || document.body).getElementsByTagName((tagName || '*'));
  return $A(children).inject([], function(elements, child) {
      var attributeValue = child.getAttribute(attribute);
      if(attributeValue != null) {
        if(!value || attributeValue == value) {
          elements.push(child);
        }
      }
      return elements;
    });
};

function toggle_link(link)
{
  alternate_text = link.getAttribute('alternate_text');
  link.setAttribute('alternate_text', link.innerHTML);
  link.innerHTML = alternate_text;
}

function limit_input_words(field, counter, limit) {

  if (field.value.charAt(field.value.length - 1) == ' '){

    var words = field.value.replace(/ +/g, ' ').split(' ');

    if(counter) {
      counter.innerHTML = (limit - words.length) + " words left";
    }
  } else {
    return;
  }
}

function element_location(e, relative){
  var x = 0, y = 0;
  var e_copy = e;

  while(e_copy.parentNode){
    x +=  e_copy.offsetLeft ? e_copy.offsetLeft : 0;
    y += e_copy.offsetTop ? e_copy.offsetTop : 0;
    if(e_copy == document.body || relative === false) { break; }
    e_copy = e_copy.parentNode;
  }

  return [x,y];
}

function get_cookie(name)
{
  var search = name + "=";
  var cookie_string = document.cookie;
  var result = null;

  if (cookie_string.length > 0) {
    offset = cookie_string.indexOf(search);
    if (offset != -1) {
      offset += search.length;
      end = cookie_string.indexOf(";", offset);
      if (end == -1) {
        end = cookie_string.length;
      }
      result = unescape(cookie_string.substring(offset, end));
    }
  }
  return result;
}


/* lifespan is in days */
function set_cookie (name, value, lifespan, access_path)
{
  var cookietext = name + "=" + escape(value);

  if (lifespan != null) {
    var today=new Date();
    var expiredate = new Date();
    expiredate.setTime(today.getTime() + (1000 * 60 * 60 * 24 * lifespan));
    cookietext += "; expires=" + expiredate.toGMTString();
  }
  if (access_path != null) {
    cookietext += "; PATH=" + access_path;
  }
  document.cookie = cookietext;
  return get_cookie(name);
}

function delete_cookie(name, path) {
  set_cookie(name, "deleted", -1, path)
}
