ExpandableList = HtmlElement.extend({
	_class: 'expandable-list',

	initialize: function() {
		this.injectTop('a', { href: '#', 'class': 'arrow', events: {
			click: function(event) {
				var list = this.getParent();
				list.modifyClass('expanded', !list.hasClass('expanded'));
				event.stop();
			}
		}});
	}
});

SideList = HtmlElement.extend({
	_class: 'side-list',

	initialize: function() {
		// Shorten each list entry to fit the available space
		$$('div.entry', this).each(function(entry) {
			var title = $('div.title a', entry);
			// Span is needed for correct width, otherwise whole column is returned.
			var date = $('div.date span', entry);
			if (title && date) {
				var width = entry.getWidth() - date.getWidth();
				var text = title.getFirstNode();
				var str = text.getText();
				// Remove trailing white space, since we're matching non-white
				// in backward loop.
				var pos = str.length - str.match(/([\s]*)$/)[1].length;
				// Depending on the prediction, either one or the other of
				// the following loops is needed.
				// Now first go backwards until the height fits.
				while (title.getWidth() > width) {
					str = str.substring(0, pos).trim(' .,');
					text.setText(str + '\u2026');
					// Find the previous word using regexp, including whitespace.
					var word = (str.match(/([\s]*[^\s]*)$/) || [])[1];
					if (!word)
						break;
					pos -= word.length;
				}
			}
		});
	}
});

// TODO: See if this can be merged with SideList as it repeats functionality
AutoFit = HtmlElement.extend({
	_class: 'auto-fit',

	initialize: function() {
		var height = this.getHeight();
		var content = this.getFirst();
		var text = content.getLastNode();
		var str = text.getText();
		// Remove trailing white space, since we're matching non-white
		// in backward loop.
		var pos = str.length - str.match(/([\s]*)$/)[1].length;
		// Depending on the prediction, either one or the other of
		// the following loops is needed.
		// Now first go backwards until the height fits.
		while (content.getHeight() > height) {
			str = str.substring(0, pos);
			text.setText(str + '\u2026');
			// Find the previous word using regexp, including whitespace.
			var word = (str.match(/([\s]*[^\s]*)$/) || [])[1];
			if (!word)
				break;
			pos -= word.length;
		}
	}
});


// This is identical with server sided code
var lighterSettings = {
	altLines: 'hover',
	indent: 4,
	mode: 'ol',
	fuel: 'js',
	jsStyles: false
};

$document.addEvent('domready', function() {
	var h = unescape(document.location.hash);
	if (h) scrollToElement(h.substring(1));
	var code = $$('.text pre, .reference-class pre');
	if (code.length) {
		code.light(lighterSettings).each(function(obj, i) {
			var start =code[i].getProperty('start');
			if (start)
				obj.element.setProperty('start', start);
		});
	}
});

var lastMemberId = null;
function toggleMember(id, scrollTo) {
	if (lastMemberId && lastMemberId != id) {
		var prevId = lastMemberId;
		lastMemberId = null;
		toggleMember(prevId);
	}
	var link = $('#' + id + '-link');
	if (link) {
		var desc = $('#' + id + '-description');
		var v = !link.hasClass('hidden');
		lastMemberId = v && id;
		link.modifyClass('hidden', v);
		desc.modifyClass('hidden', !v);
		if (!desc.code && v)
			desc.code = $$('pre', desc).light(lighterSettings);
		if (scrollTo)
			scrollToMember(id);
		return false;
	}
	return true;
}

function scrollToElement(id) {
	var e = $('#' + id);
	if (e) {
		var offs = e.getOffset();
		$window.setScrollOffset(offs);
		if (e.hasClass('member'))
			toggleMember(id);
	} else {
		document.location.hash = id;
	}
}

function toggleThumbnail(id, over) {
	$('#' + id).modifyClass('hidden', over);
	$('#' + id + '_over').modifyClass('hidden', !over);
}
