var HTTP_HOST = 'www.norfolkbroads.com';
var BASEURL = '/';
var IMAGES = '/images/';
var STYLESHEETS = '/stylesheets/';
var JAVASCRIPTS = '/javascript/';
var JAVASCRIPT = '/javascript/';

/*
var Animator =
{
	animateLinear: function(duration, loops, animation, onCompletion)
	{
		var start = new Date();
		
		var loop = 0;
		tick();
		
		function tick()
		{
			var now = new Date();
			var progress = Math.min(1, (now - start) / duration);
			
			for (var i = 0; i < animation.length; i++) {
				animation[i].tick(progress);
			}
			
			if (progress < 1) {
				setTimeout(tick, 1);
			} else {
				if (++loop < loops || loops == 0) {
					start = now;
					setTimeout(tick, 1);
				} else if (typeof onCompletion != 'undefined') {
					onCompletion();
				}
			}
		}
	}
};

function FadeCssOpacity(element, startOpacity, endOpacity)
{
	this.element = $(element);
	this.startOpacity = startOpacity;
	this.endOpacity = endOpacity;
	this.opacityRange = endOpacity - startOpacity;
	
	// If IE, give element layout
	if (this.element.filters) {
		this.element.style.zoom = '100%';
	}
}

FadeCssOpacity.prototype =
{
	tick: function(progress)
	{
		var opacity = this.startOpacity = (progress * this.opacityRange);
		
		this.element.style.filter = 'alpha(opacity=' + Math.round(opacity * 100) + ')';
		this.element.style.opacity = opacity;
	}
};

var topNavHover =
{
	home: {
		html:
			'Feature Highlight, Directory, the<br />latest East Anglian news.',
		width: null
	},
	directory: {
		html:
			'Directory website listings from accommodation<br />to boating to restaurants and shopping.',
		width: null
	},
	guides: {
		html: 'Guides &amp; Information from around East Anglia<br />ranging from schools to beaches.',
		width: null
	},
	focus: {
		html: 'Regional Focus and Local interest including the Broads,<br />Wildlife, History, Communities and History.',
		width: null
	},
	features: {
		html: 'Webcams, Forum, Photographs and brochures, get<br />involved here. Send a postcard, get the weather.',
		width: null
	},
	whatson: {
		html: 'Whats On and Where to go. Attractions to<br />festivals, Food and wine and local theatre.',
		width: null
	},
	freeads: {
		html: 'Free Ads for East Anglia and the East of England.',
		width: null
	},
	community: {
		html: 'The Community information section with<br />News, local notices and forums.',
		width: null
	},
	'shop': {
		'html': 'Online shopping for a range of local produce<br />and goods from around East Anglia.',
		'width': null
	}
};

var topNavHoverElement = null;

attachListener(window, 'load',
	function ()
	{
		attachListener($('top-nav-home'), 'mouseover', function () { topNavMouseOver('home'); });
		attachListener($('top-nav-home'), 'mouseout',  function () { topNavMouseOut('home');  });
		
		attachListener($('top-nav-directory'), 'mouseover', function () { topNavMouseOver('directory'); });
		attachListener($('top-nav-directory'), 'mouseout',  function () { topNavMouseOut('directory');  });
		
		attachListener($('top-nav-guides'), 'mouseover', function () { topNavMouseOver('guides'); });
		attachListener($('top-nav-guides'), 'mouseout',  function () { topNavMouseOut('guides');  });
		
		attachListener($('top-nav-focus'), 'mouseover', function () { topNavMouseOver('focus'); });
		attachListener($('top-nav-focus'), 'mouseout',  function () { topNavMouseOut('focus');  });
		
		attachListener($('top-nav-features'), 'mouseover', function () { topNavMouseOver('features'); });
		attachListener($('top-nav-features'), 'mouseout',  function () { topNavMouseOut('features');  });
		
		attachListener($('top-nav-whatson'), 'mouseover', function () { topNavMouseOver('whatson'); });
		attachListener($('top-nav-whatson'), 'mouseout',  function () { topNavMouseOut('whatson');  });

		attachListener($('top-nav-freeads'), 'mouseover', function () { topNavMouseOver('freeads'); });
		attachListener($('top-nav-freeads'), 'mouseout',  function () { topNavMouseOut('freeads');  });
		
		attachListener($('top-nav-community'), 'mouseover', function () { topNavMouseOver('community'); });
		attachListener($('top-nav-community'), 'mouseout',  function () { topNavMouseOut('community');  });
		
		attachListener($('top-nav-shop'), 'mouseover', function () { topNavMouseOver('shop'); });
		attachListener($('top-nav-shop'), 'mouseout',  function () { topNavMouseOut('shop');  });
	}
);

function topNavMouseOver(name)
{
	topNavMouseOut(name);
	
	with (document.body.appendChild(topNavHoverElement = document.createElement('DIV')))
	{
		innerHTML = topNavHover[name]['html'];

		className = 'top-nav-hover';
		style.position = 'absolute';
		if (topNavHover[name]['width'] != null) {
			style.width = topNavHover[name]['width'] + 'px';
		}
		
		var tab = $('top-nav-' + name);
		var tab_offset = getOffset(tab);
		var top_nav = $('top-nav');
		var top_nav_offset = getOffset($('top-nav'));
		
		var hover_left = Math.max(1, tab_offset.left - top_nav_offset.left);
		
		if (name != 'home') {
			hover_left += 1;
		}
		
		var overlap = (hover_left + 1) + topNavHoverElement.offsetWidth - top_nav_offset.width;
		if (overlap > 0) {
			hover_left -= overlap;
		}
		
		style.left = (top_nav_offset.left + hover_left) + 'px';
		style.top  = (tab_offset.top + tab_offset.height + 1) + 'px';
		
		Animator.animateLinear(300, 1, [
			new FadeCssOpacity(topNavHoverElement, 0, 0.9)
		]);
	}
}

function topNavMouseOut(name)
{
	if (topNavHoverElement) {
		topNavHoverElement.parentNode.removeChild(topNavHoverElement);
		topNavHoverElement = null;
	}
}

function $(element)
{
	if (typeof element == 'string') {
		element = document.getElementById(element);
	}
	
	return element;
}
*/
function getOffset(element)
{
	if (element)
	{
		var offset = {left: 0, top: 0, width: element.offsetWidth, height: element.offsetHeight};
		
		while (element)
		{
			offset.left += element.offsetLeft;
			offset.top += element.offsetTop;
			element = element.offsetParent;
		}
	}
	else
	{
		var offset = {left: null, top: null, width: null, height: null};
	}
	
	return offset;
}

