// vim: set ts=4 sts=4 sw=4 si noet:

jm.tinymce = function() {
	var fileBrowserCallback = function(fieldName, url, type, win) {
//		var id = $('#ContentSchoolId').val();
		var id = $('#JockeyHuntId').val();
		if (id) {
			var url = jm.cfg.base + '/attachments/browse/foreign_key:' + id +
				'/type:' + type;
		} else {
			var id = $('#ContentSchoolId').val();
			var url = jm.cfg.base + '/attachments/browse/school_id:' + id +
				'/type:' + type;
		}

		tinyMCE.activeEditor.windowManager.open({
				file : url,
				title : 'File Browser',
				width : 520,
				height : 400,
				resizable : "yes",
				inline : "yes",
				close_previous : "no",
				popup_css: false
			}, {
				window : win,
				input : fieldName
			});
		return false;
	}

	var selectAttachment = function(evt) {

		var win = tinyMCEPopup.getWindowArg("window");
		var input = tinyMCEPopup.getWindowArg("input");
		var res = tinyMCEPopup.getWindowArg("resizable");
		var inline = tinyMCEPopup.getWindowArg("inline");

		var url = this.attributes.surl.value;

		win.document.getElementById(tinyMCEPopup.getWindowArg("input")).value = url;

		// are we an image browser
		if (typeof(win.ImageDialog) != "undefined") {
			// we are, so update image dimensions...
			if (win.ImageDialog.getImageData)
				win.ImageDialog.getImageData();

			// ... and preview if necessary
			if (win.ImageDialog.showPreviewImage)
				win.ImageDialog.showPreviewImage(URL);
		}

		tinyMCEPopup.close();
	}

	var submitForm = function(evt) {
		tinyMCE.triggerSave();
		return true; // let browser to continue form submission
	}

	var init = function(el) {
		return this;
	}

	return {
		field: null,
		init: init,
		selectAttachment: selectAttachment,
		submitForm: submitForm,
		fileBrowserCallback: fileBrowserCallback
	}
}().init(this);


