// JavaScript Document
$(function() {

	// Sortable Module List
	$('#module-list').sortable({
		axis: 'y',
		placeholder: 'module-list-placeholder',
		opacity: 0.7,
		forcePlaceholderSize: true,
		update: function(event, ui) {
			order = $('#module-list').sortable('serialize');
		},
		handle: $('.handle',this),
		tolerance:'pointer'
	});
	
	// Show/Hide close button for messages
	$('.message').hover(
		function() {
			$('a.close',this).css('visibility','visible');
		},
		function() {
			$('a.close',this).css('visibility','hidden');
		}
	);
	
	// Close message buttons
	$('.message .close').click(function(e) {
		e.preventDefault();
		$(this).parent('.message').fadeOut(700, function() {
			$(this).remove();
		});
	});
	
	// Tooltips
	$('.tooltip').tooltip({
		showURL: false,
		track: true
	});
	
	// Module Content Togglers
	$('.toggle').click(function(e) {
		e.preventDefault();
		
		module = $(this).parent().parent('.module');
		title = $(this).parent('h2');
		
		if($(this).hasClass('expanded')) {
		
			// Change Icon
			$(this).removeClass('expanded');
			$(this).addClass('collapsed');
			
			// Change Tooltip
			$(this).attr('title','Expand');
			$(this).tooltip({
				showURL: false,
				track: true
			});
			
			// Hide Content
			$(title).css({
				'borderWidth':'0px',
				'marginBottom':'0px',
				'paddingBottom':'0px'
			});
			$('.module-content', module).hide();
			$('.module-footer', module).hide();
			
			
		} else if($(this).hasClass('collapsed')) {
		
			// Change Icon
			$(this).removeClass('collapsed');
			$(this).addClass('expanded');
			
			// Change Tooltip
			$(this).attr('title','Collapse');
			$(this).tooltip({
				showURL: false,
				track: true
			});
				
			// Show Content
			$(title).css({
				'borderWidth':'0px 0px 1px 0px',
				'marginBottom':'10px',
				'paddingBottom':'10px'
			});					
			$('.module-content', module).show();
			$('.module-footer', module).show();															
		}
	});
});