
	// Body onload utility (supports multiple onload functions)

	isMac = (navigator.appVersion.indexOf("Mac")!=-1) ? true : false;
	NS4 = (document.layers) ? true : false;
	IEmac = ((document.all)&&(isMac)) ? true : false;
	IE4plus = (document.all) ? true : false;
	IE4 = ((document.all)&&(navigator.appVersion.indexOf("MSIE 4.")!=-1)) ? true : false;
	IE5 = ((document.all)&&(navigator.appVersion.indexOf("MSIE 5.")!=-1)) ? true : false;
	ver4 = (NS4 || IE4plus) ? true : false;
	NS6 = (!document.layers) && (navigator.userAgent.indexOf('Netscape')!=-1) ? true:false;

	var gSafeOnload = new Array();

	function SafeAddOnload(f)
	{
		if (IEmac && IE4) { // IE 4.5 blows out on testing window.onload
			window.onload = SafeOnload;
			gSafeOnload[gSafeOnload.length] = f;
		}
		else if (window.onload) {
			if (window.onload != SafeOnload) {
				gSafeOnload[0] = window.onload;
				window.onload = SafeOnload;
			}
			gSafeOnload[gSafeOnload.length] = f;
		}
		else {
			window.onload = f;
		}
	}

	function SafeOnload()
	{
		for (var i=0;i<gSafeOnload.length;i++) {
			gSafeOnload[i]();
		}
	}

	// Hover function for IE (menu related)
	function sfHover()
	{
		var sfEls = document.getElementById("menu").getElementsByTagName("LI");
		for (var i=0; i<sfEls.length; i++) {
			sfEls[i].onmouseover = function() {
					this.className += " sfhover"; 
				}
			sfEls[i].onmouseout = function() {
					this.className = this.className.replace(new RegExp(" sfhover\\b"), "");
				}
		}
	}
	if (window.attachEvent) {window.attachEvent("onload", sfHover)};
	
	function bgClr(obj, color) {
		obj.style.backgroundColor = color;
	}

	function makeBigger() {
		addClass('html', 'bigger');
	}

	function makeSmaller() {
		remClass('html', 'bigger');
	}

	function doId(action, id, discard) 
	{
		currentUrl = currentUrl.replace(/\/[^\/]+\.html/i, "/" + action + ".html");
		if (id) {
			rIU('id', id, discard);
		}
		else if (currentQuery) {
			document.location = baseHref + currentUrl + '?' + currentQuery;
		}
		else {
			document.location = baseHref + currentUrl;
		}
   	}

	// Print Article
	function printMe()
	{
		if (window.print) {
			window.print();
		}
	}

	// Go to Letter
	function gTL(letter, lexico)
	{
		if (lexico) {
			gTD(lexico, letter);
		}
		else {
			var form = 'flsearch';
			with (document.forms[form]) {
				if (letter.match(/\d+/)) {
					lq.value = letter;
				}
				else {
					lq.value = letter+'*';
				}
				submit();
			}
		}
		return false;
	}
	
	// Go to Dictionary
	function gTD(lexico, search)
	{
		switch (lexico){
			case '2':
			case 'kriaras':
				url = 'medieval_greek/kriaras/search.html?lq='+search;
			break;
			case '3':
			case 'georgakas':
				url = 'modern_greek/tools/lexica/georgakas/search.html?lq='+search;
			break;
			case '1':
			case 'triantafyllides':
				url = 'modern_greek/tools/lexica/triantafyllides/search.html?lq='+search;
			break;
		}
		document.location = baseHref + url;
	}

	// Form Submit
	function sL(form)
	{
		with (document.forms[form]) {
			sI = sin.options[sin.selectedIndex].value;
			if (sI == 'triantafyllides' || sI == 'georgakas' || sI == 'reverse') {
				action = 'modern_greek/tools/lexica/' + sI + '/search.html';
			}
			else if (sI == 'kriaras') {
				action = 'medieval_greek/kriaras/search.html';
			}
		}
		fS(form);
	}
	
	// Form Submit
	function fS(form)
	{
		with (document.forms[form]) {
			submit();
		}
	}

	// Form Reset
	function fR(form)
	{
		with (document.forms[form]) {
			reset();
		}
	}
	
	// Replace in URL
	function rIU(param, val, discard, dont_redirect)
	{
		var query = currentQuery;
		if (discard) query = '';
		if (query == '') {
			if (val != '') {
				query = param + '=' + val;
			}
		}
		else {
			query = '&' + query + '&';
			// var ereg = new RegExp("&" + param + "=[a-z0-9\_\+\ \.\-]+", "i");
			
			var ereg = new RegExp('&' + param + '=.*?&(?!#\d+;)', 'i');
			if (query.match(ereg)) {
				if (val == '') {
					query = query.replace(ereg, '&');
				}
				else {
					query = query.replace(ereg, '&' + param + '=' + val + '&');
				}
			}
			else if (val != '') {
				query = '&' + param + '=' + val + query;
			}
			query = query.substring(1, query.length - 1);
		}
		if (dont_redirect) {
			currentQuery = query;
		}
		else {
			query = (query) ? '?' + query : '';
			document.location = baseHref + currentUrl + query;
		}
		return false;
	}

	// Go to Word ID
	function gTW(word_id)
	{
		var form = 'flsearch';
		with (document.forms[form]) {
			dq.value = word_id;
			submit();
			dq.value = '';
		}
		return false;
	}

	// Go to Word ID
	function W(word_id)
	{
		var form = 'flsearch';
		with (document.forms[form]) {
			lq.value = word_id;
			submit();
			lq.value = '';
		}
		return false;
	}

	// Go to Text (Ancient Greek)
	function aT(text_id, hilight_id, line_id)
	{
		document.location = baseHref + 'ancient_greek/tools/corpora/anthology/content.html' + '?t=' + text_id + '&h=' + hilight_id + '&l=' + line_id + '#pinpoint';
	}

	// Go to Text (Modern Greek)
	function mT(text_id, hilight_id, line_id)
	{
		document.location = baseHref + currentUrl.replace(/search\.html/, 'content.html') + '?t=' + text_id + '&h=' + hilight_id + '&l=' + line_id + '#pinpoint';
	}

	// Go to Text
	function T(collection, text, hilight)
	{
		// Commented out due to functionality change. Clicking for a text the
		// user is redirected to the whole text and not the concordance. Kept
		// here for historical reasons (someone may change his mind :-))
		/*
		with (document.forms.flsearch) {
			lq.value = text;
			submit();
		}
		*/
		document.location = baseHref + currentUrl.replace(/search\.html/, 'content.html') + '?collection_id=' + collection + '&text_id=' + text + '&hilight=' + hilight;
	}

	// Go to Page
	function P(collection, page, hilight)
	{
		document.location = baseHref + currentUrl.replace(/search\.html/, 'content.html') + '?collection_id=' + collection + '&page_id=' + page + '&hilight=' + hilight;
	}

	var pages, anchors, inlines, shown;

	function setupPage()
	{
		// setupPages();
		// setupAnchors();
		// setupInlines();
	}

	function setupPages()
	{
		var i;

		if (!document.getElementById) return;

		// get all pages
		pages = getElementsByClass('page');
		if (pages.length) {
			for (i = 1; i < pages.length; i++) {
				pages[i].className = 'page_hidden';
			}
		}
		shown = 0;
	}

	function navPage(page) {
		pages[shown].className = 'page_hidden';
		pages[page - 1].className = 'page_shown';
		shown = page - 1;
	}

	function setupAnchors()
	{
		var i, targ, targarr;

		if (!document.getElementById) return;

		// get all anchors
		anchors = document.getElementsByTagName("a");
		for (i = 0; i < anchors.length; i++) {
			// check if href links to an anchor on this page
			if (anchors[i].href.indexOf("#") != -1 && anchors[i].href.indexOf(document.URL) != -1) {
				// get name of target anchor
				targ = anchors[i].href.substring(anchors[i].href.indexOf("#") + 1);
				// find target anchor
				targarr = document.getElementsByName(targ);
				if (targarr.length) {
					anchors[i].id = "__" + targ;	// save target as id with prefix
					//anchors[i].href = anchors[i].href.substring(0, anchors[i].href.indexOf('#') + 1);
					//listen('click', anchors[i], scrollToAnchor);
				}
			}
		}
	}

	function setupInlines()
	{
		var i, j;

		if (!document.getElementById) return;

		// get all inlines
		var inlines = getElementsByClass('inline');
		if (inlines.length) {
			for (i = 0; i < inlines.length; i++) {
				inlines[i].className = 'hidden_inline';
				for (j = 0; j < anchors.length; j++) {
					if (anchors[j].id == '__' + inlines[i].id.substring(1)) {
						listen('click', anchors[j], showInline);
						break;
					}
				}
			}
		}
	}

	function showInline(e)
	{
		var panel, inline;

		e.preventDefault();

		inline = document.getElementById('i' + e.target.id.substring(2));
		SetPanel(inline.innerHTML);

		panel = document.getElementById('panel');
		if (!panel.state) { panel.state = 'down'; } // Initial set
		// Open panel
		if (panel.state != 'up') {
			setSlide(panel);
		}
	}

	function SetPanel(str) {
		var panel_wrapper;
		panel_wrapper = document.getElementById('pcontent');
		panel_wrapper.innerHTML = str;
	}

	function hidePanel() {
		var panel;
		panel = document.getElementById('panel');
		if (panel.state == 'up') {
			setSlide(panel);
		}
	}

	function setSlide(thisPanel) {
		var body = document.getElementsByTagName("body");
		if (IE4plus) {
			if (thisPanel.state == 'down') {
				body[0].style.paddingBottom = '301px';
				thisPanel.state = 'up';
				thisPanel.style.overflow = 'visible';
			}
			else {
				body[0].style.paddingBottom = '0px';
				thisPanel.state = 'down';
				thisPanel.style.overflow = 'hidden';
			}
		}
		else {
			// grab the height (this is a number such as -500)
			thisPanel.height = 300;
			var numframes;
			
			// if this panel is fully down
			if (thisPanel.state == 'down') {	
				// re-style the panel so it's only just out of site
				thisPanel.style.height = '0';
				// set number of frames the animation will take
				numFrames = 12;
			}
			else {
				// calculate the number of frames the animation will take
				// (bearing in mind it could be halfway through an up or down movement)
				var finPos = 0;
				var nowPos = thisPanel.offsetHeight - 17;
				// nowPos = nowPos.substring(0, (nowPos.length - 2));
				var toGo = (nowPos - finPos);
				numFrames = Math.round((12 / thisPanel.height) * toGo);
				numFrames = (numFrames == 0) ? 1 : numFrames;
			}
			
			// set y start position
			var yStart = thisPanel.offsetHeight - 17;
			var yEnd;
			// set y end position
			switch (thisPanel.state) {
				case 'up':
					body[0].style.paddingBottom = '0px';
					thisPanel.state = 'downing';
					yEnd = 0;
				break;
				case 'downing':
					thisPanel.state = 'uping';
					yEnd = thisPanel.height;
				break;
				case 'down':
					body[0].style.paddingBottom = '302px';
					thisPanel.state = 'uping';
					yEnd = thisPanel.height;
				break;
				case 'uping':
					thisPanel.state = 'downing';
					yEnd = 0;
				break;
			}
			
			// set current frame number
			frameNum = 0;
			// calculate y distance to be moved each frame
			var yDist = (yEnd - yStart)/(numFrames - 1);
			
			// run the sliding function
			runSlide(thisPanel, yStart, yDist, frameNum, numFrames);
		}
	}

	function runSlide(thisPanel, yStart, yDist, frameNum, numFrames)
	{
		// if the animation still has frames left to run
		if (frameNum < numFrames) {
			// alert((yStart + yDist * frameNum) + "px");
			thisPanel.style.height = (yStart + yDist*frameNum) + "px";
			frameNum++;
			return setTimeout(function(){runSlide(thisPanel, yStart, yDist, frameNum, numFrames)}, 8);
		}
		// if the animation is complete
		else {
			// if it was moving down
			if (thisPanel.state == 'downing') {
				// set to all the way down
				thisPanel.state = 'down';
			}
			else {
				thisPanel.state = 'up';
				thisPanel.style.height = "300px";
			}
		}
		return true;
	}
	
	function accordion(name)
	{
		new Rico.Accordion(name, {panelHeight:200});
	}
	
	function quoteString(li)
	{
		var el = document.getElementById('lq');
		var token = li.innerHTML;
		if (token.match(/\ |\"|\-/)) {
			el.value = '"' + token + '"';
		}
		else {
			el.value = token;
		}
	}

	function showTab(tab_id) {
		var el = null;
		if (document.trans) {
			el = document.trans;
		}
		else {
			el = parent.trans;
		}
		var allTabs = el.getElementsByClass('tab');
		for (var i = 0; i < allTabs.length; i++) {
			if (allTabs[i].id == tab_id) {
				allTabs[i].style.display = 'block';
			}
			else {
				allTabs[i].style.display = 'none';
			}
		}
		if (document.getElementById('tabs')) {
			allTabs = document.getElementById('tabs').firstChild.childNodes;
			for (var i = 0; i < allTabs.length; i++) {
				if (tab_id == 't' + (i + 1)) {
					allTabs[i].id = 'ctab';
				}
				else {
					allTabs[i].id = 'foo';
				}
			}
		}
	}
	
	function getSel(e)
	{
		var txt = '';
		if (window.getSelection)
		{
			txt = window.getSelection();
		}
		else if (document.getSelection)
		{
			txt = document.getSelection();
		}
		else if (document.selection)
		{
			txt = document.selection.createRange().text;
		}
		return txt;
	}

	function bibl_contents()
	{
		lnk = document.getElementById('bibl_cont');
		div = document.getElementById('bibl_contents');
		if (lnk.className == 'bibl_expand')
		{
			lnk.className = 'bibl_contract';
			div.className = '';
		}
		else if (lnk.className == 'bibl_contract')
		{
			lnk.className = 'bibl_expand';
			div.className = 'hidden';
		}
	}
	function sm2(file_id,file_url)
	{
		soundManager.createSound({id:file_id, url:file_url});
		soundManager.play(file_id);
	}
	function show_hide()
	{
		div = document.getElementById('cima');
		if (div.className == 'hide_elem') div.className = '';
		else if (div.className == '') div.className = 'hide_elem';
	}
	function bib_range(elem)
	{
		switch(elem.options[elem.selectedIndex].value) {
			case 'bt':
				alert('Πληκτρολογίστε αρχική και τελική χρονολογία στο διπλανό πεδίο σύμφωνα με την παρακάτω μορφή:\n\nxxxx, xxxx');
			break;
		}
	}
	function Terms(term,form)
	{
		inp = document.getElementById('term_inp');
		inp.value = term;
		fS(form);
	}