/**
Start our xmlhttp object.
*/
function initXMLHTTP() {
	var xmlhttp;
	/*@cc_on @*/
	/*@if (@_jscript_version >= 5)
  	try {
  		xmlhttp=new ActiveXObject("Msxml2.XMLHTTP")
 	} catch (e) {
  		try {
    			xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
  		} catch (E) {
   			xmlhttp=false
  		}
 	}
	@else
 	xmlhttp=false
	@end @*/
	if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
	 	try {
	  		xmlhttp = new XMLHttpRequest();
	 	} catch (e) {
	  		xmlhttp=false;
	 	}
	}	
	return xmlhttp;
}

function main() {
	document.getElementById('date').innerHTML = getLocalDay();
	//getTopStory();
}

function getLocalDay() {
	myDate = new Date();
	month = month2text(myDate.getMonth());
	day = myDate.getDate();
	year = parseInt(myDate.getYear())+1900;
	return month + " " + day + " of " + year;
}

function getTopStory() {
	xmlhttp = initXMLHTTP();
	xmlhttp.open("GET","/?action=get-top-story",true);
	xmlhttp.onreadystatechange = function() {
		if(xmlhttp.readyState == 4) {
			var article = xmlhttp.responseXML.getElementsByTagName('article')[0];
			var title = article.getElementsByTagName('title')[0].firstChild.nodeValue;
			var content = article.getElementsByTagName('description')[0].firstChild.nodeValue;
			content = content.substring(0, content.indexOf('</p>'));
			var date_added = article.getElementsByTagName('pubDate')[0].firstChild.nodeValue;
			var id = article.getElementsByTagName('id')[0].firstChild.nodeValue;
			document.getElementById('top_story').innerHTML='<h4>' + title + '</h4><div>' + content + '</div><a class="read_more" href="" onclick="readarticle(\'last\');return false;" >Read more</a>';
			getRSSHeaders();
		}
	}
	xmlhttp.send(null);
	return false;
}

function getRSSHeaders() {
	xmlhttp = initXMLHTTP();
	xmlhttp.open("GET","/?action=get-rss",true);
	xmlhttp.onreadystatechange = function() {
		if(xmlhttp.readyState == 4) {
			items = xmlhttp.responseXML.getElementsByTagName('item');
			var headers = document.createElement('div');
			headers.setAttribute('id', 'headers_div');
			var zeLink = '';
			for (var idx = 0 ; idx<items.length ; idx++) {
				var tmpItem = items[idx];
				var pubDate = tmpItem.getElementsByTagName('pubDate')[0].firstChild.nodeValue;
				var niceDate = pubDate.substring(0, pubDate.indexOf(' '));
				var title = document.createTextNode(niceDate + " - " + tmpItem.getElementsByTagName('title')[0].firstChild.nodeValue);
				zeLink = document.createElement('a');
				var zeid = parseInt(tmpItem.getElementsByTagName('id')[0].firstChild.nodeValue);
				//zeLink.setAttribute('href', tmpItem.getElementsByTagName('link')[0].firstChild.nodeValue);
				zeLink.setAttribute('href', '?;' );
				zeLink.setAttribute('onclick' , 'readarticle(' + zeid + ');return false;');
				
				zeLink.appendChild(title);
				headers.appendChild(zeLink);
				zeid=0;
			}
			document.getElementById('rss_headers').appendChild(headers);
		}
	}
	xmlhttp.send(null);
	return false;
}

function month2text(monthNum) {
	switch(monthNum) {
		case 0: return "January";
		case 1: return "February";
		case 2: return "March";
		case 3: return "April";
		case 4: return "May";
		case 5: return "June";
		case 6: return "July";
		case 7: return "August";
		case 8: return "September";
		case 9: return "October";
		case 10: return "November";
		case 11: return "December";
		default: return '';
	}
}

function readarticle(article_id) {
	document.getElementById('top_story_container').style.display = 'none';
	xmlhttp = initXMLHTTP();
	xmlhttp.open("GET","/?action=get-story&story_id=" + article_id,true);
	xmlhttp.onreadystatechange = function() {
		if(xmlhttp.readyState == 4) {
			var article = xmlhttp.responseXML.getElementsByTagName('article')[0];
			var title = article.getElementsByTagName('title')[0].firstChild.nodeValue;
			var content = article.getElementsByTagName('description')[0].firstChild.nodeValue;
			var date_added = article.getElementsByTagName('pubDate')[0].firstChild.nodeValue;
			var id = article.getElementsByTagName('id')[0].firstChild.nodeValue;
			document.getElementById('article_read').innerHTML='<span class="date">' + date_added + '</span><h4>' + title + '</h4><div>' + content + '</div>';
			document.getElementById('send-to-friend').style.display='block';
			document.body.scrollTop=0;
		}
	}
	xmlhttp.send(null);
	return false;
}

function SendToFriend() {
	em_from = document.getElementById('input-friend-from').value;
	em_to = document.getElementById('input-friend-to').value;
	em_msg = escape(document.getElementById('tx-friend-message').value);
	
	xmlhttp = initXMLHTTP();
	xmlhttp.open("GET","/?action=sendfriend&from=" + em_from + "&to=" + em_to + "&msg=" + em_msg ,true);
	xmlhttp.onreadystatechange = function() {
		if(xmlhttp.readyState == 4) {
			var article = xmlhttp.responseXML.getElementsByTagName('article')[0];
			var title = article.getElementsByTagName('title')[0].firstChild.nodeValue;
			var content = article.getElementsByTagName('description')[0].firstChild.nodeValue;
			var date_added = article.getElementsByTagName('pubDate')[0].firstChild.nodeValue;
			var id = article.getElementsByTagName('id')[0].firstChild.nodeValue;
			document.getElementById('top_story').innerHTML='<h4>' + title + '</h4><div>' + content + '</div>';
			getRSSHeaders();
		}
	}
	xmlhttp.send(null);
	return false;
}

function viewArchives() {
	
}

function printMe() {
	window.print();
}