function Tab( sTabElement )
{
	if ( !document.getElementById || !this.initTab( sTabElement ) )
		alert( 'Tab could not be initialized' );
};

Tab.prototype.initTab = function( sTabElement )
{
	this._rpc = '/rpc.php';
	this._sLastClicked = '';
	this._currentpage = 1;
	this._pages = 1;
	this._selectedCategory = false;
	
//	this._eTab = document.getElementById( sTabElement );
//	this._eDefinitionList = document.getElementById( 'definitionlist' );
//	this._eDefinition = document.getElementById( 'detaildefinition' );
//	this._eDescription = document.getElementById( 'detaildescription' );
//	this._sDefinition = '';
//	this._sDescription = '';
	
//	if ( typeof this._eTab != 'undefined' || typeof this._eDefinitionList != 'undefined' || typeof this._eDefinition != 'undefined' || typeof this._eDescription != 'undefined' )
		return true
	
	return false;
};


Tab.prototype.activateTab = function( sTabId, iPage )
{
	this._sCategory = sTabId;
	if ( typeof iPage == "undefined" )
		iPage = 1;
				
	this.activateLabel( sTabId );
	if ( this.activateContent( sTabId, iPage ) )
		oS = new AjaxStats( sTabId );
	
//	alert( 'show tab ' + sTabId )
	return false;
}


/**
 *
 * @return  boolean  succes
 */
Tab.prototype.activateLabel = function( sTabId )
{
	if ( this._sLastClicked != '' )
		this.changeClass( this._sLastClicked, "category" );
	
	bSucces = this.changeClass( sTabId, "category active" );
	
	if ( bSucces )
		this._sLastClicked = sTabId;
		
	return bSucces;
}


Tab.prototype.activateContent = function( sCategory, iPage )
{
	if ( typeof iPage == "undefined" )
		iPage = 1;
		
	var oXML     = new klib3.xml();
	oXML._parent = this;
	
	oVariable = new Object();
	oVariable.command = "loadtipcategory";
	oVariable.category = sCategory;
	oVariable.page = iPage;
	oVariable._format = 'xml';
//	alert( "activate content: page is " + oVariable.category );
	
	if ( typeof this.onload == "function" )
		oXML.onload = this.onload;
	else
	{
		oXML.onload  = function()
		{
			this._parent._onActivateContent( this.getData().firstChild );
		}
	}
	
	if ( oXML.post( this._rpc, true, oVariable ) )
		return true;
	else
		return false;
};

Tab.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;
};

Tab.prototype.getNodeValue = function( oNode )
{
	if ( oNode )
	{
		if ( oNode.nodeType == 3 || oNode.nodeType == 4 )
			return oNode.nodeValue;
			
		return arguments.callee( oNode.firstChild );
	}
	return false;
};

Tab.prototype.changeClass = function( sElementId, sClassName )
{
	eChange = document.getElementById( sElementId );
	if ( typeof eChange != 'object' )
		return false;
	
	eChange.className = sClassName;
		
	return true;
};


