function AnswerList( nCusid, nItem, sPath, bPopItem, nUserID )
{
	if ( document.getElementById )
		this.init( nCusid, nItem, sPath, bPopItem, nUserID );
};

AnswerList.prototype.init = function( nCusid, nItem, sPath, bPopItem, nUserID )
{
	this._rpc = '/rpc.php';
	this._init = false;
	
	this._command = "loadanswer";
	this._eList = document.getElementById( "answerlist" );
		
	this._maxitem = nItem ? nItem : 5;
	this._path = sPath;
	this._cusid = nCusid;
	this._popitem = bPopItem ? !!bPopItem : false;
	this._userid = parseInt( nUserID );
	
	this._createMessage();	
	
	if ( typeof this._eList == 'undefined' )
		return false;
	
	this._list = Array();
	this._pagetotal = null;
	this._pagecurrent = 1;
	this._page = 1;
	this._busyclass = "ajaxprogress";
	this._loading = false;
	
	// load initial list
	this._load();
	
	this._init = true;
	
	return true;
};

AnswerList.prototype._createMessage = function()
{
	this._eMessage = document.createElement( "div" );
	this._eMessage.className = "message";
			
	var eNotok = document.createElement( "div" );
	eNotok.className = "notok";
	eNotok.innerHTML = "Meld als ongeschikt";
	this._eMessage.appendChild( eNotok );

	var eRemove = document.createElement( "div" );
	eRemove.className = "remove";
	eRemove.innerHTML = "Verwijderen";
	this._eMessage.appendChild( eRemove );
	
	var eImage = document.createElement( "img" );
	eImage.className = "avatar";
	eImage.title = "";
	this._eMessage.appendChild( eImage );
	
	var eDate = document.createElement( "div" );
	eDate.className = "date";
	this._eMessage.appendChild( eDate );
	
	var eName = document.createElement( "div" );
	eName.className = "name";
	this._eMessage.appendChild( eName );
	
	var eContent = document.createElement( "div" );
	eContent.className = "content";
	this._eMessage.appendChild( eContent );
	
	var ePrivate = document.createElement( "div" );
	ePrivate.className = "private";
	this._eMessage.appendChild( ePrivate );
};

AnswerList.prototype.loadPage = function( mPage )
{
	if ( !this._loading )
	{
		var bLoad = true;
		
		if ( typeof mPage =='number' && mPage > 0 && mPage <= this._pagetotal && this._pagecurrent != mPage )
			this._pagecurrent = mPage;

		else if ( mPage == "prev" )
			this._pagecurrent--;
		
		else if ( mPage == "next" )
			this._pagecurrent++;
		
		else
			bLoad = false;
				
		if ( bLoad )
			this._load();
	}
};

AnswerList.prototype._load = function()
{
	this._loading = true;
	
	if ( this._init )
		document.body.className += ( " " + this._busyclass );
	
	var oXML = new klib3.xml();
	oXML._parent = this;
	
	oVariable = new Object();
	oVariable.command = this._command;
	oVariable._format = "xml";
	oVariable.path = this._path;
	oVariable.cusid = ""+this._cusid;
	oVariable.pop = this._popitem ? "1" : "0";

	if ( typeof this.onload == "function" )
		oXML.onload = this.onload;
	else
	{
		oXML.onload  = function()
		{
			this._parent._onLoad( this.getData() );
		}
	}
	
	oXML.post( this._rpc, true, oVariable );
	
	return false;
};

AnswerList.prototype._onLoad = function( oXML )
{
	this._list = Array();
	oXML = oXML.documentElement;
	
	if ( oXML && oXML.childNodes && oXML.childNodes.length > 0 )
	{
		for ( var i = 0; i < oXML.childNodes.length; ++i )
		{
			if ( oXML.childNodes[ i ].nodeName == "content" )
			{
				var n = 0;
				for ( var j = 0; j < oXML.childNodes[ i ].childNodes.length; ++j )
				{                        
					var oNode = oXML.childNodes[ i ].childNodes[ j ];
					if ( oNode.nodeType == 1 )
						this._list[ n++ ] = oNode;
				}
			}
		}
	}

	this._build();
	this._loading = false;
	
	document.body.className = document.body.className.replace( new RegExp( this._busyclass, "gi" ), "" );
};

AnswerList.prototype._build = function()
{
	if ( this._list.length > 0 )
	{
		this._eList.innerHTML = "";
		this._buildList();
		this._buildPaging();
	}
};

