function $(s) {
	
	p = (arguments[1])? arguments[1] : document;
	t = 'id';
	n = false;
	if(s.match(/^((#|\.)[^\s]+)\s(.+?)$/i)) {
		return $(RegExp.$3, $(RegExp.$1, p));
	}
	if(s.match(/^\.(\w+)(\[(\d)\])?/)) {
		t = 'class';
		s = RegExp.$1;
		if(RegExp.$2 != "") {
			n = parseInt(RegExp.$3);
		}
	} else if(s.match(/^-(\w+)(\[(\d)\])?/)) {
		t = 'tag';
		s = RegExp.$1;
		if(RegExp.$2 != "") {
			n = parseInt(RegExp.$3);
		}
	} else {
		s = s.replace(/(#)/gi, '');
	}
	if(t == 'id') {
		if(p == document) {
			if(p.getElementById(s))
				return p.getElementById(s);
		} else {
			_e = p.getElementsByTagName('*');
			for(i=0; i<_e.length; i++) {
				if(_e[i].id && _e[i].id == s) {
					return _e[i];
				}
			}
		}
	} else if(t == 'class') {
		_elem = Array();
		_e = p.getElementsByTagName('*');
		for(i=0; i<_e.length; i++) {
			if(_e[i].className && _e[i].className == s) {
				_elem.push(_e[i]);
			}
		}
		if(n !== false) {
			return _elem[n];
		}
		return _elem;
	} else if(t == 'tag') {
		_elem = p.getElementsByTagName(s);
		if(n !== false) {
			return _elem[n];
		}
		return _elem;
	}
	return false;
}


window.onload = function() {
	// Get the <div tag which holds the menu
	if(document.getElementById('navigation')) {
		menu_holder = document.getElementById('navigation');
		// Get each of the <a tags within the menu
		aArr = menu_holder.getElementsByTagName('a');
		
		// Get the current URL and remove the domain name portion
		uri = location.href.replace(/^https?:\/\/.+?(\/|$)/, '');
		// Remove any hash tags from the end
		uri = uri.replace(/#.+?$/, '');
		// Split into the different folders
		uri = uri.split('/');
		
		var total_selected = 0;
		for(i=0; i<aArr.length; i++) {
			if(((uri[0] != '' && aArr[i].href.match(uri[0])) || (uri[0]=='' && aArr[i].href=="http://www.backroadstouring.co.uk/"))) {
				aArr[i].parentNode.className += ' selected';
				total_selected++
			}
			if(menu_holder.getElementsByTagName('ul')[0].className != 'drop-down') {
				if(!aArr[i].innerHTML.match(/<br\s?\/?>/i)) {
					aArr[i].style.paddingTop = '12px';
					aArr[i].style.paddingBottom = '0px';
					aArr[i].style.height = '28px';
				}
			}
		}
		if(!total_selected && uri[0].match(/-tours$/i)) {
			for(i=0; i<aArr.length; i++) {
				if(aArr[i].href.match(/tour-listings/i)) {
					aArr[i].parentNode.className += ' selected';
					total_selected++
					break;
				}
			}
		}
		
		if(typeof load_dropdown_menus == 'function') {
			load_dropdown_menus();
		}
	}
}
var ref = false;
if(location.href.match(/#([^=\/]+?)$/i)) {
	ref = RegExp.$1;
	expires = new Date();
	expires.setDate(expires.getDate() + 30);
	document.cookie = 'ref=' + ref +'; expires='+ expires +'; path=/';
	location.hash = '';
} else if(document.cookie.match(/ref=(.+?)(;|$)/i)) {
	ref = RegExp.$1;
	expires = new Date();
	expires.setDate(expires.getDate() + 30);
	document.cookie = 'ref=' + ref +'; expires='+ expires +'; path=/';
}
if(ref == 'battlefields') {
	document.getElementById('logo').style.backgroundImage = 'url(/images/battlefields_header.jpg)';
	document.getElementById('logo').style.cursor = 'pointer';
	document.getElementById('logo').onclick = function() { location.href = 'http://battlefieldsbybackroads.co.uk'; }
}
