$(function() {
	// Menu
	$('#menu ul li a').live('click', function(e)
	{
		// Close other menus
		var links = $(this).parent().siblings('li').children('a').filter('.active');
		var siblings = $(links).siblings('ul');
		
		$(links).removeClass('active');
		
		if(siblings.length > 0)
		{
			// $(siblings).slideUp('slow', 'easeOutBounce');
			$(siblings).slideUp();
		}
		
		$(this).addClass('active');
		
		// Expand children
		var children = $(this).parent().children('ul');
		
		if(children.length > 0)
		{
			// $(children).slideToggle('slow', 'easeOutBounce');
			$(children).slideToggle();
		}
		
		if(children.length > 0)
		{
			return false;
		}
	});
	
	// Current Menu item
	var controller = $('#menu ul').attr('data-controller');
	var action = $('#menu ul').attr('data-action');
	
	var $top = $('#' + controller);
	
	if($top.siblings('ul').length > 0)
	{
		$top.addClass('active');
		
		// Enable this to select the active menu item on page load.
		//$top.click();
		//$top.parent().find(" ." + action).addClass('active');
	}
	else
	{
		$top.addClass('active');
	}
	
	// Submenus
	$('#menu ul li').each(function(i, el)
	{
		if($(el).children('ul').length > 0)
		{
			$(el).addClass('has_submenu');
		}
	});
	
	// Remove margin from bottom of boxes
	$('#content .box .content p:last-child').css('margin', '0');
	
	// Table clicking
	if($('tr[data-location]').length > 0)
	{
		$('tr[data-location]').click(function()
		{
			window.location = $(this).attr('data-location');
		});
	}
	
	// Checkboxes
	if($('tr th input:checkbox').length > 0)
	{
		$('tr th input:checkbox').click(function(e)
		{
			e.stopPropagation();
			
			var checked = $(this).attr('checked') ? true : false;
			$(this).closest('table').find('input:checkbox').attr('checked', checked);
		});
	}
		
	// Stop checkboxes from clicking the row.
	if($('tr td input:checkbox').length > 0)
	{
		$('tr td input:checkbox').click(function(e)
		{
			e.stopPropagation();
		});
	}
	
	// Datatables
	if($('table:not(.no_dt)').length > 0)
	{
		$('table:not(.no_dt)').dataTable({
			"bStateSave": true
		});	
	}
	
	// Because the controls are floated, they need a clear under it.
	if($('.dataTables_paginate').length > 0)
	{
		$('.dataTables_paginate').after('<div class="clear"></div>');
	}
	
	// Modals.
	if($('a[rel*=bocks]').length > 0)
	{
		$('a[rel*=bocks]').bocks();
	}
	
	// Developer Panel.
	if($('#panel').length > 0)
	{
		$('#panel').click(function()
		{
			$(this).animate({bottom: 0});
		});
	}
	
	// Tabs - unused for now.
	if($('.box .title ul').length > 0)
	{
		$('.box').find('.content:gt(0)').hide();
		
		$('.box .title ul li a').click(function(e)
		{
			e.stopPropagation();
			
			$(this).parent().parent().find('a').removeClass('active');
			$(this).addClass('active');
			
			$(this).parent().parent().parent().siblings('.content').hide();
			var id = $(this).attr('href');
			$(id).fadeIn();
			
			return false;
		});
		
		// Activate the first tab.
		$('.box .title ul li a:first').click();
	}
	
	// Do the counter.
	if($('select[multiple=multiple]').length > 0)
	{
		// Add selection count to select multiple.
		$('select[multiple=multiple]').each(function(el)
		{
			var count = $(this).find('option:selected').length;
			
			$(this).siblings('label').first().append(' (<span class="count">' + count + '</span> selected)');
		});
		
		$('select[multiple=multiple]').change(function()
		{
			var count = $(this).find('option:selected').length;
			
			$(this).siblings('label').first().find('span.count').text(count);
		});
	}
	
	// Mass action dropdowns.
	if($('select.massaction').length > 0)
	{
		$('select.massaction').change(function()
		{
			$(this).parent().attr('action', $(this).val());
			$(this).parent().submit();
		});
	}
});
