/*
 * --------------------------------------------------------------------------------------------------------------------
 * News entries list functions
 * --------------------------------------------------------------------------------------------------------------------
 */
/**
 * Template for the news
 * @type {String}
 */
var sEntryTpl;
/**
 * Current page count
 * @type {Integer}
 */
var iCurrentPage = 1;
/**
 * Number of total result pages
 * @type {Integer}
 */
var iNbrPages = 1;
// misc generals
/**
 * Module name
 * @type {String}
 */
var sModuleName = 'news';

function InitEntriesList (){
	var _oEntriesContainer = document.getElementById('newsEntries');
	sEntryTpl = _oEntriesContainer.innerHTML;
	_oEntriesContainer.innerHTML = '';
};

function InitListScript(pRootUrl){

	sRootUrl = pRootUrl;
	InitEntriesList();
	UpdateListing();
};

function UpdateListing(){
	if(CreateXMLHttpRequest()) {
    	RequestNewsList();
    }
};

function RequestNewsList() {
	
	// build Url
	var _sUrl = sRootUrl;
	// build param string to send by Post
	_sUrl += '?module=' + sModuleName + '&page=ajax&action=list';
	_sUrl += '&CurrentPage=' + iCurrentPage;
	
	// Execute the request
	oHttpRequest.onreadystatechange = BuildListing;
	oHttpRequest.open('get', _sUrl, true);
	oHttpRequest.send(null);	
};

function BuildListing() {
	
	if (oHttpRequest.readyState == 4) {
		if (oHttpRequest.status == 200) {
			
			var _oEntriesContainer = document.getElementById('newsEntries');
			_oEntriesContainer.innerHTML = '';
			
			// Put response into xmlobject
			var _xmlDoc = oHttpRequest.responseXML;
			
			// Get the number of results
			_iTotalResultCount = _xmlDoc.documentElement.getElementsByTagName('noLimit_count').item(0).firstChild.nodeValue;
			
			// Get the news in an array, loop through it and create a row for each news			
			var _arrXmlData = _xmlDoc.documentElement.getElementsByTagName('news');
			
			if (_arrXmlData.length > 0) {
				// we got results
				
				// check if the user wants to display all results on one page
				var _iEntriesPerPage = 10;//document.getElementById('ResultsPerPage').value;
				if(_iEntriesPerPage > 0){
					// calculate the number of pages we have
					var _iRest = _iTotalResultCount % _iEntriesPerPage;
					iNbrPages = Math.floor(_iTotalResultCount/_iEntriesPerPage);
					if (_iRest > 0) {iNbrPages++;}
				} else {
					// no results
					iCurrentPage = 1;
					iNbrPages = 1;
				}
			} else {
				// no results
				iCurrentPage = 1;
				iNbrPages = 1;
				_iTotalResultCount = 0;
			}
			document.getElementById('NewsCurrentPageTop').innerHTML = iCurrentPage;
			document.getElementById('NewsPageCountTop').innerHTML = iNbrPages;
			document.getElementById('NewsResultsCountTop').innerHTML = _iTotalResultCount;
			
			document.getElementById('NewsCurrentPageBottom').innerHTML = iCurrentPage;
			document.getElementById('NewsPageCountBottom').innerHTML = iNbrPages;
			document.getElementById('NewsResultsCountBottom').innerHTML = _iTotalResultCount;
			
			// Show/Hide next and previous results entries buttons
			if (iCurrentPage > 1){
				ShowInlineElement('PreviousBottom');
				ShowInlineElement('PreviousTop');
			} else {
				HideInlineElement('PreviousBottom');
				HideInlineElement('PreviousTop');
			}
			
			if (iCurrentPage < iNbrPages) {
				ShowInlineElement('NextBottom');
				ShowInlineElement('NextTop');
			} else {
				HideInlineElement('NextBottom');
				HideInlineElement('NextTop');
			}
	
			var _iNbr = _iTotalResultCount - ((iCurrentPage-1) * _iEntriesPerPage) + 1;
			
			for (var i = 0; i < _arrXmlData.length; i++) {
				
				var _iElementId = _arrXmlData[i].getElementsByTagName('id').item(0).firstChild.nodeValue;
				// Build new row
				var _sNewEntry = sEntryTpl;
				
				/* get the news data from the xml tags */
				var _sTitle = _arrXmlData[i].getElementsByTagName('title').item(0).firstChild.nodeValue;
				var _sText = _arrXmlData[i].getElementsByTagName('text').item(0).firstChild.nodeValue;
				var _sCreationDate = _arrXmlData[i].getElementsByTagName('creation_date').item(0).firstChild.nodeValue;
				var _sCreationTime = _arrXmlData[i].getElementsByTagName('creation_time').item(0).firstChild.nodeValue;
				
				_sNewEntry = _sNewEntry.replace(/__Text/gi, _sText.replace(/\n/gi, '<br />'));
				_iNbr -= 1;
				_sNewEntry = _sNewEntry.replace(/__Title/gi, "N°" + _iNbr + " :: " + _sTitle + " :: " + _sCreationDate + " " + _sCreationTime);
				_oEntriesContainer.innerHTML += _sNewEntry;
			}// End foreach news
		} else {
			alert('Bei dem Request ist ein Problem aufgetreten.');
		}
		//HideBlockElement('NewsLoadingList'); 
	}
};

function GotToResultEntriesPage (pPageNumber) {
	
	var _iPage = pPageNumber;
	
	if((_iPage != '') && (_iPage != iCurrentPage) && (_iPage <= iNbrPages)){
		iCurrentPage = _iPage;
		HideInlineElement('PreviousBottom');
		HideInlineElement('PreviousTop');
		HideInlineElement('NextBottom');;
		HideInlineElement('NextTop');
		UpdateListing();	
	}
};

function GotToLastPage(){
	GotToResultEntriesPage(iNbrPages);
}
function GotToFirstPage(){
	GotToResultEntriesPage(1);
}

function PreviousResultEntries() {
	iCurrentPage--;
	HideInlineElement('PreviousBottom');
	HideInlineElement('PreviousTop');
	HideInlineElement('NextBottom');
	HideInlineElement('NextTop');
	UpdateListing();	
};

function NextResultEntries() {
	iCurrentPage++;
	HideInlineElement('PreviousBottom');
	HideInlineElement('NextBottom');
	UpdateListing();	
};



