/*
 * This plugin automates the replacement of html elements with form
 * elements for dynamic editing of content in-situe.
 *
 * To use this properly, html should be as follows:
 *
 * <div id="editblog" value="<?php echo $this->blogId ?>" saveText="Save Changes" prcessingText="please wait...">
 *   <span class="someclass">The label text: </span><div class="someclass basictextarea" selector=".someclass" name="Body">The body text</div>
 * </div>
 * <a href="#" onclick="$('#editblog').editable(this, '/api/js/blog/update.php')">Edit</a>
 *
 * The 'selector' attribute shoudl contain a standard jquery selector
 * string to match all the elements directly relating to the editable
 * item. This allows for the description to be shown and hidden during
 * and after the editig process if, for example, the description is
 * blank and so not to be displayed.
 *
 * The class 'basictextarea' is what controls what form element should
 * be used to edit the html element.
 *
 * Valid element classes include:
 *  - basictextarea: Text area with some basic text format capabilities
 *  - plaintextarea: Normal text area
 *  - plaintext: Normal text input box
 *  - tagsbox: Normal text area, but filters out all the link html on
 *      activation and reformats the tag links on save.
 *
 * The 'tagsbox' element type uses one addtional attribute called
 * 'tagurl'. This attribute should be set to the url to be sed when
 * reconstructing the tag links, as follows:
 *
 * <dt class="photoTags jhide">Photo tags:</dt>
 * <dd class="photoTags jhide tagsbox" selector=".photoTags" name="phototags" tagurl="/gallery/photo/view/id/"></dd>
 *
 */

jQuery.fn.extend({
    editable: function(oActivator, sUrl) {
    	if ((this.attr('editing') != -1) && (this.attr('editing') != 1)) {
			this.attr('editing', 1);
			$(oActivator).attr('oldvalue', $(oActivator).html());
			$(oActivator).html(this.attr('saveText'));
			$('a.plaintext', this).each(function() {
					$(this).after('<div onclick="'+$(this).attr('onclick')+'" name="'+$(this).attr('name')+'" class="'+$(this).attr('class')+' editedLinkPlaintext" href="'+$(this).attr('href')+'">'+$(this).html()+'</div>');
					$(this).remove();
			});
			$('.plaintext', this).each(function() {
					$(this).attr('oldvalue', $(this).html());
					$(this).html('<input type="text" class="inline" name="'+$(this).attr('name')+'" value="'+$(this).html()+'" />');
					$($(this).attr('selector')).slideDown();
			});
			$('.plaintextarea', this).each(function() {
					tempHeight = $(this).height();
					tempWidth = $(this).width();
					$(this).attr('oldvalue', $(this).html());
					$(this).html('<textarea class="inline" name="'+$(this).attr('name')+'">'+$(this).html()+'</textarea>');
					$('textarea', this).height(tempHeight);
					$('textarea', this).width(tempWidth);
					$($(this).attr('selector')).slideDown();
			});
			$('.basictextarea', this).each(function() {
					tempHeight = $(this).height();
					tempWidth = $(this).width();
					if (tempHeight > 80) {
						tempHeight = tempHeight-5;
					} else {
						tempHeight = 80;
					}
					if (tempWidth > 30) {
						tempWidth = tempWidth-5;
					} else {
						tempWidth = 'auto';
					}
					$(this).attr('oldvalue', $(this).html());
					$(this).html('<textarea style="width: '+tempWidth+'; height: '+tempHeight+';" class="inline" name="'+$(this).attr('name')+'" id="'+$(this).attr('name')+'">'+$(this).html()+'</textarea>');
					$($(this).attr('selector')).slideDown();
					$('#'+$(this).attr('name')).width(tempWidth);
					try {
						$('#'+$(this).attr('name')).wysiwyg(wysiwygConfig);
					} catch (e) {
					}
			});
			$('.tagsbox', this).each(function() {
					$(this).attr('oldvalue', $(this).html());
					$(this).html('<textarea class="inline" name="'+$(this).attr('name')+'">'+$(this).html().replace(/<\/?[^>]+(>|$)/g, "")+'</textarea>');
					$($(this).attr('selector')).slideDown();
			});
			$('.hiddenradio', this).each(function() {
					$(this).attr('oldvalue', $(this).html());
					$(this).slideDown();
			});
			$('.selectarea', this).each(function() {
					$(this).attr('oldvalue', $(this).html());
					$(this).slideDown();
			});
		} else if (this.attr('editing') == 1) {
			this.attr('editing', -1);
			$(oActivator).html(this.attr('prcessingText'));
			sRpcPost = '';
			$('div.editedLinkPlaintext', this).each(function() {
					$(this).after('<a oldvalue="'+$(this).attr('oldvalue')+'" onclick="'+$(this).attr('onclick')+'" class="'+$(this).attr('class')+'" href="'+$(this).attr('href')+'"><input class="inline" type="text" value="'+$('input', this).val()+'" name="'+$('input', this).attr('name')+'"/></a>');
					$(this).remove();
			});
			$('.editedLinkPlaintext', this).removeClass('editedLinkPlaintext');
			$('.plaintext', this).each(function() {
					eval('vTemp'+$('input', this).attr('name')+"=$('input', this).val()");
					sRpcPost += $('input', this).attr('name')+': vTemp'+$('input', this).attr('name')+', ';
			});
			$('.plaintextarea', this).each(function() {
					eval('vTemp'+$('textarea', this).attr('name')+"=$('textarea', this).val()");
					sRpcPost += $('textarea', this).attr('name')+': vTemp'+$('textarea', this).attr('name')+', ';
			});
			$('.basictextarea', this).each(function() {
					eval('vTemp'+$('textarea', this).attr('name')+"=$('#"+$('textarea', this).attr('name')+"').val()");
					sRpcPost += $('textarea', this).attr('name')+': vTemp'+$('textarea', this).attr('name')+', ';
			});
			$('.tagsbox', this).each(function() {
					eval('vTemp'+$('textarea', this).attr('name')+"=$('textarea', this).val()");
					sRpcPost += $('textarea', this).attr('name')+': vTemp'+$('textarea', this).attr('name')+', ';
			});
			$('.hiddenradio', this).each(function() {
					eval('vTemp'+$(this).attr('name')+"=$(\"input[@name='"+$(this).attr('name')+"']\", this).fieldValue()");
					sRpcPost += $(this).attr('name')+': vTemp'+$(this).attr('name')+', ';
			});
			$('.selectarea', this).each(function() {
					sSelectedIds = '';
					$('.selected', $(this)).each(function() {
							sSelectedIds += $(this).attr('selectedid')+', ';
						});
					if (sSelectedIds != '') {
						sSelectedIds = sSelectedIds.substr(0, sSelectedIds.length - 2);
					}
					eval('vTemp'+$(this).attr('name')+"='"+sSelectedIds+"'");
					sRpcPost += $(this).attr('name')+': vTemp'+$(this).attr('name')+', ';
			});
			$(oActivator).attr('id', 'link'+$(this).attr('editid'));
			eval('$.post(sUrl, { '+sRpcPost+'id: "'+$(this).attr('editid')+'" }, function(aData) { eval("aData = "+aData); procEdRes("'+$(oActivator).attr('id')+'", aData, "'+$(this).attr('id')+'") });');
		}
    }
});

