// prevent firebug errors
if (!window.console || !console.firebug) {
    var names = ["log", "debug", "info", "warn", "error", "assert", "dir", "dirxml", "group", "groupEnd", "time", "timeEnd", "count", "trace", "profile", "profileEnd"];
    window.console = {};
    for (var i = 0; i < names.length; ++i)
        window.console[names[i]] = function() {}
}

// Allow for IE version detection
Prototype.Browser.IE6 = Prototype.Browser.IE && parseInt(navigator.userAgent.substring(navigator.userAgent.indexOf("MSIE")+5)) == 6;
Prototype.Browser.IE7 = Prototype.Browser.IE && parseInt(navigator.userAgent.substring(navigator.userAgent.indexOf("MSIE")+5)) == 7;
Prototype.Browser.IE8 = Prototype.Browser.IE && !Prototype.Browser.IE6 && !Prototype.Browser.IE7;

function show_gdialog(title, content) {
	window.gDialog = new goog.ui.Dialog(null, true);
	window.gDialog.setDisposeOnHide(true);
	window.gDialog.setTitle(title);
	window.gDialog.setButtonSet(null);
	window.gDialog.setDraggable(false);
	window.gDialog.setContent(content);
	window.gDialog.setBackgroundElementOpacity(0.7);
	window.gDialog.setVisible(true);
	window.gDialog.reposition();
	$$('.modal-dialog-bg')[0].setStyle('position: fixed; height: 100%; width: 100%;');
}

function stayintouch_pop() {
	this.out = '<div class="stayintouch"><p><strong>On the page that follows, click the "Register" link to set up your profile .</strong></p><p>We&rsquo;ll let you know when jobs become available that match your selection criteria.</p>';
	this.out += '<p style="text-align: center;"><img src="' + window.BASE_URL + 'images/regform-instrux.jpg" width="353" height="150" alt="[instructions]" border="0"></p>';
	this.out += '<p style="text-align: center;"><a href="https://www.healthcaresource.com/wheelerclinic/index.cfm?fuseaction=login.main" target="_top"><img src="' + window.BASE_URL + 'images/btn_continue.png" border="0" alt="Continue" width="80" height="32"></a></p>';
	this.out += '</div>';
	show_gdialog('Instructions', this.out);
}

function video_pop_OLD(vid) {
	this.out = '<iframe width="425" height="349" src="http://www.youtube.com/embed/' + vid + '" frameborder="0" allowfullscreen></iframe>';
	show_gdialog('Video', this.out);
}

function video_pop(filename) {
	xajax_video_pop(filename);
}

function vimeo_pop(vid) {
	xajax_vimeo_pop(vid);
}

function scrollToEl(e) {
	var el = $(e);
	Effect.ScrollTo(el, {
		afterFinish: function() { new Effect.Highlight(el); }
	});
}

function hide_subnav(except) {
	$$('.primarynav').each(function(pn) {
		if (pn.id != ('primarynav_' + except)) {
			pn.removeClassName('active');
		}
	});
	$$('.navsub_item').each(function(nsi) {
		var ns = nsi.up('.navsubOuter');
		if (ns) {
			if (nsi.id != ('navsub_' + except)) {
				ns.style.display = 'none'; // Hide the subnav menu
			}
		} else {
			var nss = nsi.up('.navsubSpecial');
			if (nss) {
				if (nsi.id != ('navsub_' + except)) {
					nss.style.display = 'none';
				}
			}	
		}
	});
}

function subnav_slide(e) {
	// Fix (i.e., lock) left/right position of submenus relative to the viewport.
	var sn = $(window.activeSubmenu);
	if (sn && sn.viewportOffset()[0] > 0) {
		var lvl = sn.id.split('_').pop(); // "sn" is the subnav item targeted by the click.
		var m = $('primarynav_' + lvl); // "m" is the corresponding main nav item
		var leftpos = m.positionedOffset()[0];
		var rightSide = leftpos + sn.getWidth();
		var leftval = m.positionedOffset()[0];
		if (rightSide > 963) {
			leftval = 963 - sn.getWidth();
		}
		sn.setStyle('left: ' + leftval + 'px;');
	}
}

