Estate = {}






Estate.Custom = ( function() {
	/* START PUBLIC */
	return {
		ActiveClassToggleReset: function( collectionParentId, tagName ) {
			var error;
			error = Estate.Check.ElementById( collectionParentId );
			if ( error != "" ) throw new Error( error );

			error = Estate.Check.VariableType( tagName, "string" )
			if ( error != "" ) throw new Error( error );
			
			
			
			var collectionParent = document.getElementById( collectionParentId )
			var elementsCollection = collectionParent.getElementsByTagName( tagName )

			for ( var i=0; i < elementsCollection.length; i++ ) {
				elementsCollection[i].className = elementsCollection[i].className.replace("active", "")
			}
		}
	}
	/* END PUBLIC */
})();






Estate.Events = ( function() {
	/* START PUBLIC */
	return {
		/*
		 * Summary:
		 * Adds a function to an event on an element
		 * 
		 * Usage:
		 * Estate.Events.AddEvent( document.getElementById('elementId'), functionName, "onclick" )
		 */
		AddEvent: function( element,		/* the element on which the event is placed */
						   	  newFunction,	/* the function that has to be added to the event */
							  eventType		/* the event type itself */
							  ) {
			var error;
			error = Estate.Check.VariableType( element, "object" )
			if ( error != "" ) throw new Error( error );

			error = Estate.Check.VariableType( newFunction, "function" )
			if ( error != "" ) throw new Error( error );
			
			
			
			var oldEvent = eval("element." + eventType);
			var eventContentType = eval("typeof element." + eventType)
			
			if ( eventContentType != 'function' ) {
				eval("element." + eventType +"=newFunction")
			} else {
				eval("element." + eventType) = function() {
					if ( oldEvent ) {
						oldEvent();
					}
					newFunction();
				}
			}
		},

		/*
		 * Summary:
		 * With ActiveClassToggleInCollection you can add a onclick event on a set of elements. the onclick adds or removes a specific classname
		 * 
		 * Usage:
		 * Estate.Events.ActiveClassToggleInCollection("parentElementId", "ChildTagName", "eventType")
		 */
		ActiveClassToggleInCollection: function( collectionParentId,	/* This holds a collection of elements which will get the event */
												   tagName,				/* The type of tag the event must be attached on */
												   eventType			/* the type of event that must be added */
												   ) {
			var error;
			error = Estate.Check.ElementById( collectionParentId );
			if ( error != "" ) throw new Error( error );

			error = Estate.Check.VariableType( tagName, "string" )
			if ( error != "" ) throw new Error( error );

			error = Estate.Check.VariableType( eventType, "string" )
			if ( error != "" ) throw new Error( error );
			
			
			
			var collectionParent = document.getElementById( collectionParentId )
			var elementsCollection = collectionParent.getElementsByTagName( tagName )
			
			for ( var i=0; i < elementsCollection.length; i++ ) {
				Estate.Events.AddEvent( elementsCollection[i],
										function() {	Estate.Custom.ActiveClassToggleReset( "accordion", "li" );
														Estate.CSSTools.ClassToggle( this, "active" )
														},
										eventType )
			}
		}
	}
	/* END PUBLIC */
})();







Estate.StringTools = ( function() {
	/* START PUBLIC */
	return {
		GetFilenameFromUrl: function() {
	        var file_name = document.location.href;
	        var end = ( file_name.indexOf("?") == -1 ) ? file_name.length : file_name.indexOf("?");
	        return file_name.substring( file_name.lastIndexOf("/")+1, end );
		}
	}
	/* END PUBLIC */
})();






Estate.Forms = ( function() {
	/* START PUBLIC */
	return {
		SelectAll: function( collectionParent, formFieldType ) {
			var error;
			error = Estate.Check.Element( collectionParent );
			if ( error != "" ) throw new Error( error );



			var formFieldsCollection = collectionParent.getElementsByTagName( "input" )
			if (collectionParent.alt == "true") {
				collectionParent.alt = ""
			} else {
				collectionParent.alt = "true"
			}
			var isAllChecked = collectionParent.alt

			for ( var i=0; i < formFieldsCollection.length; i++ ) {
				if (formFieldsCollection[i].type == formFieldType) {
					if (collectionParent.alt != "true") {
						formFieldsCollection[i].checked = ""
					} else {
						formFieldsCollection[i].checked = "checked"
					}
				}
			}
		}
	}
	/* END PUBLIC */
})();






Estate.Navigation = ( function() {
	/* START PUBLIC */
	return {

		/*
		 * Summary:
		 * Navigates one step back if browser history is available, otherwise the static link in the href is used
		 * 
		 * Usage:
		 * <a href="contact.asp" onclick="return Estate.Navigation.LinkBack()">Terug</a>
		 */
		LinkBack: function () {
			if ( history.length > 0 ) {
				history.go( -1 )
				return false
			} else {
				return true
			}
		}
	}
	/* END PUBLIC */
})();






