/**
 * JavaScript to integrate document browsing and selection with the editor.
 *
 * THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 *
 * @package clearFusionCMS
 * @copyright Copyright (c) 2014 fusionLib. All rights reserved.
 * @link http://clearfusioncms.com
 */

$fl(document).ready(function() {
	$fl('.treeviewwidget a').bind('click', function(evt) {
		var tgt = evt.target.tagName != 'A' ? $fl(evt.target).parent().attr('href').replace(/^#/, '') : $fl(evt.target).attr('href').replace(/^#/, '');

		// Set the target
		var inp = parent.top.$('#active-input').val();
		parent.top.$('#' + inp).val(tgt.replace('&#91;&#91;', '[[').replace('&#93;&#93;', ']]'));

		// Close dialog
		parent.top.$("#md-fileselect").data('simplemodal').hide();

		evt.preventDefault();
		evt.stopPropagation();
		return false;
	});
});