/*
 * @author stuartb
 * @date 2008.10.08
 * @description Wizard forms made easy.
 */
jQuery.fn.wizard = function(settings)
{
    settings = jQuery.extend({
         show: function(element) { return true; },
         prevnext: true,
         submitpage: null
      }, settings);

    // Hide all pages save the first.
    jQuery(this).children(".wizardpage").hide();
    jQuery(this).children(".wizardpage:first").show();
    settings.show(jQuery(this).children(".wizardpage:first"));
    
    // Also highlight the first nav item.
    jQuery(this).children(".wizard-nav").children("a:first").addClass("active");
    
    // Wire progress thingy
    jQuery(this).children(".wizard-nav").children("a").click(function(){
		if(action == "edit" || cmp == "attend_event"){
			var target = jQuery(this).attr("href");
			jQuery(this).parent().parent().children(".wizardpage").hide();
			jQuery(target).fadeIn('slow');
			settings.show(jQuery(target));
			jQuery(this).parent().children('a').removeClass('active', 'slow');
			jQuery(this).addClass('active', 'slow');
		}
        return false;
    });
    
    // Prevent form submission on a wizard page...
    jQuery(this).children(".wizardpage").each(function(i){
        // unless there is a submit button on this page
        if((settings.submitpage == null && jQuery(this).find('input[type="submit"]').length < 1) ||
           (settings.submitpage != null && !$(this).is(settings.submitpage)))
        {
            $(this).find('input,select').keypress(function(event){
                return event.keyCode != 13;
            });
        }
    });
    
    if(settings.prevnext)
    {
        // Add prev/next step buttons
		if(cmp == "add_event"){
			if(action == "edit"){
				jQuery(this).children(".wizardpage")
				.append('<div class="row wizardcontrols"></div>')
				.children(".wizardcontrols")
					//.append('<input type="button" class="wizardprev" value="" id="previous-go" /><img src="../images/spacer.gif" width="3" height="1" /><input type="button" class="wizardnext" value="" id="next-go" /><img src="../images/spacer.gif" width="3" height="1" />'+((location.href.search(/edit/)!=-1)?'<input type="button" class="button" id="update-go" value="" onClick="check_event_validate();" />':'<input type="button" class="button" style="display: none;"  value="" id="save-go" onClick="ajaxFileUpload();" />'));
					.append('<input type="button" style="float:left;" class="wizardprev" value="" id="previous-go" /><img src="../images/spacer.gif" width="3" height="1" />'+((location.href.search(/edit/)!=-1)?'<input type="button" style="display:inline;" class="button" id="update-go" value="" onClick="check_event_validate();" />':'<input type="button" class="button" style="display: none;"  value="" id="save-go" onClick="ajaxFileUpload();" />')+'<img src="../images/spacer.gif" width="3" height="1" /><input type="button" style="display:inline; margin-left: 0pt;" class="wizardnext" value="" id="next-go" />');
			}
			else{
				jQuery(this).children(".wizardpage")
				.append('<div class="row wizardcontrols"></div>')
				.children(".wizardcontrols")
					.append('<input type="button" style="float:left;" class="wizardprev" value="" id="previous-go" /><img src="../images/spacer.gif" width="3" height="1" />'+((location.href.search(/edit/)==-1)?'<input type="button" style="display:inline;" class="button" id="save-go" value="" onClick="check_event_validate();" />':'<input type="button" class="button" style="display: none;"  value="" id="save-go" onClick="ajaxFileUpload();" />')+'<img src="../images/spacer.gif" width="3" height="1" /><input type="button" style="display:inline; margin-left: 0pt;" class="wizardnext" value="" id="next-go" />');
					//.append('<input type="button" class="wizardprev" value="" id="previous-go" /><input type="button" class="wizardnext" value="" id="next-go" />'+((location.href.search(/edit/)==-1)?'<input type="button" class="button" id="save-go" value="" onClick="check_event_validate();" />':'<input type="button" class="button" style="display: none;"  value="" id="save-go" onClick="ajaxFileUpload();" />'));
			}
		}
		else if(cmp == "agent_registration"){
			jQuery(this).children(".wizardpage")
			.append('<div class="row wizardcontrols"></div>')
			.children(".wizardcontrols")
					.append('<input type="button" style="float:left;" class="wizardprev" value="" id="previous-go" /><img src="../images/spacer.gif" width="3" height="1" />'+((location.href.search(/edit/)!=-1)?'<input type="button" style="display:inline;" class="button" id="update-go" value="" onClick="check_validate();" />':'<input type="button" class="button" style="display: none;" value="" id="save-go" onClick="validate2();" />')+'<img src="../images/spacer.gif" width="3" height="1" /><input type="button" style="display:inline;" class="wizardnext" value="" id="next-go" />');
		}
		else if(cmp == "agent_edit_account"){
			jQuery(this).children(".wizardpage")
			.append('<div class="row wizardcontrols"></div>')
			.children(".wizardcontrols")
					.append('<input type="button" style="float:left;" class="wizardprev" value="" id="previous-go" /><img src="../images/spacer.gif" width="3" height="1" />'+((location.href.search(/edit/)!=-1)?'<input type="button" style="display:inline;" class="button" id="save-go" value="" onClick="check_validate();" />':'<input type="button" class="button" style="display: none;" value="" id="save-go" onClick="validate2();" />')+'<img src="../images/spacer.gif" width="3" height="1" /><input type="button" style="display:inline;" class="wizardnext" value="" id="next-go" />');
		}
/*		else if(cmp == "attend_event"){
			jQuery(this).children(".wizardpage")
			.append('<div class="row wizardcontrols"></div>')
			.children(".wizardcontrols")
					.append('<input type="button" style="float:left;" class="wizardprev" value="" id="previous-go" /><img src="../images/spacer.gif" width="3" height="1" />'+((location.href.search(/edit/)!=-1)?'<input type="button" style="display:inline;" class="button" id="save-go" value="" onClick="check_validate();" />':'<input type="button" class="button" style="display: none;" value="" id="save-go" onClick="validate2();" />')+'<img src="../images/spacer.gif" width="3" height="1" /><input type="button" style="display:inline;" class="wizardnext" value="" id="next-go" />');
		}*/
        jQuery('.wizardpage:first input[type="button"].wizardprev').hide(); // hide prev button on first page
		jQuery('.wizardpage:last input[type="button"].button').show(); // hide prev button on first page
        jQuery('.wizardpage:last input[type="button"].wizardnext').hide();  // hide next button on last page
		
        // Wire prev/next step buttons
        jQuery(this).children(".wizardpage")
        .children(".wizardcontrols")
        .children('input[type="button"].wizardprev').click(function(){
            var wizardpage = jQuery(this).parent().parent(); // wizardcontrols div, wizardpage div
            var wizardnav  = wizardpage.parent().children(".wizard-nav")
            
            wizardpage.hide();
            wizardpage.prev().fadeIn(); 
            settings.show(wizardpage.prev());
            
            try{ wizardpage.prev().find("input:first").focus(); } catch(err) {}
            wizardnav.children('a').removeClass('active', 'slow');
            wizardnav.children('a[href="#' + wizardpage.attr('id') + '"]').prev().addClass('active', 'slow');
        });
        jQuery(this).children(".wizardpage")
        .children(".wizardcontrols")
        .children('input[type="button"].wizardnext').click(function(){
				var wizardpage = jQuery(this).parent().parent(); // wizardcontrols div, wizardpage div
				var wizardnav  = wizardpage.parent().children(".wizard-nav")
				if(wizardpage.attr('id')=="FirstPage")
					var msg = validate_package();
				else if(wizardpage.attr('id')=="SecondPage")
					var msg = validate();
				else if(wizardpage.attr('id')=="ThirdPage")
					var msg = validate1();
				else if(wizardpage.attr('id')=="FirstPge")
					var msg = event_validate();
				else if(wizardpage.attr('id')=="SecondPge")
					var msg = event_validate1();
				else if(wizardpage.attr('id')=="ThirdPge")
					var msg = event_validate2();
				else if(wizardpage.attr('id')=="ForthPge")
					var msg = event_validate3();
				else
					var msg=0;

				if(msg == 0){
				wizardpage.hide();
				wizardpage.next().fadeIn();
				settings.show(wizardpage.next());
				
				try{ wizardpage.prev().find("input:first").focus(); } catch(err) {}
				wizardpage.prev().find("input:first").focus();
				wizardnav.children('a').removeClass('active', 'slow');
				wizardnav.children('a[href="#' + wizardpage.attr('id') + '"]').next().addClass('active', 'slow');
			}
        });
    }

    return jQuery(this);
};