// JavaScript Document

/**
 *	@author		Jeffrey Angelo Krist
 *	@copyright	Jeffrey Angelo Krist 2008
 **/
function isBrowser(name)
{
	var browser;
	
	if (window.XMLHttpRequest && document.all) 
		browser = "IE7";
	else if(!window.XMLHttpRequest && document.all)
		browser = "IE6";
	else
		browser = "FF";
			
	if(name)
	{	
		browsers = name.split("|");
	
		for(i = 0; i < browsers.length; i++)
		{
			if(browsers[i] == browser)
				return true;
		}
		
		return false;
	}
	
	return browser;
}	


/**
 *	@author Jeffrey Angelo Krist
 *	@copyright @author 2007
 *	@note	Optimezed!
 *	@todo	Performancetests
 **/
function findElementsByAttr(root, attribute, value, tagname)
{
	var childNodes2Array = function(childs)
	{
		//alert(childs instanceof Array);
		var result = new Array();
		
		for(var it2 = 0; it2 < childs.length; it2++)
		{
			if(childs[it2].nodeType == 1)
				result.push(childs[it2]);
		}
		
		return result;
	}
	
	var result = new Array();
	
	if(attribute == 'class' && isBrowser("IE6|IE7")) attribute = "className"; 
	
	var instanceCheck = isBrowser("FF") ? root instanceof Array || root instanceof NodeList : root instanceof Array;
	
	if(instanceCheck)
	{
		var i = root.length;
		
		if(tagname != null)
		{
			var r = [];
			
			i = root.length;
			while(i--)
			{
				var s = [], undefined;
				var e = root[i];
				
				while (e !== undefined)
				{
					while (e)
					{
						if (e.nodeType == 1)
						{
							if(e.hasChildNodes())
							{
									
								var r2 = findElementsByAttr(childNodes2Array(e.childNodes), attribute, value, tagname);
								
								for(var x in r2)
									r.push(r2[x]);
							}
								
								
							if (e.tagName == tagname.toUpperCase())
							{
								if(attribute == '' && value == '')
									r.push(e);
								else if(e.getAttribute(attribute) == value)
									r.push(e);

							}
						}
			
						e = e.nextSibling;
					}
			
					e = s.pop();
				}
			}
			return r; 
		}
		else
		{
			var r = [];
			
			i = root.length;
			while(i--)
			{
				var s = [], undefined;
				var e = root[i];
				
				while (e !== undefined)
				{
					while (e)
					{
						if (e.nodeType == 1)
						{
							if(e.hasChildNodes())
							{
								var r2 = findElementsByAttr(new Array(e.childNodes), attribute, value, tagname);
								for(var x in r2)
									r.push(x);
							}
							if(attribute == '' && value == '')
								r.push(e);
							else if(e.getAttribute(attribute) == value)
								r.push(e);
						}
			
						e = e.nextSibling;
					}
			
					e = s.pop();
				}
			}
			return r; 
		}
	}
	else
	{
		if (!tagname) tagname = '*';
   		var s = root.getElementsByTagName(tagname), i = s.length;
		while (i--)
		{        	
			if(attribute == '' && value == '')
				result.push(s[i]);
			else if (s[i].getAttribute(attribute) == value)
				result.push(s[i]);
		}

	}
	
    return result;
}

/**
 *	Multi callback handler
 *	@author		Jeffrey Angelo Krist
 *	@copyright	Jeffrey Angelo Krist 2008
 **/
function CallbackHandler(functionsMustCompleted)
{
	this.callbacks 				= [];
	this.callbackParameter		= null;
	this.functionsMustCompleted = functionsMustCompleted ? functionsMustCompleted : 0;

	this.functionsCompleted		= 0;
	
	/**
	 *	Register a new callback function
	 **/
	this.register = function(callbackFunction)
	{
		// remember the function
		this.callbacks[this.callbacks.length] = callbackFunction;
		
		return this;
	};
	
	this.increment = function(number)
	{
		this.functionsMustCompleted += number ? number : 1;
	};
	
	/**
	 *
	 **/
	this.registerParameter = function(para)
	{
		this.callbackParameter = para;
	}
	
	/**
	 *	Sets a process to be done
	 **/
	this.completed = function()
	{		
		this.functionsCompleted++;
		
		//If the number of processes which must be done equals the number of registers callback functions
		if(this.functionsMustCompleted == this.functionsCompleted)
		{
			// execute the callbacks
			for(i = 0; i < this.callbacks.length; i++)
				this.callbacks[i](this.callbackParameter);
		};
	};
};

