/*
	richpop: window popping utility for use in Rich Media CH world

	richpop(win, url, data, ref)
	where:
	
		win: HTML window target name;  implies certain window parameters
		url: valid path from the calling HTML page to destination page
		data: QUERY_STRING parameters to append to destination URL
		ref: optional indicator of referer; e.g., ch_p02_27, ti1997, co1mx2002

	includes cookie testing for popping country report window that requires
	cookies.js to be included, whose functions require JS1.1.

*/

function richpop(win, url, data, ref) {
	// don't bother unless we have a destination URL
	if (url != null && url != "") {
		var params = "";
		var height = 0;
		var width = 0;
		var otherparams = "";
		var qs = "";

		// override info_ page window parameters for Carla's Chapter Player sanity
		if (url.indexOf('/info_') != -1) {
			win = "info";
		}

		if (win == null || win == "") {
			// no window specified; use nameless one
			win = "_blank";
		} else if (win == "qt") {
			width = 390;
			height = 325;
		} else if (win == "minitext") {
			width = 435;
			height = 485;
			otherparams = "scrollbars,resizable,";
		} else if (win == "info") {
			width = 435;
			height = 485;
			otherparams = "scrollbars,resizable,";
		} else if (win == "ti") {
			// "data" should be in YYYY format, e.g., 2001
			width = 600;
			height = 385;
		} else if (win == "ch") {
			width = 730;
			height = 422;
			otherparams = "status,";
		} else if (win.substring(0,2) == "co") {
			// "data" should be in COYYYYT(T) format (ISO code, year, topic/indicator number), e.g. ru19486
			width = 730;
			height = 300;
			// see which window we should actually pop up
			var otherwin = "co1";
			// rudimentary JS1.1 detection since getNextCoWin uses Array, split, etc.
			// pre-JS1.1 browsers just get 1 CR at a time
			if (document.images) {
				otherwin = getNextCoWin();
			}
			// make sure we got a good value, otherwise, use safe default
			if (otherwin != null && (otherwin.substring(0,2) == "co")) {
				win = otherwin;
			} else {
				win = "co1";
			}
		} else if (win == "textco") {
			// rich media text-only country reports
			otherparams = "menubar,toolbar,resizable,scrollbars,";
		} else if (win == "glossary") {
			width = 354;
			height = 400;
		}

		if (width != 0) {
			params += "width=" + width + ",";
		}
		if (height != 0) {
			params += "height=" + height + ",";
		}
		if (otherparams != "") {
			params += otherparams;
		}

		// if we've already got some window parameters
		if (params != "") {
			// strip trailing , from params
			params = params.substring(0,params.length-1);
			// see if we're running Mac Netscape
			var browserName = navigator.appName;
			var browserAgent = navigator.userAgent.toLowerCase();
			if (browserName.indexOf("Netscape") != -1 && browserAgent.indexOf("mac") != -1) {
				// for Mac Netscape, let them navigate among windows from screen-top menu
				params += ",menubar";
			}
		}

		// add data and referer as query_string if appropriate
		if ((data != null && data != "") || (ref != null && ref != "")) {
			qs += "?"
			if (data != null && data != "") {
				qs += data;
			}
			if (ref != null && ref != "") {
				qs += "&ref=" + ref;
			}
		}
		if (qs != "") {
			var hashloc = url.indexOf("#");
			if (hashloc != -1) {
				// if the URL includes a named anchor, we need to stick in the query_string before it
				url = url.substring(0, hashloc) + qs + url.substring(hashloc, url.length);
			} else {
				// otherwise, just tack on the query_string at the end
				url += qs;
			}
		}

		// dispose of an old one if we've got it:
		var winRef = null;
		if (params != "") {
			winRef = window.open(url,win,params);
		} else {
			winRef = window.open(url,win);
		}
		if (winRef != null) {
			winRef.focus();
		}
/*
		if (parent != null && parent != self) {
			// give the parent frameset a copy of this window reference
			eval("parent." + win + " = winRef;");
		}
*/

		// to stop the HREF from running...
		return false;

	} // otherwise, we don't have a URL, don't do anything.
}



// checks some cookies (set by co.html itself), then returns co1 or co2 as appropriate
// **REQUIRES cookies.js TO BE SOURCED AS WELL!!**
function getNextCoWin() {
	var nextWin = "co1";
	var isClosed1 = (parseInt(getPref("rmcr_win_co1")) != 1);
	var isClosed2 = (parseInt(getPref("rmcr_win_co2")) != 1);
	var recent = getPref("rmcr_last");
	if (isClosed1) {
		// window 1 is closed, use it
		nextWin = "co1";
	} else if (isClosed2) {
		// window 1 is open, but 2 is closed, use it
		nextWin = "co2";
	} else if (recent == "co1") {
		// both windows are open, but they opened 1 more recently, use window 2
		nextWin = "co2";
	} else {
		// default
		nextWin = "co1";
	}
	return nextWin;
}