//
// functions.js
//
// Miscellaneous javascript code to support OrnothLand dynamic behaviors.
//
// 090905 odal Initial version
// 090910 odal Added getDiarySearch and getDiaryNew functions
// 110418 odal Added isLinkExternal, pmcClick functions

// Get the HTTP Object
function getHTTPObject(){
	if (window.ActiveXObject) return new ActiveXObject("Microsoft.XMLHTTP");
	else if (window.XMLHttpRequest) return new XMLHttpRequest();
	else {
		alert("Your browser does not support AJAX.");
		return null;
	}
}
 
// Change the value of the outputText field (main content area)
// Note that this is an AJAX callback function; don't call it directly
//   101119 odal added fade-in effect
function setOutput(){
	if(httpObject.readyState == 4){
		document.getElementById('outputText').innerHTML = httpObject.responseText;
		
		$('#outputText').fadeIn('slow'); // 101119
		
	}
}
 
// AJAX call to display diary entries for a particular quarter
//   101119 added hide effect
function getDiaryQuarter(yyyy,qq){

	$('#outputText').hide(); //101119

	// Set default values for year and quarter
	now = new Date;
	if (!yyyy) {
		theYear=now.getYear();
		if (theYear < 1900) theYear=theYear+1900;
		var yyyy = theYear; 
	}
	if (!qq) { 
		theMonth = now.getMonth();
		var qq = Math.floor(theMonth/3)+1;
	}
	
	// Call getdiary.php asking for a particular quarter
	httpObject = getHTTPObject();
	if (httpObject != null) {
		_gaq.push(['_trackPageview', "/getdiary.php/"+yyyy+"/"+qq]);
		httpObject.open("GET", "getdiary.php?m=quarter&y="+yyyy+"&q="+qq, true);
		httpObject.send(null);
		httpObject.onreadystatechange = setOutput;
	}
}
 
// AJAX call to display diary entries containing a particular search string
function getDiarySearch(searchstring) {

	// user hit the cancel button, exit silently
	if (null==searchstring) return;
	 
	// user left the search string blank, exit with message
	if (''==searchstring) { 
		alert("You didn't provide a search string. What are you looking for?");
		return;
	}

	// Call getdiary.php asking for matches for a particular search string
	httpObject = getHTTPObject();
	if (httpObject != null) {
		_gaq.push(['_trackPageview', "/getdiary.php/search&searchq"+searchstring]);
		httpObject.open("GET", "getdiary.php?m=search&q="+searchstring, true);
		httpObject.send(null);
		httpObject.onreadystatechange = setOutput;
	}
}

// AJAX call to display diary entries since a particular date
function getDiaryNew() {
	$lasttime = getCookie('diarylasttime');

	// If there's no last visit cookie, show the user some instructions
	if (''==$lasttime) { 
		document.getElementById('outputText').innerHTML = '<p>Whenever you select the What\'s New menu item, you will see only the diary entries that have been added since your last visit. That way, you always know what you have and have not read.</p><p>However, in order to ensure that you have some context, it will also show you any entries that appeared in the seven days prior to your last visit, as well.</p><p>Note that this will only be useful if you visit OrnothLand at least once a month. If you visit the site less frequently than that, it makes more sense to use the quarterly selections in the navigation menu.</p>';

	} else {
		// Call getdiary.php asking for everything new (php figures out the date range itself)
		httpObject = getHTTPObject();
		if (httpObject != null) {
			_gaq.push(['_trackPageview', "/getdiary.php/new"]);
			httpObject.open("GET", "getdiary.php?m=new", true);
			httpObject.send(null);
			httpObject.onreadystatechange = setOutput;
		}
	}	

	// And whenever we're called, we set the last visit cookie to today minus offset
	var offset = 7; // number of extra days' entries we want to show the user
	var d = new Date();
	d.setDate(d.getDate() - offset); 
	$thistime = d.getFullYear() + '-' + (d.getMonth()+1) + '-' + d.getDate();
	setCookie('diarylasttime',$thistime,90);
}

// Get um cookie
function getCookie(c_name) {
	if (document.cookie.length>0) {
	  c_start=document.cookie.indexOf(c_name + "=");
	  if (c_start!=-1){
	    c_start=c_start + c_name.length+1;
	    c_end=document.cookie.indexOf(";",c_start);
	    if (c_end==-1) c_end=document.cookie.length;
	    return unescape(document.cookie.substring(c_start,c_end));
	    }
	  }
	return "";
}

// Set um cookie
function setCookie(c_name,value,expiredays) {
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value)+((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}

// Eat um cookie
function delCookie(c_name) {
	var value = "";
	document.cookie=c_name+ "=" +escape(value)+";expires=-1";
}

// Is dis linky outbound?
function isLinkExternal(link) {
    var r = new RegExp('^https?://(?:www.)?' + location.host.replace(/^www./, ''));
        return !r.test(link);
}

// Register a PMC Click
function pmcClick(fromLink) {
//	alert("Clicked /outgoing/pmc" + fromLink);
	_gaq.push(['_setCustomVar',1,'PMC_clicker','Yes',1]);
	_gaq.push(['_trackPageview', '/outgoing/pmc' + fromLink]);
};

var httpObject = null;

