(function() {

	// Vytvoříme nový plugin do editoru
	tinymce.create('tinymce.plugins.EledaPlugin', {

		/**
		 * Init
		 * 
		 * Inicializuje plugin.
		 *
		 * @param {tinymce.Editor} editor
		 * @param {string} url
		 */
		init: function(editor, url) {

			// Unikátní identifikátor
			var id = 0;
			var tagName = 'span';

			// V tuto chvíli ještě není editor plně inicializován, proto přidáme nový
			// event listener, který upraví hodnotu “id” po dokončení inicializaci editoru
			editor.onInit.add(function() {

				// Získáme obsah editoru
				var content = editor.getContent();


				// zjistime nejvyssi IDcko v elementech:
				// projdeme si vsechny elementy:
				var elements = content.match(/span id="[0-9]*"/gi);
				if (elements) 
				{
					for (var i = 0; i < elements.length; i++) {

						// marchneme si ID:
						if (id < elements[i].substr(12,(elements[i].substr(12) + '').indexOf('"')))
						{
							id = parseInt(elements[i].substr(12,(elements[i].substr(12) + '').indexOf('"')));
						}

					};
				}

				// pro dalsi ID elementu pricteme 1
				id = id+1;
			});


			/**
			 * Creator
			 *
			 * Funkce, která vytváří bubliny a gapfilly.
			 */
			var creator = function(type) {

				if (editor.selection.getContent({format:"text"}).length === 0)
				{
					window.alert('Please select text.');
					return;
				}

				// HTML šablona pro bubliny a gapfilly

				if (type == 1) {
					var template = '<'+tagName+' id="{{id}}" class="gap" title="{{description}}">{{selection}}</'+tagName+'>';
				} else {
					var template = '<'+tagName+' id="{{id}}" class="bubble" title="{{description}}">{{selection}}</'+tagName+'>';
				}

				// Data, kterými nahrazujeme obsah šablony
				var data = {};

				// Nastavíme typ
				data.type = type;

				// Nastavíme id
				data.id = id;
				id++;

				// Získáme označený text
				data.selection = editor.selection.getContent({
					format: 'text'
				});


				var appendSpace = false;

				if (data.selection.substring(data.selection.length - 1) == ' ')
				{
					data.selection = data.selection.substring(0, data.selection.length - 1);
					data.selection = $.trim(data.selection);
					appendSpace = true;
				}

				// Skript končí, pokud vybraný text už obsahuje nějaký <element>
				// nebo je jeho součástí (velmi volná kontrola)7
				if (editor.selection.getContent().indexOf("<"+tagName) != -1 || $(editor.selection.getNode()).is(tagName))
				{
					alert('Selected text already contains special element.');
					return false;
				}

				// Získáme popis bubliny (pro gapfil neplati)
				if (type == 1)
				{
					data.description = '';
				}
				else
				{
					data.description = prompt('Zadejte prosím popis');
				}



				// Nahradíme šablonu
				var arr = [
					'id',
					'type',
					'selection',
					'description'
				];

				for (i = 0; i < arr.length; i++)
				{
					template = template.replace(new RegExp('{{' + arr[i] + '}}'), data[arr[i]]);
				}


				if (appendSpace)
				{
					template += ' ';
				}

				// Vložíme obsah bubliny zpět do editoru
				editor.selection.setContent(template, {
					format: 'html'
				});
			};


			editor.addCommand('createGap', function() { creator(1); });
			editor.addCommand('createBubble', function() { creator(2); });

			// Vytvoříme nové tlačítko “createBubbleButton”
			editor.addButton('createBubbleButton', {
				title: 'Create a Bubble',
				cmd: 'createBubble',
				image: '../../img/tinymce/bubble.png'
			});

			// Vytvoříme nové tlačítko “createGapButton”
			editor.addButton('createGapButton', {
				title: 'Create a Gap',
				cmd: 'createGap',
				image: '../../img/tinymce/gap.png'
			});

		},

		/**
		 * GetInfo
		 *
		 * Vrátí informace o pluginu.
		 */
		getInfo: function() {
			return {
				longname: 'Eleda Plugin',
				author: 'Clevis s.r.o.',
				authorurl: 'http://www.clevis.cz/',
				version: '1.0.0'
			};
		}

	});

	  // Zaregistrujeme plugin
	  tinymce.PluginManager.add('eleda', tinymce.plugins.EledaPlugin);

})();


// kliknuti na radiobuttony zajisti skryti/zobrazeni funkce pro grapfill
$('input[type=radio]').live('click', function() {
	isCheckedRadioButtonGap();
});

// zjistuje, zda-li jde o gapfill modul, podle toho povoli funkci pro 
// vytvoreni gapfilu
function isCheckedRadioButtonGap()
{
	if($("#frmcontentForm-contentTypeId").val() == '5')
	{
		tinyMCE.activeEditor.controlManager.setDisabled('createGapButton', false);
	} 
	else
	{
		tinyMCE.activeEditor.controlManager.setDisabled('createGapButton', true);
	}
}

// ovladani tabu ve formulari pro tvorbu komponent
$('a#tabs').live('click', function() {
	var $original = $('a#tabs.active').removeClass('active');
	$(this).addClass('active');

	$('.tab-fragment').addClass('tab-hide');
	$('#' + $(this).attr('name')).removeClass('tab-hide');
});