AnswerList.prototype._buildList = function()
{
	var iStart = ( this._pagecurrent - 1 ) * this._maxitem;
	var iEnd = iStart + this._maxitem;
	var aList = this._list.slice( iStart );
	var sPath = this._path.substr( 0, this._path.indexOf( "/", 1 ) );
	var sPathUser = this._path.substr( this._path.indexOf( "/", 1 )+1, this._path.length );
	var bUserProfile = (sPath == "/profiel" && sPathUser == this._userid);
	
	this._pagetotal = Math.ceil( this._list.length / this._maxitem );
		
	if ( aList.length > 0 )
	{
		var n = 0;
		
		for ( var i in aList )
		{
			if ( n++ >= this._maxitem )
				break;
			
			var eMessage = this._eMessage.cloneNode( true );
			
			if ( this.getNodeValue( this.getSiblingByName( aList[ i ].firstChild, "expertprofile" ) ) == "1" )
				eMessage.className += " expert";
			
			var bPrivateMessage = this.getNodeValue( this.getSiblingByName( aList[ i ].firstChild, "privatemessage" ) ) == "1";
			var bPrivateProfile = this.getNodeValue( this.getSiblingByName( aList[ i ].firstChild, "privateprofile" ) ) == "1";
			
			if ( this.getNodeValue( this.getSiblingByName( aList[ i ].firstChild, "new" ) ) == "1" )
				eMessage.className += " new";

			for ( var j in eMessage.childNodes )
			{
				
				var sContent = this.getNodeValue( this.getSiblingByName( aList[ i ].firstChild, "content" ) );
				var nID = this.getNodeValue( this.getSiblingByName( aList[ i ].firstChild, "id" ) );
				var nUserid = parseInt( this.getNodeValue( this.getSiblingByName( aList[ i ].firstChild, "userid" ) ) );
				var sUsername = this.getNodeValue( this.getSiblingByName( aList[ i ].firstChild, "username" ) );
				var sSanitizedusername = this.getNodeValue( this.getSiblingByName( aList[ i ].firstChild, "sanitizedusername" ) );
				var sProfileOwnerFirstname = this.getNodeValue( this.getSiblingByName( aList[ i ].firstChild, "firstname" ) );
				var sProfileOwnerPrefix = this.getNodeValue( this.getSiblingByName( aList[ i ].firstChild, "prefix" ) );
				var sProfileOwnerLastname = this.getNodeValue( this.getSiblingByName( aList[ i ].firstChild, "lastname" ) );
                
				switch( eMessage.childNodes[ j ].className )
				{					
					case "avatar" : 
						if ( !this._userid && ( bPrivateMessage || bPrivateProfile ) )
						{
							eMessage.childNodes[ j ].src = "/media/image/layout/privateprofile.gif"; 
							eMessage.childNodes[ j ].alt = "Dit profiel is niet openbaar"; 
						}
						else
						{
							eMessage.childNodes[ j ].src = this.getNodeValue( this.getSiblingByName( aList[ i ].firstChild, "avatar" ) ); 
							eMessage.childNodes[ j ].alt = sUsername;
							eMessage.childNodes[ j ]._href = "/profiel/" + nUserid + "/" + sSanitizedusername;
							eMessage.childNodes[ j ].onclick = function(){ window.location.href = this._href; };
							eMessage.childNodes[ j ].className += " pointer";
						}
						break;

					case "date" : 
						if ( !this._userid && ( bPrivateMessage || bPrivateProfile ) )
							eMessage.childNodes[ j ].innerHTML = "want zijn/haar profiel is niet openbaar gesteld."; 
						else
							eMessage.childNodes[ j ].innerHTML = this.getNodeValue( this.getSiblingByName( aList[ i ].firstChild, "created" ) ); 
						break;

					case "name" :  
						if ( !this._userid && ( bPrivateMessage || bPrivateProfile ) )
						{
							eMessage.childNodes[ j ].innerHTML = "Het bericht van deze persoon is niet zichtbaar,"; 
							eMessage.childNodes[ j ].className += "private"; 
						}
						else
						{
							eMessage.childNodes[ j ].innerHTML = sUsername; 
							eMessage.childNodes[ j ]._href = "/profiel/" + nUserid + "/" + sSanitizedusername;
							eMessage.childNodes[ j ].onclick = function(){ window.location.href = this._href; }; 
						}
						break;

					case "content" :                                 
						
						if( sProfileOwnerFirstname  )
						{
							sProfileOwnerFirstname = sProfileOwnerFirstname ? sProfileOwnerFirstname : ""; 
							sProfileOwnerPrefix = sProfileOwnerPrefix ? sProfileOwnerPrefix + " " : ""; 
							sProfileOwnerLastname = sProfileOwnerLastname ? sProfileOwnerLastname : ""; 
							
							if( sContent )
							{
								sContent = sContent.replace( "{voornaam}", sProfileOwnerFirstname ); 
								sContent = sContent.replace( "{achternaam}", sProfileOwnerPrefix + sProfileOwnerLastname );
							}
						}
						
						sContent = sContent ? sContent : "";
						eMessage.childNodes[ j ].innerHTML = sContent == "__private__" ? "&nbsp;" : sContent; 
						break;

					case "notok" : 
						if ( ( this._userid && nUserid != this._userid && sContent != "__private__" ) || ( nUserid != this._userid && !bPrivateMessage && !bPrivateProfile ) )
						{
							eMessage.childNodes[ j ].src = this.getNodeValue( this.getSiblingByName( aList[ i ].firstChild, "notok" ) ); 
							eMessage.childNodes[ j ]._href = "/meld-als-ongeschikt" + sPath + "/antwoord/" + nID;
							eMessage.childNodes[ j ].onclick = function(){ window.location.href = this._href; }; 
						}
						else
						{
							eMessage.childNodes[ j ].className += " visibilityhidden";
						}
						break;

					case "remove" : 
						if ( this.getNodeValue( this.getSiblingByName( aList[ i ].firstChild, "removable" ) ) == "1" || bUserProfile )
						{
							eMessage.childNodes[ j ].src = this.getNodeValue( this.getSiblingByName( aList[ i ].firstChild, "remove" ) ); 
							eMessage.childNodes[ j ]._href = "/verwijder" + sPath + "/antwoord/" + nID;
							eMessage.childNodes[ j ].onclick = function(){ if ( confirm( 'Weet je zeker dat je dit antwoord wilt verwijderen?' ) ) { window.location.href = this._href; } }; 
						}
						else
						{
							eMessage.childNodes[ j ].className += " visibilityhidden";
						}
						break;
						
					case "private" :
						eMessage.childNodes[ j ].style.display = ( this._userid && bPrivateMessage ) ? "block" : "none";
						eMessage.childNodes[ j ].innerHTML = "Dit bericht is priv&eacute; verstuurd";
						break;

					default : break;
				}
			}
			
			this._eList.appendChild( eMessage );
		}
	}
};

