// functions used by the facebook app on the home pages


function processMessage(message) {
//	document.write(message + "<br>");
	adjMessage = "";													// set adjMessage to nothing
	tempMessage = message;
	index = 0;
	end = message.length;
	do {
		linkStart = tempMessage.search(/http/i);
		if (linkStart == -1) {											// no links
			adjMessage = adjMessage + tempMessage.substring(0,end);
			index = end;
		}
		else {															// has links
			if (linkStart == index) {									// at the start of link
				linkEnd = tempMessage.indexOf(" ");						// get the end of link
				if (linkEnd == -1) {						// link  with nothing after
					adjMessage = adjMessage + "<a href='" + tempMessage.substring(index,end) + "' target='_blank'>" + tempMessage.substring(index,end) + "</a>";
					index = end;
				}
				else {										// link that has more to follow
					adjMessage = adjMessage + "<a href='" + tempMessage.substring(index,linkEnd) + "' target='_blank'>" + tempMessage.substring(index,linkEnd) + "</a>";
					tempMessage = tempMessage.substring(linkEnd,end);
					index = linkEnd;
				}
			}
			else {											// next is text
				adjMessage = adjMessage + tempMessage.substring(0,linkStart);
				tempMessage = tempMessage.substring(linkStart,end);
				if (tempMessage == "") {
					index = end;
				}
				else {
					index = 0;
				}
			}
		}
	}
	while (index < end);
	
	adjMessage = adjMessage.replace(/\n/g,"<br>");
	
	return adjMessage;
//	document.write(adjMessage + "<br>");
//	document.write("<br>");
}
// *******************************************************************************************************************************************

// parse a post photo link to get the id
// of the album and create the link to it.
function getAlbum(link) {
	z = link.indexOf("set=a.");

	str = link.substring(z, link.length);

	a = str.indexOf(".");
	b = str.indexOf(".",a+1);
	c = str.indexOf(".",b+1);

	fbid = str.substring(a+1,b);
	aid = str.substring(b+1,c);
	id = str.substring(c+1, str.length);

	album = "http://www.facebook.com/album.php?fbid=" + fbid + "&id=" + id + "&aid=" + aid;

	return album;
}
// *************************************************************************************************************************************************

/*
This function can be used 2 ways. the first
requires date and time passed as numbers.
Remember months start at 0 with Jan.
This is also true of weekdays starting with Sun.
0:00 is midnight and 23:59 ends the day.
For this method use this function title.
*/
// ****************************************************************************************************
/*
// **** This may need to have time zone addressed on implementation

function showTime(cmonth, cday, cyear, chour, cminute, csecond)
{	
	// create the date object for the posts create date
	createDate = new Date();
	createDate.setMonth(cmonth.value,cday.value);
	createDate.setFullYear(cyear.value);
	createDate.setHours(chour.value, cminute.value, csecond.value,0);	// 0 is the milliseconds - no need
*/
// *************************************************************************************************************************************************

// the second use is for iCal formatted date-tame passed as a string

