function makeScroll() {
	if ( jQuery.fn.liScroll ) {		
		$('#headlines li').prepend('&gt; ');
		$('#headlines').liScroll({ travelocity: .04 });
	}
}

// given a menu list, check whether it's opened and show the arrow pointing down or right in the previous header
jQuery.fn.allantChangeArrow = function() {
	return this.each(function() {
		var src = $(this).data('opened') ? '/images/arrow_black_down.png' : '/images/arrow_black_right.png';
		$(this).prev(':header').find('img.folderIcon').attr('src', src);		
	});
};

function collapseStuff() {
	var $co = $('#mainContent .collapseMe');
	$co.each( function() {
			var obj = $(this);
			var hdr = obj.prev();
			if ( hdr.is('.collapseHdr') ) {
				obj.hide();
				hdr.css({ cursor: 'pointer', textDecoration: 'underline' }).click(function(){ obj.slideToggle(); });
			}
	});
	
	if ( location.hash !== "" ) {
		openHash( location.hash );
	} else {
		$co.filter(':first').not('.keepClosed').show();
	}	
	
	if ( $('a[name]').length > 0 ) {
		// Load in plugin that checks for hash changes
		$.getScript('/js/jquery.ba-hashchange.min.js', function(){
      $(window).bind('hashchange', openHash);
    });
	}
}

function openHash( ) {
	if ( location.hash.length == 0 ) { return; }
	var h = location.hash.substr(0,1) === "#" ? location.hash.substr(1) : location.hash;
	var $anchor = $( "a[name='" + h + "']:first");
	if ( $anchor.length > 0 ) {
		$('.collapseMe').hide();
		$anchor.nextAll('.collapseMe:first').show();
		var offset = $anchor.offset();
		if ( offset != null ) {
			window.scrollTo( 0, parseInt( offset.top, 10 ) );
		}
	}
}

jQuery(function($){
	// create headline ticker
	var tickerFile = 'ticker.html';
	var $body = $('body');

	if ($body.hasClass('measurement')) {
		tickerFile = 'ticker_measurement.html';
	} else if ($body.hasClass('solution')) {
		tickerFile =  $body.is('#programmers') ? 'ticker_programmers.html' : 'ticker_solution.html';
	} else if ($body.hasClass('working')) {
		tickerFile = 'ticker_working.html';
	}
	var tickerURL = '/includes/' + tickerFile;
	
	$('#ticker').load(tickerURL + ' #headlines', makeScroll);
	$('#subnav h3 + ul')        // find all first-level sub menus
		.hide()                 // close them
		.data('opened', false)  // record that they're closed
		.prev()                 // go up to the h3 entity, add arrow, set click event to open submenu
			.append(' <img class="folderIcon" src="/images/arrow_black_right.png" />')
			.css({cursor: 'pointer'})
			.click(function(){ 
				var $list = $(this).next('ul');
				var newState = ! $list.data('opened');
				$list.data('opened', newState).allantChangeArrow().slideToggle(); 
			});
	
	collapseStuff();
	
	// expand the submenu that the current page belongs to
	$('#subnav h3.showHere + ul').show().data('opened', true).allantChangeArrow();

	// make second-tier list bullets into dashes
	$('#subcontent ul ul').css('margin-left','10px').find('li').css('list-style-type', 'none').prepend('- ');

	// submit search form on enter key
	$('input[name=q]').keypress(function(e){
		if (e.which == 13) {
			this.form.submit();
			return false;
		}
		return true;
	});
	
});

