// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

/* fade flashes automatically */
/*Event.observe(window, 'load', function() { 
  $A(document.getElementsByClassName('alert')).each(function(o) {
    o.opacity = 100.0
    Effect.Fade(o, {duration: 3.5})
  });
});*/

// respond to a click in a checkbox by changing the class of the first enclosing div
function check(box) {
	var div = box;
	while(div.nodeName != "DIV") {
		//alert("type = "+div.nodeName);
		div = div.parentNode;
	}
	//alert("before: "+div.className);
	var cur_class = div.className;
	div.className = (box.checked) ? cur_class+"_selected" : cur_class.replace("_selected","");
	//alert("after: "+div.className);
}

function clickclear(thisfield, defaulttext) {
	if (thisfield.value == defaulttext) {
		thisfield.value = "";
	}
}

function toggleCheckAll(state) {
	//alert("hi");
	var f = document.shelf.elements;
	for (var ii=0; ii < document.shelf.elements.length; ii++) {
		if(document.shelf.elements[ii].getAttribute("type") == "checkbox") {
			var cur_state = document.shelf.elements[ii].checked;
			if(state != cur_state) {
				document.shelf.elements[ii].checked = state;
				check(document.shelf.elements[ii]);
			}
		}
	}
	return false;
}

// from http://diveintojs.com/demo/delicious-drop-down/menu.html
function MenuController()
{
	this.CurrentMenu = null;
	this.Open = function(submenu) {
		// If there is currently a menu open we close it
		if(this.CurrentMenu != null) {
			this.Close();
		}
		// Set the selected menu
		this.CurrentMenu = document.getElementById(submenu);
		// Set it to show (display: none and display: block in CSS is used to toggle visibility)
		this.CurrentMenu.style.display = "block";
		// Bind our close function to the document's click function so if we click somewhere
		// it will close the menu
		document.onclick = this.Close;
	}
	// Our close function
	var _this = this;
	this.Close = function() {
		// If there's no open menu we exit
		if(_this.CurrentMenu == null)
			return true;
		// Hide the open menu
		_this.CurrentMenu.style.display = "none";
		// Reset our tracking variable
		_this.CurrentMenu = null;
		// Remove our document click function
		document.onclick = null;
		return true;
	}
}
