//************************************************/
/* IPB3.2.x Javascript							*/
/* -------------------------------------------- */
/* quickquote.js - Quick Quote Button code		*/
/* (c) Extreame Inc 2011						*/
/* -------------------------------------------- */
/* Author: Anonymous    						*/
/************************************************/
/* Please read the attached text file to add the required edits to your skin templates.
/* ===================================================================================================== */
/* IPB3.2.x Quick Quote Button */

var _quickquote = window.IPBoard;

_quickquote.prototype.quickquote = {
	inSection: '',
	fastReplyId: '',
	
	init: function()
	{

		document.observe("dom:loaded", function(){
			if( ipb.topic.inSection == 'topicview' )
			{
				ipb.delegate.register('._trigger_quick_quote', ipb.quickquote.ajaxQuote);
			}

		});
		

	},
	
	/* ------------------------------ */
	/**
	 * Retrieve quote via AJAX and insert into editor
	*/
	ajaxQuote: function(e, elem)
	{
		if ( DISABLE_AJAX )
		{
			return false;
		}
		
		// If user is holding ctrl or command, just submit since they
		if ( e.ctrlKey == true || e.metaKey == true || e.keyCode == 91 )
		{
			return false;
		}
		
		Event.stop(e);
		
		pid = elem.readAttribute('pid');
		
		var url = ipb.vars['base_url'] + 'app=forums&module=ajax&section=topics&do=quote&t=' + ipb.topic.topic_id + '&p=' + pid + '&md5check=' + ipb.vars['secure_hash'];
		Debug.write( url );
		
		new Ajax.Request(	url,
							{
								method: 'get',
								evalJSON: 'force',
								encoding: ipb.vars['charset'],
								onSuccess: function(t)
								{ 
									if (  t.responseJSON && t.responseJSON['error'] )
									{
										ipb.global.errorDialogue( t.responseJSON['error'] );
										
										return false;
									}
									else if ( t.responseText && t.responseText != 'nopermission' )
									{
										editor	= ipb.textEditor.getEditor( ipb.topic.fastReplyId );
										var text = getSelection();
										
										var returnText = t.responseText;
										
										var splitText = returnText.split("']");

										var allText = splitText[0] + "']" + text + "[/quote]\n\n";
										
										editor.insert( allText, true );
									}
								}
							}
						);
	},

};

function getSelection() {
	return (!!document.getSelection) ? document.getSelection() :
	       (!!window.getSelection)   ? window.getSelection() :
	       document.selection.createRange().text;
}

ipb.quickquote.init();
