// Requires clarkeology.com/js/library.js, just for the "addEvent" I think

/**
 * Really old! Pre jquery
 * 
 * @name	processReferer.js
 * @author	PC <paulypopex+php@gmail.com>
 * @date	Wed Sep 29 09:58:07 BST 2010 fixing bing referer error
 */


$( function () {
	var qVar = new Array( "q", "p", "query", "results", "qry", "qry_str", "searchfor", "keywords" );
	var r = document.referrer;
	var h = location.href;
	var refererInfo = "";
	var searchTerm = "";

	// Set up like this so I can include this script using HTML::Template or just include it as is
	var boardScript = '<tmpl_var name="boardScript">';
	if (	boardScript == '' || boardScript.indexOf( "<" ) == 0 ) {
		boardScript = "/cgi-bin/board.cgi";
	}
	var searchScript = "";
	var xmlScript = '<tmpl_var name="xmlScript">';
	if (	xmlScript == '' || xmlScript.indexOf( "<" ) == 0 ) {
		xmlScript = "/php/xml.php";
	}
	var bidVar = '<tmpl_var name="bidVar">';
	if (	bidVar == '' || bidVar.indexOf( "<" ) == 0 ) {
		bidVar = "b";
	}
	var fVar = '<tmpl_var name="fVar">';
	if (	fVar == '' || fVar.indexOf( "<" ) == 0 ) {
		fVar = "f";
	}

	if (	h.indexOf( "stoke" ) != -1 ) {
		// Got some site specific stuff in here, blog URL used to be clarkeology.com/stokey/
		refererInfo += '<p>We have left Stoke Newington now but the archives of this blog might have what you are looking for... proper address for this site is <a href="http://www.clarkeology.com/blog/">www.clarkeology.com/blog</a>, keep reading if you\'re interested in what I\'m up to now, or <a href="' + boardScript + '"?' + bidVar+ '=3&' + fVar + '=1">go search</a>... most likely you want to go straight to <a href="http://www.clarkeology.com' + searchScript + '/Il+Bacio">Il Bacio</a>...</p>';
	}
	if (	r && r != "" ) {
		var baseDomain = location.hostname;
		if (	r.indexOf( "http://" + baseDomain ) == -1 ) {
			// d( 'Thanks for visiting this site from <a rel="nofollow" href="' + r + '">' + r + '</a>' );
			for (	var i = 0; i < qVar.length; i ++ ) {
				var p = r.indexOf( qVar[i] + "=" );
				if (	p != -1 ) {
					// d( "p is " + p );
					var offset = qVar[i].length + 1;
					searchTerm = r.substring( p + offset, r.length );
					var e = searchTerm.indexOf( "&" );
					if (	e != -1 ) {
						searchTerm = searchTerm.substring( 0, e );
					}
					searchTerm = unescape(( searchTerm.split( "+" )).join( " " ));
					// d( "searchTerm is " + searchTerm );
					searchTerm = searchTerm.replace( /folkestone/i, "" );
					searchTerm = searchTerm.replace( /^\s+/, "" );
					searchTerm = searchTerm.replace( /\s+$/, "" );
					searchTerm = searchTerm.replace( /<.+$/, "" );
					// d( "searchTerm is now " + searchTerm );
					if (	searchTerm && searchTerm != "" ) {
						refererInfo += '<p>Were you actually looking for <a href="' + searchScript + '/' + escape( searchTerm.replace( /\s+/g, '+' )) + '">' + searchTerm + '</a>?</p>';
						if (	searchTerm.indexOf('nude') != -1 || searchTerm.indexOf('naked') != -1 ) {
							refererInfo += '<p>If <a rel="nofollow" href="' + searchScript + '/' + escape( searchTerm ) + '">' + searchTerm + '</a> is something rude, or illegal you will not find it on this site and I may report you to the interweb porno police.</p>';
						}
					}
				}
			}
		}
		// else {	d( document.referrer + ' is like ' + baseDomain );
		// }
	}
//	if (	location.href.indexOf( "#landingPageTest" ) != - 1 ) {
//		searchTerm = "landing page";
//	}
//	if (	searchTerm ) {
//		var newDiv = document.createElement("div");
//		var parentElement = document.body;
//		var infoPane = document.getElementById("infoPane");
//		if (	infoPane ) {
//			parentElement = infoPane;
//		}
//		parentElement.appendChild(newDiv);
//		newDiv.name = newDiv.id = 'refererInfo';
//		newDiv.style.display = "inline";
//		newDiv.innerHTML = refererInfo;
//		d( "h is " + h + " and xmlScript is " + xmlScript + ", is it a search already?" );
//		// Disable this if it's a cgi already, too many cgi requests...
//		if (	window.jQuery && $('body.wiki').length ) {
//			// alert( 'aborting this is already a search page' );
//		}
//		else if (	h.indexOf( "cgi" ) == -1 || xmlScript.indexOf( "cgi" ) == -1 ) {
//			var newDiv = document.createElement("div");
//			var parentElement = document.body;
//			parentElement.appendChild(newDiv);
//			newDiv.name = newDiv.id = 'ajaxRefererLinks';
//			var closeTag = document.createElement("a");
//			closeTag.id = "closeTag";
//			closeTag.style.cssFloat = "right";
//			closeTag.href = "javascript:void(closeAjaxRefererLinks());";
//			closeTag.title = "Close this window";
//			closeTag.innerHTML = "X";
//			newDiv.appendChild(closeTag);
//			var title = document.createElement("h1");
//			title.innerHTML = "Related links";
//			newDiv.appendChild(title);
//
//			// try {
//			// 	if ( ajaxLoading ) {
//			// 		var loadingP = document.createElement("p");
//			// 		loadingP.appendChild( ajaxLoading( ));
//			// 		newDiv.appendChild(loadingP);
//			// 	}
//			// 	else {
//			// 		d( "Hmm, no ajaxLoading..." );
//			// 	}
//			// }
//			// catch ( e ) {
//			// 	d( "ajaxLoading error; " + e );
//			// }
//
//			// var url = xmlScript + "?mode=rss&xml=" + searchTerm;
//			var url = '/' + escape( searchTerm ) + '.xml';
//			url = url.replace(/%20/g, '+' );
//			// var url = xmlScript + "?xml=" + searchTerm;
//			// d( "Loading " + url );
//			window.loadPage ? loadPage( function ( request ) {
//				d( request.responseText );
//				var ul = document.createElement("ul");
//				try {	xmlDoc = request.responseXML;
 //                               	var items = request.responseXML.documentElement.getElementsByTagName("item");
//					d( "Got " + items.length + " items in the rss" );
 //                                       for (   var i = 0; i < items.length; i ++ ) {
//						// d( i );
//						var t = '';
 //                                               try {	
  //                                              	// t = items[i].getElementsByTagName('title').firstChild.nodeValue;
//							t = getFromXml( items[i], 'title' );
//							// d( "title is " + t );
//						}
//						catch ( e ) {
//							d( "Error getting title; " + e );
//						}
//						var l = '';
//						try {	// l = items[i].getElementsByTagName('link').firstChild.nodeValue;
//							l = getFromXml( items[i], 'link' );
//							d( "link is " + l );
//						}
//						catch ( e ) {
//							d( "Error getting title; " + e );
//						}
//						if (	t && l ) {
//							var li = document.createElement("li");
//							var a = document.createElement("a");
//							a.innerHTML = t;
//							a.href = l;
//							a.title = "Click to read this in full...";
//							li.appendChild(a);
//							ul.appendChild(li);
//						}
//						else {	d( "Didn't get t (" + t + ") or l (" + l + ")" );
//						}
//					}
//				}
//				catch ( e ) {
//					d( "Error in responseXML parsing; " + e );
//				}
//				d( "Got " + ul.childNodes.length + " items in the ul after xml parsing" );
//				if (	ul.childNodes.length > 0 ) {
//					var results = request.responseText.split("\n");
//					d( "Got " + results.length + " csv results" );
//					if (	results.length > 1 ) { // 1 line = no results...
//						for (	var i = 0; i < results.length; i ++ ) {
//							// d( "results " + i + " is '" + results[i] + "'" );
//							var resultLine = results[i].split(",");
//							if (	resultLine[0] && resultLine[4] ) {
//								var li = document.createElement("li");
//								var a = document.createElement("a");
//								a.innerHTML = resultLine[1];
//								// a.href = boardScript + "?mParent=" + resultLine[0]; // duh change this
//								a.href = resultLine[4]; // Just a bodge until we XML it properly...
//								a.title = "Click to read this in full...";
//								li.appendChild(a);
//								ul.appendChild(li);
//							}
//						}
//						newDiv.appendChild(ul);
//						window.$ && $( loadingP ).remove();
//					}
//				}
//				d( "Got " + ul.childNodes.length + " items in the ul" );
//				if (	! ul.childNodes.length || ul.childNodes.length == 0 ) {
//					var p = document.createElement("p");
//					p.innerHTML = "Found nothing, might have just timed out though...";
//					newDiv.appendChild(p);
//					window.$ && $( loadingP ).remove();
//					window.$ && $( newDiv ).remove();
//				}
//			}, url ) : $(newDiv).remove();
//			// d( "Called loadpage" );
//
//			var p = document.createElement("p");
//			p.innerHTML = "I see you came here from a search result (for <a href='" + searchScript + "/" + escape( searchTerm ) + "'>" + searchTerm + "</a>')! Links to things I've written on this site will pop up here, hold tight...";
//			newDiv.appendChild(p);
//			p = document.createElement("p");
//			p.innerHTML = "Any suggestions about this feature, please <a href='" + boardScript + "?mParent=8554&mSubject=Ajax+landing+page'>leave me a message about it</a>.";
//			newDiv.appendChild(p);
//		}
//	}
} );
// function closeAjaxRefererLinks () {
// 	$("ajaxRefererLinks").hide();
// }

