$j(document).ready(function() {

	var currentSelected;

	$j('.colorpicker').each(function() {
		
		var picker = $j(this);
		
		picker.find('div[title]').each(function() {
	
			$j(this).css('background', '#'+$j(this).attr('title'));
			
			$j(this).click(function() {
				picker.siblings('.colorpicker-selected').first().css('background-color', '#'+$j(this).attr('title'));
				picker.siblings('.colorpicker-value').first().val($j(this).attr('title'));
				picker.siblings('.colorpicker-value').first().change();
			});
			
		});
		
		picker.siblings('.colorpicker-selected').first().css('background-color', '#'+picker.siblings('.colorpicker-value').first().val());
	});
	
	if($j('#documents-table tbody').sortable) {
		$j('#documents-table tbody').sortable({ axis: 'y', cursor: 'move', cancel: 'a, input', opacity: 0.8 });
	}
	
	$j('#documents-table tbody').bind('sortupdate', function(event, ui) {
		$j.ajax({ type: "get", url: "sort4.php", data: $j(this).sortable('serialize')});
		
		var i = 1;
		$j('#documents-table tbody input.sort').each(function() {
			$j(this).val(i);
			i++;
		})
	});
	
	$j('#documents-deleteall').click(function(e) {
		if (!confirm("Are you sure you want to delete all documents in this gallery?")) {
			e.preventDefault()
		}
	});
	
	$j('#downloadall').click(function() {
		console.debug('<?=$editid?>');
		window.location = 'downloadall.php?id=<?=$editid?>';
		
	});
	
	if ($j("#notenoughcredits").dialog) {
		$j("#notenoughcredits").dialog({
			resizable: false,
			modal: true,
			autoOpen: false,
			buttons: {
				Ok: function() {
					$j(this).dialog('close');
				}
			}
		});
		
		$j("#draftsaved").dialog({
			resizable: false,
			movable: false,
			modal: true,
			autoOpen: false,
			buttons: {
				Ok: function() {
					$j(this).dialog('close');
					window.location.reload();
				}
			}
		});
		
		$j("#confirmcreditsspend").dialog({
			resizable: false,
			modal: true,
			autoOpen: false,
			buttons: {
				Ok: function() {
					window.location = $j('#publishlink').attr('href');
					$j(this).dialog('close');
				},
				Cancel: function() {
					$j(this).dialog('close');
				}
			}
		});
	}
});

function showDialog(id) {
	$j('#'+id).dialog('open');	
}