/**
 * News module related javascript.
 *
 */

jQuery(function() {

	jQuery("form.editor").each(function() {
		// Disable changing language when editing a news item.
		jQuery("select[name=lang]").attr("disabled", "disabled");

		jQuery(this).find(".date_field").each(function() {
			var date_field = jQuery(this);
			var date_format = date_field.attr("format");

			// Date selection field
			date_field.datepicker({dateFormat: date_format});
		});
		
		// UI used for inserting links into the summary editor at the end of the editor
		jQuery("#insert_link").click(function(event) {
			var link_title = jQuery("#link_title").val();
			var link_url = jQuery("#link_url").val();
			var link_text = '[' + link_title + '|' + link_url + "] ";
			var summary = jQuery("textarea[name='news[summary]']");
			summary.val(summary.val() + link_text);
			event.preventDefault();
		});
	});

	// Delete confirmation
	jQuery(".actions .delete").click(function(event) {
		if (!confirm("This item will be deleted without the ability to restore. Delete ?"))
			event.preventDefault();
	});

	// Open mail link in popup
	jQuery(".actions .popup").click(function(event) {
		var url = jQuery(this).attr("href");
		window.open(url, 'signaturenewsletter', 'width=950,height=580,location=no,menubar=no,toolbar=no,scrollbars=1').focus();
		event.preventDefault();
	});

});