function setAsHomepage(link)
{
	var user_agent = navigator.userAgent.toLowerCase();
	var url = 'http://' + HTTP_HOST;
	
	if (user_agent.indexOf('opera') != -1) {
		alert(
			'Click OK, then press Ctrl-F12 on your keyboard.\n' +
			'Change your home page address to\n' +
			url
		);
	} else if (user_agent.indexOf('safari') != -1) {
		alert(
			'Click OK, then:\n' +
			'\n' +
			'* Press Command-, on your keyboard\n' +
			'* Change your home page address to\n' +
			'   ' + url
		);
	} else if (user_agent.indexOf('mac_powerpc') != -1) {
		alert(
			'Click OK, then:\n' +
			'\n' +
			'* Press Command-; on your keyboard\n' +
			'* Click on "Browser Display" on the left hand side\n' +
			'* Change your home page address to ' + url
		);
	} else if (window.external) {
		link.style.behavior = 'url(#default#homepage)';
		link.setHomePage(url);
	} else {
		alert('Click OK, then use the Tools > Options menu');
	}
}

function addToFavourites(url)
{
var user_agent = navigator.userAgent.toLowerCase();
	var url = document.location;
	
	if (user_agent.indexOf('opera') != -1) {
		if(user_agent.indexOf('9.') != -1) {
			alert('Click OK, then press Ctrl-D on your keyboard');
		} else {
			alert('Click OK, then press Ctrl-T on your keyboard');
		}
	} else if (user_agent.indexOf('safari') != -1) {
		alert('Click OK, then press Command-D on your keyboard');
	} else if (user_agent.indexOf('mac_powerpc') != -1) {
		alert('Click OK, then press Command-D on your keyboard');
	} else if( window.external ) {
		// IE Favorite
		window.external.AddFavorite( document.location, document.title); 
	} else if (window.sidebar) {
		window.sidebar.addPanel(document.title, url, "");
	} else {
		alert('Click OK, then press Ctrl-D on your keyboard');
	}

}

