/* Make sure prototype is loaded first! */
var prototype_loaded = true;

if (!window.$) {
	prototype_loaded = false;
	window.$ = function(objectID) { return document.getElementById(objectID); }

	window.onload = function() {
		for (var i = 0; i < window_load_functions.length; i++) {
			window_load_functions[i]();
		}
	}

	window.onresize = function() {
		for (var i = 0; i < window_resize_functions.length; i++) {
			window_resize_functions[i]();
		}
	}
}

function add_window_onload_function(func) {
	if (!prototype_loaded) {
		window_load_functions.push(func);
	} else {
		Event.observe(window, 'load', func);
	}
}

function add_window_onresize_function(func) {
	if (!prototype_loaded) {
		window_resize_functions.push(func);
	} else {
		Event.observe(window, 'resize', func);
	}
}

/**
 * Allows for multiple functions to be performed at window.onload or window.onresize.
 */
var window_load_functions = [];
var window_resize_functions = [];

// deprecated
function activate_search_element(e) {
	activate_form_element(e);
}

function activate_form_element(e) {
  if (e.style.color != "black") { e.style.color = "black"; e.value = ""; }
}

// http://www.quirksmode.org/viewport/compatibility.html
function get_window_inner_dimensions() {
	var x,y;
	if (self.innerHeight) // all except Explorer
	{
		x = self.innerWidth;
		y = self.innerHeight;
	}
	else if (document.documentElement && document.documentElement.clientHeight)
		// Explorer 6 Strict Mode
	{
		x = document.documentElement.clientWidth;
		y = document.documentElement.clientHeight;
	}
	else if (document.body) // other Explorers
	{
		x = document.body.clientWidth;
		y = document.body.clientHeight;
	}

	return [ x, y ];
}

/**
 * Import the JavaScript-enabled stylesheet.
 */
function javascript_enabled_stylesheet() {
  var s = document.createElement('link');
  s.type = "text/css";
  s.rel = "stylesheet";
  s.href = "/styles/javascript_enabled.css";
  document.getElementsByTagName('head')[0].appendChild(s);
}

/**
 * Monitor for changes to the width of an element, and change the styles of
 * another element when the size reaches a certain threshold.
 */
function generate_resize_monitor(target, normal_style, narrow_style) {
	if (document.all) {
  	normal_style = normal_style.replace(/\! *important/,'');
  	narrow_style = narrow_style.replace(/\! *important/,'');
	}
	add_window_onload_function(function() {
		var resize_function = function() {
			$(target).style.width = ($('subpage-content').offsetWidth < 580) ? narrow_style : normal_style;
		};

		if ($(target)) {
			resize_function();
			add_window_onresize_function(resize_function);
		}
	});
}

var page_management_function = function() {
	var form_element_names = [ 'words', 'query' ];

  var nodes = [ 'search-hubblesite','search-newscenter','search-gallery' ];
  for (var i = 0; i < nodes.length; ++i) {
    var which = nodes[i];
	  if ($(which)) {
	    var node = $(which);
	    var field = node.children[1];

	    if (field) {
        var generate_onfocus = function(which) {
          return function() {
            which.value = '';
            which.style.color = 'black';
            which.onfocus = null;
          };
        };

  	    field.value = 'search';

	      field.onfocus = generate_onfocus(field);
	    }

      node.onsubmit = function() {
        node.children[0].innerHTML = 'Searching...';
      }
	  }
	}

	// Ensure that the page height fits the left nav menu.
	if ($('container') && $('top-components') && $('search-section-bar') && $('nav-box-container')) {
		var offset_size = $('top-components').offsetHeight + $('search-section-bar').offsetHeight + 80;
		$('container').style.minHeight = ($('nav-box-container').offsetHeight + offset_size) + "px";

		// min-height not directly supported, force height instead
		if (($('nav-box-container').offsetHeight + offset_size) > $('container').offsetHeight) {
			$('container').style.height = ($('nav-box-container').offsetHeight + offset_size) + "px";
		}
	}
};

add_window_onload_function(page_management_function);

var spacer_manager = function() {
	var spacer_li;

  var ul_top_links = $('top-links');
	if (ul_top_links) {
		var all_lis = ul_top_links.getElementsByTagName("li");

		var pre_spacer_li;
		var post_spacer_li;
		var spacer_li;
		var spacer_found = false;

		for (var i = 1, il = all_lis.length; i < il; ++i) {
			if (spacer_found) { post_spacer_li = all_lis[i]; break; }
			if (all_lis[i].className == "spacer") {
				spacer_found = true;
				spacer_li = all_lis[i];
			} else {
				pre_spacer_li = all_lis[i];
			}
		}

		if (spacer_li) {
			var spacer_handler = function() {
				var do_hide = (pre_spacer_li.offsetTop != post_spacer_li.offsetTop);

				spacer_li.style.display = (do_hide ? "none" : "");
			};
			add_window_onresize_function(spacer_handler);
			spacer_handler();
		}
	}
};

add_window_onload_function(spacer_manager);

/**
 * Embed a Java applet into a Web page to work around the IE
 * patent restriction.
 * @deprecated Split this out into a separate file.
 */
function EmbedApplet() {
	var args = arguments;

	this.target_container = document.getElementById(args[0]);

  this.applet_attributes = {};
	this.param_elements    = {};
	this.alternate_content = "";

	for (var i = 1; i < args.length; i += 2) {
		var key = args[i];
		var value = args[i + 1];

		switch(key) {
			case "code":
			case "width":
			case "height":
			case "archive":
			case "codebase":
			  this.applet_attributes[key] = value;
				break;
			case "alternateContent":
			  this.alternate_content = value;
				break;
			default:
			  this.param_elements[key] = value;
				break;
		}
	}

	var applet_tag = "<applet";
	for (var i in this.applet_attributes) { applet_tag += (" " + i + "=\"" + this.applet_attributes[i] + "\""); }
	applet_tag += ">";

	for (var i in this.param_elements) { applet_tag += ("<param name=\"" + i + "\" value=\"" + this.param_elements[i] + "\" />"); }
  applet_tag += this.alternate_content + "</applet>";

	this.target_container.innerHTML = applet_tag;
}

/**
 * Include hubblesite_global.js right after you include your master stylesheets.
 */

javascript_enabled_stylesheet();