// $Id: photos.js,v 1.3 2008/08/09 09:06:14 eastcn Exp $
Drupal.behaviors.photos = function (context) {
	$('a.click-des').click( function() {
		$('#edit-des').val($('.desbody').text());
		$('#imgedit').show(300);
		$('.editdes').hide(300);
	});
	$('#edit-editnone').bind('click', function(){ 
		$('.editdes').show(300);
		$('#imgedit').hide(300);
		return false;
	});
	$('#imagesedit input[@type="checkbox"]').click( function(){
		$(this).parents('tr:first')[ this.checked ? 'addClass' : 'removeClass' ]('selected');
	});
	$('.allck').click(function() {
		$("input[@class='form-checkbox']").each(function() {
			$(this).attr("checked", true);
			$(this).parents('tr:first')[ this.checked ? 'addClass' : 'removeClass' ]('selected');
		})
	});
	$('.allnone').click(function() {
		$("input[@class='form-checkbox']").each(function() {
			$(this).attr("checked", false);
			$(this).parents('tr:first')[ this.checked ? 'addClass' : 'removeClass' ]('selected');
		})
	});
//
	$('a.clickimage').click(function() {
		$("input[@class='photos-p'],input[@class='photos-pp']").each(function() {
			if($(this).attr("checked")) {
				t = $(this).val().split('&&&');
				img = '<a class="image-quote" href="/photos/image/' + t[2] + '/album">' + '<img alt="' + t[1] + '" src="' + t[0] + '"></a>';
				$('.photo-msg').show();
				t = getSelectedText('edit-body',img);
				$(this).parents('.photos-quote').hide(300,function(){
					$(this).remove();
				}); 
				$('.photo-msg').append(Drupal.t('Picture has been inserted.'));
			}
			
		})
		return false;
	})
	function getSelectedText(field,img){
		var selectedText;
		var textField = window.parent.document.getElementById(field);
		textField.focus();
		if ('undefined' != typeof(textField.selectionStart)) {
			//img = '' + textField.value.substring(textField.selectionStart, textField.selectionEnd) + '';
			textField.value = textField.value.substring(0, textField.selectionStart) + img + textField.value.substring(textField.selectionEnd, textField.value.length);
		}
		else if (document.selection) {
			//img = '' + document.selection.createRange().text + '';
			document.selection.createRange().text = img;
		}
		return false;
	}
};