jQuery.fn.innerWrap = function() {
    var a, args = arguments;
    return this.each(function() {
        if (!a)
            a = jQuery.clean(args, this.ownerDocument);
        // Clone the structure that we're using to wrap
        var b = a[0].cloneNode(true),
            c = b;
        // Find the deepest point in the wrap structure
        while ( b.firstChild )
            b = b.firstChild;
        // append the child nodes to the wrapper
        jQuery.each(this.childNodes, function(i, node) { 
            b.appendChild(node); 
        });
        jQuery(this)
            // clear the element
            .empty()
            // add the new wrapper with the previous child nodes appeneded
            .append(c);
    });
};

$(document).ready(function() {
	$('dl.collapse dd').hide();
	$('dl.collapse dt').each(function () {
		$(this).innerWrap('<a href="#" title="Expand"></a>');
		$(this).children('a').append(' <img src="images/plus.gif" alt="Expand" />');
	});

	$('dl.collapse').find('a').click(function() {
		if ($(this).children('img').attr('src') == "images/minus.gif") {
			$(this).attr('title','Expand');
			$(this).children('img').attr('src', "images/plus.gif");
			$(this).children('img').attr('alt', 'Expand');
		} else {
			$(this).attr('title','Collapse');
			$(this).children('img').attr('src', "images/minus.gif");
			$(this).children('img').attr('alt', 'Collapse');
		}
		$(this).parent('dt').next('dd').slideToggle();
		return false;
	});

	var page = "";
	$('.services #col1 ul li a').click(function() {
		page = this.getAttribute('href', 2);
		page = page.replace('#', '');
		autoExpand(page);
		return false;
	});

	if (window.location.hash) {
		page = window.location.hash;
		page = page.replace('#', '');
		autoExpand(page);
	}

	function autoExpand(item) {
		$('dl.collapse dd').each(function() {
			if ($(this).is(":visible")) {
				$(this).slideToggle();
			}
		});
		switch(item) {
		case "medicalauthorization":
			//alert('med auth');
			$('a[name=medicalauthorization]').parent().slideToggle();
			$('a[name=recordretrieval]').parent().slideToggle();
			break;
		case "subpoena":
			//alert('subpoena');
			$('a[name=subpoena]').parent().slideToggle();
			$('a[name=serviceofprocess]').parent().slideToggle();
			break;
		case "courtreporting":
			//alert('courtreporting');
			$('a[name=courtreporting]').parent().slideToggle();
			break;
		case "investigations":
			//alert('investigations');
			$('a[name=investigations]').parent().slideToggle();
			$('a[name=skiptrace]').parent().slideToggle();
			break;
		case "courier":
			//alert('courier');
			$('a[name=courier]').parent().slideToggle();
			break;
		}
	}
});

