// DOM support?
var W3CDOM = (document.createElement && document.getElementsByTagName);

// catch any errors so they don't annoy the user
window.onerror = function () {return true;};

// For Mozilla browsers only...
// Executes init function as soon as the DOM is loaded rather than waiting for the window.onload
if (document.addEventListener) {
	document.addEventListener("DOMContentLoaded", init, null);
}

// for all other browsers
// Executes init() after entire window is loaded (fallback method for all other browsers)
window.onload = init;

// Some arrays for our rollovers
var mouseOvers = new Array();
var mouseOuts = new Array();

// Init() function should fire after DOM is loaded
function init() {
	
	if (!W3CDOM) return;
	
	// Quit if this function (init()) has already been called
	if (arguments.callee.done) return;

	// Flag this function so we don't do the same thing twice
	arguments.callee.done = true;
	
	//alert("init");
	
	// Replace our search field with an actual "Search" field in Safari...
	var searchField = document.getElementById('query_field');
	var isSafari = ((parseInt(navigator.productSub)>=20020000)&&(navigator.vendor.indexOf("Apple Computer")!=-1));
	if (isSafari && searchField) {
		// replace current value of input field
		searchField.setAttribute('value', '');
		// change type to "search"
		searchField.setAttribute('type', 'search');
		searchField.setAttribute('autosave', 'com.ut.search');
		searchField.setAttribute('results', '5');
		searchField.setAttribute('placeholder', "Tribute name");
		searchField.style.color = "black";
	} else if (searchField) {
		// For other browsers, we add some functionality to our search field...
		var defaultValue = searchField.getAttribute('value');
		searchField.onfocus = function () { if (this.value == defaultValue) this.value = ''; this.style.color = 'black' };
		//searchField.onblur = function () { if (this.value == '') { this.value = defaultValue; this.style.color = '#999999' } };
	}
		
	// Add mouseover events to our navigation images
	var nav = document.getElementById('navigation');
	var imgs = nav.getElementsByTagName('img');
	
	for (var i=0;i<imgs.length;i++) {
		if (imgs[i].className == "active") { 
			// If the image has a class of "active" then don't do the rollover for it, instead show the active state only
			var suffix = imgs[i].src.substring(imgs[i].src.lastIndexOf('.'));
			imgs[i].src = imgs[i].src.substring(0,imgs[i].src.lastIndexOf('.')) + "_on" + suffix;
		} else {
			// Load our arrays with the new images we'll use for our rollovers
			var suffix = imgs[i].src.substring(imgs[i].src.lastIndexOf('.'));
			mouseOuts[i] = new Image();
			mouseOuts[i].src = imgs[i].src;
			mouseOvers[i] = new Image();
			mouseOvers[i].src = imgs[i].src.substring(0,imgs[i].src.lastIndexOf('.')) + "_on" + suffix;
			// Attach mouseover function
			imgs[i].onmouseover = mouseGoesOver;
			imgs[i].onmouseout = mouseGoesOut;
			imgs[i].number = i;
		}
	}
	
	// Add rollover for any other button
	function addRollover (daID) {
		if (document.getElementById(daID)) {
			var rollbutton = document.getElementById(daID);
			// If the image name already ends with "_on" we assume the button does not need a rollover state
			if ((rollbutton.src.substring(rollbutton.src.lastIndexOf('.')-3,rollbutton.src.lastIndexOf('.'))) == "_on") {
				return;
			}
			var rollbuttonover = new Image();
			var rollbuttonout = new Image();
			// Add to our new image the rollover version of the image (the image name plus "_on")
			var suffix = rollbutton.src.substring(rollbutton.src.lastIndexOf('.'));
			rollbuttonover.src = rollbutton.src.substring(0,rollbutton.src.lastIndexOf('.')) + "_on" + suffix;
			rollbuttonout.src = rollbutton.src;
			rollbutton.onmouseover = function() {
				this.src = rollbuttonover.src;
			}
			rollbutton.onmouseout = function() {
				this.src = rollbuttonout.src;
			}
		}
	}
	
	// set up the tabs
	if(document.getElementById("title_1"))
	{
		setUpTabs("title_1", "title_2", "media_manager_collection_1", "media_manager_collection_2", "title_is_active", "media_manager_collection_active");
	}

	if(document.getElementById("media_manager_collection_2_right"))
	{
		setUpTabs("title_1_right", "title_2_right", "media_manager_collection_1_right", "media_manager_collection_2_right", "title_is_active", "media_manager_collection_active");
	}
	
	// hack: i want to set up two different tabs, and they both need to be in the init.
	if(document.getElementById("approval_title_1"))
	{
		setUpTabs("approval_title_1", "approval_title_2", "approval_media_manager_collection_1", "approval_media_manager_collection_2", "approval_title_is_active", "approval_media_manager_collection_active");
	}
  
}



// Mouseover function
function mouseGoesOver() {
	this.src = mouseOvers[this.number].src;
}

// Mouseout function
function mouseGoesOut() {
	this.src = mouseOuts[this.number].src;
}


// for Internet Explorer only (comments are part of the executed code, don't delete the code/comments below)
// This is to make IE execute our init() function after the DOM has loaded and before the normal window.onload
/*@cc_on @*/
/*@if (@_win32)
	document.write("<script defer src=/javascripts/ie_onload.js><"+"/script>");
/*@end @*/