/**
 *	Calculates the real offset of the given element
 *
 *	@author		Jeffrey Angelo Krist
 *	@copyright	Jeffrey Angelo Krist 2008
 *
 *	@param	Object	element
 *	@return array[left, top]
 **/
function findPos(element)
{
	var curleft = curtop = 0;
	
	if (element.offsetParent)
	{
		curleft = element.offsetLeft;
		curtop = element.offsetTop;
		
		while (element = element.offsetParent)
		{
			curleft += element.offsetLeft;
			curtop += element.offsetTop;
		}
	}
	return [curleft, curtop];
}

/**
 *	Creates a string from the given object or array
 *
 *	@author		Jeffrey Angelo Krist
 *	@copyright	Jeffrey Angelo Krist 2008
 *
 *	@param	Object	object 			the object or array which would be serialized
 *	@param	String	parentPrefix	(optional) prefix
 **/
function object2string(object, parentPrefix)
{	
	var arr = new Array();
	
	for(key in object)
	{
		if(typeof(object[key]) == "object")
			arr[arr.length] = object2string(object[key], parentPrefix + "[" + key + "]");
		else if(parentPrefix)
			arr[arr.length] = parentPrefix + "[" + key + "]=" + escape(object[key]);
		else
			arr[arr.length] = key + "=" + escape(object[key]);
	}
	
	return arr.join("&", arr);
};


/**
 *	Multi event handling
 *
 *	@author		Jeffrey Angelo Krist
 *	@copyright	2007 - Jeffrey Angelo Krist
 **/
var events =
{
	/**
	 *	Holds the events in the following dimensions: elementName, owner
	 **/
	events : [],
	
	/**
	 *	Registers a event on the given element
	 *
	 *	@param	String	contains the name of the element including the property
	 *	@param	Mixed	contains the owner, can be Int, String or Object
	 * 	@return	Object 	with a 'callback' property. This must be set with the desired event function
	 **/
	register : function(elementName, owner)
	{
		var that = this;
		
		if(!owner)
		{
			do
			{
				var owner = Math.random();
			}
			while(this.events[elementName] && this.events[elementName][owner])
		}
		/**
		 *	This will be called when a event is triggered. It calls the registered callback functions.
		 **/
		eventFired = function(elementName, owner, arguments)
		{
			// walk through the correct element in the event array and calls the functions in it
			for(owner in that.events[elementName])
				that.events[elementName][owner].callback.apply(this, arguments);
		};
		
		// registers the function eventFired to the given event
		eval(elementName + ' = function(){ eventFired(elementName, owner, arguments); }');
		
		// if this event is 'new', define it and make an array of it
		if(!this.events[elementName])
			this.events[elementName] = [];
		
		// create a new object, containing the 'callback' property, save it in the events array, and return the object
		return this.events[elementName][owner] = 	{
														callback: 	function()
																	{
																	}
													};
	},
	
	/**
	 *	Unregister a registered event by the given elementName and owner
	 *
	 *	@param	String	contains the name of the element including the property
	 *	@param	Mixed	contains the owner, can be Int, String or Object
	 **/
	unregister : function(elementName, owner)
	{
		// check if the event exists; delete it
		if(this.events[elementName][owner])
			delete this.events[elementName][owner];
	},
	
	/**
	 *	Checks if an event is registered
	 *
	 *	@param	String	contains the name of the element including the property
	 *	@param	Mixed	(optional) contains the owner, can be Int, String or Object
	 *	@return	Bool	true if the event exists, otherwise False
	**/
	exists : function(elementName, owner)
	{
		// check if the event exists; delete it
		if(owner != undefined && this.events[elementName][owner])
			return true;
		else if(this.events[elementName])
			return true;
		else
			return false;
	}
};

/**
 *	@author	Unknown..
 **/
