function add_event(e, t, f) {
  if (typeof e[t] != 'function') {
    e[t] = f;
  } else {
    var old = e[t];
    e[t] = function() {
      if (old) {
        old();
      }
      f();
    }
  }
}

function add_onload(f) {
  add_event(window, 'onload', f);
}

function move_cursor(e, p) {
  if(e.createTextRange) {
    var range = e.createTextRange();
    range.move('character', p);
    range.select();
  } else {
    if(e.selectionStart) {
      e.focus();
      e.setSelectionRange(p, p);
    } else
    e.focus();
  }
}

function key_code(e) {
  return e?e.which:event.keyCode;
}

move_focus = function(e) {
  e.focus();
  if (e.value) move_cursor(e, e.value.length);
}

var modal_close_cbs = [];
var modal_open_cbs = [];

function clear_modal_cbs() {
  modal_close_cbs = [];
  modal_open_cbs = [];
}

function add_modal_cb(f, ar) {
  if (typeof f != 'function') {
    f = new Function(f);
  }
  ar[ar.length] = f;
}

function add_modal_open_cb(f) {
  add_modal_cb(f, modal_open_cbs);
}

function add_modal_close_cb(f) {
  add_modal_cb(f, modal_close_cbs);
}

function do_modal_cb(ar) {
  for (var i=0; i<ar.length; i++) {
    ar[i]();
  }
}

function do_modal_close_cb() {
  do_modal_cb(modal_close_cbs);
}

function do_modal_open_cb() {
  do_modal_cb(modal_open_cbs);
}

function modal_close() {
  new Effect.Shrink('modal_box', {queue:'end', duration: 0.5});
  new Effect.toggle('modal_bg', 'appear', {queue:'end', duration: 0.5, afterFinish: do_modal_close_cb });
}

function modal_open(f) {
  if (typeof f != 'function') {
    f = new Function(f);
  }
  new Effect.toggle('modal_bg', 'appear', {queue:'end', duration: 0.5});
  new Effect.Grow('modal_box', {queue:'end', duration: 0.5, afterFinish: do_modal_open_cb });
}

function modal(content, cb_open, cb_close) {
  clear_modal_cbs();
  add_modal_open_cb(cb_open);
  add_modal_close_cb(cb_close);
  add_modal_close_cb(function() { move_focus(document.activeElement); });
  new Ajax.Updater('modal_content', content, {
    method:'get'
    ,evalScripts:true
    ,onComplete: modal_open()
  });
}

function get_target(e) {
  e = e ? e : window.event;
  return (typeof e.target != 'undefined') ? e.target : e.srcElement;
}

function get_style(e, s) {
 if (e.currentStyle)
  return e.currentStyle[s]
 else if (document.defaultView && document.defaultView.getComputedStyle)
  return document.defaultView.getComputedStyle(e, '')[s]
 else
  return e.style[s]
}

Number.prototype.toHex = function () {var o = this.toString(16);return o[1] ? o : "0" + o;}

function rgb2hex(s) {
  return s.replace(/rgb\((\d+)\,\s(\d+)\,\s(\d+)\)/g, function (a, b, c, d) {return "#" + parseInt(b).toHex() + parseInt(c).toHex() + parseInt(d).toHex();});
}

function get_bg_color(e) {
  return rgb2hex(get_style(e, 'backgroundColor'));
}

function get_parent(e, n) {
  for (var i=0; i<n; i++) {
    e = e.parentNode;
  }
  return e;
}

function hilite(e, color, q, loc) {
  var bg_save = get_bg_color(e);
  if (bg_save == color) {
    var que = Effect.Queues.get(q);
    que.each(function(effect) { effect.cancel(); });
  }
  $(e).highlight({
    startcolor:    bg_save
    ,endcolor:     color
    ,restorecolor: color
    ,queue: {
      position: loc
      ,limit: 2
      ,scope: q
    }
  });
}