Estate.CSSTools = ( function() {
	/* START PUBLIC */
	return {
		
		/*
		 * Summary:
		 * With ClassToggle you can let a specific class on an element be switched on or off
		 * 
		 * Usage:
		 * Estate.CSSTools.ClassToggle( document.getElementById('elementId'), 'className')
		 */
		ClassToggle: function( el, className ) {
			var error;
			error = Estate.Check.Element( el );
			if ( error != "" ) throw new Error( error );

			error = Estate.Check.VariableType( className, "string" )
			if ( error != "" ) throw new Error( error );



			if ( el.className.indexOf( className ) < 0) {
				el.className += " "+ className
			} else {
				while ( el.className.indexOf( className ) >= 0) {
					el.className = el.className.replace( " "+ className, "" )
					el.className = el.className.replace( className, "" )
				}
			}
		}
	}
	/* END PUBLIC */
})();






Estate.Check = ( function() {
	/* START PUBLIC */
	return {
		ArgumentsCount: function( CurrentArgumentsLength, CorrectArgumentsLength ) {
			if ( CurrentArgumentsLength != CorrectArgumentsLength ) {
				return "Error: wrong number of arguments. There argument count should be "+ CorrectArgumentsLength +", but it is "+ CurrentArgumentsLength;
			}
			return ""
		},

		ElementById: function( ElementID, RequiredTagName ) {
			if ( typeof( ElementID ) != "string" ) {
				return "Error: cannot find HTML element with the id '"+ ElementID +"', because such an element does not exists.";
			}
			if ( !document.getElementById(ElementID) ) {
				return "Error: cannot find HTML element with the id '"+ ElementID +"'";
			}
			if ( arguments.length > 1 && typeof( RequiredTagName ) == "string" ) {
				if ( document.getElementById(ElementID).tagName.toLowerCase() != RequiredTagName && RequiredTagName != "" ) {
					return "Error: HTML element with ID '"+ ElementID +"' has the tagname '"+ document.getElementById(ElementID).tagName +"' but it should be '"+ RequiredTagName +"'";
				}
			}
			return ""
		},

		Element: function( Element ) {
			if ( typeof( Element.tagName ) == "undefined" ) {
				return "Error: HTML element expected. Type of checked variable is " + typeof( Element )
			}
			return ""
		},

		VariableType: function( Variable, ExpectedVariableType ) {
			if ( typeof( Variable ) != ExpectedVariableType ) {
				return "Error: unexpected variable type. There variable type should be "+ ExpectedVariableType +", but it is "+ typeof( Variable );
			}
			return ""
		}
	}
	/* END PUBLIC */
})();






Estate.GetElements = ( function() {
	/* START PUBLIC */
	return {
		ByClassName: function ( CurrentArgumentsLength, CorrectArgumentsLength ) {
			if (document.getElementsByClassName) {
				getElementsByClassName = function (className, tag, elm) {
					elm = elm || document;
					var elements = elm.getElementsByClassName(className),
						nodeName = (tag)? new RegExp("\\b" + tag + "\\b", "i") : null,
						returnElements = [],
						current;
					for(var i=0, il=elements.length; i<il; i+=1){
						current = elements[i];
						if(!nodeName || nodeName.test(current.nodeName)) {
							returnElements.push(current);
						}
					}
					return returnElements;
				};
			}
			else if (document.evaluate) {
				getElementsByClassName = function (className, tag, elm) {
					tag = tag || "*";
					elm = elm || document;
					var classes = className.split(" "),
						classesToCheck = "",
						xhtmlNamespace = "http://www.w3.org/1999/xhtml",
						namespaceResolver = (document.documentElement.namespaceURI === xhtmlNamespace)? xhtmlNamespace : null,
						returnElements = [],
						elements,
						node;
					for(var j=0, jl=classes.length; j<jl; j+=1){
						classesToCheck += "[contains(concat(' ', @class, ' '), ' " + classes[j] + " ')]";
					}
					try	{
						elements = document.evaluate(".//" + tag + classesToCheck, elm, namespaceResolver, 0, null);
					}
					catch (e) {
						elements = document.evaluate(".//" + tag + classesToCheck, elm, null, 0, null);
					}
					while ((node = elements.iterateNext())) {
						returnElements.push(node);
					}
					return returnElements;
				};
			}
			else {
				getElementsByClassName = function (className, tag, elm) {
					tag = tag || "*";
					elm = elm || document;
					var classes = className.split(" "),
						classesToCheck = [],
						elements = (tag === "*" && elm.all)? elm.all : elm.getElementsByTagName(tag),
						current,
						returnElements = [],
						match;
					for(var k=0, kl=classes.length; k<kl; k+=1){
						classesToCheck.push(new RegExp("(^|\\s)" + classes[k] + "(\\s|$)"));
					}
					for(var l=0, ll=elements.length; l<ll; l+=1){
						current = elements[l];
						match = false;
						for(var m=0, ml=classesToCheck.length; m<ml; m+=1){
							match = classesToCheck[m].test(current.className);
							if (!match) {
								break;
							}
						}
						if (match) {
							returnElements.push(current);
						}
					}
					return returnElements;
				};
			}
			return getElementsByClassName(className, tag, elm);
		}
	}
	/* END PUBLIC */
})();






