/*
 * @author stuartb / modified by marc.winter@kaeuferportal.de
 * @date 2008.10.08
 * @description Wizard forms made easy.
 */
jQuery.fn.wizard = function(settings)
{


	settings = jQuery.extend({
		show: function(element) { return true; },
		nextprev: true,
		submitpage: null
	}, settings);

	// hide all but first
	var obj = jQuery(this);

	jQuery('.packs', obj).hide().addClass('question-pack');
	jQuery('.question-pack:first', obj).show();
	jQuery('p.submit', obj).hide();
	settings.show(jQuery('div:first', obj));

	// Prevent form submission on a wizard page
	jQuery('.question-pack', obj).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.nextprev) {
		// Add prev/next step buttons
		jQuery('.question-pack', obj).append('<div class="slide"></div>').children('.slide').append(
			'<a class="prev">Zurück</a>'+
			'<a class="next">Weiter</a>'
		);
		jQuery('.question-pack:first a.prev', obj).hide();
		jQuery('.question-pack input[type=submit]', obj).hide();

		jQuery('.question-pack:last a.next', obj).hide();
		jQuery('.question-pack:last .slide', obj).append('<input type="image" src="/images/smallform/submit_btn.png" style="float:right;"/>');
		//jQuery('.question-pack:last a.next').text('Abschicken');
		//jQuery('.question-pack:last a.next').click(function(){
		//  jQuery(this).parents('form').submit();
		//});

    // prev
		jQuery('.question-pack', obj).children('.slide').children('a.prev').click(function(){
			var wizardpage = jQuery(this).parent().parent();
			wizardpage.hide();
			wizardpage.prev().show();
			settings.show(wizardpage.prev());
		});
		// next
		jQuery('.question-pack:not(:last)', obj).children('.slide').children('a.next').click(function(){
			var wizardpage = jQuery(this).parent().parent();
			wizardpage.hide();
			wizardpage.next().show();
			settings.show(wizardpage.next());
		});

		// checks checked state and goes to the next step
		jQuery('.question-pack', obj).each(function(){
			var this_fieldset  = jQuery(this).children('fieldset');
			var fieldset_count = this_fieldset.size();
			var fieldset_path  = '.request-wizard .packs:visible fieldset '
			var checked_boxes  = 0;

			this_fieldset.each(function(){
				jQuery(this).children().children().children('input:checkbox').click(function(){
					if( (jQuery(fieldset_path+'input:checkbox:checked').size()) > 0 ){
						checked_boxes = 1;
					};
				});
				jQuery(this).children().children().children('input:radio').click(function(){
					checked_status = jQuery(fieldset_path+'input:radio:checked').size();
					if((checked_status+checked_boxes) == fieldset_count){
						jQuery('.packs:visible .slide .next', obj).click();
					};
				});
			});
		});
		// progressbar with 10% added
		var max_fields = jQuery('.packs', obj).size();
		var progress = 0;
		var final_progress = 0;

		jQuery('.question-pack', obj).each(function(){
			progress = progress + (100/max_fields);
			final_progress = progress-(100/max_fields);
			final_progress = Math.ceil(final_progress+10)
			jQuery(this).append(
				'<div class="progress">'+
				'Fortschritt '+final_progress+'%'+
				'<div class="progress-bar">'+
				'<div style="width:'+final_progress+'%"></div>'+
				'</div></div>'
			);
		});
	}
	return jQuery(this);
};
