
var Links2Go = {
    gradesbyindex : [7, 8, 9, 10, 11, 12],
    gradetagsbyindex : ['7th_grade', '8th_grade', '9th_grade', '10th_grade', '11th_grade', '12th_grade'],
    gradebuttons : [],
    selected_button : '',
    selected_grade : 0,
    
    check_field_incase_of_backbutton : ''
};


Links2Go.initialize = function() {
    var gradebuttons = $$('#chooseyourgradelist .gradechoice a');
    
    gradebuttons.each(function(gradebutton) {
        gradebutton.observe('click', Links2Go.handle_step_one_grade_link_click.bindAsEventListener(gradebutton));
    });
    
    Links2Go.gradebuttons = gradebuttons;
    
    // incase they hit the back button
    Links2Go.check_field_incase_of_backbutton = new PeriodicalExecuter(Links2Go.check_button_highlight, 0.3);
}

/*
* check_button_highlight()
* in case of back button press
*/
Links2Go.check_button_highlight = function() {

    if (Links2Go && Links2Go.gradetagsbyindex && !Links2Go.selected_button && $('gradefield') && $('gradefield').value) {
        var i = Links2Go.gradetagsbyindex.indexOf($('gradefield').value);
        
        if (i < 0) {
            $('gradefield').value = '';
            return;
        }
        
        Links2Go.select_grade_button(Links2Go.gradebuttons[i]);
        
        Links2Go.check_field_incase_of_backbutton.stop();
    }
}


/**
* Methods related to step one.
*/

/*
* select_grade_button(gradebutton)
*/
Links2Go.select_grade_button = function(gradebutton) {

    if (gradebutton.hasClassName('selected')) {
        Links2Go.selected_grade = 0;
        
        gradebutton.removeClassName('selected');
        
        Links2Go.selected_button = '';
        $('gradefield').value    = '';
    }
    else {
        
        Links2Go.gradebuttons.each(function(gb) {
            gb.removeClassName('selected');
        });
        
        gradebutton.addClassName('selected');
        
        Links2Go.selected_button = gradebutton;
        
        var index = Links2Go.gradebuttons.indexOf(gradebutton);
        
        Links2Go.selected_grade = Links2Go.gradesbyindex[index];
        
        $('gradefield').value = Links2Go.gradetagsbyindex[index];
    }
}

/*
* handle_step_one_grade_link_click(e)
*/
Links2Go.handle_step_one_grade_link_click = function(e) {

    Event.stop(e);
    
    Links2Go.select_grade_button(this);
}



Event.observe(window, 'load', Links2Go.initialize);





