/* Created by jankoatwarpspeed.com */

(function(jQuery) {
    jQuery.fn.formToWizard = function(options) {
        options = jQuery.extend({  
            submitButton: "" 
        }, options); 
        
        var element = this;

        var steps = jQuery(element).find("fieldset");
        var count = steps.size();
        var submmitButtonName = "#" + options.submitButton;
        jQuery(submmitButtonName).hide();

		var stepNames = new Array('<p>1. Introduction</p>','<p>2. SDK</p><p>Selection</p>','<p>3. Profiles &</p><p>Container Selection</p>','<p>4. Additional</p><p>Information</p>','<p>5. Contact</p><p>Information</p>');

        // 2
        jQuery(element).before("<ul id='steps'></ul>").append("<a href='javascript:closeLicensingForm();' class='step_close'>&nbsp;</a>");

        steps.each(function(i) {
            jQuery(this).wrap("<div id='step" + i + "'></div>");
            jQuery(this).append("<p class='step_pagination' id='step" + i + "commands'></p>");

            // 2
            var name = jQuery(this).find("legend").html();
            jQuery("#steps").append("<li id='stepDesc" + i + "'><span>" + name + "</span><a href='#' onclick=\"_gaq.push(['_trackPageview', '/Licensing/Tab_" + (i+1) + ".html']);\" class='realStepSelect' id='realStepSelect-" + i + "'>" + stepNames[i] + "</a></li>");

            if (i == 0) {
                createNextButton(i);
				createNext2Button(i);
                selectStep(i);
            }
            else if (i == count - 1) {
                jQuery("#step" + i).hide();
                createPrevButton(i);
				createNext2Button(i);
            }
            else {
                jQuery("#step" + i).hide();
                createPrevButton(i);
                createNextButton(i);
				createNext2Button(i);
            }
        });

        function createPrevButton(i) {
            var stepName = "step" + i;
            jQuery("#" + stepName + "commands").append("<a href='#' onclick=\"_gaq.push(['_trackPageview', '/Licensing/Tab_" + i + ".html']);\" id='" + stepName + "Prev' class='prev'>&nbsp;</a>");

            jQuery("#" + stepName + "Prev").bind("click", function(e) {
                jQuery("#" + stepName).hide();
                jQuery("#step" + (i - 1)).show();
                jQuery(submmitButtonName).hide();
                selectStep(i - 1);
				return false;
            });
        }

        function createNextButton(i) {
            var stepName = "step" + i;
            jQuery("#" + stepName + "commands").append("<a href='#' onclick=\"_gaq.push(['_trackPageview', '/Licensing/Tab_" + (i+2) + ".html']);\" id='" + stepName + "Next' class='next'>&nbsp;</a>");

            jQuery("#" + stepName + "Next").bind("click", function(e) {
                jQuery("#" + stepName).hide();
                jQuery("#step" + (i + 1)).show();
                if (i + 2 == count)
                    jQuery(submmitButtonName).show();
                selectStep(i + 1);
				return false;
            });

        }

        function createNext2Button(i) {
            var stepName = "step" + i;

			jQuery("#" + stepName + "commands").append("<a href='#' onclick=\"_gaq.push(['_trackPageview', '/Licensing/Tab_" + (i+2) + ".html']);\" id='" + stepName + "Next2' class='step_next_bottom'>&nbsp;</a>");

            jQuery("#" + stepName + "Next2").bind("click", function(e) {
                jQuery("#" + stepName).hide();
                jQuery("#step" + (i + 1)).show();
                if (i + 2 == count)
                    jQuery(submmitButtonName).show();
                selectStep(i + 1);
				return false;
            });
      }

        function selectStep(i) {
            jQuery("#steps li").removeClass("current");
            jQuery("#stepDesc" + i).addClass("current");
        }

		
		jQuery(".realStepSelect").bind("click", function(e) {
			var i = jQuery('#steps .current').attr('id');
			var i = i.replace('stepDesc','');
			
			var stepName = "step" + i;
			
			var si = jQuery(this).attr('id');
			si = si.split('-');
			si = si[1];
			
			jQuery("#" + stepName).hide();
			jQuery("#step" + (si)).show();
			if (si + 2 == count)
				jQuery(submmitButtonName).show();
			selectStep(si);
			return false;
		});
		


    }
})(jQuery); 
