// JQuery Single Submit Form
// Prevents form to be submited while actions are in progress

(function ($) {
    $.extend({
        SingleSubmit:{
			// Parser
			parse:function(context){
				var tag = (!context || context == document)? false : context.tagName.toLowerCase();
				// Link
				if(tag == "a" || tag == "input"){
					$.SingleSubmit.build(context);
				// Single form
				} else if(tag == "form"){
					$.SingleSubmit.build(context);
				// HTML zone
				} else {
					var forms = (!context)? $(".singlesubmit") : $(".singlesubmit",context);
					forms.each(function(){
						$.SingleSubmit.build(this);
					});
				}
			},
			// Builder
			build:function(form){
				var tag = (!form)? false : form.tagName.toLowerCase();
				// Link
				if(tag == "a" || tag == "input"){
					// Store params
					$.store(form);
					form.removeAttribute("disabled");
					$(form).mousedown(function(){
						$.SingleSubmit.disable(form);
					});
				// Form
				} else {
					// Store submit buttons & params
					$.store(form);
					$.addparam("buttons",$("*[type='submit']",form),form);
					form.params.buttons.each(function(){
						this.removeAttribute("disabled");
					});
					// Events
					$(form).submit(function(){
						$.SingleSubmit.disable(form);
					});
				}
			},
			// Disable
			disable:function(form){
				var tag = (!form)? false : form.tagName.toLowerCase();
				var message = (form.params.message)? form.params.message : null;
				// Link
				if(tag == "a" || tag == "input"){
					// Add button CSS class and disabled attribute
					$(form).addClass("SSFormDisabled");
					form.setAttribute("disabled","disabled");
					// Add button mask if has
					if(form.params.mask == "link"){
						$.SingleSubmit.setmask(form,message);
					}
				// Form
				} else {
					// Add form CSS class
					$(form).addClass("SSFormSubmited");
					// Add form mask if has
					if(form.params.mask == "form"){
						$.SingleSubmit.setmask(form,message);
					}
					// Buttons
					form.params.buttons.each(function(){
						// Add button CSS class and disabled attribute
						$(this).addClass("SSFormDisabled");
						this.setAttribute("disabled","disabled");
						// Add button mask if has
						if(form.params.mask == "button"){
							$.SingleSubmit.setmask(this,message);
						}
					});
					// Disable submit
					$(form).bind("submit",$.SingleSubmit.blocker);
				}
				// Set delay if has
				if(form.params.delay){
					setTimeout(function(){
						$.SingleSubmit.enable(form);
					},parseInt(form.params.delay)*1000);
				}
			},
			// Enable
			enable:function(form){
				var tag = (!form)? false : form.tagName.toLowerCase();
				// Link
				if(tag == "a" || tag == "input"){
					// Remove button CSS class and disabled attribute
					$(form).removeClass("SSFormDisabled");
					form.removeAttribute("disabled");
					// Remove button mask if has
					if(form.mask){
						form.mask.parentNode.removeChild(form.mask);
					}
				// Form
				} else {
					// Remove form CSS class
					$(form).removeClass("SSFormSubmoted");
					// Remove form mask if has
					if(form.mask){
						form.mask.parentNode.removeChild(form.mask);
					}
					// Buttons
					form.params.buttons.each(function(){
						// Remove button CSS class and disabled attribute
						$(this).removeClass("SSFormDisabled");
						this.removeAttribute("disabled");
						// Remove button mask if has
						if(this.mask){
							this.mask.parentNode.removeChild(this.mask);
						}
					});
					// Disable submit
					$(form).unbind("submit",$.SingleSubmit.blocker);
				}
			},
			// Block submit
			blocker:function(){
				return false;
			},
			// Set mask
			setmask:function(element,message){
				element.mask = document.createElement("div");
				element.offsetParent.appendChild(element.mask);
				$(element.mask).addClass("SSFormOverlay").css({
					position:"absolute",
					left:element.offsetLeft+"px",
					top:element.offsetTop+"px",
					width:$(element).outerWidth()+"px",
					height:$(element).outerHeight()+"px"
				});
				// Insert waiting message if has
				if(message){
					$(element.mask).html("<div class=\"SSFormMessage\">"+message+"</div>");
					var text = element.mask.firstChild;
					$(text).css({
						position:"absolute",
						left:"50%",
						top:"50%"
					}).css({
						marginLeft:-(text.offsetWidth/2)+"px",
						marginTop:-(text.offsetHeight/2)+"px"
					});
				}
			}
		}
    });
})(jQuery);