function mainnav_clicked(e) {
	var navitem = e.findElement('.primarynav');
	var id = navitem.id.split('_').pop();
	var sn = $('navsubOuter_' + id);
	hide_subnav(id);
	if (navitem.hasClassName('active')) {
		if (sn && (sn.getStyle('display') == 'block')) {
			// Hide the submenu
			navitem.removeClassName('active');
			if (sn) { 
				//sn.setStyle('display: none;');
				new Effect.SlideUp(sn, { duration: 0.3});
			}
		} else {
			// Show the submenu.
			if (sn) {
				var leftpos = navitem.positionedOffset()[0];
				var rightSide = leftpos + sn.getWidth();
				var leftval = navitem.positionedOffset()[0];
				if (rightSide > 963) {
					// Don't let sub-menus extend beyond the right border of the site.
					leftval = 963 - sn.getWidth();
				}
				//sn.setStyle('display: block; visibility: visible; position: absolute; left: ' + leftval + 'px;');
				sn.setStyle('visibility: visible; position: absolute; left: ' + leftval + 'px;');
				new Effect.SlideDown(sn, { duration: 0.3});
				window.activeSubmenu = sn.id;
				Event.observe(window, 'resize', subnav_slide);			
			}
		}
	} else {
		// Show the submenu
		navitem.addClassName('active');
		if (sn) {
			var leftpos = navitem.positionedOffset()[0];
			var rightSide = leftpos + sn.getWidth();
			var leftval = navitem.positionedOffset()[0];
			if (rightSide > 963) {
				// Don't let sub-menus extend beyond the right border of the site.
				leftval = 963 - sn.getWidth();
			}
			//sn.setStyle('display: block; visibility: visible; position: absolute; left: ' + leftval + 'px;');
			sn.setStyle('visibility: visible; position: absolute; left: ' + leftval + 'px;');
			new Effect.SlideDown(sn, { duration: 0.3});
			window.activeSubmenu = sn.id;
			Event.observe(window, 'resize', subnav_slide);			
		}
	}
}

function fix_columns() {
	var sl = $('sidebar_left');
	var mc = $('main_column');
	var mch;
	var n = $('news_col1');
	var nn = $('news_col2');
	var nnn = $('news_col3');
	if (sl && mc) {
		$('main_column').style.height = '';
		var slh = sl.getHeight();
		mch = mc.getHeight();
		if (slh > mch) {
			mc.setStyle('height: ' + slh + 'px;');
		}
	}
	if (n && nn && nnn) {
		var nh = n.getHeight();
		var nnh = nn.getHeight();
		var nnnh = nnn.getHeight();
		if (nnh < nh || nnh < nnnh) {
			var maxh = Math.max(nh, nnh, nnnh);
			$('news_col2interior').setStyle('height: ' + (maxh - 12) + 'px;');
		}
	}
	
	// If a "featured image" is present in the page, don't let the text wrap around it.
	var fi = $('featured_image');
	var mcbottom;
	var fibottom;
	var fifixed = false;
	var sTest = $('services');
	var bTest = $('beh');
	var lastDesc;
	if (mc) { lastDesc = mc.descendants().pop(); }
	var ldbottom;
	if (mc && fi && (sTest == null) && (bTest == null)) {
		while (fifixed === false) {
			mcbottom = mc.cumulativeOffset().top + mc.getHeight();
			fibottom = fi.cumulativeOffset().top + fi.getHeight();
			if (fibottom < mcbottom) {
				fi.setStyle('margin-bottom: ' + (mcbottom - fibottom) + 'px;');
				mcbottom = mc.cumulativeOffset().top + mc.getHeight();
				fibottom = fi.cumulativeOffset().top + fi.getHeight() + parseInt(fi.getStyle('margin-bottom'));
				if (fibottom > mcbottom) { 
					if (window.fcRecursion) { window.fcRecursion++; } else { window.fcRecursion = 1; }
					if (window.fcRecursion < 100) {
						// Limit number of recursions.
						fix_columns(); // recurse
					}
					fifixed = true; 
				}
				if (lastDesc) {
					ldbottom = lastDesc.cumulativeOffset().top + lastDesc.getHeight();
					if (ldbottom > mcbottom) {
						if (window.fcRecursion) { window.fcRecursion++; } else { window.fcRecursion = 1; }
						if (window.fcRecursion < 100) {
							// Limit number of recursions.
							fix_columns(); // recurse
						}
						fifixed = true;
					}
				}
				if (fibottom == mcbottom) { fifixed = true; }
			} else { 
				fifixed = true; 
			}
		}
	}
}

