/*
	name			: ClassBehaviours, the javascript framework based on class-name parsing
	update			: 9.11.9
	author			: Maurice van Creij
	dependencies	: jquery.classbehaviours.js
	info			: http://www.classbehaviours.com/

    This file is part of jQuery.classBehaviours.

    ClassBehaviours is a javascript framework based on class-name parsing.
    Copyright (C) 2008  Maurice van Creij

    ClassBehaviours is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    ClassBehaviours is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with ClassBehaviours. If not, see http://www.gnu.org/licenses/gpl.html.
*/

	// create the jQuery object if it doesn't already exist
	if(typeof(jQuery)=='undefined') jQuery = function(){};

	// create the root classbehaviours object if it doesn't already exist
	if(typeof(jQuery.classBehaviours)=='undefined') jQuery.classBehaviours = function(){};

	// create the handlers child object if it doesn't already exist
	if(typeof(jQuery.classBehaviours.handlers)=='undefined') jQuery.classBehaviours.handlers = function(){}

	// Disable form elements during a submit, to avoid multiple submits on slow servers
	jQuery.classBehaviours.handlers.disableAfterSubmit = {
		// properties
		name: 'disableAfterSubmit',
		// methods
		start: function(node){
			// get the form tag and modify the submit event
			formNodes = node.getElementsByTagName('FORM');
			if(node.nodeName=='FORM')
				node.onsubmit = this.disableForm;
			else
				for(var a=0; a<formNodes.length; a++)
					formNodes[a].onsubmit = this.disableForm;
			// get all the buttons and modify the click event
			buttonNodes = node.getElementsByTagName('BUTTON');
			for(var a=0; a<buttonNodes.length; a++)
				buttonNodes[a].onclick = this.disableButton;
			// get all the links and modify the click event
			linkNodes = node.getElementsByTagName('A');
			for(var a=0; a<linkNodes.length; a++)
				linkNodes[a].onclick = this.disableLink;
			// get all the inputs and modify the click event
			inputNodes = node.getElementsByTagName('INPUT');
			for(var a=0; a<inputNodes.length; a++)
				if(inputNodes[a].type=="button" || inputNodes[a].type=="submit")
					inputNodes[a].onclick = this.disableButton;
		},
		// events
		disableForm: function(that){
			var objNode = (typeof(this.nodeName)=='undefined') ? that : this ;
			var das = jQuery.classBehaviours.handlers.disableAfterSubmit;
			objNode.onsubmit = das.canceled;
		},
		disableButton: function(that){
			var objNode = (typeof(this.nodeName)=='undefined') ? that : this ;
			var das = jQuery.classBehaviours.handlers.disableAfterSubmit;
			objNode.disabled = true;
			objNode.className += ' disabled';
			objNode.onclick = null;
			objNode.onmousedown = das.canceled;
		},
		disableLink: function(that){
			var objNode = (typeof(this.nodeName)=='undefined') ? that : this ;
			var das = jQuery.classBehaviours.handlers.disableAfterSubmit;
			objNode.className += ' disabled';
			objNode.onmousedown = das.canceled;
		},
		canceled: function(){
			return false;
		}
	}

	// add this addon to the jQuery object
	if(typeof(jQuery.fn)!='undefined'){
		// extend jQuery with this method
		jQuery.fn.disableAfterSubmit = function(){
			return this.each(
				function(){
					jQuery.classBehaviours.handlers.disableAfterSubmit.start(this);
				}
			);
		};
		// set the event handler for this jQuery method
		$(document).ready(
			function(){
				$(".disableAfterSubmit").disableAfterSubmit();
			}
		);
	}
