// Set up global AddThis variables
var addthis_config = {"data_track_clickback":true};


// Initialise page
function initialise(boolRssDisplay, boolMailerDisplay, strFolder, strFile) {
	buildMenus();
	metaDataCentre(boolRssDisplay, boolMailerDisplay, strFolder, strFile);
	currentMenu(strFolder, strFile);
	copyright();
	$.getScript('http://www.google-analytics.com/urchin.js', function() {
		_uacct = "UA-375850-4";
		urchinTracker();
	});
}


// Initialise home page
function initialiseHomePage() {
	buildMenus();
	copyright();
	quotes();
	//mailerLink();
}


// Initialise error page
function initialiseErrorPage() {
	buildMenus();
	copyright();
}


// Build menus
function buildMenus() {
	var strHTML;

	// Menu 'portfolio'
	strHTML  = '<li><a title="A summary of my portfolio" id="portfolio_index" href="../portfolio/index.html">A Summary of My Portfolio</a></li>\n';
	strHTML += '<li><a title="Details of the work I did on the Learning Zone Clips Library project at the BBC" id="portfolio_bbc" href="../portfolio/bbc.html"><abbr title="British Broadcasting Corporation">BBC</abbr></a></li>\n';
	strHTML += '<li><a title="Details of the work I did on the DNA project at the BBC" id="portfolio_dna" href="../portfolio/dna.html"><abbr title="Douglas Noel Adams">DNA</abbr></a></li>\n';
	strHTML += '<li><a title="Details of the work I did on the h2g2 website" id="portfolio_h2g2" href="../portfolio/h2g2.html"><abbr title="The Hitchhiker\'s Guide to the Galaxy">h2g2</abbr></a></li>\n';
	strHTML += '<li><a title="Details of the work I did on the Museum of Childhood website" id="portfolio_moc" href="../portfolio/moc.html">Museum of Childhood</a></li>\n';
	strHTML += '<li><a title="Details of the work I did on my personal websites" id="portfolio_personal" href="../portfolio/personal.html">Personal Websites</a></li>\n';
	strHTML += '<li><a title="Details of the work I did on the Surrey County Council intranet" id="portfolio_scc" href="../portfolio/scc.html">Surrey County Council</a></li>\n';
	strHTML += '<li><a title="Details of the work I did on the Victoria and Albert Museum website" id="portfolio_vanda" href="../portfolio/vanda.html">Victoria and Albert Museum</a></li>\n';
	strHTML += '<li><a title="Details of the work I did on the Which? website" id="portfolio_which" href="../portfolio/which.html">Which?</a></li>\n';
	strHTML += '<li><a title="Details of the work I did on the Xara website" id="portfolio_xara" href="../portfolio/xara.html">Xara</a></li>\n';
	addSubmenu('portfolio', strHTML);

	// Menu 'about_me'
	strHTML  = '<li><a title="A summary of my skills" id="about_me_index" href="../about_me/index.html">A Summary of My Skills</a></li>\n';
	strHTML += '<li><a title="My Curriculum Vitae" id="about_me_curriculum_vitae" href="../about_me/curriculum_vitae.html">Curriculum Vitae</a></li>\n';
	strHTML += '<li><a title="Information about my skills as a web developer" id="about_me_web_development" href="../about_me/web_development.html">Web Development</a></li>\n';
	strHTML += '<li><a title="Information about my skills as a web producer" id="about_me_web_production" href="../about_me/web_production.html">Web Production</a></li>\n';
	strHTML += '<li><a title="Information about my skills as a web journalist" id="about_me_web_journalism" href="../about_me/web_journalism.html">Web Journalism</a></li>\n';
	strHTML += '<li><a title="Information about my skills with content management systems" id="about_me_content_management_systems" href="../about_me/content_management_systems.html">Content Management Systems</a></li>\n';
	strHTML += '<li><a title="Information about my skills as an online community expert" id="about_me_online_communities" href="../about_me/online_communities.html">Online Communities</a></li>\n';
	strHTML += '<li><a title="Information about my management and team skills" id="about_me_people_skills" href="../about_me/people_skills.html">People Skills</a></li>\n';
	addSubmenu('about_me', strHTML);

	// Menu 'about_site'
	strHTML  = '<li><a title="Who did what on this site, and with which pieces of software" id="about_site_site_credits" href="../about_site/site_credits.html">Site Credits</a></li>\n';
	strHTML += '<li><a title="For those who want to use my content on their own websites" id="about_site_using_my_content" href="../about_site/using_my_content.html">Using My Content on Your Site</a></li>\n';
	strHTML += '<li><a title="Some helpful tips on using this site" id="about_site_using_this_site" href="../about_site/using_this_site.html">Using this Site</a></li>\n';
	strHTML += '<li><a title="An accessibility statement for this site" id="about_site_accessibility" href="../about_site/accessibility.html">Accessibility Statement</a></li>\n';
	addSubmenu('about_site', strHTML);

	// Menu 'guestbook'
	strHTML  = '<li><a title="Feel free to view my Guestbook" id="guestbook_index" href="../guestbook/viewforum.php?f=1" accesskey="9">View My Guestbook</a></li>\n';
	strHTML += '<li><a title="If you\'d like to sign my Guestbook, I\'d be delighted!" id="guestbook_sign" href="../guestbook/posting.php?mode=newtopic&f=1">Sign My Guestbook</a></li>\n';
	strHTML += '<li><a title="You can send me a private message, if you would prefer" id="guestbook_private" href="../guestbook/posting.php?mode=newtopic&f=2">Send Me a Private Message</a></li>\n';
	strHTML += '<li><a title="Some nice (and nasty) comments taken from my Guestbook" id="guestbook_site_testimonials" href="../guestbook/site_testimonials.html">Site Testimonials</a></li>\n';
	addSubmenu('guestbook', strHTML);

	// Menu 'search'
	strHTML  = '<li><a title="An advanced search engine, so you can find exactly what you\'re after" id="search_index" href="../cgi-bin/search.cgi">Search</a></li>\n';
	strHTML += '<li><a title="An index of every single page on this site" id="search_site_map" href="../search/site_map.html">Site Map</a></li>\n';
	addSubmenu('search', strHTML);

	// Insert placeholder text
	hideField('searchTerms', 'Enter terms');
	$('#searchTerms').focus(function() { showField('searchTerms', 'Enter terms'); });
	$('#searchTerms').blur(function() { hideField('searchTerms', 'Enter terms'); });

	hideField('searchTerms2', 'Enter terms');
	$('#searchTerms2').focus(function() { showField('searchTerms2', 'Enter terms'); });
	$('#searchTerms2').blur(function() { hideField('searchTerms2', 'Enter terms'); });

	// Prevent empty searches
	$('#mainSearch').submit(function() {
		  return (($('#searchTerms').val().length > 0) && ($('#searchTerms').val() != 'Enter terms'));
	});

	// Move access keys to correct menu items
	$('#about_site > a').removeAttr('accesskey');
	$('#guestbook > a').removeAttr('accesskey');
}


