var moz = (document.nodeName)? true:false;	// Check if browser is mozilla
var ie = (document.all)? true:false;		// Check if browser is ie
var win = null;

var BUTTONS_DIR = "/wp-content/themes/uumc/graphics/";

// Preload the buttons
var buttons = new Array("listen",
						"getmore"
);

buttons['listen'] 		= new Array("listen-link.gif","listen-hover.gif");
buttons['getmore'] 		= new Array("getmore-link.gif","getmore-hover.gif");

function rollovers () {
	for (i = 0; i < buttons.length; i++) {
		button = buttons[i];
		img = (ie)? document.all(button) : document.getElementById(button);
	
		if (img) {

			buttons[button][2] = new Image();
			buttons[button][2].src = BUTTONS_DIR + buttons[button][0];
			buttons[button][3] = new Image();
			buttons[button][3].src = BUTTONS_DIR + buttons[button][1];
				
			img.onmouseover = hover;
			img.onmouseout = normal;		
		}
						
	}
}

function hover (e) {
    e = (e) ? e : ((window.event) ? window.event : "");
    
    if (e) {
    	img = get_button(e);
		img.src = buttons[img.id][3].src;
	}

}

function normal (e) {
    e = (e) ? e : ((window.event) ? window.event : "");
 
    if (e) {
    	img = get_button(e);
    	img.src = buttons[img.id][2].src;
    } 
}

function get_button (e) {
	var button;
    if (e.target) {
        button = (e.target.nodeType == 3) ? e.target.parentNode : e.target;
    } else {
        button = e.srcElement;
    }
    return button;
}

function menusInit() {
	var menus = document.getElementById("navigation").getElementsByTagName("li");
	for (var i = 0; i< menus.length; i++) {
		menus[i].onmouseover=function() {
			this.className+=" hover";
		}
	
		menus[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" hover\\b"), "");
		}
	}
}

// this function is need to work around 
// a bug in IE related to element attributes
function hasClass(obj) {
	var result = false;
	if (obj.getAttributeNode("class") != null) {
		result = obj.getAttributeNode("class").value;
	}
	return result;
}

function stripe () {

	// the flag we'll use to keep track of 
	// whether the current row is odd or even
	var even = false;
	
	// if arguments are provided to specify the colours
	// of the even & odd rows, then use the them;
	// otherwise use the following defaults:
	var evenClass = arguments[1] ? arguments[1] : "evenrow";
	var oddClass = arguments[2] ? arguments[2] : "oddrow";
	
	// obtain a reference to the desired table
	// if no such table exists, abort
	var tables = document.getElementsByTagName("table");
	if (! tables) { return; }
	
	for (var g = 0; g < tables.length; g++) {
	
		// by definition, tables can have more than one tbody
		// element, so we'll have to get the list of child
		// <tbody>s 
		var tbodies = tables[g].getElementsByTagName("tbody");
	
		// and iterate through them...
		for (var h = 0; h < tbodies.length; h++) {
			
			// find all the <tr> elements... 
			var trs = tbodies[h].getElementsByTagName("tr");
			
			// ... and iterate through them
			for (var i = 0; i < trs.length; i++) {
	
				// avoid rows that have a class attribute
				// or backgroundColor style
				if (!hasClass(trs[i]) && ! trs[i].style.backgroundColor) {
	
					// get all the cells in this row...
					var tds = trs[i].getElementsByTagName("td");
					
					// and iterate through them...
					for (var j = 0; j < tds.length; j++) {
	
						var mytd = tds[j];
						
						// avoid cells that have a class attribute
						// or backgroundColor style
						if (! hasClass(mytd) && ! mytd.style.backgroundColor) {
						
							mytd.className = even ? evenClass : oddClass;
						
						}
					}
				}
				// flip from odd to even, or vice-versa
				even =  ! even;
			}
		}
	}
}


function NewWindow (mypage,myname,w,h,scroll) {
	LeftPosition = (screen.width) ? (screen.width-w)/2 : 0;
	TopPosition = (screen.height) ? (screen.height-h)/2 : 0;
	settings = 'height='+h+',width='+w+',top='+TopPosition+',left='+LeftPosition+',scrollbars='+scroll+',resizable';
	win = window.open(mypage,myname,settings);
}

function exitLink (url) {
	if(q = confirm("By clicking this link, you will be leaving the Urbana United Methodist Church website and enter another website created, operated and maintained by another organization."))
		document.location.href = url;
}


function initBehaviors () {
	stripe();
	menusInit();
	rollovers();	
}

window.onload = initBehaviors;

