// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults
/* Links */
function externalLinksNewWindow() {
  var links = $$('a[href^=http://], a[href^=https://]');
  for(var i=0; i < links.length; i++) {
    links[i].writeAttribute('target', '_blank'); // Open external links in new window by setting target attribute
  }
}

function externalFormsNewWindow() {
  var forms = $$('form[action^=http://], form[action^=https://]');
  for(var i=0; i < forms.length; i++) {
    forms[i].writeAttribute('target', '_blank'); // Open external forms in new window by setting target attribute
  }
}

function replaceEmailLinks() {
  $$('a.email').each(function(element){
    var link = element.innerHTML;  //Links should be formatted just like this: name AT domain DOT com
    link = link.replace(/ AT /, '@'); 
    link = link.replace(/ DOT /, '.');
    element.writeAttribute('href', 'mailto:' + link);
    element.update(link);
  });
}

// Used to hide submit buttons, inputs, etc.
function hideNonJavascriptElements() {
  $$('.js-hide').each(function(element){
    element.hide();
  });
}

// Used to show javascript specific instructions
function showJavascriptElements() {
  $$('.js-show').each(function(element){
    // show() won't override styles set in a stylesheet, so we use this
    element.style.display = 'block';
  });
}

// Sequence Number Form submission
function submitForm(form_id){
  $(form_id).request({
    onFailure: function() { 
      $('messages').show();
      $('messages').update('Error updating sequence order.'); 
      $('messages').fade({duration: 2.0});
    },
    onSuccess: function(t) {
      $('messages').show();
      $('messages').update('Sequence updated!'); 
      $('messages').fade({duration: 2.0});
    }
  });
};

Event.observe(window, 'load', function() { 
  externalLinksNewWindow();
  externalFormsNewWindow();
  hideNonJavascriptElements();
  showJavascriptElements();
});

// Google Analytics Code
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-8063602-41']);
_gaq.push(['_trackPageview']);
(function() {
  var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
  ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
  (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(ga);
})();
