$(document).ready(function() {	
	// Load the news feed:		
	var feed_url = "http://whatsnew.themovingbody.com.sg/feeds/posts/default";
	$.get("scripts/xml_proxy.php", {url: feed_url}, function(d) {
		var max_feeds = 40;	
		var html = "";
		var count = 0;
		$(d).find('entry').each(function() {
			if (count >= max_feeds) return;
			
			// Easy fields
			var $entry = $(this);
			var id = $entry.find('id').text();				
			var title = $entry.find('title').text();
			var content = $entry.find('content').text();
			
			// Date fields:				
			var published = parseDate($entry.find('published').text().substring(0,10));
			var updated = parseDate($entry.find('updated').text().substring(0,10));
			
			// Link:
			var link = "";
			$entry.find('link').each(function() {
				if ($(this).attr("type") == "text/html") link = $(this).attr("href");												  
			});
			
			// Author:
			var author = new Object();
			author.name = $entry.find('author').find('name').text();
			author.uri = $entry.find('author').find('uri').text();
			author.email = $entry.find('author').find('email').text();
			
			// Generate html:
			html 	+= '<div class="news_feed_item">';
			html 	+= '	<div class="news_feed_date">'+published.getDate()+'.'+(published.getMonth()+1)+'.'+published.getFullYear()+'</div>';
			html	+= '	<div class="news_feed_title">'+title+'</div>';
			html	+= '	<div class="news_feed_description">'+stripTags(content).substr(0,70)+'...</div>';
			html 	+= '	<div class="news_feed_readmore"><a href="'+link+'" target="_blank">...More</a></div>';
			html	+= '</div>';								
			
			// Divider?
			if (count%4 != 3 && count < max_feeds-1) html += '<img src="images/news_feed_divider.png" width="19" height="76" alt="divider" style="float:left" />';
			else html += '<div style="clear:both; height:9px"><!-- --></div>';
			
			count++;
		});		
		
		// Change the feed html content:
		$("#news_feed").html('<div id="news_feed_data">'+html+'</div>');
		$("#news_feed_data").fadeIn();
	});	
});

function nextFeeds() {
	var y_ini = 12;
	var new_y = parseInt($("#news_feed_data").css("top")) - 109;	
	if (new_y <= y_ini - $("#news_feed_data").height()) new_y = y_ini;
	$("#news_feed_data").animate({top: new_y});
}

function parseDate(dateStringInRange) {
	var isoExp = /^\s*(\d{4})-(\d\d)-(\d\d)\s*$/,
		date = new Date(NaN), month,
		parts = isoExp.exec(dateStringInRange);

	if(parts) {
	  month = +parts[2];
	  date.setFullYear(parts[1], month - 1, parts[3]);
	  if(month != date.getMonth() + 1) {
		date.setTime(NaN);
	  }
	}
	return date;
}

function stripTags(html) {
	return $('<div>').html(html).text();
}	

function launchWS(winName) {
	//window height and width
	myHeight = screen.height*.80;
	myWidth = 784;
	
	//widow height bounds
	if ( myHeight < 556 ) {
	myHeight = 556;
	} else if (myHeight>700) {
	myHeight = 700;
	}
	
	//get screen size, and cacl center screen positioning
	var height = screen.height;
	var width = screen.width;
	var leftpos = width / 2 - myWidth / 2;
	var toppos = (height / 2 - myHeight / 2) - 40;
	
	//open window 
	msgWindow=window.open(winName,"ws_window","toolbar=no,location=no,directories=no,resizable=yes,menubar=no,scrollbars=no,status=yes,width=" + myWidth + ",height="+ myHeight + ", left=" + leftpos + ",top=" + toppos);
	
	//focus window
	setTimeout('msgWindow.focus()',1);
}