function clear_all_menuTimers() {
	var timercount = window.menuTimers.size();
	var i;
	for (i=0; i<timercount; i++) {
		if ((typeof window.menuTimers[i]) != 'undefined') {
			clearTimeout(window.menuTimers[i]);
		}
	}
}

function nav_mouseover(e) {
	var navitem = e.findElement('.primarynav');
	var id = navitem.id.split('_').pop();
	clear_all_menuTimers();
	showSubnav(id);
}

function nav_mouseout(e) {
	var navitem = e.findElement('.primarynav');
	var id = navitem.id.split('_').pop();
	window.menuTimers[id] = setTimeout("hideSubnav("+id+")", window.menuTimeout);	
}

function subnav_mouseover(e) {
	var el = e.findElement('.navsubOuter, .navsubSpecial');
	var id = el.id.split('_').pop();
	clearTimeout(window.menuTimers[id]);
}

function subnav_mouseout(e) {
	var el = e.findElement('.navsubOuter, .navsubSpecial');
	var id = el.id.split('_').pop();
	window.menuTimers[id] = setTimeout("hideSubnav("+id+")", window.menuTimeout);	
}

function hideSubnav(sid) {
	var sn = $('navsubOuter_' + sid);
	var navitem = $('primarynav_' + sid);
	hide_subnav(sid);
	if (sn) {
		navitem.removeClassName('active');
		sn.setStyle('display: none; visibility: hidden;');
	}
}

function showSubnav(sid) {
	var sn = $('navsubOuter_' + sid);
	var navitem = $('primarynav_' + sid);
	var nmi = $('navmain-inner');
	hide_subnav(sid);
	navitem.addClassName('active');
	if (sn) {
		var nmi_left = nmi.cumulativeOffset()[0];
		var nmi_width = nmi.getWidth();
		var nmi_right = nmi_left + nmi_width;
		var leftpos = navitem.cumulativeOffset()[0];
		var rightSide = leftpos + sn.getWidth();
		var leftval = leftpos;
		if (rightSide > nmi_right) {
			// Don't let sub-menus extend beyond the right border of the site.
			leftval = nmi_right - sn.getWidth();
		}
		sn.setStyle('display: block; visibility: visible; position: absolute; left: ' + leftval + 'px;');
		window.activeSubmenu = sn.id;
		Event.observe(window, 'resize', subnav_slide);
	}
}

function init_submenus_clickable() {
	$$('.primarynav').each(function(p) {
		p.observe('click', mainnav_clicked);
	});
}

function init_submenus() {
	window.menuTimeout = 200; // in milliseconds
	window.menuTimers = [];
	$$('.primarynav').each(function(p) {
		p.observe('mouseover', nav_mouseover);
		p.observe('mouseout', nav_mouseout);
	});
	$$('.navsubOuter, .navsubSpecial').each(function(s) {
		s.observe('mouseover', subnav_mouseover);
		s.observe('mouseout', subnav_mouseout);
	});
}

function morenews() {
	// Can't press the button twice in quick succession...
	if (window.moreNewsDisabled === true) { return false; }
	window.moreNewsDisabled = true;
	$('news_spinner').style.visibility = 'visible';
	
	// Bring the next batch of stories into the page.	
	xajax_morenews(window.storiesInPage);
}

function calendar_advance() {
	if (window.calendarButtonsDisabled === true) { return false; }
	window.calendarButtonsDisabled = true;
	$('cal-spinner').style.visibility = 'visible';
	xajax_calendar_advanceFrom(window.calendarYear, window.calendarMonth);
}

function calendar_retreat() {
	if (window.calendarButtonsDisabled === true) { return false; }
	window.calendarButtonsDisabled = true;
	$('cal-spinner').style.visibility = 'visible';
	xajax_calendar_retreatFrom(window.calendarYear, window.calendarMonth);
}