function auto_hilite(e, newbg, n, q) {
  var c = get_parent(e, n);
  var bg = get_bg_color(c);
  add_event(e, 'onfocus', function(e) {
    hilite(c, newbg, q, 'front');
  });
  add_event(e, 'onblur', function(e) {
    hilite(c, bg, q, 'end');
  });
}

function auto_hilite_by_class(c, newbg, n, q) {
  if (!q) q='';
  var e = document.getElementsByClassName(c);

  for (var i=0; i<e.length; i++) {
    var f = e[i].ancestors().collect(function(s) { if (s.tagName == 'FORM') return s; }).compact()[0];
    auto_hilite(e[i], newbg, n, q + f.id);
  }
}

function fade(e, f, t, q, loc) {
  var que = Effect.Queues.get(q);
  que.each(function(effect) { effect.cancel(); });
  new Effect.Opacity(e, {
    from: e.getOpacity()
    ,to: t
    ,queue: {
      position: loc
      ,scope: q
    }
  });
}

function auto_fade(e, f, t, q) {
  if (!$(e.htmlFor).checked) {
    e.setOpacity(f);
  } else {
    e.setOpacity(t);
  }
  add_event(e, 'onmouseover', function(z) {
    var form = $('buildit');
    var els = form.getInputs('radio', $(e.htmlFor).name);
    for (var i=0; i<els.length; i++) {
      el = els[i];
      if (!el.checked) {
        fade(el.parentNode.parentNode, t, f, q + 'foo', 'end');
      }
    }
    fade(e, f, t, q, 'end');
  });
  add_event(e, 'onmouseout', function(z) {
    if (!$(e.htmlFor).checked) {
      fade(e, t, f, q, 'end');
    }
  });
}

function single_auto_fade(e, f, t) {
  for (var i=0; i<e.length; i++) {
    var el = $(e[i]);
    auto_fade(el, f, t, el);
  }
}

function mouseover_fade_by_class(c, f, t) {
//  var e = $$(c);
  var e = document.getElementsByClassName(c);

  for (var i=0; i<e.length; i++) {
    auto_fade(e[i], f, t, c + i);
  }
}

function on_element_focused(e) {
  if (e && e.target)
    document.activeElement = e.target == document ? null : e.target;
}

function add_form_modal(e, s) {
  add_event(e, 'onsubmit', function(ev) {
    modal(s);
    return false;
  });
}

function add_modal(e, s) {
  add_event(e, 'onclick', function(ev) {
    modal(s);
    return false;
  });
}

function auto_modal(c, p) {
  var s = document.getElementsByClassName(c);

  for (var i=0; i<s.length; i++) {
    add_modal(s[i], p + s[i].href.match(/\/([^\/#]+)(?:#[^\/]+)?$/i)[1]);
  }
}

function auto_buildit(c) {
  auto_modal(c, 'other/modal-build-');
}

function auto_contact(c) {
  auto_modal(c, 'other/modal-contact-');
}

function auto_quote(c) {
  auto_modal(c, 'other/modal-quote-');
}

function auto_buy(c) {
  auto_modal(c, 'other/modal-buy-');
}

function auto_search_load(c) {
  var s = document.getElementsByClassName(c);

  for (var i=0; i<s.length; i++) {
    add_form_modal(s[i], 'other/modal-search.php');
  }
}

show_form_el =  function(e, c) {
  new Effect.toggle(e, 'appear', {queue:'end', duration: 0.1});

  var s = document.getElementsByClassName(c);

  for (var i=0; i<s.length; i++) {
    new Effect.toggle(s[i], 'appear', {queue:'end', duration: 0.1});
  }
}

add_onload(function() {
//  add_event(document, 'focus', on_element_focused);

  auto_hilite_by_class('auto_hilite_2', '#ffff99', 2);
  auto_hilite_by_class('auto_hilite_3', '#ffff99', 3);

  auto_search_load('modal_search');
  auto_quote('modal_quote');
  auto_buy('modal_buy');
  auto_buildit('modal_build');
  auto_contact('modal_contact');
});