function ajaxObject(url, callbackFunction)
{
  var that=this;      
  this.updating = false;
  this.abort = function() {
    if (that.updating) {
      that.updating=false;
      that.AJAX.abort();
      that.AJAX=null;
    }
  }
  this.update = function(passData,postMethod,postData) { 
    if (that.updating) { return false; }
    that.AJAX = null;                          
    if (window.XMLHttpRequest) {              
      that.AJAX=new XMLHttpRequest();              
    } else {                                  
      that.AJAX=new ActiveXObject("Microsoft.XMLHTTP");
    }                                             
    if (that.AJAX==null) {                             
      return false;                               
    } else {
      that.AJAX.onreadystatechange = function() {  
      try
	  { 
	  if (that.AJAX.readyState==4) 
		 {             
			  that.updating=false;                
			  that.callback(that.AJAX.responseText,that.AJAX.status,that.AJAX.responseXML);        
			  that.AJAX=null;                                         
		}
	} catch(err)
	{
		}                                                      
      }                                                        
      that.updating = new Date();                              
      if (/post/i.test(postMethod)) {
        var uri=urlCall+'/'+passData; //+'/timestamp='+(that.updating.getTime()); 
        that.AJAX.open("POST", uri, true);
        that.AJAX.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        that.AJAX.setRequestHeader("Content-Length", passData.length);
        that.AJAX.send("AJAX=1&timestamp=" + (that.updating.getTime()) + "&" + postData);
      } else {
        // disabled!
		//var uri=urlCall+'/'+passData; //+'/timestamp='+(that.updating.getTime()); 
        //that.AJAX.open("GET", uri, true);                             
        //that.AJAX.send(null);                                         
      }              
      return true;                                             
    }                                                                           
  }
  var urlCall = url;        
  this.callback = callbackFunction || function (responseText, responseStatus, responseXML) { alert("No Function"); };
}

/**
 *	@author		Jeffrey Angelo Krist
 *	@copyright	Jeffrey Angelo Krist 2008
 **/