function showTime(iCalTime)
{

	// This bit of code will take an iCal date string and convert to a js Date object as a stand alone.
	// get the iCal string from the call object
	fbTime = iCalTime
	
	// convert all the string values to integer
	cyear = parseInt(fbTime.slice(0,4),10);
	cmonth = (parseInt(fbTime.slice(5,7),10)) - 1;			// subtract one to move the month number into js month form Jan=1 in iCal, Jan=0 in js
	cday = parseInt(fbTime.slice(8,10),10);
	chour = parseInt(fbTime.slice(11,13),10);
	cminute = parseInt(fbTime.slice(14,16),10);
	csecond = parseInt(fbTime.slice(17,19),10);
	
	// create the date object for the posts create date
	createDate = new Date();
	createDate.setMonth(cmonth,cday);
	createDate.setFullYear(cyear);
	createDate.setHours(chour,cminute,csecond,0);			// 0 is the milliseconds - no need
	
// ******************************************************************************************************
	
	// create the current date object and UTC
	currentDate = new Date();
	currentYear = currentDate.getFullYear();
	currentUtC = Date.parse(currentDate);
	
	// get the users current time zone, sets direction 
	// multiplying by -1 sets correct adjustment direction
	currentTimeZone = -1 * currentDate.getTimezoneOffset();	

// ********************************************************************************************************
/*
	// It appears that get Timezone Offset now takes care of DST as well
	
	// compensate for end user timezone as createDate is GMT
	// compares a winter and summer date to determine DST or not.
	Date.prototype.stdTimezoneOffset = function() {
		jan = new Date(this.getFullYear(), 0, 1);
		jul = new Date(this.getFullYear(), 6, 1);
		return Math.max(jan.getTimezoneOffset(), jul.getTimezoneOffset());
	}
	
	// returns true if it is DST
	Date.prototype.dst = function() {
		return this.getTimezoneOffset() < this.stdTimezoneOffset();
	}
	
	// set timezone offset for user
	if (currentDate.dst()) {
		offset = currentTimeZone + 60;							// daylight savings time add 60 minutes
	}
	else {
		offset = currentTimeZone;
	}
// substitute offset in place of (currentTimeZone) in statement below if DST adjustment is needed.
// It appears currentTimeZone takes care of DST as well presently.
*/	
// ******************************************************************************************************************
	
	// adjust the createDate to users timezone
	createDate.setMinutes(createDate.getMinutes() + currentTimeZone);

	// convert the adjusted create date to UTC
	createUtC = Date.parse(createDate);

	// array of full names as UTC uses abbreviations
	weekday = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];
	longMonth = ["January","February","March","April","May","June","July","August","September","October","November","December"];
	timeAmPm = ["12","1","2","3","4","5","6","7","8","9","10","11"];
	
	// get the parts of the create date
	createYear = createDate.getFullYear();
	createMonth = longMonth[createDate.getMonth()];
	createDayNum = createDate.getDate();
	createHour24 = createDate.getHours();								// returns 24h time
	createMinute = createDate.getMinutes();
	createDay = createDate.getDay();
	
	// prog trick to format leading 0 on single digit date
	// add 100 change to string and grab last two elements
	createMinute = ((createMinute + 100).toString()).slice(-2);
	
	// change to 12h time and am-pm
	if (createHour24 <= 11) {
		createAmPm = "am";
		if (createHour24 == 0 ) {										// if hour is 0 - it is midnight and needs to show 12
			createHour = 12;
		}
		else {
			createHour = createHour24;
		}
	}
	else {
		createAmPm = "pm";
		createHour = timeAmPm[createHour24 - 12];						// -12 changes 24h to 12h equivalent except for noon - array above takes care of this
	}
	
	// set up the output date and time
	outputShortDate = createMonth + " " + createDayNum;
	outputLongDate = createMonth + " " + createDayNum + ", " + createYear;
	outputTime = createHour + ":" + createMinute + " " + createAmPm;
	
	if (createYear == currentYear) {										// only process for current year - older outputs date-time
		elapsedTime = currentUtC - createUtC;
	
		if (elapsedTime < 0 ) {												// handle a negative time as an error and show nothing
			outputStr = "";
		}
		else {
			if (elapsedTime == 0 ) {										// if 0 then make it 1 second
				elapsedTime = 1000;
			}
		
			if (elapsedTime < 60000){
			   outputStr = elapsedTime/1000 + " seconds ago";				//displays seconds 1 to 59.5
			}
			else if (elapsedTime < 3570000){								// at 59.5 sec go ahead and call it 1 minute
				checkMin = Math.round(elapsedTime/60000);
				if (checkMin == 1){
			   		outputStr = "about 1 minute ago";
			   	}
			   	else {
			   		outputStr = checkMin + " minutes ago<br>";				//displays 2 min to 59.5
			   	}
			}
			else if (elapsedTime < 5400000){								//displays 1 hour starting and 59.5 min - 1.5 hr
			   outputStr = "about 1 hour ago";
			}
			else if (elapsedTime < 84564000){
			   outputStr = Math.round(elapsedTime/3600000) + " hours ago";	//displays hr 1.5 to 23.5
			}
			else if (elapsedTime < 171000000){
			   outputStr = "Yesterday at " + outputTime;					//displays yesterday 23.5 hr to 47.5 hr (day 1)
			}
			else if (elapsedTime < 430200000){
				outputStr = weekday[createDay] + " at " + outputTime;		//displays day name and time 47.5 hr to 119.5 hr (day2-4)			
			}
			else {
				outputStr = outputShortDate + " at " + outputTime;			// displays date-time after 4 days for current year
			}
		}
	}
	else {
		outputStr = outputLongDate + " at " + outputTime;					// displays date-time for past years
	}
	return outputStr;
}
// *************************************************************************************************************************************************


