// http://homework.nwsnet.de/news/ea21_turn-nested-lists-into-a-collapsible-tree-with-jquery
// Execute this after the site is loaded.
$(function() {
    // Find list items representing folders and
    // style them accordingly.  Also, turn them
    // into links that can expand/collapse the
    // tree leaf.
    $('li.collapse > ul').each(function(i) {
        // Find this list's parent list item.
        var parent_li = $(this).parent('li');

        // Style the list item as folder.
        parent_li.addClass('folder');

        // Temporarily remove the list from the
        // parent list item, wrap the remaining
        // text in an anchor, then reattach it.
        var sub_ul = $(this).remove();
        parent_li.wrapInner('<a href="javascript:void(0)" />').find('a').click(function() {
            // Make the anchor toggle the leaf display.
            sub_ul.toggle();
        });
        parent_li.append(sub_ul);
    });

    // Hide all lists except the outermost.
    $('li.collapse ul').hide();
});



/*
$(document).ready(function () {
	attachCustomSelectEvents();
}
*/

function browserIsIE6() {
	var oBrowser = $.browser;
	return (oBrowser.msie && parseInt(oBrowser.version) <= 6);
}

function attachCustomSelectEvents() {
	var rCustomSelects = $("div.select");
	if(!rCustomSelects.length) { return; }
	$(rCustomSelects).bind("click", null, toggleCustomSelectOptions);
	$(rCustomSelects).bind("mouseleave", null, hideCustomSelectOptions);

	// attach hover events to select dropdown items
	if(browserIsIE6()) {
		$("li", rCustomSelects).hover(showCustomSelectOptionHighlight, hideCustomSelectOptionHighlight);
	}

	$("li", rCustomSelects).bind("click", null, updateCustomSelectLabel);
	var oTest = $("ul", rCustomSelects);
	$("ul", rCustomSelects).bind("mouseleave", null, hideCustomSelectOptions);
}

function showCustomSelectOptionHighlight(e) {
	var oEl = e.target;
	$(oEl).addClass("hover");
}

function hideCustomSelectOptionHighlight(e) {
	var oEl = e.target;
	$(oEl).removeClass("hover");
}

function toggleCustomSelectOptions(e) {
	var oEl = e.target;
	var oSelectContainer = $(oEl).closest("div.select");

	if($(oSelectContainer).hasClass("selectActive")) {
		$(oSelectContainer).removeClass("selectActive");
	} else {
		$(oSelectContainer).addClass("selectActive");
	}
}

function showCustomSelectOptions(e){
	var oEl = e.target;
	$(oEl).closest("div.select").addClass("selectActive");
}

function hideCustomSelectOptions(e){
	var oEl = e.target;
	var oRelatedTarget = e.relatedTarget;
	//var sRelatedTargetTag = String(oRelatedTarget.tagName).toLowerCase();
	var oSelect = $(oEl).closest("div.select").get(0);

	// prevent hide if user mouses back into the parent select from the options panel
	if($(oRelatedTarget).closest("div.select").get(0) == oSelect) {
		return;
	}

	$(oSelect).removeClass("selectActive");
}

function updateCustomSelectLabel(e) {
	var oEl = e.target;

	var sValue = oEl.getAttribute("value") || oEl.innerHTML;
	var sLabel = oEl.innerHTML;

	var oSelect = $(oEl).closest("div.select").get(0);
	var oLabel = $("em", oSelect).get(0);

	oSelect.setAttribute("value", sValue);
	oLabel.innerHTML = sLabel;
}
