// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

function quoteText(text) {
	$('post_body').focus();
	$('post_body').value = "[quote]" + text + "[/quote]\r\n";
}

var ModalWin = {
	attach: function(div) {
		var modalWin;

    var id = $(div).id;
    if (modalWin == $(id)) return modalWin;

    modalWin = $(div);
				
    Object.extend(modalWin, ModalWin.Commands);

		// trap key and mouse events so that they don't change focus from editor
    // this.keyPressListener = modalWin.watchKeyPress.bindAsEventListener(this);
		//this.watchClickEvents();
				
		//Event.observe($$('.modal_win .buttonz').first(), 'click', function(e){
			//Event.stop(e);
			//modalWin.close()
		//});
		
		//Event.observe($$('.modal_win input').first(), 'click', function(e){
			//Event.stop(e);
			//modalWin.close()
		//});

		modalWin.select("a.close").invoke('observe','click', function(e){
			Event.stop(e);
			modalWin.close();
		});

    return modalWin;
	}
}

var $E = ModalWin.Commands = (function() {
	function reveal() {
		$('overlay').show();
		this.show();
		var first_input = this.select("input:not([type~=hidden]), textarea")[0];
		if (first_input)	
			first_input.focus();
			if (first_input.value != "")
			 first_input.select();
	}
	
	function close() {
		this.hide();
		this.select("input, textarea").invoke('clear');
		$('overlay').hide();
	}
	
	//function watchKeyPress(e) {
    // Close if escape is pressed or if there's a click outside of the ModalWin
    //if (e.keyCode == 27 || !Event.element(e).descendantOf(this)) this.close();
  //}
	
  return {
     reveal: reveal,
     close: close,
     commands: $H({})
  };
})();