function menuBehavior() {
	//drop down menu behavior
	jQuery("#referenceList li").click(
		function () {
			jQuery(this).children("img:nth-child(2)").toggle();
			jQuery(this).children("img:first-child").toggle();
			
			if(jQuery(this).children("ul:hidden").length) {
				jQuery(this).children("ul").show("fast");
			}
			else {
				jQuery(this).children("ul").hide("fast");
			}
			return false;
		}
	);
}
		
jQuery(document).ready(
	function() {
		//initialize menu
		jQuery("#referenceList li ul").hide();
		myBehaviors = new oAjaxAttacher(
			'#referenceList',
			'#ajaxTarget',
			'#ajaxTarget',//'#',
			'.messages',
			['#page', '#pageType', '#pageNode', '#pageNodeType'],
			[collapseAutoAttach, activeselectAutoAttach, uploadAutoAttach],
			".messages:contains('updated'), .messages:contains('created'), .messages:contains('deleted')",
			refTrigger,
			"Loading...",
			{'formLink' : '#main', 'libraryLink' : '#main'}, //specific filters(value) for links with classes(key),
			{0:'libraryLink' }
		);
		myBehaviors.linkTarget = jQuery(myBehaviors.linkTarget);
		myBehaviors.linkSource = jQuery(myBehaviors.linkSource);
		attachLinksToTarget(myBehaviors.linkSource, myBehaviors);
		menuBehavior();
	}
);

var listState;
var listStateVisible;
function saveListState() {
	listState = jQuery('#referenceList :hidden');
	listStateVisible = jQuery('#referenceList :visible');
}

function restoreListState() {
	var selector;
	for(var i in listState) {
		selector = '#' + listState[i].id;
		jQuery(selector).hide();
	}
	for(var i in listStateVisible) {
		selector = '#' + listStateVisible[i].id;
		jQuery(selector).show();
	}
}

var refTrigger = function (scope, oOptions) {
	jQuery.ajax({
		oOptions : oOptions,
		url: document.location.href,//'/resources',
		data: 'menu=1',
		dataType:"html",
		complete: function(res, status) {
			saveListState();
			jQuery("#referenceList").html('Loading...');
			if ( status == "success" || status == "notmodified" ) {
				var responseText = res.responseText;
			
				//Hide the link target to our manipulations to it do not look messy.
				jQuery("#referenceList").hide();
				
				//Store the response in a temporary jQuery object so we can more easily operate on it
				var temp = jQuery("<div/>").prepend(responseText.replace(/<script(.|\s)*?\/script>/g, ""));
							
							
				//Filter the html response by the designated responseFilter.  If that element is not found, simply inject all the HTML
				if(jQuery('#referenceList', temp).length) {
					jQuery("#referenceList").html(jQuery('#referenceList', temp).html());
				}
				else {
					jQuery("#referenceList").html(temp.html());
				}
					
				//call other js functions that may need to responsd to the new html
				for(var i in this.oOptions.jsResponseCallbacks) {
					this.oOptions.jsResponseCallbacks[i]();
				}
				menuBehavior();
				restoreListState();
				jQuery("#referenceList").show("fast");
				attachLinksToTarget(jQuery("#referenceList"), this.oOptions);	
			}
			else {
				jQuery("#referenceList").html("Error loading page, please try again later.")
			}
		}
	});
}