/**
 * jQuery Stepy - A Wizard Plugin - http://wbotelhos.com/stepy
 * 
 * @author	  Washington Botelho
 * @twitter   wbotelhos
 * 
 * Licensed under The MIT License
 * http://opensource.org/licenses/mit-license.php
 * 
 */

(function(d){d.fn.stepy=function(j){options=d.extend({},d.fn.stepy.defaults,j);if(this.length==0){a("Invalid selector!");return;}else{if(this.length>1){return this.each(function(){d.fn.stepy.apply(d(this),[j]);});}}$global=d(this);var h=$global.attr("id"),o=$global.children("fieldset"),s=o.size(),r="",q="",i;var f=d('<ul class="stepy-titles"></ul>').insertBefore($global);if(options.validate&&$global.is("form")){$global.append('<div class="stepy-error"></div>');}o.each(function(t){i=d(this);i.attr("id",h+"-step-"+t).addClass("step").append('<p id="'+h+"-buttons-"+t+'"></p>');q=(i.attr("title")!="")?i.attr("title"):"--";r=i.children("legend:first").html();f.append('<li id="'+h+"-title-"+t+'">'+q+"<span>"+r+"</span></li>");if(t==0){k(t);}else{l(t);i.hide();if(t!=s-1){k(t);}}});f.children("li:first").addClass("current-step");var p=$global.children(".finish"),m=$global,g=options;if(options.finish){if(p.length){p.hide().click(function(){e(m,s-1,g);}).appendTo($global.find("p:last"));}else{if($global.is("form")){a("You should create a button with a class named \"finish\" when the attribute 'finish' is true.");}}}if(options.titleClick){f.children().click(function(){var u=parseInt(d(this).attr("id").match(/\d/)),v=parseInt(f.children(".current-step").attr("id").match(/\d/)),t=u;if(u>v){t=n(m,g,u);}if(u!=v){b(m,t);if(g.finish&&t+1==s){p.show();}}});}else{f.children().css("cursor","default");}function l(t){d("<a/>",{id:h+"-back-"+t,href:"javascript:void(0);","class":"button-back",html:options.backLabel}).click(function(){b(m,t-1);p.hide();}).appendTo(d("p#"+h+"-buttons-"+t));}function k(t){d("<a/>",{id:h+"-next-"+t,href:"javascript:void(0);","class":"button-next",html:options.nextLabel}).click(function(){var u=n(m,g,t+1);b(m,u);if(g.finish&&u+1==s){p.show();}}).appendTo(d("p#"+h+"-buttons-"+t));}function n(x,w,v){var t=v,y=true;if(w.validate){for(var u=0;u<v;u++){y=e(m,u,w)&&y;if(w.block&&!y){t=u;break;}}}return t;}return $global;};d.fn.stepy.defaults={backLabel:"&lt; Back",block:false,errorImage:false,finish:true,nextLabel:"Next &gt;",titleClick:false,validate:false};d.fn.stepy.step=function(f,g){b(c(f,g,"step"),f-1);d.fn.stepy;};function a(f){if(console&&console.log){console.log(f);}}function c(h,i,f){var g=$global;if(i){g=d(i);if(!g.length){a('"'+i+'" is a invalid ID for the public funtion $.fn.stepy.'+f+"().");return;}}return g;}function b(h,f){var j=h.attr("id"),g=h.children("fieldset").size(),i;if(f>g-1){f=g-1;}h.children("fieldset").hide().end().children("fieldset").eq(f).show();h.prev().children().removeClass("current-step").end().children().eq(f).addClass("current-step");if(h.is("form")){h.children("fieldset").eq(f).find(":input:visible").each(function(){i=d(this);if(!i.attr("disabled")){i.focus();return false;}});}}function e(h,g,f){if(!h.is("form")){return true;}var l=h.attr("id"),j=true,i=h.children("fieldset").eq(g),k=h.prev("ul.stepy-titles").children();i.find(":input").each(function(){j=j&&h.validate().element(d(this));if(j===undefined){j=true;}if(!j){if(f.block){b(h,g);}if(f.errorImage){k.eq(g).addClass("error-image");}h.validate().focusInvalid();}else{if(f.errorImage){k.eq(g).removeClass("error-image");}}});return j;}})(jQuery);

