var MooTact=new Class({Implements:[Options,Events],options:{url:"/mootact","class":"mootact",prefix:"mootact_","form-rows":[{html:'<label for="mootact_name">Name</label><input type="text" id="mootact_name" name="mootact[name]" class="name" />'},{html:'<label for="mootact_email">E-mail</label><input type="text" id="mootact_email" name="mootact[email]" class="email" />'},{html:'<label for="mootact_subject">Subject</label><input type="text" id="mootact_subject" name="mootact[subject]" class="subject" />'},{html:'<label for="mootact_message">Message</label><textarea id="mootact_message" name="mootact[message]" class="message" ></textarea>'},{html:'<input type="submit" id="mootact_send" value="Send"/>',"class":"submit"}],pelem:null,position:{position:"center"},title:"Contact Form"},toElement:function(){return $(this.element)},initialize:function(a){this.setOptions(a);if(this.options.pelem==null){this.options.pelem=document.body}this.pelem=this.options.pelem;this.element=new Element("form",{id:"mootact-"+Math.floor(Math.random()*1000000000).toString(16),method:"POST",action:this.options.submitURL,"class":this.options["class"],html:'<a href="#" class="dismiss">&nbsp;</a><h2>'+this.options.title+'</h2><p class="error general"></p>'});$each(this.options["form-rows"],function(b){this.element.adopt(new Element("div",b).addClass("form-row"))}.bind(this));this.element.addEvent("submit",function(b){b.stop();this.mail()}.bind(this));$(this).getElement("a.dismiss").addEvent("click",function(b){b.stop;this.dismiss()}.bind(this));this.genError=$(this).getElement(".error.general");$(this).store("MooTact",this);return this},dismiss:function(){$(this).dispose();$(this.pelem).unmask();this.fireEvent("dismiss");try{delete this}catch(a){}},show:function(){$(this.pelem).mask();document.body.adopt(this.element);$(this).position(this.options.position);$(this).set("spinner",{message:"Please wait ...","class":"mask mootact",containerPosition:{position:"centerTop",offset:{y:100,x:-50}}})},errMsgs:{},showErrors:function(a){if(a.general){this.showGenError(a.general)}a.fields&&$each(a.fields,this.showFieldError.bind(this))},showGenError:function(a){a&&this.genError.set("html",a)},showFieldError:function(b,a){var c=this.options.prefix+a;if(!$(this).hasChild(c)){this.showGenError(b);return}this.errMsgs[c]=new Element("div",{"class":"field error",id:c+"_err",html:b});$(this).adopt(this.errMsgs[c]);this.errMsgs[c].position({relativeTo:$(c),position:"bottomRight",offset:{x:-25,y:-20}});$(c).addEvent("focus",function(){this.errMsgs[c].dispose()}.bind(this))},mail:function(){var a=this;$(a).spin();$each(a.errMsgs,function(b){$(b).dispose()});new Request.JSON({url:this.options.url,onSuccess:function(b){$(a).unspin();if(!b){a.showErrors({general:"Error: The server returned an error"});return}if(b.success){a.fireEvent("success");a.dismiss();return}if(b.exception){a.showErrors(b.exception?b.exception:{general:"Error: The server returned an error"})}a.fireEvent("error")},onException:function(){a.showErrors({general:"Error: The server returned an error"});$(a).unspin();a.fireEvent("exception")},onFailure:function(){a.showErrors({general:"Error: The server returned an error"});$(a).unspin();a.fireEvent("failure")}}).post($(this));a.fireEvent("send")}});
