//** Ancestral Scotland javascript 
//** © VisitSCotland 2010
// Alter tabbed content behaviour depending on whether a Google Site Search 
// has been performed by the immediately previous request.
// Tested on local PC by editing "a" tag using firebug so that the link produced   
// by google search points to ancestral.local:8080, and with   
// alert("search term: " + getReferrerGoogleSearchTerm()); 

function inittabs(tabelement){
	// Create the Tabs only when loading normally, not following a Search. 
	if (getReferrerGoogleSearchTerm() == null) {
		var tabcontent=new ddtabcontent(tabelement);
		tabcontent.setpersist(false);
		tabcontent.setselectedClassTarget("link"); //"link" or "linkparent"
		tabcontent.init();
		return tabcontent;	
	} 
	return null;
}	

function getReferrerGoogleSearchTerm() {
	var ref = document.referrer;
	if (ref.indexOf('?') == -1) return null; 
	if (ref.indexOf('&q=') == -1) return null; 
	var qs = ref.substr(ref.indexOf('?')+1);
	var qsa = qs.split('&');

	for (var i=0;i<qsa.length;i++) {
		var qsip = qsa[i].split('=');
		if (qsip.length == 1) continue;
		if (qsip[0] == 'q') { 
			var wordstring = unescape(qsip[1].replace(/\+/g,' '));
			return wordstring;
		}
	}
}
