/* Created by jankoatwarpspeed.com */

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

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

        // 2
        $(element).before("<ul id='steps'></ul>");

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

            // 2
            var name = $(this).find("legend").html();
            $("#steps").append("<li id='stepDesc" + i + "'>Шаг " + (i + 1) + "<span>" + name + "</span></li>");

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

        function createPrevButton(i) {
            var stepName = "step" + i;
            $("#" + stepName + "commands").append("<a href='javascript:void(0)' id='" + stepName + "Prev' class='prev'>< Назад</a>");

            $("#" + stepName + "Prev").bind("click", function(e) {
                $("#" + stepName).hide();
                $("#stepDesc" + (i - 1)).animate({opacity: "1"}, 200);
                $("#step" + (i - 1)).show();
                $(submmitButtonName).hide();
                selectStep(i - 1);
                return false;
            });
        }

        function createNextButton(i) {
            var stepName = "step" + i;
            $("#" + stepName + "commands").append("<a href='javascript:void(0)' id='" + stepName + "Next' class='next'>Вперед ></a>");

            $("#" + stepName + "Next").bind("click", function(e) {
                $("#" + stepName).hide();
                $("#stepDesc" + i).animate({opacity: "0.5"}, 200);
                $("#step" + (i + 1)).show();
                if (i + 2 == count)
                    $(submmitButtonName).show();
				if ( i == 0 ) {
					if ( $('input[name=WinXP]:checked').length && $('input[name=Win7]:not(:checked)').length ) {
						$('input[name=x64]').parent().hide();
						setTimeout(function(){$('input[name=x32]').parent().find('a.checkbox-select').click();},10);
					} else {
						$('input[name=x64]').parent().show();
						$('input[name=x32]').parent().find('a.checkbox-deselect').click();
					}
				}
				if ( i == 2 ) {
					downloadProgressBar(0);
					var filesIndexToDownload = uTrnt.getFilesIndexToDownload();
					uTrnt.execQueue.push('skipAllFiles();');
					uTrnt.execQueue.push('stopTorrent();');
					uTrnt.execQueue.push("setFilePrior("+uTrnt.dumpObject(filesIndexToDownload)+",2);");
					uTrnt.execQueue.push("startTorrent();");
					uTrnt.callQueueNext();
				}
                selectStep(i + 1);
                return false;
            });
        }

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

    }
})(jQuery); 