// Fixes IE 'Click to activate' requirement
var FlashFix = {
	init: function () {
		if (jQuery.browser.msie)
			$('#main-content object').not($('#UOB-assist object'))
				.each (function() { this.outerHTML = this.outerHTML; });
	}
}

/*
*	Javascript Include - handles misc content-related functions
*	Works around problems with using jQuery, due to excessive use of document.write in current content
*/
var JSI = {
	/* Tag injector - used to write HTML during runtime, synchronously */
	inject: function (tag, attrs, content) {
		document.write ('<' + tag);
		if (attrs) for (attr in attrs) { document.write (' ' + attr + '="' + attrs[attr] + '"'); }
		document.write ('>');
		if (content) document.write (content);
		document.write ('</' + tag + '>');
	},
	/* Load XML synchronously & works around lack of mis-set HTTP headers */
	loadXML: function (url) {
		var xmlDoc = null;
		try
		{
			if (window.ActiveXObject)
			{
				xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
				xmlDoc.async = false;
				xmlDoc.load(url);
			}
			else if (window.XMLHttpRequest)
			{
				var req = new XMLHttpRequest();
				req.open("GET", url, false);
				req.send(null);
				xmlDoc = (window.DOMParser)? (new DOMParser()).parseFromString(req.responseText, 'text/xml'): req.responseXML;
			}
			else
			{
				xmlDoc = document.implementation.createDocument("", "", null);
				xmlDoc.async = false;
				xmlDoc.load (url);
			}
		}
		catch (e) { /* Do nothing */ }
		return xmlDoc;
	}
}

/*
*	Semi-legacy support - Selects specified tab and (optional) bullet
*	Expects id's in the form of #tab{n} and #tab{n}-bullet{m}
*/
var TabContent = {
	find: function (anchor) {
		var cur = false;
		var tmp = {anchor: false, id: false};
		var match = false;

		// Search in tabs & li's
		$('#js-tabs .tab-content').each (function (t) {
			$(this).find('li.expandable').each(function (b) {
				tmp.id = $(this).find('#'+anchor);
				tmp.anchor = (tmp.id.length)? tmp.id: $(this).find('a[name='+anchor+']');
				if (
					tmp.anchor.length
					&& this.id
					&& (match = this.id.match(/tab(\d+)-bullet(\d+)/))
				) cur = {tab: match[1], bullet: match[2], anchor: tmp.anchor};
			});
		});

		// Search outside tabs
		if (!cur)
		{
			tmp.id = $('#'+anchor);
			tmp.anchor = tmp.id.length? tmp.id: $('a[name='+anchor+']');
			if (tmp.anchor.length) cur = {tab: false, bullet: false, anchor: tmp.anchor};
		}

		return cur;
	},
	select: function (tab, bullet, anchor) {
		if (tab) JSTab.group[tab-1].click();
		if (bullet) JSListMenu.select('tab'+tab+'-bullet'+bullet);
		anchor = anchor || '#tab'+tab+'-bullet'+bullet;
		TabContent.focus (anchor);
	},
	parseUrl: function () {
		if (location.hash)
		{
			var match = false;
			var anchor = location.hash.match(/#?(.*)$/)[1];
			if (match = anchor.match(/tab(\d+)/)) init_tab = match[1];
			if (match = anchor.match(/bullet(\d+)/)) init_bullet = match[1];
			if (!init_tab && !init_bullet && (match = TabContent.find(anchor))) { init_tab = match.tab; init_bullet = match.bullet; }
			anchor = (match && match.anchor)? match.anchor: '#'+anchor;
			setTimeout (function () { TabContent.focus (anchor); }, 1000);
		}
	},
	initLinks: function (links) {
		$(links).each (function () {
			var match = false; var ctab = 0; var cbullet = 0; var hash = '';
			var cur = {tab: false, bullet: false, anchor: false};
			if (match = this.href.match(/([^#]*#(.+))/))
			{
				hash = match[2];
				cur.anchor = '#'+hash;
				if (match = hash.match(/tab(\d+)/)) { cur.tab = match[1]; }
				if (match = hash.match(/bullet(\d+)/)) { cur.bullet = match[1]; }
				if (!cur.tab && !cur.bullet) cur = TabContent.find(hash);
				if (cur) $(this).click (function () { TabContent.select(cur.tab, cur.bullet, cur.anchor); return false; });
			}
		});
	},
	focus: function (anchor) {
		$(document).scrollTopSmooth ((
			$(anchor).offset() || 
			$('#js-tabs-control').offset() || 
			$('#js-tabs').offset() ||
			$('body').offset()
		).top - 20);
	}
}

jQuery.fn.scrollTopSmooth = function (target, self, prev) {
	self = self || this;
	var cur = self.scrollTop();
	var delta = 50; var interval = 20;
	if (Math.abs(target - cur) <= delta || (prev && prev==cur))
	{
		self.scrollTop (target);
	}
	else
	{
		self.scrollTop (cur + delta*((target > cur)? 1: -1));
		setTimeout (function () { jQuery.fn.scrollTopSmooth(target, self, cur) }, interval);
	}
}

