/*--
#    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(){

  // all content starts hidden
  jQuery('.wizardcontent').hide();
	jQuery('#wizardcontent').hide();
  // initialize the wizard state
  
  
  // loads new state based on button clicked
  jQuery('#wizard :submit').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
    current_state -= 1;
    // Checking if step is okay
    //console.log(steps[current_state-1].submit());
    if (jQuery(this).attr('id') == 'registerNext'){
        if(steps[current_state].submit()){
            jQuery('#wizardcontent').hide();
            nextStep();
        } else
            steps[current_state].error()
    }
    else if (jQuery(this).attr('id') == 'registerPrevious'){ previousStep() }
  });
});

function load_state(current_state){
    document.body.style.cursor = 'wait';
	//disable all buttons while loading the state
	//console.log(current_state);
    jQuery('#registerPrevious').attr("disabled","disabled");
	//jQuery('#registerNext').attr("disabled","disabled")
    if (current_state == 1)
        jQuery('#registerPrevious').hide();
    else
        jQuery('#registerPrevious').show();
  //load the content for this state into the wizard content div and fade in
  //var content = jQuery('#step_' + current_state).html();
  // Next button url, this link will quit the registration process
if (typeof steps[current_state-1].exit != 'undefined' && steps[current_state-1].exit)
    window.location = steps[current_state-1].href;
 // Loading next step
else 
  jQuery.get(
    steps[current_state-1].href,
    function(response){
        jQuery('#wizardcontent').html(response);
        refreshPoints();
        jQuery('#wizardcontent').fadeIn("slow");
        jQuery.unblockUI();
    }
  )
 
  /*jQuery.ajax({
        url: steps[current_state-1].href,
        cache: false,
        async: false,
        success: function(response){
            
        }
    });*/

  var content = steps[current_state-1]
  //console.log(current_state);
  //jQuery('#wizardcontent').html(content);
	//jQuery('#wizardcontent').fadeIn("slow");
  //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("Step " + 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 && iterator < 7){ 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 == 7){ step.addClass('done'); }
    iterator++;
  });
	//depending on the state, enable the correct buttons
	switch(current_state){
		case 1:
	  	jQuery('#registerNext').removeAttr("disabled");
			break;
		case 7:
			jQuery('#registerPrevious').removeAttr("disabled");
			break;
		default:
			jQuery('#registerPrevious').removeAttr("disabled");
			jQuery('#registerNext').removeAttr("disabled");
	}

    // Displaying next button text
    if (typeof steps[current_state-1].nextValue != 'undefined')
        jQuery('#registerNext').text(steps[current_state-1].nextValue);

    // Hiding/Showing skip button
    if (typeof steps[current_state-1].skip != 'undefined' && !steps[current_state-1].skip)
        jQuery('#skip').hide();
     else
        jQuery('#skip').show();

    document.body.style.cursor = 'default';
    jQuery("#skip").css('cursor', 'pointer');
}
