var verticalAccordion;

Effect.FadeIn = function(element) {
    element = $(element);
    new Effect.Appear(element, arguments[1] || {});
}

Effect.FadeOut = function(element) {
    element = $(element);
    new Effect.Fade(element, arguments[1] || {});
}

Effect.Fader = function(element) {
    element = $(element);
    if(element.style.display == 'none') {
    new Effect.FadeIn(element, arguments[1] || {}); }
    else { new Effect.FadeOut(element, arguments[1] || {}); }
}



Event.observe(window, 'load', function() {
   hide_spinners();
   load_accordion();
   Event.observe('box1_forward', 'click', box1_forward_action);
   Event.observe('box2_backward', 'click', box2_backward_action);
   Event.observe('box2_forward', 'click', box2_forward_action);
   Event.observe('box3_backward', 'click', box3_backward_action);
   Event.observe('box3_forward', 'click', box3_forward_action);
   Event.observe('box4_backward', 'click', box4_backward_action);
});

function hide_spinners() { $$('div.hideonload').each(function(x) { x.hide(); }); }

function box1_forward_action()  { if(validate_box1()) { load_tests(); load_compare(); show_box(2); } }
function box2_forward_action()  { if(validate_box2()) { compute_rating(); show_box(3); } else { show_missing_test_dialogue(); } }
function box3_forward_action()  { show_box(4); }

function box2_backward_action() { show_box(1); }
function box3_backward_action() { show_box(2); }
function box4_backward_action() { show_box(3); }

function validate_box1()
{
   return true;
}

function validate_box2()
{
    retval = true;
    $$('.result').each(function(x){
	val = $F(x);
	if(val == null || val.length == 0) retval = false;
    });
    return retval;
}

function show_missing_test_dialogue()
{
    alert("Please enter results for each test.");
}

function compute_percentile()
{
   all = ['submit=true'];
   $$('.result').each(function(x) { all.push($(x).id +'=' + $F(x)); });
   $$('.send').each(function(x) { all.push($(x).id +'=' + $F(x)); });
   new Ajax.Updater('percentile_placeholder', '/sparqulator/percentile?' + all.join('&'), {
      onComplete: function(dontcare) { $('percentile_indicator').hide(); new Effect.Appear($('percentile_placeholder')); },
      onLoading: function(dontcare) { $('percentile_indicator').show();  $('percentile_placeholder').hide();  }
   });
}

function compute_rating()
{
   all = ['submit=true'];
   $$('.result').each(function(x) { all.push($(x).id +'=' + $F(x)); });
   $$('.send').each(function(x) { all.push($(x).id +'=' + $F(x)); });
   new Ajax.Updater('rating_placeholder', '/sparqulator/rating?' + all.join('&'), {
      onComplete: function(dontcare) { $('rating_indicator').hide(); $('rating_placeholder').show(); },
      onLoading:  function(dontcare) { $('rating_indicator').show(); $('rating_placeholder').hide(); }
   });
}


function load_compare(a)
{
   new Ajax.Updater('compare_placeholder', '/sparqulator/compare_rating', {
       parameters: { sport: $F('sport') },
       evalScripts: true,
       onComplete: function(dontcare) { $('compare_indicator').hide(); $('compare_placeholder').show(); },
       onLoading:  function(dontcare) { $('compare_indicator').show(); $('compare_placeholder').hide(); }
   });
}

function load_tests(a)
{
   new Ajax.Updater('test_placeholder', '/sparqulator/tests', {
      parameters: { sport: $F('sport'), gender: $F('gender') },
      onComplete: function(dontcare) { 
 	 hide_all_hints();
 	 add_hint_events();
 	 $('test_indicator').hide();
 	 new Effect.SlideDown($('test_placeholder'));
      },
      onLoading: function(dontcare) { $('test_indicator').show();  $('test_placeholder').hide(); }
   });
}

function hide_all_hints()
{
   $$('span.hint').each(function(x) { x.hide(); });
}

function add_hint_events()
{
   $$('.result').each(function(x) {
      Event.observe(x, 'focus', show_hint_for);
      Event.observe(x, 'blur', hide_all_hints);
   });
}

function show_hint_for(x)
{
   /*
     These few lines look bad, sorry
     it's ie's fault
     line 1 - get the true source / target of the event that was fired
     line 2 - go back and find the first (and hopefully only) hint for that item
   */
   ele = (x.target || x.srcElement).parentNode;
   $$('#' + ele.id + ' .hint')[0].show();
}

function load_accordion()
{
      verticalAccordion = new accordion('vertical_container', {
      resizeSpeed: 9,
      classNames : {
         toggle : 'vertical_accordion_toggle',
         toggleActive : 'vertical_accordion_toggle_active',
         content : 'vertical_accordion_content'
      },
      direction: 'vertical',
      defaultSize: { height : null,  width : null }
   });
   show_box(1);
}

function show_box(num) { verticalAccordion.activate($$('#vertical_container .vertical_accordion_toggle')[num-1]); }

function switch_units(url_to_load)
{
   new Ajax.Request(url_to_load, {
      onComplete: function(dontcare) { load_tests(null); },
      onLoading: function(dontcare) { $('test_indicator').show();  $('test_placeholder').hide(); }
   });
}