/*
*************************************************

PLANETEYE
JavaScript Functions

Created by the wacky troop at Happy Cog
http://www.happycog.com/

*************************************************
*/


/**
 * Toggle Tabs
 *
 * @created Wed Aug 13 13:07:54 EDT 2008
 * @return void
 * @author Mark Huot
 **/
$(document).ready(function()
{
	$('*:has(> .toggle)').each(function()
	{
		var target = $('.toggle:first', this);
		var toggles = $('.toggle', this);
		
		target.before('<p id="toggler"></p>');
		
		$('.toggle h2:first-child', this).each(function(count)
		{
			var parent = $(this).parents('.toggle');
			var anchor = $(document.createElement('a'));
			anchor.attr('href', '#');
			anchor.addClass($(this).parents('.toggle').attr('id'));
			anchor.click(function()
			{
				$(this).parent().find('a').removeClass('selected');
				$(this).addClass('selected');
				
				$(toggles).css('display', 'none');
				$(parent).css('display', 'block');
				return false;
			});
			anchor.html($(this).html());
			
			if(count == 0)
			{
				anchor.addClass('selected');
			}
			else
			{
				$('#toggler').append(' / ');
			}
			
			$('#toggler').append(anchor);
		});
		
		toggles.css('display', 'none');
		toggles.eq(0).css('display', 'block');
	});
});









/**
 * Add a Note
 *
 * @created Wed Aug 13 13:07:54 EDT 2008
 * @return void
 * @author Mark Huot
 **/
$(document).ready(function()
{
	function add_note_events()
	{
		$('.add-note, .notes .edit').click(function()
		{
			var anchor = this;
			
			$.get('/snippets/add-note.html', function(data)
			{
				$(anchor).replaceWith(data);
				$('.save-note').click(function()
				{
					var notes = $(this).parents('.notes')
					
					// $.get('', function(data)
					// {
					// 	notes.replaceWith(data);
					// });
					notes.replaceWith('<p class="add-note"><a href="#">Add a note</a></p>');
					add_note_events();
					return false;
				});
			});
			
			return false;
		});
	}
	
	add_note_events();
});

/**
 * Duplicate Actions
 *
 * @created Wed Aug 13 13:07:54 EDT 2008
 * @return void
 * @author Mark Huot
 **/
$(document).ready(function()
{
	$('div#travel_pack_contents:has(.actions)').each(function()
	{
		var actions = $('.actions', this).clone();
		actions.addClass('actions-top');
		
		$(this).prepend(actions);
	});
});

/**
 * Replace Buttons
 *
 * @created Wed Aug 13 13:07:54 EDT 2008
 * @return void
 * @author Mark Huot
 **/
$(document).ready(function()
{
	$('.actions input[type="submit"], .actions input[type="button"]').each(function()
	{
		var name = $(this).attr('name');
		var value = $(this).val();
		var a = $(document.createElement('a'));
		a.attr('href', '#');
		a.click(function()
		{
			$(this).parents('form').prepend('<input type="hidden" name="'+name+'" value="'+value+'" />');
			$(this).parents('form').submit();
			return false;
		});
		a.html($(this).val());
		$(this).replaceWith(a);
	});
});

/**
 * Add Uncheck All
 *
 * @created Thur Aug 20 12:54 EDT 2008
 * @return	void
 * @author	Adam Bullied
 **/
$(document).ready(function()
{
	$('.actions').prepend('<li><a href="#" class="uncheck-all">Uncheck All</a></li>');
	$('.uncheck-all').click(function()
	{
		$(this).parents('form').find('input[type="checkbox"]').attr('checked','');
		return false;
	});
});

/**
 * Add Check All
 *
 * @created Wed Aug 13 13:07:54 EDT 2008
 * @return void
 * @author Mark Huot
 **/
$(document).ready(function()
{
	$('.actions').prepend('<li><a href="#" class="check-all">Check All</a></li>');
	$('.check-all').click(function()
	{
		$(this).parents('form').find('input[type="checkbox"]').attr('checked', 'checked');
		return false;
	});
});