Tab.prototype.addPageList = function()
{
	document.getElementById( "pagelist" ).innerHTML = "";
	if ( this._pages > 1 )
	{
		var ePageList = document.createElement( 'span' );
		ePageList.className = "pagelist";
		
		// ePrev 
		
		var ePrevImage = document.createElement( 'img' );
		ePrevImage.src = "/images/layout/previous.gif";
		
		var ePrev = document.createElement( 'span' );
		ePrev.className = "arrowprev";
		
		if ( this._currentpage != 1 )
		{
			ePrevImage._index  = 1;
			ePrevImage._cat    = this._sLastClicked;//'category1';  // TODO de categorie moet nog dynamisch gemaakt worden!
			ePrevImage._parent = this;
			ePrevImage.onclick = this.__doActivateContent;
		}

		ePrev.appendChild( ePrevImage );
		
		// eNext Fixen
		
		var eNextImage = document.createElement( 'img' );
		eNextImage.src = "/images/layout/next.gif";
		
		var eNext = document.createElement( 'span' );
		eNext.className = "arrownext";

		if ( this._currentpage != this._pages )
		{
			eNextImage._index  = parseInt( this._currentpage ) + 1;
			eNextImage._cat	   = this._sLastClicked;//'category1';
			eNextImage._parent = this;
			eNextImage.onclick = this.__doActivateContent;			
		}
		
		eNext.appendChild( eNextImage );
		
		for( var i = 1; i <= this._pages; i++ )
		{
			var ePageAnchorText = document.createElement( 'span' );
			ePageAnchorText.innerHTML = i + " ";			
			
			if ( i != this._currentpage ) 
			{
				var ePageAnchor = document.createElement( 'a' );
				ePageAnchor.className = "pagelink";
				ePageAnchor._index  = i;
				ePageAnchor._cat    = this._sLastClicked;//'category1';  // TODO de categorie moet nog dynamisch gemaakt worden!
				ePageAnchor._parent = this;
				ePageAnchor.onclick = this.__doActivateContent;
				ePageAnchor.appendChild( ePageAnchorText );
	
				ePageList.appendChild( ePageAnchor );
			}
			else
				ePageList.appendChild( ePageAnchorText );
						
		}
	
		document.getElementById( "pagelist" ).appendChild( ePrev );
		document.getElementById( "pagelist" ).appendChild( ePageList );
		document.getElementById( "pagelist" ).appendChild( eNext );
		
	}
	
	return true;
}
Tab.prototype.__doActivateContent = function()
{
	this._parent.activateContent( this._cat, this._index );
	return false;
};
Tab.prototype.addTip = function( sId, sTitle, sContent, sAuthor, sAuthortype, sPrio )
{
	if ( sPrio == "true")
	{
		var eTitle = document.createElement( 'span' );
		eTitle.innerHTML = "<!-- prio icon -->"+sTitle+ " <img src='/images/layout/belangrijk.gif' alt='' title='' /><br />";
		eTitle.className = "tiptitle";
	}else{
		var eTitle = document.createElement( 'span' );
		eTitle.innerHTML = ""+sTitle+ "<br />";
		eTitle.className = "tiptitle";
	}
	
	if ( sAuthortype == "anonymouse" )
	{
		var eContent = document.createElement( 'p' );
		eContent.innerHTML = sContent;
		eContent.className = "tipcontent";
	
		var eAuthor = document.createElement( 'span' );
		eAuthor.innerHTML = "<strong><font color='#3b82c4'>Tip van:</font></strong> Anoniem <br /><br />";
		eAuthor.className = "tipauthor";
	}else if ( sAuthortype == "prof" ){
		var eContent = document.createElement( 'p' );
		eContent.innerHTML = sContent;
		eContent.className = "tipcontentblue";
		
		var eAuthor = document.createElement( 'span' );
		eAuthor.innerHTML = "<strong><font color='#3b82c4'>Tip van:</font></strong> " + sAuthor+ " (Onze professional) <br /><br />";
		eAuthor.className = "tipauthor";
	}else{
		var eContent = document.createElement( 'p' );
		eContent.innerHTML = sContent;
		eContent.className = "tipcontent";
		
		var eAuthor = document.createElement( 'span' );
		eAuthor.innerHTML = "<strong><font color='#3b82c4'>Tip van:</font></strong> " + sAuthor+ "<br /><br />";
		eAuthor.className = "tipauthor";
	}
		
		
	var eTip = document.createElement( 'div' );
	eTip.className = "tipclass";
	eTip.appendChild( eTitle );
	eTip.appendChild( eContent );
	eTip.appendChild( eAuthor );
	
	document.getElementById( "tabscontent" ).appendChild( eTip );
	
	if ( sPrio == "true" )
		var oStats = new AjaxStats( this._sCategory, sId );
	
	return true;
}

Tab.prototype.clearTips = function ()
{
	document.getElementById( "tabscontent" ).innerHTML = "";
}

Tab.prototype._onActivateContent = function( oXML )
{
	if ( oXML && oXML.childNodes && oXML.childNodes.length > 0 )
	{
		for ( var i = 0; i < oXML.childNodes.length; ++i )
		{
			if ( oXML.childNodes[ i ].nodeType == 1 )
			{
				// retrieve num pages from xml
				this._pages = this.getNodeValue( this.getSiblingByName( oXML.childNodes[ i ].firstChild, "pages" ) );
				this._currentpage = this.getNodeValue( this.getSiblingByName( oXML.childNodes[ i ].firstChild, "currentpage" ) );
						
				this.clearTips();
				for ( var j = 0; j < oXML.childNodes[ i ].childNodes.length; ++j )
				{
					if ( oXML.childNodes[ i ].childNodes[ j ].nodeType == 1 && oXML.childNodes[ i ].childNodes[ j ].nodeName == "item" )
					{
						var sId = this.getNodeValue( this.getSiblingByName( oXML.childNodes[ i ].childNodes[ j ].firstChild, "ctcid" ) );
						var sTitle = this.getNodeValue( this.getSiblingByName( oXML.childNodes[ i ].childNodes[ j ].firstChild, "title" ) );
						var sContent = this.getNodeValue( this.getSiblingByName( oXML.childNodes[ i ].childNodes[ j ].firstChild, "content" ) );
						var sAuthor = this.getNodeValue( this.getSiblingByName( oXML.childNodes[ i ].childNodes[ j ].firstChild, "author" ) );
						var sAuthortype = this.getNodeValue( this.getSiblingByName( oXML.childNodes[ i ].childNodes[ j ].firstChild, "authortype" ) );
						var sPrio = this.getNodeValue( this.getSiblingByName( oXML.childNodes[ i ].childNodes[ j ].firstChild, "prio" ) );
						
						this.addTip( sId, sTitle, sContent, sAuthor, sAuthortype, sPrio );				
					}
				}
				
				this.addPageList();
				window.scrollTo( 0, 0 );
			}
		}
	}
};



