// Javascript library for Galaxy Hunter

// Retrieve a search parameter value.
function getSearchParameter(p) {
  var pp = "&" + p;
  var s;
  if (ghsearch.length <= 0)
    ghsearch = window.location.search;
  if (ghsearch.length <=0)
    s = "&";
  else
    s = "&" + ghsearch.substring(1);
  var ip = 0;
  var iv, iamp, nc;
  var done = false;

  var v = new Object;
  v.exists = false;
  v.value = '';
  v.fChar = 0;
  v.lChar = 0;
  
  while (!done) {
	ip = s.indexOf(pp, ip);
  	if (ip >= 0) {
	  nc = s.charAt(ip + pp.length);
	  if (nc == "" || nc == "&") {
		v.exists = true;
	  	v.value = true;
		v.fChar = ip;
		v.lChar = ip + pp.length - 1;
		done = true;
      } else if (nc == "=") {
    	iv = s.indexOf("=", ip) + 1;
    	iamp = s.indexOf("&", iv);
    	if (iamp < 0) iamp = s.length;
		v.exists = true;
    	v.value = s.substring(iv, iamp);
		v.fChar = ip;
		v.lChar = iamp - 1;
		done = true;
	  } else 
		ip += p.length;
  	} else {
	  done = true;
	}
  }
  return v;
}

// ap: Add Paramter: Add paramter to page's search string.  If it is already present, the current value will be replaced.
function ap(p) {
  var nv;
  if (arguments.length < 2)
    nv = p;
  else
    nv = p + "=" + arguments[1];
  var ov = getSearchParameter(p);
  var s = ghsearch;
  if (!ov.exists) {
    if (s.length == 0)
	  s = "?" + nv;
	else
	  s = s + "&" + nv;
  } else {
    s = s.substring(0, ov.fChar + 1) + nv + s.substring(ov.lChar +1);
  }
  ghsearch = s;
}

// dp: Delete parameter: remove a parameter from the page's search string.
function dp(p) {
  var v = getSearchParameter(p);
  if (v.exists) {
    var s = ghsearch;
	var lChar = Math.min(v.lChar + 1, s.length);
	var fChar = v.fChar;
	if (fChar == 0 && lChar < s.length) {
	  fChar = 1;
	  lChar += 1;
	}
	ghsearch = s.substring(0, fChar) + s.substring(lChar);
  }
}

// getRandom: Returns an integer between 0 and n
function getRandom (n) {
  return Math.round (Math.random () * n);
}

// nps: Next Page with Search: Go to the specified URL but append the current search list.
function nps(url) {
  var s;
  if (ghsearch.length <= 0)
    s = window.location.search;
  else
    s = ghsearch;
  window.location = url + s;
}

// apnps: Append Parameter/Next Page with Search: Combine ap and nps into a single call.
function apnps(url, p) {
  if (arguments.length < 3)
    ap(p);
  else
    ap(p, arguments[2]);
  nps(url);
}

// setDefaultSrc: Set the default src for an image that uses MM_swapImage/Restore.
function setDefaultSrc() { //v3.0
  var i,j=0,x,a=setDefaultSrc.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; x.oSrc=a[i+2];x.src=x.oSrc;x.isSwapped=false;}
}

// isModDone: For the itinerary pages, decide which modules have been completed.
function isModDone(root) {
	if (getSearchParameter("mStart").exists)
		setDefaultSrc("startsaf_btn", "", root + "graphics/startsaf_btn_done.gif");
	if (getSearchParameter("mBias").exists)
		setDefaultSrc("bias_btn", "", root + "graphics/bias_btn_done.gif");
	if (getSearchParameter("mSS").exists)
		setDefaultSrc("samplesize_btn", "", root + "graphics/samplesize_btn_done.gif");
	if (getSearchParameter("mVS").exists)
		setDefaultSrc("compare_btn", "", root + "graphics/compare_btn_done.gif");
	if (getSearchParameter("mLast").exists)
		setDefaultSrc("laststop_btn", "", root + "graphics/laststop_btn_done.gif");
}

// setfi: Set the itinerary coming from if its a specific one.
function setfi(module) {
  ap('fi', module);
}

// goitin: Go to either the generic itinerary, or the last specific one.
function goitin() {
  var fi = getSearchParameter("fi");
  var module = "";
  if (fi.exists) {
    if (fi.value == "start") {
	  nps("../itinerary-start.html");
	  return;
	}
    module = fi.value + "/";
  }
  nps("../" + module + "itinerary.html");
}

// rto: Return TO- Return to a specific value of a parameter.
function rto(pname, alturl) {
  var url = getSearchParameter(pname);
  dp(pname);
  if (!url.exists)
    if (alturl)
	  nps(alturl);
	else
      history.back();
  else
    nps(url.value);
}

// gomain: close the activity window and send the main window to the specified url.
function gomain(url) {
  window.open(url, "original");
  window.close();
}

function STSCI_openMBrWindow(theURL,winName,features) {
	if (document.all) {
		var r = /width=([0-9]*),height=([0-9]*)/;
		if (features.match(r)) {
		  var width = Number(RegExp.$1) + 30;
			var height = Number(RegExp.$2) + 20;
		}
		features = features.replace(r, "width=" + width + ",height=" + height);
	}
  if (!document.STScI_popups) document.STScI_popups = new Array;
  if (document.STScI_popups[winName])
    if (document.STScI_popups[winName].close)
      document.STScI_popups[winName].close();
  request_popup_window(theURL,
											 winName,
											 features,
											 'popup-request',
											 function(result) { document.STScI_popups[winName] = result }
											);
  document.MM_returnValue = false;
}

function STSCI_closeMBrWindows() {
  for (var i in document.STScI_popups)
    if (document.STScI_popups[i].close)
      document.STScI_popups[i].close();
  document.MM_returnValue = false;
}

/**
 * Gracefully attempt to open a popup window.  If the window is blocked by a popup blocker,
 * report this to the visitor and provide a link which will let them open the window.
 * Actions taken by a user (clicking a link, for example) will usually be allowed by
 * popular popup blockers.
 */
function request_popup_window(url, name, features, target, success_callback) {
	var opened_window = window.open(url, name, features);
	if ((opened_window == null) || (typeof(opened_window.closed) == "undefined")) {
		var t     = document.getElementById(target);
		var aTag  = document.createElement("a");
		aTag.href = "#";
		aTag.onclick = function() {
		  var result = request_popup_window(url, name, features, target, success_callback); t.removeChild(aTag);
		}
		aTag.innerHTML = "It looks like your popup blocker is enabled.  Click here to open the requested popup window.";
		t.appendChild(aTag);
	}
  
  success_callback(opened_window);
	return opened_window;
}

// Initializers.
var ghsearch = "";

function do_resize() {
  var target_object = document.getElementsByTagName("table")[0];
  var target_width = target_object.offsetWidth + 40;
  var target_height = target_object.offsetHeight + 40;
  window.resizeTo(target_width, target_height);
	
	document.location.href.match(/\/([^\/]*)$/);
	var page = RegExp.$1;
	
	var new_width = Math.ceil(target_width / 20) * 20;
	var new_height = Math.ceil(target_height / 20) * 20;
	
  prompt("Coordinates for window size for\n" + document.location.href + ": ", 
				 page + ";width=" + new_width + ",height=" + new_height);
}