var OverlayPage = OverlayPage ? OverlayPage : 
{
	overlay				: null,
	title				: null,
	container			: null,
	loader				: null,
	iframe				: null,
	iframeOldH			: null,
	callback			: function(){},
	
	start : function ()
	{
		this.overlay = document.createElement('div');
		{
			this.overlay.id = "Overlay_overlay";
			this.overlay.style.display = 'block';
			this.overlay.style.position = 'absolute';
			this.overlay.style.top = '0px';
			this.overlay.style.left = '0px';
			this.overlay.style.zIndex = '1';
			this.overlay.style.width = '100%';
			this.overlay.style.height = '100%';
			this.overlay.style.height = '10000px';
			
			this.overlay.onclick = function()
			{
				OverlayPage.hide();	
			}
		}
		
		document.body.style.overflowY = 'hidden';
					
		this.overlay.style.opacity = 0.50;
		this.overlay.style.filter = 'alpha(opacity="50")';
		this.overlay.style.background = 'url("' + baseUrl + '/media/images/basicPage/overlay.gif") repeat scroll 0%';
		
		
		this.container = document.createElement('div');
		{
			var htmlWidth = 14;
			var htmlHeight = 14;
						
			
			
			this.container.style.width 		= htmlWidth + 'px';
			this.container.style.height 	= htmlHeight + 'px';
			
			this.container.style.position 		= 'absolute';
			this.container.style.left 			= '50%';
			this.container.style.top 			= '50%';
			this.container.style.marginLeft 	= '-' + Math.round(htmlWidth / 2) 	+ 'px';
			
			if(isBrowser("IE6|IE7"))
				this.container.style.marginTop 		= (document.body.scrollTop + Math.round((document.body.clientHeight - htmlHeight) / 2) - (document.body.clientHeight / 2)) 	+ 'px';
			else
				this.container.style.marginTop 		= '-' + Math.round(htmlHeight / 2) + 'px';


			this.container.style.padding		= '5px';
		
			this.container.style.zIndex = 2;
		
			this.loading = document.createElement('span');
			{
				var img = document.createElement('img');
				img.src = baseUrl + '/media/images/basicPage/loading.gif';	
				
				this.loading.appendChild(img);
			}
			this.container.appendChild(this.loading);
		}
		
		document.body.insertBefore(this.overlay, 	document.body.childNodes[0]);
		document.body.insertBefore(this.container, 	document.body.childNodes[1]);
	},
	
	drawFrame : function(url)
	{
		
	},
	
	hide : function(force, scope, performCallback)
	{
		if(!force && !confirm('Wilt u het overliggende scherm sluiten?'))
			return;

		if(!scope)
		{
			if(performCallback != false)
				this.callback();
			
			document.body.removeChild(document.body.childNodes[1]);
			document.body.removeChild(document.body.childNodes[0]);
			
			document.body.style.overflowY = 'auto';
		}
		else if(scope == 1)
		{

			var thisbody = parent.parent.document.body;
			
			if(performCallback != false)
				parent.parent.OverlayPage.callback();

			thisbody.removeChild(thisbody.childNodes[1]);
			thisbody.removeChild(thisbody.childNodes[0]);
			
			thisbody.style.overflowY = 'auto';
		}
		
		this.overlay = null;
	},
	
	iframeLoaded : function()
	{
		var w, h;
		
		if(isBrowser("FF"))
		{
			w = this.iframe.contentWindow.document.body.parentNode.scrollWidth;
			h = this.iframe.contentWindow.document.body.parentNode.scrollHeight;
		}
		else
		{
			w = this.iframe.contentWindow.document.body.scrollWidth;
			h = this.iframe.contentWindow.document.body.scrollHeight;
		}
		
		this.loaded(this.iframe.contentWindow.document.title, w, h);
	},
	
	iframeResized : function()
	{
		var w, h;
		
		
		this.iframe.width = 10;
		this.iframe.height = 10;
			
		if(isBrowser("FF"))
		{
			w = this.iframe.contentWindow.document.body.parentNode.scrollWidth;
			h = this.iframe.contentWindow.document.body.parentNode.scrollHeight;
		}
		else
		{
			w = this.iframe.contentWindow.document.body.scrollWidth;
			h = this.iframe.contentWindow.document.body.scrollHeight;
		}
		
		w += 20;
		
		if(isBrowser("FF"))
			h += 40;
		else
			h += 20;
				
		{
			this.iframe.width = w;
			this.iframe.height = h;
		}
		
		{
			
			this.container.style.width 		= w + 'px';
			this.container.style.height 	= h + 'px';

			this.container.style.marginLeft 	= '-' + Math.round(w / 2) 	+ 'px';
			
			
			if(isBrowser("IE6|IE7"))
				this.container.style.marginTop 		= (document.body.scrollTop + Math.round((document.body.clientHeight - h) / 2) - (document.body.clientHeight / 2)) 	+ 'px';
			else
				this.container.style.marginTop 		= '-' + Math.round(h / 2) 	+ 'px';
		}
		
		if(isBrowser('FF'))
			this.title.style.width 	= (w - 6) + 'px';
		else
			this.title.style.width 	= w + 'px';
	},
	
	goto : function(url, callback)
	{
		if(this.overlay != null)
			this.hide(true, 0, false);
		
		this.callback = callback || function(){};
		
		this.start();
		
		this.iframe = document.createElement("iframe");
		{
			this.iframe.src = baseUrl + url;
			this.iframe.width = 1;
			this.iframe.height = 1;
			this.iframe.style.zIndex = 3;
			this.iframe.frameBorder = 0;
		}
		this.container.appendChild(this.iframe);
		
		this.iframe.contentWindow.iframeHandler = this;
	},
	
	loaded : function(title, width, height)
	{
		if(!this.container)
			return;
		
		width += 20;
		
		if(isBrowser("FF"))
			height += 40;
		else
			height += 20;
			
		this.container.removeChild(this.loading);
		
		this.title = document.createElement('div');
		{
			if(isBrowser('FF'))
				this.title.style.width 			= (width - 6) + 'px';
			else
				this.title.style.width 			= width + 'px';
				
			this.title.style.padding		= '3px';
			
			this.title.style.backgroundColor = '#556D79';
			
			this.title.style.color 		= 'white';
			this.title.style.fontSize	= '12px';
			
			this.title.innerHTML = title;
			
			closeImg = document.createElement('img');
			{
				closeImg.src 	= baseUrl + '/media/images/close.gif';
				closeImg.width 	= 12;
				closeImg.height = 12;
				closeImg.align 	= 'right';
				closeImg.style.marginTop = '-14px';
				closeImg.style.marginRight = '2px';
				
				that = this;
				
				closeImg.onclick = function()
				{
					that.hide();
				}
			}
			this.title.appendChild(closeImg);
		}
		
		this.container.insertBefore(this.title, this.iframe);
		
		{
			this.iframe.width = width;
			this.iframe.height = height;
			
			this.iframe.style.display = '';
			this.iframe.style.padding = '10px';
		}
		
		{
			
			this.container.style.width 		= width + 'px';
			this.container.style.height 	= height + 'px';
			
			this.container.style.position 		= 'absolute';
			this.container.style.left 			= '50%';
			this.container.style.top 			= '50%';
			this.container.style.marginLeft 	= '-' + Math.round(width / 2) 	+ 'px';
			
			if(isBrowser("IE6|IE7"))
				this.container.style.marginTop 		= (document.body.scrollTop + Math.round((document.body.clientHeight - height) / 2) - (document.body.clientHeight / 2)) 	+ 'px';
			else
				this.container.style.marginTop 		= '-' + Math.round(height / 2) 	+ 'px';
				
			this.container.style.padding		= '1px';
		
			this.container.style.zIndex = 2;
			this.container.style.border = '1px solid #C3C4C6';
			this.container.style.backgroundColor = 'white';
		}
	}
};

