Element.addMethods({ 
	fire: Element.fire.wrap( function(proceed,element) {
		args = $A(arguments);
		// alert("We are here - " + $(element).identify() + " event: " + args[2])
// var eventName = args[2];
// if (eventName.match(/lightview:hidden/)) {
// alert("We are here - " + $(element).identify() + " event: " + args[2])
// }

	        args.shift();
		e = $(element);
		// alert("Found element");
		if(e && !e.parentNode && e!=document ) {			// && eventName.match(/^lightview:/) ) {
			// alert("Going to monkey with element");
			var fragment = document.createDocumentFragment();
			fragment.appendChild(e);
		}
		args[0] = e;
		return proceed.apply(this, args);
	} )
});

function fill_content_type(elem) {
  new_content_type = "application/octet-stream"; //the default
  if (!elem.value) {return(true)}

  //allowed extensions
  var ext = Array;
  ext['pdf'] = "application/pdf";
  
  //word
  ext['doc'] = "application/msword";
  
  //excel
  ext['xla'] = "application/vnd.ms-excel";
  ext['xlc'] = "application/vnd.ms-excel";
  ext['xlm'] = "application/vnd.ms-excel";
  ext['xls'] = "application/vnd.ms-excel";
  ext['xlt'] = "application/vnd.ms-excel";
  ext['xlw'] = "application/vnd.ms-excel";
  
  found = elem.value.match(/\.([^.]*)$/);
  if (found && found[1]) {
    new_content_type = ext[found[1].toLowerCase()] || new_content_type;
  }
  
  //set the hidden field
  $('content-type').value = new_content_type;
}

// Event handler fired when user clicks to create page with editor
function show_newsletter_editor(event) {
	$$('.nl_mce_spacing').invoke('hide');
	init_page_editor();		// Using same MCE config as pages
	document.stopObserving('lightview:opened', show_newsletter_editor);
}

function hide_newsletter_editor(event) {
	tinyMCE.execCommand('mceRemoveControl', false, 'newsletter_html_content');
	$$('.nl_mce_spacing').invoke('show');
}

function show_page_editor(event) {
	init_page_editor();
	$$('.nl_mce_spacing').invoke('hide');
	document.stopObserving('lightview:opened', show_page_editor);
	// Going to want to hide editor BEFORE lightview closes, so lightview:hidden no workee for that.
	// $$('.lv_Button.lv_Close').invoke('observe','click',hide_page_editor);
	// new theory: destroy all of those divs when lightview hides 'em, which is handled by lightview:hidden in _combined
}

function hide_page_editor(event) {
	tinyMCE.execCommand('mceRemoveControl', false, 'page_html_content');
	$$('.nl_mce_spacing').invoke('show');
}

function show_info_editor(event) {
	$$('.nl_mce_spacing').invoke('hide');
	init_intro_editor();
	document.stopObserving('lightview:opened', show_info_editor);
	$$('.lv_Button.lv_Close').invoke('observe','click',hide_info_editor);
}

function hide_info_editor(event) {
	tinyMCE.execCommand('mceRemoveControl', false, 'info_html_content');
	$$('.nl_mce_spacing').invoke('show');
}

function show_introduction_editor(event) {
	$$('.nl_mce_spacing').invoke('hide');
	init_introduction_editor();
	document.stopObserving('lightview:opened', show_introduction_editor);
	//$$('.lv_Button.lv_Close').invoke('observe','click',hide_page_editor);
}