function calskip(evt) {
	if (window.calendarButtonsDisabled === true) { return false; }
	window.calendarButtonsDisabled = true;
	$('cal-spinner').style.visibility = 'visible';
	var menu = $('calskip');
	var val = menu.options[menu.selectedIndex].value;
	var pcs = val.split('-');
	var yr = pcs[0];
	var mnth = pcs[1];
	xajax_calendar_skipTo(yr, mnth);
}

function calselect(yr, mnth) {
	var mnthStr = '' + mnth;
	if (mnthStr.length == 1) { mnthStr = '0'+mnthStr; } // pad with leading zero
	var datestr = '' + yr + '-' + mnthStr;
	var cs = $('calskip');
	var opts = $A(cs.options);
	var optCount = opts.size();
	var i;
	for (i=0; i<optCount; i++) {
		if (opts[i].value == datestr) {
			cs.selectedIndex = i;
			return true;
		} 
	}
	return false;
}

function calupdate(yr, mnth) {
	if (Prototype.Browser.Gecko === true) {
		$('calendar_skipform').setStyle('margin-top: 8px;');
	}
	calselect(yr, mnth);
	$('calskip').observe('change', calskip); 
}

function newsletterSignup() {
	var em = $F('mailing_list_email');
	if (em.blank() === true) {
		alert('Please enter an email address before clicking the "Sign up" button.');
		return false;
	}
	xajax_newsletter_signup(em);
}

function buttonpress(e) {
	var b = e.findElement('.wiggle');
	var p = b.getStyle('position');
	if (p == 'absolute') {
		b.absolutize().setStyle('top: 1px; left: 1px;');
	} else {
		b.relativize().setStyle('top: 1px; left: 1px;');
	}
}

function buttonrelease(e) {
	var b = e.findElement('.wiggle');
	b.setStyle('top: 0px; left: 0px;');
}

function maphide(except) {
	var ex = 'mappop_' + except;
	$$('.mappop').each(function(p) {
		if (p.id != ex) { p.style.display = 'none'; }
	});
}

function hidemappop(town) {
	var pop = $('mappop_' + town);
	if (pop) {
		pop.style.display = 'none';
	}
}

function mappop(e) {
	var el = e.findElement('area');
	var town = el.id.split('_')[1];
	clearTimeout(window.mapTimers[town]);
	maphide(town);
	var pop = $('mappop_' + town);
	if (pop) {
		pop.style.display = 'block';
	}
}

function mapstop(e) {
	var el = e.findElement('area');
	var town = el.id.split('_')[1];
	var pop = $('mappop_' + town);
	window.mapTimers[town] = setTimeout("hidemappop('" + town + "')", window.mapTimeout);
}

function mappopover(e) {
	var el = e.findElement('.mappop');
	var town = el.id.split('_')[1];
	clearTimeout(window.mapTimers[town]);
}

function mappopout(e) {
	var el = e.findElement('.mappop');
	var town = el.id.split('_')[1];
	window.mapTimers[town] = setTimeout("hidemappop('" + town + "')", window.mapTimeout);
}

function init_buttons() {
	var w = $$('.wiggle');
	w.each(function(b) {
		b.observe('mousedown', buttonpress);
		b.observe('mouseup', buttonrelease);
	});
}

function init_locmap() {
	window.mapTimeout = 200;
	window.mapTimers = [];
	if ($('bigmap_imm')) {
		$$('#bigmap_imm area').each(function(a) {
			a.observe('mouseover', mappop);
			a.observe('mouseout', mapstop);
		});
		$$('.mappop').each(function(mp) {
			mp.observe('mouseover', mappopover);
			mp.observe('mouseout', mappopout);
		});
	}
}

function global_init() {
	fix_columns();
	init_submenus();
	init_buttons();
	init_locmap();
	var cs = $('calskip');
	if (cs) { 
		cs.observe('change', calskip); 
		if (Prototype.Browser.Gecko === true) {
			$('calendar_skipform').setStyle('margin-top: 8px;');
		}
	}
}
Event.observe(window, 'load', global_init);
