document.observe("dom:loaded", function() {

    var pref_text  = $$('.chart_text');
    var pref_chart = $$('.chart');
    var pref_marker = $$('.marker');
    
  //This section is for ajax pagination
  // the element in which we will observe all clicks and capture
  // ones originating from pagination links
  var container = $(document.body)

  if (container) {
    var img = new Image
    img.src = '/images/spinner.gif'

    function createSpinner() {
      new Element('img', { src: img.src, 'class': 'spinner' })
    }

    container.observe('click', function(e) {
      var el = e.element()
      if (el.match('.pagination a')) {
        el.up('.pagination').insert(createSpinner())
        new Ajax.Request(el.href, { method: 'get' })
        e.stop()
      }
    })
  }
  //ajax pagination section ends
    
    if(pref_text[0] != null) {
    pref_text[0].addClassName('selected');

    pref_text.each(function(pref_text_element, i) {
      pref_text_element.observe('mouseover', function() {
        pref_text.invoke('removeClassName', 'selected');
        pref_text[i].addClassName('selected');
        pref_marker.invoke('removeClassName', 'marker_selected');
        pref_marker[i].addClassName('marker_selected');
      });
    });    
    pref_chart.each(function(pref_chart_element, i) {
      pref_chart_element.observe('mouseover', function() {
        pref_text.invoke('removeClassName', 'selected');
        pref_text[i].addClassName('selected');
        pref_marker.invoke('removeClassName', 'marker_selected');
        pref_marker[i].addClassName('marker_selected');
      });
    });
  }
});

(function ($) {
  $.fn.form = function () {
    return $(this).each(function () {
      $(this).submit(function (evt) {
        $(this).find('.formSubmittable').each(function () {
          var text = $(this).attr('rel');
          $(this).unbind('click');
          $(this).find('input[type=submit], input[type=button]').attr("disabled", "disabled").unbind('click');
          if (text) {
            $(this).find('input[type=submit], input[type=button]').val(text);
            $(this).find('.submit_msg').text(text);
          }
          $(this).parent().addClass('loading');
        });
      });
    });
  };
  
  
  $.fn.setClass = function (klass, bSet) {
    if (bSet) {
      $(this).addClass(klass);
    } else {
      $(this).removeClass(klass);
    }
  }
  
  var intervalId;
  
  function checkTimeout () {
    $.get("/users/check_session_timed_out", function (resp) {
      var sessionExpired = resp.session_expired;
      if (sessionExpired) {
        clearInterval(intervalId);
        window.location.href = window.location.href;
      }
    }, 'json');
  }
  
  function beginTimeoutCheck () {
    // There is a hidden input with id 'current_user' that will be 1 if logged in and 0 if logged out
    // 0 as int resolves to false
    if (parseInt($('#current_user').val(), 10)) {
      intervalId = setInterval(checkTimeout, 3 * 60 * 1000);
    }
  }
  
  $(document).ready(function () {
    beginTimeoutCheck();
    
    $('#welcome_vss').each(function (){
      $(document).bind('reveal.facebox', function () {
        $('#facebox .content .datepicker').datepicker({changeMonth: true, changeYear: true, yearRange: "-100:+1"});
        $('#facebox .content .has-inner-label').hasInnerLabel();

        $('#facebox .content form').submit(function(evt) {
          evt.preventDefault();
          $.post($(this).attr('action'), $(this).serialize(), function (resp) {
            if (resp.error) {
              $('#facebox .content .error_message').text(resp.message);
            } else {
              $('#facebox .content .error_message').text("");
              if ($('#go_to_lsa')[0].checked) {
                window.location.href = '/surveyor/orienteer';
              }
              $(document).trigger('close.facebox');
            }
          }, 'json');
        });

        $('#facebox .content .edit_vss_submit_link').click(function (evt) {
          evt.preventDefault();
          $('#facebox .content form').submit();
        });

        $('#facebox .content .close_win').click(function (evt) {
          evt.preventDefault();
          $(document).trigger('close.facebox');
        });

      });
      $.facebox({ ajax: $(this).attr('href')});
    });
  });
})(jQuery);