// Add a submenu to the left-hand navigation
function addSubmenu(strMenuId, strHTML) {
	// Create submenu
	$('#' + strMenuId).append('<ul id="submenu_' + strMenuId + '">' + strHTML + '</ul>');

	// Hide submenu in a way that doesn't break in Safari
	$('#submenu_' + strMenuId).css('display','none')

	// Add arrow to menu item
	$('#' + strMenuId).css('background', "url('../images/common/down_arrow.gif') 183px top no-repeat");

	// Add open/close event
	$('#' + strMenuId + ' > a').toggle(openMenu, closeMenu);
}


// Open a submenu
function openMenu(objEvent) {
	var strId = $(this).parent().attr('id');

	// Open menu
	$('#submenu_' + strId).slideDown('slow');

	// Change arrow
	$('#' + strId).css('background-image', "url('../images/common/up_arrow.gif')");

	// Stop default action
	return false;
}


// Close a submenu
function closeMenu(objEvent) {
	var strId = $(this).parent().attr('id');

	// Close menu
	$('#submenu_' + strId).slideUp('slow');

	// Change arrow
	$('#' + strId).css('background-image', "url('../images/common/down_arrow.gif')");

	// Stop default action
	return false;
}


// Open the current menu
function currentMenu(strMenuId, strMenuItem) {
	// Open current submenu
	$('#submenu_' + strMenuId).show();

	// Add open/close event, but other way round
	$('#' + strMenuId + ' > a').unbind('click');
	$('#' + strMenuId + ' > a').toggle(closeMenu, openMenu);

	// Change arrow
	$('#' + strMenuId).css('background-image', "url('../images/common/up_arrow.gif')");

	// Highlight current page and remove link
	strLinkText = $('#' + strMenuId + '_' + strMenuItem).text();
	$('#' + strMenuId + '_' + strMenuItem).parent().text(strLinkText).css('color', '#f00');
}


