var jtrans_xmlhttp, jtrans_news_items=false, jtrans_current_item=-1, jtrans_timerbar, jtrans_btimer=false;

if (window.XMLHttpRequest) {
	// IE7+, Firefox, Chrome, Opera, Safari
  jtrans_xmlhttp = new XMLHttpRequest();
}
else {
	// IE6, IE5
  jtrans_xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}

var jtrans_imgcache;
function jtrans_load_news(s) {
	var json, h, i, j, age, now, when;
	jtrans_news_items = new Array();
	jtrans_imgcache = new Array();
	
	if (JSON && JSON.parse) {
		json = JSON.parse(s);
	}
	else {
		// json = eval(s);
		json = !(/[^,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]/.test(s.replace(/"(\\.|[^"\\])*"/g, ''))) && eval('(' + s + ')');
	}
	
	now = new Date();
	now = now.getTime();
	j = 0;
	for (i in json) {
		h = '';
		age = (now - json[i].pub_date) / 1000;
		if (age < 50) when = 'Less than 1 minute ago';
		else if (age < 95) when = 'About one minute ago';
		else if (age<50*60) when = Math.round(age/60)+' minutes ago';
		else if (age<95*60) when = 'About an hour ago';
		else if (age<23*3600) when = Math.round(age/3600)+' hours ago';
		else if (age<1.6*86400) when = 'Yesterday';
		else if (age<8*86400) when = Math.round(age/86400)+' days ago';
		else if (age<1.6*604800) when = 'About a week ago';
		else if (age<40*604800) when = Math.round(age/604800)+' weeks ago';
		else if (age<1.6*31536000) when = 'About a year ago';
		else when = Math.round(age/31536000)+' years ago';
		when = when.replace(/ /g,' '); // replace spaces with nonbreaking spaces
		if (json[i].thumbnail.length > 0) {
			h += '<div class="newsthumb"><a href="'+json[i].link+'"><img src="'+json[i].thumbnail+'" width="66" height="49" alt="" /></a></div>\n';
			jtrans_imgcache[j] = new Image();
			jtrans_imgcache[j].src = json[i].thumbnail;
			j++;
		}
		h += '<p><big><strong><a href="'+json[i].link+'">'+json[i].title+'</a></strong></big></p>\n';
		h += '<p>'+json[i].description+' <small>('+when+')</small></p>\n';
		jtrans_news_items[i] = h;
	}
	jtrans_news_items[i] = document.getElementById('newsbox-inner').innerHTML;
	setTimeout('jtrans_start_timer()',10000);
}



function jtrans_init_news() {
	jtrans_xmlhttp.onreadystatechange = function() {
		if (jtrans_xmlhttp.readyState==4 && jtrans_xmlhttp.status==200) {
			jtrans_load_news(jtrans_xmlhttp.responseText);
		}
	}
	jtrans_xmlhttp.open("GET",document.location.protocol + "//japanesetranslator.co.uk/wp-content/themes/jt/scripts/json.php",true);
	jtrans_xmlhttp.send();
}



function jtrans_cycle_news() {
	jtrans_current_item = (++jtrans_current_item) % jtrans_news_items.length;
	document.getElementById('newsbox-inner').innerHTML = jtrans_news_items[jtrans_current_item];
	if (jtrans_btimer) clearTimeout(jtrans_btimer);
	jtrans_btimer = false;
	jtrans_timerbar = 0;
	document.getElementById('newsbox-timer').style.width = '0%';
	setTimeout('jtrans_start_timer()',10000);
}


function jtrans_start_timer() {
	jtrans_timerbar = 0;
	jtrans_btimer = setInterval('jtrans_bump_timer()',20);
}

function jtrans_bump_timer() {
	jtrans_timerbar++;
	if (jtrans_timerbar==100) {
		clearTimeout(jtrans_btimer);
		setTimeout('jtrans_cycle_news()',150);
	}
	document.getElementById('newsbox-timer').style.width = jtrans_timerbar+'%';
}
if (typeof(window.addEventListener) !== 'undefined') window.addEventListener("load", jtrans_init_news, false);
else if (typeof(window.attachEvent) !== 'undefined') window.attachEvent( "onload", jtrans_init_news);

