/*--
#    Name    : wizard.js
#    Author  : Jerod Santo
#    Contact : "moc.liamg@otnas.dorej".reverse
#    Date    : 2008 August  9
#    About   : Provides wizard-esque web page for forms or whatever
#								To be used in conjunction with Cody Lindley's CSS Step Menu:
#								http://codylindley.com/CSS/325/css-step-menu
#							 Insipered by a similar implementation at:
#								http://worcesterwideweb.com/2007/06/04/jquery-wizard-plugin/
#--*/

jQuery(document).ready(function() {
	
	// initialize the wizard state
	load_state(1);
	
	// loads new state based on button clicked
	jQuery('.previous, .next').click(function() {
	
		//reset the wizardcontent to hidden
	    var current_state = jQuery('#wizard').attr('class');
	    
	    //we only want the number, converted to an int
	    current_state = parseInt(current_state.replace(/(step_)/, ""));
	    
		//figure out if 'next' or 'previous' was clicked and load appropriate state
	    if (jQuery(this).hasClass('next')){ load_state(++current_state); }
	    else if (jQuery(this).hasClass('previous')){ load_state(--current_state); }
	    
	    return false;
  	});
});

function load_state(current_state)
{
	//disable all buttons while loading the state
	jQuery('.previous').hide();
	jQuery('.next').hide();
	
  	//load the content for this state into the wizard content div and fade in
	jQuery('.wizardcontent').hide();
	jQuery('#step_' + current_state).fadeIn('fast');
	
  	//set the wizard class to current state for next iteration
  	jQuery('#wizard').attr('class','step_'+ current_state);
  	var iterator = 1;
  	
  	// the state heading h3. removing is no biggie
  	jQuery('#wizard h3').text("Etape " + current_state);
  	
  	// loop through the list items and set classes for css coloring
  	jQuery('#mainNav li').each(function(){
		var step = jQuery(this)
		
	    if (iterator == current_state){ step.attr('class','current'); }
	    else if (current_state - iterator == 1){ step.attr('class','lastDone'); }
	    else if (current_state - iterator > 1){ step.attr('class','done'); }
	    else{ step.attr('class',''); }
			// special case for step 5 because it doesn't have bacground image
			if (iterator == 6){ step.addClass('mainNavNoBg'); }
	    iterator++;
  	});
  
	//depending on the state, enable the correct buttons
	switch(current_state) {
		case 1:
	  	jQuery('.next').show();
			break;
		case 6:
			jQuery('.previous').show();
			break;
		default:
			jQuery('.previous').show();
			jQuery('.next').show();
	}
}