/////////////////////////////////////////////////////////////////////
//Finds the difference between a specific time and the current time//
//Format: getTimeElapsed("YYYY-MM-DD HH:MM:SS")                    //
/////////////////////////////////////////////////////////////////////

function getTimeElapsed(pasttime){

	//Seperates date elements into specific variables
	/////////////////////////////////////////////////
	var dateandtime = pasttime.split(' ');
	var date = dateandtime[0].split('-');
	var time = dateandtime[1].split(':');
	var year = date[0];
	var month = date[1];
	var day = date[2];
	var hour = time[0];
	var minute = time[1];
	var second = time[2];
	/////////////////////////////////////////////////
	//


	//Seperates current date elements into specific 
	//variables
	/////////////////////////////////////////////////
	var d = new Date();
	var curyear = d.getUTCFullYear();
	var curmonth = d.getUTCMonth()+1;
	var curday = d.getUTCDate();
	var curhour = d.getUTCHours();
	var curminute = d.getUTCMinutes();
	var cursecond = d.getUTCSeconds();
	/////////////////////////////////////////////////
	//


	//Puts date/time differences into variables
	/////////////////////////////////////////////////
	var yeardiff = curyear-year;
	var monthdiff = curmonth-month;
	var daydiff = curday-day;
	var hourdiff = curhour-hour;
	var minutediff = curminute-minute;
	var seconddiff = cursecond-second;
	/////////////////////////////////////////////////
	//


	//Corrects negative values in time differences
	/////////////////////////////////////////////////
	var timedifference = "";
	var trueyeardiff = yeardiff;

	if(monthdiff>=0){
		var truemonthdiff = monthdiff;
	}else{
		var truemonthdiff = (12-month+curmonth);		
	}

	if(daydiff>=0){
		var truedaydiff = daydiff;
	}else{
		var truedaydiff = (daysInMonth((curmonth-1), curyear) - parseInt(day)+curday);
	}

	if(hourdiff>=0){
		var truehourdiff = hourdiff;
	}else{
		var truehourdiff = (24-hour+curhour);
	}

	if(minutediff>=0){
		var trueminutediff = minutediff;
	}else{
		var trueminutediff = (60-minute+curminute);
	}

	if(seconddiff>=0){
		var trueseconddiff = seconddiff;
	}else{
		var trueseconddiff = (60-second+cursecond);
	}
	/////////////////////////////////////////////////
	//


	//Returns a properly formatted time difference for
	//each date/time element
	/////////////////////////////////////////////////
	var inputdate = new Date(Date.UTC(year,month,day,hour,minute,second));
	var currentdate = new Date(Date.UTC(curyear,curmonth,curday,curhour,curminute,cursecond));
	var inputseconds = inputdate.getTime()/1000.0;
	var currentseconds = currentdate.getTime()/1000.0;

	var differenceInSec = currentseconds-inputseconds;

	if(differenceInSec<60){
		return trueseconddiff+" seconds ago";
	}
	if(differenceInSec>=60 && differenceInSec<(60*60)){
		return trueminutediff+" minutes ago";
	}
	if(differenceInSec>=(60*60) && differenceInSec<(60*60*24)){
		return truehourdiff+" hours ago";
	}

	var inputmonthdays = daysInMonth((month), year);
	if(differenceInSec>=(60*60*24) && differenceInSec<(60*60*24*inputmonthdays)){
		return truedaydiff+" days ago";
	}

	var secondsinyear = 0;
	for(var i=0;i<12;i++){
		secondsinyear += daysInMonth(i,year)*60*60*24;
	}

	if(differenceInSec>=(60*60*24*inputmonthdays) && differenceInSec<secondsinyear ){
		if(truemonthdiff!=0 && truedaydiff!=0){
			return truemonthdiff+" months, "+truedaydiff+" days ago";
		}
	}

	return trueyeardiff+" years, "+truemonthdiff+" months, "+truedaydiff+" days ago";
	/////////////////////////////////////////////////
	//

}


//A Simple function that finds the number of
//days in any given month
/////////////////////////////////////////////////
function daysInMonth(month,year) {
	var dd = new Date(year, month, 0);
	return dd.getDate();
} 
/////////////////////////////////////////////////
//



//Sets value of a span element with the specified
//[idname]
//
//This function should be placed in the [onload]
//attribute of the <body> tag
/////////////////////////////////////////////////
function setAllElements(idname){
	var spans = document.getElementsByTagName('span');
	var spanArrayLength = spans.length;

	for(num=0;num<spanArrayLength;num++){
		if(spans[num].className == idname){
			var pasttime = spans[num].getAttribute('alt');
			if(pasttime){
				var timepassed = getTimeElapsed(pasttime);
				spans[num].innerHTML = timepassed;
			}
		}
	}
}
/////////////////////////////////////////////////
//
