
// BEGIN: window onload abstraction ============================================

/*
	extension of an idea from simon willison
	http://simon.incutio.com/archive/2004/05/26/addLoadEvent

	use as follows:

		addWindowEvent('onload', function(){ if (something) doSomething(); });

	or

		function doSomething() { alert('yay!'); }
		addWindowEvent('onload', doSomething );
*/

// adds function call to an arbitrary window event trigger without 
// overwriting previous event handler
function addWindowEvent(event_name, function_to_add) {
  var current_event = window[event_name];

  if (typeof(current_event) != 'function') {
    window[event_name] = function_to_add;

  } else {
    window[event_name] = function() {
      current_event();
      function_to_add();
    };
  }
} // END: addWindowEvent()

// END: window onload abstraction ----------------------------------------------


// BEGIN: text field trick =====================================================

// attach this to the onfocus of an INPUT or TEXTAREA, passing the 'this' object as the argument
function textFieldStoreAndClear(el) {
	el.originalText = (el.originalText == null) ? el.value : el.originalText;
	if (el.value == el.originalText) el.value = '';
} // END: textFieldStoreAndClear()

// attach this to the onblur of an INPUT or TEXTAREA, passing the 'this' object as the argument
function textFieldRestore(el) {
	el.value = (el.value == '') ? el.originalText : el.value;
} // END: textFieldRestore()

// END: text field trick -------------------------------------------------------



// BEGIN: map functionality ====================================================

var current_map = "default";

function highlightMap(region) {
	if (!region) var region = 'default';

	document.images['regional_overview'].src = IMG_PRELOADER.i['map_' + region].src;
} // END: highlightMap()

function unhighlightMap(region) {
	if (current_map == region) return false;
	if (!region) var region = 'default';

	document.images['regional_overview'].src = IMG_PRELOADER.i['map_' + current_map].src;
} // END: highlightMap()

function gotoMap(region) {
	if (!region || !document.getElementById) return;

	var all_divs = document.getElementById('maps_content_container').getElementsByTagName('div');
	for (var i=0; i < all_divs.length; i++) {
		if ( (" " + all_divs[i].className).indexOf(" Item") > -1 ) {
			if (all_divs[i].id == ('region_' + region)) {
				all_divs[i].style.display = 'block';
				highlightMap(region);
				current_map = region;

			} else {
				all_divs[i].style.display = 'none';
			}
		}
	}
} // END: gotoMap()

// END: map functionality ------------------------------------------------------


// BEGIN: debugging code =======================================================

var DEBUG = false;
var DEBUG_MESSAGE_TYPES = ["ERROR","WARNING"];
var debug_win;

// i'd love to use something like java.lang.System.out.println(message), but IE doesn't have LiveConnect like mozilla does
function debug(message_type, message) {
	if (DEBUG && (DEBUG_MESSAGE_TYPES.join(" ").indexOf(message_type) > -1 )) {
		if (!debug_win) {
			debug_win = window.open("","","");
			debug_win.document.open();
		}
		message = message.replace(/</g, "&lt;");
		message = message.replace(/>/g, "&gt;");

		debug_win.document.write("<h1 style='font-size: 80%; margin:0;border-bottom: 1px solid #CCC;'>" + message_type  + "</h1>");
		debug_win.document.write("<pre style='font-size: 75%; margin: .5em 0 2em 0;'>" + message + "</pre>");
	}
} // END: debug()

// END: debugging code ---------------------------------------------------------

// BEGIN: Arrow Worldwide Select Box Code =======================================================

function chooseSite()
{   
	document.siteSearch.submit();
}
// END: Arrow Worldwide Select Box Code ---------------------------------------------------------