// Display emailer and/or RSS links
function metaDataCentre(boolRssDisplay, boolMailerDisplay, strFolder, strFile) {
	if (boolRssDisplay || boolMailerDisplay) $('#authorInfo').append(rssHTML(boolRssDisplay) + mailerHTML(boolMailerDisplay, strFolder, strFile));
}


// Generate HTML for the emailer link
function mailerHTML(boolDisplay, strFolder, strFile) {
	var strHTML = '';
	if (boolDisplay) {
		strHTML = '<li><strong>Share this Page</strong><br />'
		+ '<div class="addthis_toolbox addthis_default_style">'
			+ '<a class="addthis_button_email"></a>'
			+ '<a class="addthis_button_twitter"></a>'
			+ '<a class="addthis_button_facebook"></a>'
			+ '<a class="addthis_button_google"></a>'
			+ '<a class="addthis_button_favorites"></a>'
			+ '<span class="addthis_separator">|</span>'
			+ '<a href="http://www.addthis.com/bookmark.php?v=250&username=markmoxon" class="addthis_button_compact"><img src="http://s7.addthis.com/static/btn/sm-plus.gif" width="16" height="16" border="0"/></a>'
		+ '</div></li>';
	}
	return strHTML;
}


// Generate HTML for the RSS link
function rssHTML(boolDisplay) {
	var strHTML = '';
	if (boolDisplay) strHTML = '<li><strong>RSS Feed</strong><br /><a href="http://feeds.moxon.net/traveldigest" rel="alternate" type="application/rss+xml">Subscribe via <abbr title="Really Simple Syndication">RSS</abbr> <img class="rss" src="../images/common/feed-icon9x9.png" width="9" height="9" alt="RSS logo" /></a></li>';
	return strHTML;
}


// Insert year for copyright
function copyright() {
	var strHTML, d = new Date();
	strHTML  = '1995-';
	strHTML += d.getFullYear();
	$('#copyrightYear').html(strHTML);
}


// Display input field and clear placeholder text
function showField(strFieldId, strPlaceholderText) {
	// If field exists
	if ($('#' + strFieldId).length) {
		// Display field fully
		$('#' + strFieldId).css('color', '#000');

		// Remove placeholder text if present
		if ($('#' + strFieldId).val().replace(/(\r\n|\r|\n)/g, '\n') == strPlaceholderText.replace(/(\r\n|\r|\n)/g, '\n')) $('#' + strFieldId).val('');
	}
}


// Hide input field and insert placeholder text (if empty)
function hideField(strFieldId, strPlaceholderText) {
	// If field exists
	if ($('#' + strFieldId).length) {
		if (($('#' + strFieldId).val() == '') || ($('#' + strFieldId).val().replace(/(\r\n|\r|\n)/g, '\n') == strPlaceholderText.replace(/(\r\n|\r|\n)/g, '\n'))) {
			// Grey out terms field
			$('#' + strFieldId).css('color', '#707070');

			// Insert placeholder text
			$('#' + strFieldId).val(strPlaceholderText.replace(/(\r\n|\r|\n)/g, '\n'));
		}
	}
}


// Escape string, including + signs
function escape_plus(strURL) {
	var encoded = new String;
	encoded = escape(strURL);
	var re = /\+/g;
	encoded = encoded.replace(re, '%2B');
	return encoded;
}