function bookmark()
{
	var bookmarkurl   = "http://www.norfolkbroads.com";
	var bookmarktitle = "Norfolkbroads.com - East Anglia's premier online directory";
	if (window.external.AddFavorite) {
		window.external.AddFavorite(bookmarkurl,bookmarktitle)
	}
}

function toggleTextOnlyVersion(link)
{
	if (window.RegExp) {
		var stylesheet = document.getElementById('main_stylesheet');
		if (stylesheet.href.match(new RegExp('^(|.*/)textonly.css$'))) {
			stylesheet.href = STYLESHEETS + 'standard-screen.css';
			link.innerHTML = 'Text Only Version';
		} else {
			stylesheet.href = STYLESHEETS + 'textonly.css';
			link.innerHTML = 'Graphical Version';
		}
	}
}

function switchStylesheet(stylesheet)
{
	document.getElementById('main_stylesheet').href = stylesheet;
}

function jump(select)
{
	var url = select.value;
	if (url.charAt(0) == '#') {
		url = document.location.toString().substr(0, document.location.toString().length - document.location.hash.length) + url;
	}
	document.location = url;
}

function popup(link, width, height)
{
	if (typeof width == 'undefined') {
		width = 540;
	}
	
	if (typeof height == 'undefined') {
		height = 400;
	}
	
	window.open(link.href, null, 'width=' + width + ',height=' + height + ',scrollbars=yes');
}

// Cross-browser function to add an event listener to an element
// e.g attachListener(window, 'load', myWindowLoadFunction);
function attachListener(element, event_type, event_handler)
{
	if(element) {
		if (element.addEventListener) {
			element.addEventListener(event_type, event_handler, false);
		} else if (element.attachEvent) {
			element.attachEvent('on' + event_type, event_handler);
		}
	}
}

String.prototype.isBlank = function ()
{
	return (this.trim() == '');
}

String.prototype._trim = function (chars, ltrim, rtrim)
{
	if (typeof chars == 'undefined') {
		chars = ' \t\r\n';
	}
	
	var str = this.valueOf();
	
	var start_index = 0;
	if (ltrim) {
		while (start_index < this.length - 1 && chars.indexOf(str.charAt(start_index)) != -1) {
			start_index++;
		}
	}
	
	var end_index = this.length - 1;
	if (rtrim) {
		while (end_index > 0 && chars.indexOf(str.charAt(end_index)) != -1) {
			end_index--;
		}
	}
	
	return str.substr(start_index, end_index + 1);
}

String.prototype.trim  = function (chars) { return this._trim(chars, true,  true);  }
String.prototype.ltrim = function (chars) { return this._trim(chars, true,  false); }
String.prototype.rtrim = function (chars) { return this._trim(chars, false, true);  }

function toggleRow(id)
{
	with (document.getElementById(id)) {
		style.display = (style.display == 'none') ? '' : 'none';
	}
}

function toggleHomePop(list)
{
	/* CLOSE EXISTING ONES */
	var availableOverlays = new Array('accommodation', 'leisure', 'water', 'business', 'special', 'dining', 'artists', 'home', 'sports', 'retail', 'travel');
	for(i = 0; i < availableOverlays.length; i++)
	{
		if(availableOverlays[i] != list)
		{
			document.getElementById(availableOverlays[i]).style.display = 'none';	
		}
	}
	overlayBox = document.getElementById(list);
	var offsetValues = getOffset(document.getElementById(list + 'More'));
	var accommOffset = getOffset(document.getElementById('accommodationMore'));
	
	overlayBox.style.top = (offsetValues.top + 33) + 'px';
	overlayBox.style.left = (accommOffset.left) + 'px';
	if(overlayBox.style.display != 'block')
	{
		overlayBox.style.display = 'block';
	} else {
		overlayBox.style.display = 'none';
	}					
	return false;
}


window.addEvent('domready', function() {
	if(Browser.Engine.gecko || Browser.Engine.presto) {
		$$('#tn_focus').set({
			'styles': {
				'width': '167px'
			}
		});
	}
});