/**
 *	@author		Jeffrey Angelo Krist
 *	@copyright	Jeffrey Angelo Krist 2008
 **/
var InlineWindow = InlineWindow ? InlineWindow : {
	instances : new Array(),
	
	getNew : function(element, name)
	{
		newIns = new InlineWindow.childClass(element, name);
		InlineWindow.instances[name] = newIns;
	},
	
	get : function(name)
	{
		return InlineWindow.instances[name];
	},
	
	childClass : function (element, name)
	{
		that = this;
		this.element = element;
		this.name = name;
		this.iframe = null;
		this.loading = null;
		
		this.iframeOldH = null;
		
		this.truncate = function()
		{
			while(element.hasChildNodes())
				element.removeChild(element.firstChild);
		};
		
		this.drawLoading = function()
		{
			this.truncate();
				
			this.loading = document.createElement('div');
			{
				var img = document.createElement('img');
				{
					img.src = baseUrl + '/media/images/basicPage/loading.gif';	
				}
				this.loading.appendChild(img);
				
				this.loading.innerHTML += ' Laadt..';
			}
			
			this.element.appendChild(this.loading);
		};
		
		this.iframeLoaded = function()
		{
			this.element.removeChild(this.loading);
			this.iframe.style.visibility = 'visible';
		}
		
		this.iframeResized = function()
		{
			if(that.iframe.style.visibility != 'visible')
			{
				that.iframeLoaded();
				return;
			}
			
			var w, h;
		
			if(isBrowser("FF"))
			{
				w = that.iframe.contentWindow.document.body.parentNode.scrollWidth;
				h = that.iframe.contentWindow.document.body.parentNode.scrollHeight;
			}
			else
			{
				w = that.iframe.contentWindow.document.body.scrollWidth;
				h = that.iframe.contentWindow.document.body.scrollHeight;
			}
			
			if(Math.abs(that.iframeOldH - h) < 5)
				return;
			
			that.iframe.height = h + 5;
			that.iframeOldH = h + 5;
		}
		
		this.load = function(url)
		{			
			this.drawLoading();
			
			url = url.replace("#", "/");
			
			this.iframe = document.createElement("iframe");
			{
				this.iframe.width = "100%";
				this.iframe.height = "1";
				this.iframe.frameBorder = 0;
				this.iframe.style.visibility = 'hidden';
				
				this.iframe.src = url;
			}
			this.element.appendChild(this.iframe);
			
			this.iframe.contentWindow.iframeHandler = this;
		};
		
		this.goto = function(url)
		{
			document.location.href = url;
			
			this.load(url);
		};
	}
};

var pageLoadingTimeout;

function drawPageLoading()
{
	pageLoadingTimeout = setTimeout(		function()
											{
												var div = document.createElement('div');
												{
													var div2 = document.createElement('div');
													{
														var img = document.createElement('img');
														{
															img.src = baseUrl + 'media/images/common/loading.gif';	
														}
														div2.appendChild(img);
														
														div2.innerHTML += 'Pagina laadt..';
														
														div2.className = 'common pageload message';
													}
													div.appendChild(div2);
													
													div.className = 'common pageload holder';
												}
												document.body.insertBefore(div, document.body.firstChild);
											},
											2000
										);
}

function hidePageLoading()
{
	var element = findElementsByAttr(document.body, 'class', 'common pageload', 'div');
	
	clearTimeout(pageLoadingTimeout);
	
	if(element[0])
		element[0].parentNode.removeChild(element[0]);
}