var ljRSSRequestObject = false; // XMLHttpRequest Object
var ljBackend = 'js/ljrss.php'; // ljBackend url
window.setInterval("ljUpdate_timer()", 1200000); // update the data every 20 mins


if (window.XMLHttpRequest) // try to create XMLHttpRequest
	ljRSSRequestObject = new XMLHttpRequest();

if (window.ActiveXObject)	// if ActiveXObject use the Microsoft.XMLHTTP
	ljRSSRequestObject = new ActiveXObject("Microsoft.XMLHTTP");

			var maxQuoteLength = 260;
			var maxLjQuotes = 3; // max number of quotes to load
			var ljQuotesContent = '';

/*
* onreadystatechange function
*/
function ljReqChange() {

	// If data received correctly
	if (ljRSSRequestObject.readyState==4) {

		// if data is valid
		if (ljRSSRequestObject.responseText.indexOf('invalid') == -1) 
		{ 	

			// Parsing RSS
			var node = ljRSSRequestObject.responseXML.documentElement; 

			// Get Channel information
			var channel = node.getElementsByTagName('channel').item(0);

			if (channel != null) {
			
			var ljQuotesArray = new Array();
			ljQuotesContent = '';
			
			// Browse items
			var items = channel.getElementsByTagName('item');

			for (var n=0; n < items.length; n++)
			{
				if ( items[n].getElementsByTagName('title').item(0) != null ) {
				var itemTitle = items[n].getElementsByTagName('title').item(0).firstChild.data;
				} else { itemTitle = '..'; }
				var itemLink = items[n].getElementsByTagName('link').item(0).firstChild.data;
				var itemContent = items[n].getElementsByTagName('description').item(0).firstChild.data;
				
				itemContent = itemContent.replace(/\n|\r\n?/g,' ');
				itemContent = itemContent.replace( new RegExp( "<br />", "gi" ), " " );
				itemContent = itemContent.replace( new RegExp( "<br>", "gi" ), " " );
				itemContent = itemContent.replace( new RegExp( "&lt;br /&gt;", "gi" ), " " );
				itemContent = itemContent.replace(regexpRemoveTags,"");
				
				if ( itemContent.length > maxQuoteLength ) {
					itemContent = itemContent.substring(0,maxQuoteLength)+'...';
				}

				ljQuotesArray.push(itemTitle+'|'+itemContent+'|'+itemLink);
				
			}
			
	//		ljQuotesArray.reverse();
			
			if ( ljQuotesArray.length > maxLjQuotes ) { 
				ljQuotesArray = ljQuotesArray.slice(0,maxLjQuotes);
			} 			
			
			for (var n=0; n <= ljQuotesArray.length-1; n++) {
				var currentQuoteArray = ljQuotesArray[n].split("|"); // 0 - title, 1 - content, 2 - link
				
				
				ljArrVars = arrowsVar.slice(0,4);
				
				shuffleArray(ljArrVars);
				
				ljQuotesContent += '<div id="ljQuoteId'+ n +'" class="colors textbox ljQuote"><img class="arrow '+ljArrVars[0]+'"  border="0" alt="" width="32" height="32" src="img/ep.gif"><div class="theQuote"><a style="text-decoration: none;" class="noUnderline" target="_blank" href="' + currentQuoteArray[2] + '">' + currentQuoteArray[1] + '</a></div></div>';
			}
			
			}

			// Display the result
			document.getElementById("ljBlogQuotes").innerHTML = ljQuotesContent;

			$(".ljQuote").each(function (i) {
				var currentTheQuote = '#' + $(this).attr('id') + ' .theQuote';
				$(currentTheQuote).vAlign();
			});	


			// Tell the reader the everything is done
//			document.getElementById("ljStatus").innerHTML = "Done.";
			
//			HideShow('ljStatus');
			ljFinished = 1;
			
		}
		else {
			// Tell the reader that there was error requesting data
//			document.getElementById("ljStatus").innerHTML = '<div class="colors textbox">Не могу загрузить блог...</div>';
			ljFinished = 1;
		}
		

		// finished
		
	}
	
}

/*
* Main AJAX RSS reader request
*/
function LoadLJ() {

	// change the status to requesting data
//	HideShow('ljStatus');
//	document.getElementById("ljStatus").innerHTML = '<div class="colors textbox">Подгружаю блог...</div>';
	
	// Prepare the request
	ljRSSRequestObject.open("GET", ljBackend , true);
	
	// Set the onreadystatechange function
	ljRSSRequestObject.onreadystatechange = ljReqChange;
	// Send
	ljRSSRequestObject.send(null); 

}

/*
* Timer
*/
function ljUpdate_timer() {
	LoadLJ();
}