AnswerList.prototype._buildPaging = function()
{
	if ( this._pagetotal > 1 )
	{
		var nOffset = 4;
		var ePaging = document.getElementById( "answerpaging" );
		ePaging.innerHTML = "";
		
		if ( this._pagecurrent > 1 )
		{
			var eAnchor = document.createElement( "a" );
			eAnchor.href = "/geen-javascript";
			eAnchor._parent = this;
			eAnchor.onclick = function() { this._parent.loadPage( 'prev' ); return false; };
			eAnchor.innerHTML = "&lt;&lt; vorige";
			
			ePaging.appendChild( eAnchor );
		}
		
		var _pagecount = 0;
		var bDots = true;
		while ( ++_pagecount <= this._pagetotal )
		{
			if ( _pagecount == this._pagecurrent )
			{
				var eSpan = document.createElement( "span" );
				eSpan.innerHTML = _pagecount;
				
				ePaging.appendChild( eSpan );
				bDots = true;
			}
			else if ( _pagecount <= nOffset || ( _pagecount > this._pagetotal - nOffset ) || ( _pagecount > ( this._pagecurrent - nOffset ) && _pagecount < ( this._pagecurrent + nOffset ) ) )
			{
				var eAnchor = document.createElement( "a" );
				eAnchor.href = "/geen-javascript";
				eAnchor._parent = this;
				eAnchor._page = _pagecount;
				eAnchor.onclick = function() { this._parent.loadPage( this._page ); return false; };
				eAnchor.innerHTML = _pagecount;
				
				ePaging.appendChild( eAnchor );
			}
			else if ( bDots )
			{
				var eSpan = document.createElement( "span" );
				eSpan.innerHTML = "...";
				
				ePaging.appendChild( eSpan );
				bDots = false;
			}
		}

		if ( this._pagecurrent < this._pagetotal )
		{
			var eAnchor = document.createElement( "a" );
			eAnchor.href = "/geen-javascript";
			eAnchor._parent = this;
			eAnchor.onclick = function() { this._parent.loadPage( 'next' ); return false; };
			eAnchor.innerHTML = "volgende &gt;&gt;";
			
			ePaging.appendChild( eAnchor );
		}
	}
};

AnswerList.prototype.getNodeValue = function( oNode )
{
	if ( oNode )
	{
		if ( oNode.nodeType == 3 || oNode.nodeType == 4 )
			return oNode.nodeValue;
										
		return arguments.callee( oNode.firstChild );
	}
	
	return false;
};

AnswerList.prototype.getSiblingByName = function ( oNode, sName )
{
	if ( oNode )
	{
		while ( ( oNode.nodeType != 1 || ( oNode.nodeName && oNode.nodeName.toLowerCase() != sName.toLowerCase() ) ) && oNode.nextSibling )
			oNode = oNode.nextSibling;
		
		if ( oNode.nodeType == 1 && ( oNode.nodeName && oNode.nodeName.toLowerCase() == sName.toLowerCase() ) )
			return oNode;
	}
	
	return false;
};
