// tiny_mce stuff moved to toggle_editor.js

function setNoDate(whichElement) {
	var month = document.getElementById(whichElement + '_month');
	var day   = document.getElementById(whichElement + '_day');
	var year  = document.getElementById(whichElement + '_year');

	month.options[0].selected = true;
	day.options[0].selected = true;
	year.options[3].selected = true;
}

// Add trim() method to JavaScript String Type: http://www.delphifaq.com/faq/f1031.shtml
String.prototype.trim = function () {
	return this.replace(/^\s*/, "").replace(/\s*$/, "");
};

// Do JQuery stuff
var gid = 0;
var whichGidDiv = '';

// Group stuff
$(function() {
	$('#groups_modal').dialog({
		bgiframe: true,
		autoOpen: false,
		resizable: false,
		draggable: false,
		height: 200,
		width: 640,
		modal: true,
		overlay: {
			backgroundColor: '#000',
			opacity: 0.5
		},
		// Show any previously hidden group for next group.
		close: function() { $('#group_option_' + gid).show(); }
	});
});

// Callback function for success parameter of ajax call to groups_modal.
function renameGroupOnThisPage(newName) {
	$('#' + whichGidDiv).next().fadeOut('slow').text(newName).fadeIn('slow');
}

$(document).ready(function(){
	$('#tinyMCEmodalWindow').jqm();

	// cycle through all elements in .js_required class...
	$('.js_required').each(function() {
		$(this).show();	//... and display them
	});

	// The "Select: All, None" thingie in all pages with groupings plus guestbook.
	// use with: <div class="check_all_or_none js_required">Select: <span class="check_all" role="link">All</span>, <span class="check_none" role="link">None</span>.</div>
	$('.check_all').click(function() {
		$(this).parent().next().find(':checkbox').attr('checked', 'checked');
	});

	$('.check_none').click(function() {
		$(this).parent().next().find(':checkbox').removeAttr('checked');
	});

	// Set/bind stuff for renaming groups
	$('.groups_modal_link').css('cursor', 'pointer').attr('role', 'link').click(function() {
		$('.rename_group').show();
		$('#rename_group_reserved').hide();

		$('#group_move_select, #move_group_button, label[for="group_move_select"]').show();

		// Set global var for use in renameGroupOnThisPage() function. Need to find out if these can be sent as paramaters.
		whichGidDiv	= this.id;					// Global var used in renameGroupOnThisPage()
		var temp	= whichGidDiv.split('_');
		gid			= temp[1];					// Global var used in 'rename' ajax call

		// Hide the selected group from the select dropdown. Need to "show()" it again in dialog close event above.
		$('#group_option_' + gid).hide();

		// Hide the move stuff if there is only one group
		if($('.group_option').size() == 1)
			$('#group_move_select, #move_group_button, label[for="group_move_select"]').hide();

		// Disallow renaming of 'Large Images' or 'Style Graphics' but only in 'Image Bank'
		if($(this).parents().is("#images") && ($(this).next().text() == 'Large Images' || $(this).next().text() == 'Style Graphics')) {
			$('.rename_group').hide();
			$('#rename_group_reserved').show();
		}

		$('#rename_group_textbox').val($(this).next().text().trim());
		$('#groups_modal').dialog('open');
	});

	$('#rename_group_button').click(function() {
		newName = $('#rename_group_textbox').val().trim();
		serial = 'gtitle=' + $('#rename_group_textbox').serialize();
		// Update in the database
		$.post('/hostbaby2/website/groups/rename/' + gid, serial, renameGroupOnThisPage(newName));

		// Update in the select dropdown
		$('#group_option_' + gid).text(newName);
		$('#groups_modal').dialog('close');
	});

	$('#move_group_button').click(function() {
		// Update in the database and refresh upon success
		$.post('/hostbaby2/website/groups/move_below/' + gid + '/' + $('#group_move_select option:selected').val(), function(data) { location.reload(true); });
	});

	// Prevent accidental navigation away, from: http://jonstjohn.com/node/23 (hmm... doesn't seem to consider "Select: All" as a 'change' event.)
	//$(':input').bind('change', function() { setConfirmUnload(true);  });
	//$('input:text, input:textarea').bind('change', function() { setConfirmUnload(true); });
	//$(':form').bind('submit', function() { setConfirmUnload(false); });
});

// This message will be used here and in toggle_editor.js
var confirmUnloadMessage = 'You have made changes on this page. If you navigate away from this page without saving, your changes will be lost.';

function setConfirmUnload(on) {	// http://jonstjohn.com/node/23
	window.onbeforeunload = (on) ? function() { return confirmUnloadMessage; } : null;
}

// Settings page
function settings_toggle(whichRadioButton) {
	document.location = 'settings/' + whichRadioButton.name + '/' + whichRadioButton.value;
}

