var domain = '';
var hasAgeCheckCookie = false;

function clickItem(item) {
	window.location = item.children[0].href;
	return true;
}

function completePageLoad() {
	window.defaultStatus = '';
	window.status = '';
	resizeBody();
	if (document.getElementById('heading_searchquery')) {
		var searchQuery = document.location.search;
		if (searchQuery.substring(1, 7).toLowerCase() == 'query=') {
			var searchQueryTrimmed = decodeURIComponent(searchQuery.substring(7).replace(/^\s+|\s+$|(&|#).*?$/g, '').replace(/\+/g, ' '));
			document.getElementById('heading_searchquery').value = searchQueryTrimmed;
			if (document.getElementById('body_searchquery')) {
				if (document.getElementById('body_searchquery').value.length == 0) {
					document.getElementById('body_searchquery').value = searchQueryTrimmed;
				}
			}
		}
	}
	showAgeCheckIfNeeded();
	return true;
}

function getCookie(name) {
	if ((getDomain() != 'undefined') && (typeof(document.cookie) != 'undefined')) {
		var results = document.cookie.match('(^|;\\s*)?' + name + '=([^;]*)(;|$)');
		if (results) return results[2];
	}
	return null;
}

function getDomain() {
	if (domain.length > 0) return domain;
	if (window.location.href.substring(0, 7) == 'http://') {
		var fragments = window.location.href.split('/');
		if ((fragments) && (fragments.length > 2)) domain = fragments[2].replace('www.', '.');
	}
	if (domain.length == 0) domain = 'undefined';
	return domain;
}

function outBanner(item) {
	if (item.src.indexOf('_title.jpg') > 0) {
		item.src = item.src.replace('_title.jpg', '.jpg');
	}
	return true;
}

function outItem(item) {
	item.className = 'item';
	outLink();
	var imgobj = item.getElementsByTagName('IMG')[0];
	if (imgobj.src.indexOf('_active.jpg') > 0) {
		imgobj.src = imgobj.src.replace('_active.jpg', '.jpg');
	}
	return true;
}

function outLink() {
	window.status = window.defaultStatus;
	return true;
}

function outMenu(imageSrc) {
	var label = document.getElementById('heading_menulabel');
	outLink();
	if (label) {
		label.src = imageSrc;
		label.alt = '';
		label.title = '';
	}
	return true;
}

function outWarning(item) {
	outLink();
	if (item.src.indexOf('_active.jpg') > 0) {
		item.src = item.src.replace('_active.jpg', '.jpg');
	}
	return true;
}

function outWhatsThis(item) {
	outLink();
	if (item.src.indexOf('_active.gif') > 0) {
		item.src = item.src.replace('_active.gif', '.gif');
	}
	return true;
}

function overBanner(item) {
	if (item.src.indexOf('_title.jpg') < 0) {
		item.src = item.src.replace('.jpg', '_title.jpg');
	}
	return true;
}

function overItem(item, str) {
	item.className = 'itemhover';
	overLink(str);
	var imgobj = item.getElementsByTagName('IMG')[0];
	if (imgobj.src.indexOf('_active.jpg') < 0) {
		imgobj.src = imgobj.src.replace('.jpg', '_active.jpg');
	}
	return true;
}

function overLink(str) {
	window.status = str;
	return true;
}

function overMenu(item, imageSrc) {
	var label = document.getElementById('heading_menulabel');
	overLink(item.alt);
	if (label) {
		label.src = imageSrc;
		label.alt = item.alt;
		label.title = item.title;
	}
	return true;
}

function overWarning(item, str) {
	overLink(str);
	if (item.src.indexOf('_active.jpg') < 0) {
		item.src = item.src.replace('.jpg', '_active.jpg');
	}
	return true;
}

function overWhatsThis(item) {
	overLink('Advertising Information');
	if (item.src.indexOf('_active.gif') < 0) {
		item.src = item.src.replace('.gif', '_active.gif');
	}
	return true;
}

function resizeBody() {
	var resized = false;
	if (document.getElementById('body').clientHeight < 1536) {
		document.getElementById('body').style.height = '1516px';
		resized = true;
	}
	if (document.getElementById('sidebar').clientHeight < 1536) {
		document.getElementById('sidebar').style.height = '1536px';
		resized = true;
	}
	if (document.getElementById('body').clientHeight > document.getElementById('sidebar').clientHeight) {
		document.getElementById('sidebar').style.height = document.getElementById('body').clientHeight.toString() + 'px';
		resized = true;
	}
	else if (document.getElementById('body').clientHeight < document.getElementById('sidebar').clientHeight) {
		document.getElementById('body').style.height = (document.getElementById('sidebar').clientHeight - 20).toString() + 'px';
		resized = true;
	}
	if (resized) {
		document.getElementById('footing').style.top = (document.getElementById('body').clientHeight + 180).toString() + 'px';
	}
	return true;
}

function search() {
	var searchUri = 'http://www.sognadigitalmuseum.com/search.aspx';
	var searchQuery = document.getElementById('heading_searchquery').value.replace(/^\s+|\s+$/g, '');
	if (searchQuery.length > 0) {
		document.location = searchUri + '?query=' + encodeURIComponent(searchQuery).replace(/%20/g, '+');
	}
	else {
		document.location = searchUri;
	}
	return true;
}

function setCookie(name, value, expires) {
	if ((getDomain() != 'undefined') && (typeof(document.cookie) != 'undefined')) {
		var ck = name + '=' + value;
		if (!expires) ck = ck + ';expires=Mon, 31 Dec 2029 00:00:00 GMT';
		ck = ck + ';path=/;domain=' + getDomain() + ';';
		document.cookie = ck;
		return true;
	}
	return false;
}

function showAgeCheck() {
	var borderDiv = document.getElementById('warningborder');
	var contentDiv = document.getElementById('warningcontent');
	var screenDiv = document.getElementById('warningscreen');
	if ((borderDiv != null) && (contentDiv != null) && (screenDiv != null)) {
		// hide scroll bars
		try {
			document.documentElement.style.overflow = 'hidden';
		}
		catch(e1) { }
		try {
			document.body.scroll = 'no';
		}
		catch(e2) { }

		// determine object sizes
		var viewportWidth = 800;
		var viewportHeight = 640;
		if (typeof(window.innerWidth) != 'undefined') {
			// standards-compliant browsers
			viewportWidth = window.innerWidth;
			viewportHeight = window.innerHeight;
		}
		else if ((typeof(document.documentElement) != 'undefined') && (typeof(document.documentElement.clientWidth) != 'undefined') && (document.documentElement.clientWidth != 0)) {
			// IE in standards-compliant mode
			viewportWidth = document.documentElement.clientWidth;
			viewportHeight = document.documentElement.clientHeight;
		}
		else {
			// older versions of IE
			viewportWidth = document.getElementsByTagName('body')[0].clientWidth;
			viewportHeight = document.getElementsByTagName('body')[0].clientHeight;
		}
		var borderPaddingX = (viewportWidth > 648) ? Math.floor((viewportWidth - 648) / 2.0) : 0;
		var borderPaddingY = (viewportHeight > 488) ? Math.floor((viewportHeight - 488) / 2.0) : 0;
		var borderPadding = Math.min(38, Math.min(borderPaddingX, borderPaddingY));
		var borderWidth = Math.min(640, viewportWidth);
		var borderHeight = Math.min(480, viewportHeight);
		var borderLeft = Math.max(0, Math.floor((viewportWidth - borderWidth - (2 * borderPadding) - 8) / 2.0));
		var borderTop = Math.max(0, Math.floor((viewportHeight - borderHeight - (2 * borderPadding) - 8) / 2.0));

		// set content window size
		contentDiv.style.width = (Math.min(640, viewportWidth)).toString() + 'px';
		contentDiv.style.height = (Math.min(480, viewportHeight)).toString() + 'px';
		contentDiv.style.borderLeftWidth = (viewportWidth > 640) ? '2px' : '0px';
		contentDiv.style.borderRightWidth = (viewportWidth > 640) ? '2px' : '0px';
		contentDiv.style.borderTopWidth = (viewportHeight > 480) ? '2px' : '0px';
		contentDiv.style.borderBottomWidth = (viewportHeight > 480) ? '2px' : '0px';
		contentDiv.style.overflow = ((viewportWidth < 640) || (viewportHeight < 480)) ? 'scroll' : 'visible';

		// show obscurity screen
		screenDiv.style.width = viewportWidth.toString() + 'px';
		screenDiv.style.height = viewportHeight.toString() + 'px';
		try {
			screenDiv.style.opacity = 0.75;
		}
		catch(e3) { }
		try {
			screenDiv.style.filter = 'alpha(opacity=75)';
		}
		catch(e4) { }
		screenDiv.style.visibility = 'visible';

		// show content and border
		borderDiv.style.padding = borderPadding.toString() + 'px';
		borderDiv.style.left = borderLeft.toString() + 'px';
		borderDiv.style.top = borderTop.toString() + 'px';
		borderDiv.style.width = borderWidth.toString() + 'px';
		borderDiv.style.height = borderHeight.toString() + 'px';
		borderDiv.style.borderLeftWidth = (viewportWidth > borderWidth) ? '2px' : '0px';
		borderDiv.style.borderRightWidth = (viewportWidth > borderWidth) ? '2px' : '0px';
		borderDiv.style.borderTopWidth = (viewportHeight > borderHeight) ? '2px' : '0px';
		borderDiv.style.borderBottomWidth = (viewportHeight > borderHeight) ? '2px' : '0px';
		borderDiv.style.visibility = 'visible';
	}
	return true;
}

function showAgeCheckIfNeeded() {
	if (!hasAgeCheckCookie) {
		if (getDomain() == 'undefined') {
			hasAgeCheckCookie = true;
		}
		else {
			hasAgeCheckCookie = (getCookie('agecheck') == 'true');
			if (!hasAgeCheckCookie) showAgeCheck();
		}
	}
	return true;
}

function switchCharacterNames() {
	var english_layer = document.getElementById('name_english');
	var japanese_layer = document.getElementById('name_japanese');
	if (english_layer.style.zIndex >= japanese_layer.style.zIndex) {
		english_layer.style.zIndex = 1;
		japanese_layer.style.zIndex = 2;
	}
	else {
		english_layer.style.zIndex = 2;
		japanese_layer.style.zIndex = 1;
	}
	return true;
}

function verifyAge() {
	setCookie('agecheck', 'true', false);
	hasAgeCheckCookie = true;
	var borderDiv = document.getElementById('warningborder');
	var screenDiv = document.getElementById('warningscreen');
	if ((borderDiv != null) && (screenDiv != null)) {
		// show scroll bars
		try {
			document.documentElement.style.overflow = 'scroll';
		}
		catch(e1) { }
		try {
			document.body.scroll = 'yes';
		}
		catch(e2) { }

		// hide age check warning
		borderDiv.style.visibility = 'hidden';
		screenDiv.style.visibility = 'hidden';
	}
	return true;
}