$(document).ready(function() {
	
	//Add in any delete confirmations.
	$('li.delete a, a.delete').click(function(){
		return confirm('Are you sure you wish to delete this item?');
	});
	
	//This bit of code changes the text on submit buttons and disables them when clicked. It requires a hidden label field.
	/*$(":submit").click(function(){
		if(typeof $('#' + this.id + 'action').val() != 'undefined') {
			this.value = $('#' + this.id + 'action').val();
		}
		
		//In safari, opera, and possibly other browser, disabling the submit button prevents postback.
		if($.browser.mozilla) {
			this.disabled = true;
		}
		
		return true;
	});*/
			
	//Loop through all expand more links
	$("li.expandMore a, a.expandMore").each(function() {
		//alert($('div.formErrors', '#' + expandMoreElementID).length())
		var expandMoreElementID = $(this).attr("href").replace(/#/, '');
		if($('#' + expandMoreElementID + 'HideState').val() != 'noHide' && $('div.formErrors', '#' + expandMoreElementID).length == 0) {
			$('#' + expandMoreElementID).hide();
		}
		$(this).click(function() { 	
			$('#' + expandMoreElementID).slideDown(200, function() {
				$(':input[type!=hidden]', '#' + expandMoreElementID)[0].focus();
			});
				
			$('#' + expandMoreElementID + 'HideState').val('noHide');
				return false; 
		});		
     });
     
     
	//When a cancel link is clicked.
	$('a.cancel').click(function() {
		var expandMoreElementID = $(this).attr("href").replace(/#/, '');
		if($('#' + expandMoreElementID).length != 0) {
			$('#' + expandMoreElementID).slideUp(200);
		}
		return false;
	});	
		
		
     //Find any collapsable sections.
     $("a.collapseContent").each(function() {
     	
     	var anchor = this;
     	var collapseElementID = $(this).attr("href").replace(/#/, '');
     	
     	if($(anchor).hasClass('onLoad') && $('#' + collapseElementID + 'HideState').val() != 'noHide') {
			$('#' + collapseElementID).hide();
			$('span:first',anchor).hide();
		} else {
			$('span:last',anchor).hide();
		}
		
		$(anchor).click(function(){
			
			if($('span:first',anchor).css('display') == 'none') {
				
				$('#' + collapseElementID).slideDown(200, function() {
					try {
						$(':input[type!=hidden]', '#' + collapseElementID)[0].focus();
					} catch(e) {}
					
				});
				$('span:last',anchor).hide();
				$('span:first',anchor).show();
				$('#' + collapseElementID + 'HideState').val('noHide');
				return false;
				
			} else {

				$('#' + collapseElementID).slideUp(200);
				$('span:first',anchor).hide();
				$('span:last',anchor).show();
				$('#' + collapseElementID + 'HideState').val('');
				return false;
					
			}
			
		});
		     	
     });
     
     
	
});