function procEdRes(sActivator, aData, sTarget) {
	if (aData['errors'] == '') {
		$('#'+sTarget+' .plaintext').each(function() {

				// Hide editables that are now/still empty
				if ($('input', this).val() == '') {
					$($(this).attr('selector')).slideUp();
				}
				$(this).html($('input', this).val());
		});
		$('#'+sTarget+' .plaintextarea').each(function() {

				// Hide editables that are now/still empty
				if ($('textarea', this).val() == '') {
					$($(this).attr('selector')).slideUp();
				}
				$(this).html($('textarea', this).val());
		});
		$('#'+sTarget+' .basictextarea').each(function() {

				// Hide editables that are now/still empty
				if ($('textarea', this).val() == '') {
					$($(this).attr('selector')).slideUp();
				}
				$(this).html($('textarea', this).val());
		});
		$('#'+sTarget+' .tagsbox').each(function() {

				// Hide editables that are now/still empty
				if ($('textarea', this).val() == '') {
					$($(this).attr('selector')).slideUp();
				}
				sTags = '';
				aTags = $('textarea', this).val().split(', ');
				for (iTagId in aTags) {
					sTags += '<a href="/gallery/search/tag/'+aTags[iTagId]+'">'+aTags[iTagId]+'</a>, ';
				}
				$(this).html(sTags.substr(0, sTags.length - 2));
		});
		$('#'+sTarget+' .hiddenradio').each(function() {
				$(this).slideUp();
		});
		$('#'+sTarget+' .selectarea').each(function() {
				$(this).slideUp();
		});
		$('#'+sActivator).html($('#'+sActivator).attr('oldvalue'));
	} else {
		$('#'+sTarget+' .plaintext').each(function() {

				// Hide editables that are now/still empty
				if ($(this).attr('oldvalue') == '') {
					$($(this).attr('selector')).slideUp();
				}
				$(this).html($(this).attr('oldvalue'));
		});
		$('#'+sTarget+' .plaintextarea').each(function() {

				// Hide editables that are now/still empty
				if ($(this).attr('oldvalue') == '') {
					$($(this).attr('selector')).slideUp();
				}
				$(this).html($(this).attr('oldvalue'));
		});
		$('#'+sTarget+' .basictextarea').each(function() {

				// Hide editables that are now/still empty
				if ($(this).attr('oldvalue') == '') {
					$($(this).attr('selector')).slideUp();
				}
				$(this).html($(this).attr('oldvalue'));
		});
		$('#'+sTarget+' .tagsbox').each(function() {

				// Hide editables that are now/still empty
				if ($(this).attr('oldvalue') == '') {
					$($(this).attr('selector')).slideUp();
				}
				$(this).html($(this).attr('oldvalue'));
		});
		$('#'+sTarget+' .hiddenradio').each(function() {
				$(this).slideUp();
		});
		$('#'+sTarget+' .selectarea').each(function() {
				$(this).slideUp();
		});
		$('#'+sActivator).html($('#'+sActivator).attr('oldvalue'));
	}
	$('#'+sTarget).attr('editing', 0);
}
