// JavaScript Document

setTimeout('initializeTinyMCE();', 1000);

function initializeTinyMCE() {
	tinyMCE.init({ 
		mode					: "exact",
		elements				: "caption1",
		theme					: "advanced",
		plugins					: "safari",
		theme_advanced_buttons1 : "link,unlink",
		theme_advanced_buttons2 : false,
		theme_advanced_buttons3 : false,
		relative_urls			: false,
		remove_script_host		: true,
		document_base_url		: "http://"+window.location.hostname+"/",
		paste_remove_spans		: true,
		paste_remove_styles		: true,
		fix_table_elements		: true,
		force_p_newlines		: false
	});
}

function galleryGetImages()
{
	$('#images').html('<div align="center">loading...</div>');

	$.ajax({
		type: "POST",
		url: "http://"+window.location.hostname+"/ajax/galleryImages/module:gallery/",
		dataType: "html",
		data: "gid="+$('#gid').val(),
		success: function(result) {
			$('#images').html(result);
		}
	})

	if ($('#gid').val())
	{
		$('.galleryAdminControls').css('display', 'block');
	}
}

var galleryImgCaptionList = Array();

function galleryImgEdit(image)
{
	galleryImgCaptionList[image] = $('#caption_' + image).html();

	var caption = ($('#caption_' + image).html() != 'No Caption' ? $('#caption_' + image).html() : '');

	$('#caption_' + image).html('<input style="width:98%" id="captionEdit_' + image + '" /><a href="#" onclick="galleryImgUpdate('+image+'); return false;">Save</a> | <a href="#" onclick="galleryImgCaptionCancel('+image+'); return false;">Cancel</a>');
	$('#captionEdit_' + image).val(caption);
}

function galleryImgCaptionCancel(image)
{
	$('#caption_' + image).html(galleryImgCaptionList[image]);
}

function galleryImgUpdate(image)
{
	var caption = escape($('#captionEdit_' + image).val());

	$.ajax({
		type: "POST",
		url: "http://" + window.location.hostname + "/ajax/galleryImgCaption/module:gallery/",
		data: "caption=" + caption + "&img=" + image,
		success: function(result)
		{
			if (result == 1)
			{
				$('#caption_' + image).html($('#captionEdit_' + image).val());
			}
			else
			{
				$('#caption_' + image).html(galleryImgCaptionList[image]);
				alert('Error: Could not save caption.');
			}
		}
	});
}

function galleryImgUp(image, recurse)
{
	$.ajax({
		type: "POST",
		url: "http://"+window.location.hostname+"/ajax/galleryImgUp/module:gallery/",
		dataType: "html",
		data: "img="+image,
		success: function(result) {
			if (recurse) {
				galleryImgDown(result, false);
			} else {
				galleryGetImages();
			}
		}
	});
}

function galleryImgDown(image, recurse)
{
	$.ajax({
			type: "POST",
			url: "http://"+window.location.hostname+"/ajax/galleryImgDown/module:gallery/",
			dataType: "html",
			data: "img="+image,
			success: function(result) {
				if(recurse) {
					galleryImgUp(result, false);
				} else {
					galleryGetImages();
				}
			}
		});
}

function galleryAdd()
{
	$.ajax({
			type: "POST",
			url: "http://"+window.location.hostname+"/ajax/galleryAdd/module:gallery/",
			dataType: "html",
			data: "name="+$('#name').val(),
			success: function(result) {
				alert(result);
			}
		});
}

function galleryImgAdd()
{
	var caption = tinyMCE.get('caption1').getContent();

	$.ajax({
			type: "POST",
			url: "http://"+window.location.hostname+"/ajax/galleryImgAdd/module:gallery/",
			dataType: "html",
			data: "gid=" + $('#gid').val() + "&file=" + escape($('#imgFile1').val()) + "&caption=" + escape(caption),
			success: function(result)
			{
				if (result == 1)
					galleryGetImages();
				else
					alert(result);
			}
		});
}

function galleryImgDelete(image)
{
	$.ajax({
			type: "POST",
			url: "http://"+window.location.hostname+"/ajax/galleryImgDelete/module:gallery/",
			dataType: "html",
			data: "img="+image,
			success: function(result) {
				galleryGetImages();
			}
		});
}