// document read function
$(function() {
	fixcontentWrapperHeight();
	convertHRs();
	Navigation.initialize();
});


function fixcontentWrapperHeight() {
	//var navCol  = document.getElementById('navCol');
	//var mainCol = document.getElementById('mainCol');
	//var contentWrapper = document.getElementById('contentWrapper');
	//if (navCol.offsetHeight > mainCol.offsetHeight)
	//	$('#contentWrapper').css('height', (navCol.offsetHeight - (mainCol.offsetHeight - contentWrapper.offsetHeight) + 38) + 'px');
};

function convertHRs() {
	var hrs = $('#contentWrapper hr');
	hrs.each(function() {
		var newHR = $.DIV({ className: 'hr' }).insertBefore(this);
		$.DIV({ className: 'hrl' }).appendTo(newHR);
		$.DIV({ className: 'hrr' }).appendTo(newHR);
		$.DIV({ className: 'hrm' }).appendTo(newHR);
		$(this).remove();
		fixHRSize(newHR);
	});
};

function fixHRSize(hr) {
	var width = hr.offsetWidth;
	var left  = $(hr).find('.hrl').get(0);
	var right = $(hr).find('.hrr').get(0);
	$(hr).css('width', ((right.offsetLeft - left.offsetLeft) + right.offsetWidth - ($.browser.msie ? 3 : 0)) + 'px');
};


var Navigation = {
	initialize: function() {
		this.drawChildExpandos();
		this.setupExpandoClicks();
	},
	
	drawChildExpandos: function() {
		var childExpandos = $('#nav li li.expand > a');
		childExpandos.each(function() {
			var value = this.firstChild.nodeValue;
			value = (value.indexOf('+') == 0 || value.indexOf('-') == 0) ? value.substr(2) : value;
			this.firstChild.nodeValue = ($(this).parent('li').hasClass('selected') ?
				'- ' : '+ ') + value;
		});
	},
	
	setupExpandoClicks: function() {
		var expandos = $('#nav li.expand a[@href$="#"]');
		expandos.click(this.toggleOpen);
	},
	
	expandoClick: function(event) {
		Navigation.closeAll();
		var li = $(this).parent('li');
		if (!li.hasClass('current'))
			$(this).parent('li').toggleClass('selected');
		return false;
	},
	
	toggleOpen: function(event) {
		this.blur();
		var li = $(this).parent('li');
		if (li.hasClass('current')) return false;
		
		li.toggleClass('selected');
		var expandos = $('#nav li.expand').filter('> a[@href$="#"]').not(li.get(0));
		expandos.each(function() {
			var expando = $(this);
			if (!expando.hasClass('current'))
				expando.removeClass('selected');
		});
		
		var parents = li.parents('li');
		if (parents.length > 0) {
			parents.each(function() {
				$(this).addClass('selected');
			});
		}
		Navigation.drawChildExpandos();
		fixcontentWrapperHeight();
		return false;
	}
};



$.fn.hasClass = function(className) {
	return jQuery.className.has(this.get(0), className);
};