/* SELECTION TOOL JAVASCRIPT */

function switchStep(stepNum) {
    // get step elements
    var currentStep = $('.step_active');
    var newStep = $('#step' + stepNum);
    
    // switch off current step and turn on current step at same time
    currentStep.animate({
        'width': '60px'
    }, 100, function() {
        currentStep.addClass('step_inactive').removeClass('step_active');
        currentStep.find('.step_form').hide();
        $('.step .handle').each(function() {
            var headtext = $(this).find('a');
            headtext.remove();
            $(this).append(headtext.text());
        });
        for(var i = stepNum - 1; i > 0; i--) {
            $('#step' + i + ' .handle').wrapInner(document.createElement('a'));
            $('#step' + i + ' .handle a').attr('href','javascript:void();').click(function() {
                switchStep(parseInt($(this).parents('li').attr('id').replace('step','')));
                return false;
            });
        }
    });
    newStep.animate({
        'width': '400px'
    }, 100, function() {
        newStep.addClass('step_active').removeClass('step_inactive');
        newStep.find('.step_form').show();
    });
}

function initialize() {
    // activate first step
    var currentStep = 1;
    $('.step').addClass('step_inactive');
    switchStep(currentStep);
    
    // resize all steps to match largest height
    var max_height = 0;
    $('.step').each(function() {
        if(max_height < $(this).height()) {
            max_height = $(this).height();
        }
    });
    if(max_height % 2 == 1) { // because IE6 doesn't like it when you absolutely position an element inside a container of odd width/height
        max_height += 1;
    }
    $('.step').height(max_height);
    $('.handle').css('top', Math.floor((max_height - $('.handle').height()) / 2));
    $('.step_form').each(function() {
        $(this).css('top', Math.floor((max_height - $(this).height()) / 2))
    });
    $('.step_form').hide();
    $('#steps').css('visibility','visible');
    
    // add handlers to buttons
    $('p.step_next button').click(function() {
        /* special validation for selection tool */
        if(($(this).parents('#step1').length > 0 &&	$(this).parents('#step1').find('input:radio:checked').length > 0) ||
           ($(this).parents('#step2').length > 0 && $(this).parents('#step2').find('input:radio:checked').length > 0) ||
           ($(this).parents('#step3').length > 0 && $(this).parents('#step3').find('input:radio:checked').length > 0)) {
            switchStep(parseInt($(this).parents('li').attr('id').replace('step','')) + 1);
        } else {
            $(this).siblings('.error').remove();
            $(this).after($(document.createElement('p')).addClass('error').text('Please select an option before continuing.'));
        }
        return false;
    });
    $('p.step_finish button').click(function() {
        if($(this).parents('#step4').length > 0 && $(this).parents('#step4').find('input:check:checked').length == 0) {
            $(this).siblings('.error').remove();
            $(this).after($(document.createElement('p')).addClass('error').text('Please select an option before continuing.'));
            return false;
        } else {
            $(this).siblings('.error').remove();
            return true;
        }
    });
}

function newSearch() {
    $('#results_summary').fadeOut('fast', function() {
        $('#steps').fadeIn('fast');
    });
    return false;
}


/*
function initialize() {
    var current_step = 1;
    var max_height = 0;
    $('.step_form').each(function() {
        if($(this).height() > max_height) {
            max_height = $(this).height();
        }
    });
    $('.step_form').each(function() {
        $(this).css('margin-top', Math.ceil((max_height - $(this).height()) / 2) + 'px');
        $(this).css('margin-bottom', Math.ceil((max_height - $(this).height()) / 2) + 'px');
    });
    $('#steps .step_content h3').css('top', Math.ceil((max_height - 42) / 2) + 'px');
    $('.step_content').height(max_height);
    $('#steps .step_form').hide();
    switchStep(1);
    $('#steps').css('visibility','visible');
    $('p.step_next button').click(function() {
        // special validation for selection tool
        if(($(this).parents('#step1').length > 0 &&	$(this).parents('#step1').find('input:radio:checked').length > 0) ||
           ($(this).parents('#step2').length > 0 && $(this).parents('#step2').find('input:radio:checked').length > 0) ||
           ($(this).parents('#step3').length > 0 && $(this).parents('#step3').find('input:radio:checked').length > 0)) {
            switchStep(parseInt($(this).parents('li').attr('id').replace('step','')) + 1);
        } else {
            $(this).siblings('.error').remove();
            $(this).after($(document.createElement('p')).addClass('error').text('Please select an option before continuing.'));
        }
        return false;
    });
    $('p.step_finish button').click(function() {
        if($(this).parents('#step4').length > 0 && $(this).parents('#step4').find('input:check:checked').length == 0) {
            $(this).siblings('.error').remove();
            $(this).after($(document.createElement('p')).addClass('error').text('Please select an option before continuing.'));
            return false;
        } else {
            $(this).siblings('.error').remove();
            return true;
        }
    });
}

function switchStep(step) {
    $('.step_active').animate({
        'width': '70px'
    }, 'fast', function() {
        $(this).removeClass('step_active').addClass('step_inactive')
        $(this).find('.step_form').hide();
    });
    $('#step' + step).removeClass('step_inactive').addClass('step_active').width(70).animate({
        'width': '410px'
    }, 'fast', function() {
        $('#step' + step + ' .step_form').show();
    });

    $('#steps li h3').each(function() {
        var headtext = $(this).find('a');
        headtext.remove();
        $(this).append(headtext.html());
    });
    for(var i = step - 1; i > 0; i--) {
        $('#step' + i + ' h3').wrapInner(document.createElement('a'));
        $('#step' + i + ' h3 a').attr('href','javascript:void();').click(function() {
            switchStep(parseInt($(this).parents('li').attr('id').replace('step','')));
            return false;
        });
    }
}

function newSearch() {
    $('#results_summary').fadeOut('fast', function() {
        $('#steps').fadeIn('fast');
    });
    return false;
}

*/