var uid = 0001;
var statsArray = [];
var windowWidth = 0;
var windowHeight = 0;
var tilesWindowWidth = 0;
var tilesCounter = 0;


$(document).ready(function(){

	var randomnumber = createRandomNumber();
	loadXml('client/build_news_snippets_xml.asp?acctNumber='+uid+'&rnd='+randomnumber, processNewsSnippets);

});


function processNewsSnippets(theXml) {
	
	statsArray.length = 0;
	
	totalFavorites = 0;
	
	if($("statData", theXml).text() != 'none') {
	
	    $("statData", theXml).each(function(i) {
		
			var statItem = new Object();
	        statItem.id = i;
			statItem.siteDateAdded = $('siteDateAdded', this).text();
			statItem.siteTitle = unescape($('siteTitle', this).text());
			statItem.siteDescription = unescape($('siteDescription', this).text());

	        statsArray.push(statItem);
			
	    });
		
		createNewsSnippetTiles();

	}

}






function createNewsSnippetTiles() {

	var theDiv = $('#newsSnippets');
	
	$(theDiv).empty();
	
	tilesCounter = 0;
	
	var theHtml = '';
	
	theHtml +='<div class="newsSnippetsWrapper"><div class="newsSnippetsBorder"><div class="newsSnippetsPadding">';
	theHtml +='<h3>Newest News</h3>';
	
	$.each(statsArray, function(i){ 
	
		var theImageName = this.siteName;
		var theId = this.id;
		var theSiteId = this.siteId;
		var theSiteTitle = this.siteTitle;
		var theDateAdded = this.siteDateAdded;
		var theDescription = this.siteDescription;

		
		
		
		if(theDescription==''){
			theDescription='&nbsp;'
		}
		
		
		
		if(theDescription.length>=80){
			theDescription=stringLeft(theDescription,80)+'...<a href=news.asp>more</a>';
		}

		theHtml +='<div class="newsSnippetsContent"><div class="newsDetailsWrapper">';
		theHtml += '';

		theHtml += '<div class="newsSnippetsTitle">'+theSiteTitle+'</div>';
		theHtml += '<div class="newsSnippetsText">'+theDescription+'</div>';
		theHtml += '<div class="newsSnippetsDate">Added:&nbsp;'+theDateAdded+'</div>';

		theHtml += '</div></div>';
		theHtml +='<div class="clear vBuffer10"><div></div></div>';


	});
	
	theHtml +='</div></div></div>';
		
	theHtml +='<div class="clear vBuffer10"><div></div></div>';
	
	theDiv.append(theHtml);


}





function stringLeft(str, n){
	if (n <= 0)
	    return "";
	else if (n > String(str).length)
	    return str;
	else
	    return String(str).substring(0,n);
}





