// ncmh_scripts.js
// JavaScript routines from the NC Museum of History main web site
// intended for use by look-alike pages generated by the searchable
// database.

var ptop = 20;
var pleft = 20;

function ShowPopup(newurl) {

	// if (window.showModalDialog) {
	// 	alert ("showing modal");
	// 	window.showModalDialog(newurl, "popupwin", "dialog, modal, width=275, height=420, dialogWidth=275, dialogHeight=420, location=no, menubar=no, status=no, toolbar=no, scrollbars=yes, resizable=yes, alwaysRaised=yes, left=0, top=0");
	// 	window.shownModal = 1;
	// } else {

		newwin = window.open(newurl, "popupwin", "dialog, modal, width=275, height=420, location=no, menubar=no, status=no, toolbar=no, scrollbars=yes, resizable=yes, alwaysRaised=yes, left=0, top=0");

		newwin.focus();
	// }

	return false;
}

var timer = '';
function modalblurred() {
	if (window.shownModal) { return; }

	timer = setTimeout('self.focus()',300);
}

function modalfocused() {
	if (window.shownModal) { return; }

	if (timer != '')
		clearTimeout(timer);
}

function ShowKeywords(controlID) {
	newwin = window.open("keywords.html", "keywordwin", "dialog, modal, width=275, height=420, location=no, menubar=no, status=no, toolbar=no, scrollbars=yes, resizable=yes, alwaysRaised=yes, left=0, top=0");

	newwin.keywordcontrol = controlID;
	newwin.focus();

	return false;
}

function getCookie (name) {
	var dcookie = document.cookie; 
	var cname = name + "=";
	var clen = dcookie.length;
	var cbegin = 0;
	while (cbegin < clen) {
		var vbegin = cbegin + cname.length;
		if (dcookie.substring(cbegin, vbegin) == cname) { 
			var vend = dcookie.indexOf (";", vbegin);
			if (vend == -1) vend = clen;
				 return unescape(dcookie.substring(vbegin, vend));
			}
		cbegin = dcookie.indexOf(" ", cbegin) + 1;
		if (cbegin == 0) break;
	}
	return null;
}

function createCookie(name,value,days)
{
	if (days)
	{
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	var ck = name+"="+value+expires+"; path=/";
	document.cookie = ck;
}

function readCookie(name)
{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i<ca.length;i++)
	{
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function GetPopupPos () {
	pleft = getCookie(popupleft);
	ptop = getCookie(popuptop);
}

function SavePopupPos (callingwin) {

	// first get the current window position
	if (window.left) {
		pleft = window.left;
	} else if (window.xPosition) {
		pleft = window.xPosition;
	}

	// then save it
	var expires = new Date();
	expires.setYear(expires.getYear() + 1);

	createCookie("popupleft", pleft, 730);
	createCookie("popuptop", ptop, 730);
}
