function SearchArchiveList( nItem, sCommand, sSearch, sExpertise )
{
	if ( document.getElementById )
		this.init( nItem, sCommand, sSearch, sExpertise );
};

SearchArchiveList.prototype.init = function( nItem, sCommand, sSearch, sExpertise )
{
	this._rpc = '/rpc.php';
	this._init = false;
	this._search = sSearch;
	this._expertise = sExpertise;
	
	this._command = sCommand;
	this._eList = document.getElementById( sCommand + "list" );
	
	this._maxitem = nItem ? nItem : 5;
	this._createContacts();	
	
	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;
};

SearchArchiveList.prototype._createContacts = function()
{
	this._eContacts = document.createElement( "div" );
	this._eContacts.className = "archiveitem";
	
	var eTitle = document.createElement( "h3" );
	eTitle.className = "theme";
	this._eContacts.appendChild( eTitle );
	
	var eDate = document.createElement( "div" );
	eDate.className = "floatright date";
	this._eContacts.appendChild( eDate );
	
	var eImage = document.createElement( "img" );
	eImage.className = "image floatleft";
	
	this._eContacts.appendChild( eImage );
	
	eContent = document.createElement( "div" );
	eContent.className = "content";
	
	this._eContacts.appendChild( eContent ); 
	
	var eBreak = document.createElement( "br" );
	this._eContacts.appendChild( eBreak );
	
	var eLink = document.createElement( "a" );
	eLink.className = "title";
	this._eContacts.appendChild( eLink ); 
};

SearchArchiveList.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();
	}
};

SearchArchiveList.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.search = this._search;
	oVariable.expertise = this._expertise;
		
	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;
};

SearchArchiveList.prototype._onLoad = function( oXML )
{
	this._content = Array();
	oXML = oXML.documentElement;

	if ( 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" ), "" );
};

SearchArchiveList.prototype._build = function()
{
	this._eList.innerHTML = "";
	this._buildList();
	this._buildPaging();
};

SearchArchiveList.prototype._buildList = function()
{
	var iStart = ( this._pagecurrent - 1 ) * this._maxitem;
	var iEnd = iStart + this._maxitem;
	var aList = this._list.slice( iStart );
	
	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._eContacts.cloneNode( true );

			for ( var j in eMessage.childNodes )
			{
				var sTitle = this.getNodeValue( this.getSiblingByName( aList[ i ].firstChild, "title" ) );
				var sPath = this.getNodeValue( this.getSiblingByName( aList[ i ].firstChild, "contenturl" ) );
				var sMessage = this.getNodeValue( this.getSiblingByName( aList[ i ].firstChild, "text" ) );
				var sImage = this.getNodeValue( this.getSiblingByName( aList[ i ].firstChild, "image" ) );
				var sDate = this.getNodeValue( this.getSiblingByName( aList[ i ].firstChild, "createdts" ) );

				
				switch( eMessage.childNodes[ j ].className )
				{
					case "image floatleft" :  
						eMessage.childNodes[ j ].src = sImage; 
						eMessage.childNodes[ j ].title = sTitle; 
						eMessage.childNodes[ j ].alt = sTitle; 
						eMessage.childNodes[ j ].style.padding = "0 15px 5px 0";
						eMessage.childNodes[ j ].className = "floatleft";    
						break;
					case "content" :
						eMessage.childNodes[ j ].innerHTML = sMessage;  
						      						 
						break;
					case "title" :
						eMessage.childNodes[ j ].innerHTML = "Lees meer";
						eMessage.childNodes[ j ].href = sPath;   
						break;
					case "theme" :
						eMessage.childNodes[ j ].innerHTML = sTitle;  						
						break;
					case "floatright date" :
						eMessage.childNodes[ j ].innerHTML = sDate;
						break;

					default : break;
				}    
			}    
			    
			this._eList.appendChild( eMessage );
			
			
			var eSpacer = document.createElement( "div" );
			eSpacer.innerHTML = "<!-- -->";
			eSpacer.style.height = "25px";
			
			this._eList.appendChild( eSpacer );          
		}                                                
	}                                                    
};

SearchArchiveList.prototype._buildPaging = function()
{
	if ( this._pagetotal > 1 )
    {
        var nOffset = 4;
        var ePaging = document.getElementById( this._command + "paging" );
        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 );
        }
    }
};

SearchArchiveList.prototype.getNodeValue = function( oNode )
{
	if ( oNode )
	{
		if ( oNode.nodeType == 3 || oNode.nodeType == 4 )
			return oNode.nodeValue;
										
		return arguments.callee( oNode.firstChild );
	}
	
	return false;
};

SearchArchiveList.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;
};
