$.fn.tinymce = function(options) {
    return this.each(function() {
        preString = "<";
	preString += "div class='jqHTML_frame' style='width:"+$(this).css("width")+"px;height:" + ($(this).css("height")+20) + "px;'>";
        postString = "<";
	postString += "/div>";
        $(this).wrap(preString + postString);
        tinyMCE.addMCEControl(document.getElementById(this.id), this.id);
    });
}

function initMCE()
{
	tinyMCE.init({ mode : "none",
			elements : "ajaxfilemanager",
			file_browser_callback : "ajaxfilemanager",
			theme : "advanced",
			plugins : "advlink,visualchars,table,style,save,preview,contextmenu,autosave,advimage,paste,xhtmlxtras",
			theme_advanced_buttons1 : "save,separator,cut,copy,pastetext,pasteword,selectall,separator,bold,italic,underline,styleprops,separator,justifyleft,justifycenter,justifyright,justifyfull,bullist,numlist,undo,redo",
			theme_advanced_buttons2 : "tablecontrols,separator,link,unlink,image,hr,charmap",
			theme_advanced_buttons3 : "formatselect,separator,visualaid,visualchars,removeformat,cleanup,help,separator,code,preview",
			theme_advanced_toolbar_location : "top",
			theme_advanced_toolbar_align : "center",
			theme_advanced_path_location : "bottom",
			//extended_valid_elements : "a[name|href|target|title|onclick],div[class|id],img[class|src|border=0|alt|title|hspace|vspace|width|height|align|onmouseover|onmouseout|name],hr[class|width|size|noshade],font[face|size|color|style],span[class|align|style]",
			extended_valid_elements : "b/strong,i/em,h1,h2,h3,a[name|href|target|title|onclick|class|style],img[style|class|src|border=0|alt=|title|hspace|vspace|width|height|align|onmouseover|onmouseout|name],hr[class|width|size|noshade],font[face|size|color|style],span[class|align|style],div[class|align|style]",
			content_css : "/css/aabm.css",
			save_enabledwhendirty : true,
			plugin_preview_width :  800,
			plugin_preview_height :  600,
			plugin_preview_pageurl :  "/templates/default.html",
			remove_script_host :  false,
			remove_linebreaks : false,
			apply_source_formatting : true,
			relative_urls : false
	});
}
initMCE();

function ajaxfilemanager(field_name, url, type, win)
{
	var ajaxfilemanagerurl = "/js/tiny_mce/plugins/ajaxfilemanager/ajaxfilemanager.php";

	switch (type) {
		case "image":
			ajaxfilemanagerurl += "?type=img";
			break;
		case "media":
			ajaxfilemanagerurl += "?type=media";
			break;
		case "flash": //for older versions of tinymce
			ajaxfilemanagerurl += "?type=media";
			break;
		case "file":
			ajaxfilemanagerurl += "?type=files";
			break;
		default:
			return false;
	}

	var fileBrowserWindow = new Array();
	fileBrowserWindow["file"] = ajaxfilemanagerurl;
	fileBrowserWindow["title"] = "File Manager";
	fileBrowserWindow["width"] = "782";
	fileBrowserWindow["height"] = "440";
	fileBrowserWindow["close_previous"] = "no";
	tinyMCE.openWindow(fileBrowserWindow, {
	  window : win,
	  input : field_name,
	  resizable : "yes",
	  inline : "yes",
	  editor_id : tinyMCE.getWindowArg("editor_id")
	});
	
	return false;
}

$(document).ready(function(){
	$('.searchdiv').minmax();
	$('.main').minmax();
	$('#ContentContent').tinymce();
	$('#FacilityDescription').tinymce();	
	$('.tele').mask("(999) 999-9999");
	$('#FacilitySame').change(function(){
		$(".FacilityContact").toggle();
	});

	$("#remove_user").click(function(){
		$("option:selected", "#CommitteeMembers").each(function(){
			$(this).appendTo("#CommitteeAvailable");
		});
	});

	$("#add_user").click(function(){
		$("option:selected", "#CommitteeAvailable").each(function(){
			$(this).appendTo("#CommitteeMembers");
		});
	});

	$("#committees").submit(function(){
		$("option", "#CommitteeMembers").each(function(){
			$(this).attr("selected", "selected");
		});
	});
});

function showNew(thing1, thing2){
	var thisSelect = document.getElementById(thing1);
	if(thisSelect.value == "new"){
		document.getElementById(thing2).style.display = 'block';	
	} else {
		document.getElementById(thing2).style.display = 'none';
	}
}

