/**
 * Collection of methods used randomly and not written by Paul O'Shannessy
 */

// function that opens a window (via Macromedia)
function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}// Example:

// simplePreload( '01.gif', '02.gif' ); 
function simplePreload_Paul() { // takes an array instead of a list of arguments, modified from MM
	var args = simplePreload_Paul.arguments[0];
	//console.debug("preloading: " + args);
	document.imageArray = new Array(args.length);
	for(var i=0; i<args.length; i++) {
		document.imageArray[i] = new Image;
		document.imageArray[i].src = args[i];
	}
}


/**
 * @author Paul O'Shannessy
 * 
 * This is a lame menu imitator.
 * And doesn't actually do much complicated.
 * Makes use of functionality provided by the YAHOO! User Interface libraries
 */

var my_images = new Array;


var menu_timeout = 4000; // 4 seconds
var menu_selected = null;
var menu_current = null;
var menu_items = null;
var menu_selected_visible = null;
var $D = YAHOO.util.Dom;
var $E = YAHOO.util.Event;
var $ = $D.get

var fadeout = function (el) {
	if(!el) {return;}
	var a = new YAHOO.util.Anim(el, { opacity: { to: 0 } }, 1, YAHOO.util.Easing.easeOut);
	a.animate();
	$D.setStyle(el, "display", "none");
}

var fadein = function (el) {
	if(!el) {return;}
	$D.setStyle(el, "display", "");
	var a = new YAHOO.util.Anim(el, { opacity: { to: 1 } }, 1, YAHOO.util.Easing.easeOut);
	a.animate();
}

var menu_init = function () {
	// get all menu items so that we don't have to find each time
	menu_items = $D.getElementsByClassName("inner_nav", "div", "banner");
	
	new_menu_items = new Array();
	
	// find selected menu it & set it
	for (i in menu_items) {
		menu_item = menu_items[i];
		
		if ($D.hasClass(menu_item, "selected")) {
			menu_selected = menu_item;
			menu_selected_visible = true;
		}
	}
	// if none, then ______
}

var menu_on = function (s) {
	
	menu_item_id = "inner_nav_" + s;
	
	current = null
	
	for (i in menu_items) {
		menu_item = menu_items[i];
		if (menu_item.id == menu_item_id) {
			current = menu_item;
			break;
		}
	}
	
	if (menu_selected_visible && menu_selected == current) {
		return;
	}

	if(menu_selected_visible) {
		fadeout(menu_selected);
		menu_selected_visible = false;
	}
	if(menu_current) {
		fadeout(menu_current);
	}
	
	// assign current to menu_current
	menu_current = current;
	
	// show menu_current
	fadein(menu_current);
	
	if (menu_selected == menu_current) {
		menu_selected_visible = true;
	}
}

var menu_off = function (s) {
	var id = "inner_nav_" + s;
	if(menu_selected && id == menu_selected.id) {return;}
	setTimeout("menu_restore('" + id + "')", menu_timeout);
}

var menu_restore = function (w) {
	// checks to make sure that when we are restoring, it doesn't restore if the
	// current menu isnt the same as the one we thought
	el = $(w);
	if(menu_current && !menu_selected_visible && el == menu_current) {
		fadeout(menu_current);
		menu_selected_visible = true;
		fadein(menu_selected);
	}
}
$E.onContentReady("container", menu_init);


// below is code for the "4 square"

var four_square_selected = null;
var four_square_current = null;
var four_square_elements = ["student", "calendar", "building"];

var four_square_set_src = function(el, src) {
	r = new RegExp(src);
	if(r.test(el.src)) {
		return;
	}
	newsrc = el.src.replace(/(.*\/)(.*\.gif)/, '$1' + src)
	el.src = newsrc;
}

var four_square_highlight = function (s) {
	id = "four_square_" + s + "_img";
	pic_img = $(id);
	txt_img = $("four_square_text_img");
	// toggle img
	four_square_set_src(pic_img, "4_square_" + s + ".gif");
	// fade the others
	for(i = 0; i < four_square_elements.length; i++) {
		if(four_square_elements[i] != s) {
			four_square_fade(four_square_elements[i]);
		}
	}
	// update the text
	four_square_set_src(txt_img, "4_square_" + s + "_text.gif");
}

var four_square_fade = function (s) {
	id = "four_square_" + s + "_img";
	pic_img = $(id);
	four_square_set_src(pic_img, "4_square_" + s + "_fade.gif");
}

var four_square_reset = function () {
	if(!$("four_square")) {
		return;
	}
	if(four_square_selected) {
		four_square_highlight(four_square_selected)
	} else {
		for(i = 0; i < four_square_elements.length; i++) {
			four_square_fade(four_square_elements[i]);
		}
		four_square_set_src($("four_square_text_img"), "4_square_no_text.gif");
	}
}

$E.onContentReady("container", four_square_reset);


// Blur login fields, because it's cool
var field_onoff = function (el, defval, on, pass) {
	pass = (pass == null)?false:true;
	if(!on) {
		if(el.value == "") {
			el.value = defval;
			if(el.type == "password") {	el.type = "text"; }
		}
	} else {
		if(el.value == defval) {
			el.value = "";
			if(pass && el.type == "text") { el.type = "password"; }
		}
	}
}
var contact_form_errors = function(errors) {

	form_errors = "<p>The following errors have been found on your form.  Please fix before submitting again.<\/p><ul>";
	for(err in errors) {
		if(!(n = parseFloat(err) || err == 0)) { continue; } // need this to parse out random things that get added...
		form_errors += "<li>" + errors[err] + "<\/li>";
	}
	form_errors += "<\/ul>";
	el = $("form_errors");
	el.innerHTML = form_errors;
	fadein(el);
}
var contact_form_success = function() {
/*
	console.log("form success");
	el = $("form_success");
	el.innerHTML = "<p>The form was successfully submitted!</p>";
	fadein(el);
*/
	button = $("contact_form_submit");
	button.value = "Thanks for your information!";
	button.disabled = true;
	window.open('/includes_contact_form/complete.php', 'shalompopup', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=400,height=300');
}

var reservation_form_success = function() {
	button = $("reservation_form_submit");
	button.value = "Thanks for your information!";
	button.disabled = true;
	window.open('/about/reservation_form_complete.php', 'shalompopup', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=400,height=300');
}

var autofill = function(field_name, val) {
	if(el = $(field_name)) {
		el.value = val;
	}
}

/*
var tweak_gcal = function() {
	g = $("gcal_iframe");
	$D.setStyle(g, "font-size", "50%");
}
$E.onContentReady("gcal_iframe", tweake_gcal);
*/

var toggle_alumni = function(val) {
	(val == "yes") ? fadein($("alumni_info")) : fadeout($("alumni_info"));
}