function debug() {
	$('#debug').show();
}

function isTopLevel() {
	return $(this).find('div.menu').size() == 1;
}

function menuOver(ev) {
	// hide previously visible menu, if one is present
	if(window.currentMenu) {
		clearTimeout(window.hideMenu);
		menuOut();
	}
	// if this is a top level item, class it accordingly and store a reference to it
	var _this = $(this);
	if( !/active/.test(_this.attr('class')) ) {
		$(this).addClass('hover');
		window.currentMenu = $(this);
	}
}

function delayMenuOut() {
	window.currentMenu = $(this);
	window.hideMenu = setTimeout("menuOut()", 300);
}

function menuOut() {
	if ( window.currentMenu.find('div.menu').size() == 1) {
		window.currentMenu.removeClass('hover');
	}
}

function getOverState(sImg) {
    return sImg.replace(/(.*)(\.[gif|jpg|png].*)/, '$1_on$2');
}

function getOutState(sImg) {
	return sImg.replace(/(.*)(_on)(\.[gif|jpg|png].*)/, '$1$3');
}

function goToUrl(oSelect) {
	var val = $(oSelect).val();
	if(val != '') {
		// assume that a fully qualified url is an external url and open it in a new window
		if( val.indexOf('http://') != -1) {
			window.open( val );
		} else {
			window.location.href = val;
		}
	}
}

function openWin(url, w, h) {
	var props = "width=" + w + ",height=" + h + ",toolbar=no,menubar=no,personalbar=no,copyhistory=no,scrollbars=yes";
	return window.open(url, "oNewWin", props);
}

$(document).ready(
	function() {
		// force IE to load background images from cache
		try { 
			document.execCommand("BackgroundImageCache", false, true); 
		} catch(e) {} 	
	
		window.hideMenu = null;
		window.currentMenu = null;
		
		// primary nav mouse events
		$('#primary-nav li').filter(isTopLevel).mouseover(menuOver).mouseout(delayMenuOut);
		
		$('#phone-icon').mouseover(
			function(){ $('#phone').show()} 
		).mouseout(
			function(){ $('#phone').hide()} 
		);
		
		// references to primary elements
		var qContent = $('#content');
		var qSidebar = $('#sidebar');
		
		// if no #sidebar exists, or, if it's empty, then make the #content area occupy its space
		if( ( qSidebar.size() == 0 || qSidebar.children().size() == 0) && qContent.size() > 0) {
			qContent.addClass('full-column');
		}
		
		// locate rollover images and bind mouse events to them
		$('.rollover').each(
			function(index) {
                var _this = $(this);
                // force preloading 'over' images in IE
                var image = _this.attr('src');

                window[image] = new Image();
                window[image].src = getOverState( image );
                // bind mouse events
                _this.bind('mouseover',
					function() {
						var _img = $(this);
						var _src = getOverState( _img.attr('src') );
						_img.attr('src', _src);
					}
				).bind('mouseout',
					function() {
						var _img = $(this);
						var _src = getOutState( _img.attr('src') );
						_img.attr('src', _src);
					}
				);
			}
		);
	}
);