// ----------------------------------------------------------------------------
// markItUp!
// ----------------------------------------------------------------------------
// Copyright (C) 2008 Jay Salvat
// http://markitup.jaysalvat.com/
// ----------------------------------------------------------------------------
// Html tags
// http://en.wikipedia.org/wiki/html
// ----------------------------------------------------------------------------
// Basic set. Feel free to add more tags
// ----------------------------------------------------------------------------
var langue = Array();
var data = $.parseJSON($.ajax({
    type: "POST",
    url: "/getlang.php",
    data: "page=markitup&pref=mark",
    dataType: "json",
    async: false,
    success: function(){ return true }
}).responseText);
for(i=0;i<data.length;i++) {
    langue[data[i].nom] = data[i].texte;
}
mySettings = {	
    onShiftEnter:  	{keepDefault:false, replaceWith:'<br />\n'},
    onCtrlEnter:  	{keepDefault:false, openWith:'\n[*] ', closeWith:''},
    onTab:    		{keepDefault:false, replaceWith:'    '},
    previewParserPath:      '/markitup/markitup.preview.php',
    previewParserVar:	'text_parsing',
    markupSet:  [ 	
	    {name:langue['bold'], key:'B', openWith:'[b]', closeWith:'[/b]' },
		{name:langue['italic'], key:'I', openWith:'[i]', closeWith:'[/i]'  },
		{name:langue['underline'], key:'U', openWith:'[u]', closeWith:'[/u]'  },
//		{name:'Stroke through', key:'S', openWith:'<del>', closeWith:'</del>' },
		{separator:'---------------' },
		{name:langue['center'], key:'C', openWith:'[c]', closeWith:'[/c]' },
		{name:langue['justify'], key:'J', openWith:'[j]', closeWith:'[/j]'  },
		{name:langue['right'], key:'R', openWith:'[r]', closeWith:'[/r]'  },
	        {name:langue['nouvelle_ligne'], key:'N', openWith:'[n]' },
		{separator:'---------------' },
//		{name:'Picture', key:'P', replaceWith:'[img][![Url]!][/img]' },
		{name:langue['lien'], key:'L', openWith:'[url=[!['+langue['url']+']!]]', closeWith:'[/url]', placeHolder:langue['url_desc'] },
		{name:langue['mail'], key:'M', openWith:'[mail=', closeWith:']', placeHolder:'' },
		{
	        name:langue['upload'],
		key:'S',
		beforeInsert: function(markItUp) { InlineUpload.display(markItUp) },
		},
		{separator:'---------------' },
	{name:langue['liste_puce'], openWith:'[list]\n[*] ', closeWith:'\n[/list]', replaceWith:function(markitup) { return markitup.selection.replace(/\n(\n*)/g,'$1\n[*] ')}},
	{name:langue['liste_numero'], openWith:'[list=[!['+langue['debut']+']!]]\n[*] ', closeWith:'\n[/list]'},
		{name:langue['liste_item'], openWith:'[*] '},
                {separator:'---------------' },
		{name:langue['titre']+' 1', key:'1', openWith:'[h1]', closeWith:'[/h1]', placeHolder:langue['titre_ici'] },
		{name:langue['titre']+' 2', key:'2', openWith:'[h2]', closeWith:'[/h2]', placeHolder:langue['titre_ici'] },
		{name:langue['titre']+' 3', key:'3', openWith:'[h3]', closeWith:'[/h3]', placeHolder:langue['titre_ici'] },
		{name:langue['titre']+' 4', key:'4', openWith:'[h4]', closeWith:'[/h4]', placeHolder:langue['titre_ici'] },
		{name:langue['titre']+' 5', key:'5', openWith:'[h5]', closeWith:'[/h5]', placeHolder:langue['titre_ici'] },
		{name:langue['titre']+' 6', key:'6', openWith:'[h6]', closeWith:'[/h6]', placeHolder:langue['titre_ici'] },
                {separator:'---------------' },
//		{name:'Clean', className:'clean', replaceWith:function(markitup) { return markitup.selection.replace(/<(.*?)>/g, "") } },		
	{name:langue['preview'], className:'preview',  call:'preview'}
	]
}


/*
* Inline uploading of images for the Jquery MarkItUp editor.
* @see http://markitup.jaysalvat.com/
*
* The purpose of this is to avoid having to have already uploaded an image (and
* to know the URL) when adding an img tag to edited content. This widget handles
* the upload itself.
*
* The "Picture" button will reveal a dialog from which an image to be uploaded
* is chosen. The upload's form target is a hidden iframe also within the dialog.
* Once the iframe has loaded, the JSON response is parsed and the img tag is built
* and then added to the editor selection.
*
* The server-side script should respond with a JSON object wrapped inside a textarea tag.
* @see http://jquery.malsup.com/form/#file-upload
*
* The JSON object should include the following:
* on success:
* - status: string, 'success'
* - src: string, the complete URL to the image
* - width: string, the image width in pixels
* - height: string, the image height in pixels
*
* on error:
* - msg: string, whatever error msg you want to display
*
* All of the upload form inputs are submitted, so the server-side script might also make use of
* the class or id assigned to the image. For example, if the image information is to be saved to
* a database. In that case, it might also be desirable to have this script create hidden fields
* with further data, like a content ID, so as to better identify the image.
*
* Keep in mind that one might have both this widget and the classic Picture buttons in the same toolbar.
*
* This particular example REQUIRES the jQuery.loading and jQuery.json plugins.
* @see http://code.google.com/p/jquery-loading-plugin/
* @see http://code.google.com/p/jquery-json/
*
* @author brian ally, brian ~at~ zijn-digital ~dot~ com
* @copyright 2010, brian ally
* @version 0.4
* @license do what you want with it
*/
var InlineUpload = {
	dialog: null,
	block: '',
	offset: {},
	options: {
		container_class: 'markItUpInlineUpload',
		form_id: 'inline_upload_form',
		action: '/markitup/markitup.upload.php',
		inputs: {
			title: { label: langue['titre_fichier'], id: 'inline_upload_title', name: 'inline_upload_title' },
			file: { label: langue['fichier'], id: 'inline_upload_file', name: 'inline_upload_file' }
			},
		submit: { id: 'inline_upload_submit', value: langue['upload'] },
		close: 'inline_upload_close',
		iframe: 'inline_upload_iframe'
		},
	display: function(hash) {
		var self = this;
/* Find position of toolbar. The dialog will inserted into the DOM elsewhere
* but has position: absolute. This is to avoid nesting the upload form inside
* the original. The dialog's offset from the toolbar position is adjusted in
* the stylesheet with the margin rule.
*/
		this.offset = $(hash.textarea).prev('.markItUpHeader').offset();

/* We want to build this fresh each time to avoid ID conflicts in case of
* multiple editors. This also means the form elements don't need to be
* cleared out.
*/
		this.cleanUp = function() {
			self.dialog.fadeOut().remove();
			}
		this.dialog = $([
			'<div class="',
			this.options.container_class,
			'"><div><form id="',
			this.options.form_id,
			'" action="',
			this.options.action,
			'" target="',
			this.options.iframe,
			'" method="post" enctype="multipart/form-data"><label for="',
			this.options.inputs.title.id,
			'">',
			this.options.inputs.title.label,
			'</label><input name="',
			this.options.inputs.title.name,
			'" id="',
			this.options.inputs.title.id,
			'" type="text" /><label for="',
			this.options.inputs.file.id,
			'">',
			this.options.inputs.file.label,
			'</label><input name="',
			this.options.inputs.file.name,
			'" id="',
			this.options.inputs.file.id,
			'" type="file" /><br /><input id="',
			this.options.submit.id,
			'" type="button" value="',
			this.options.submit.value,
			'" /></form><div id="',
			this.options.close,
			'"></div><iframe id="',
			this.options.iframe,
			'" name="',
			this.options.iframe,
			'" src="about:blank"></iframe></div></div>',
			].join(''))
			.appendTo(document.body)
			.hide()
			.css('top', this.offset.top)
			.css('left', this.offset.left);


/* init submit button */
		$('#'+this.options.submit.id).click(function() {
			var form_data = {
				dataType: "json",
				success: function(response, statusText, xhr, form) {
					if(response.status== 'success') {
						if(response.balise == 'url') {
							this.block = [
								'[url=',
								response.src,
								']',
								$('#'+self.options.inputs.title.id).val(),
								'[/url]'
								];
							}
						else {
							this.block = [
								'[img]',
								response.src,
								'[/img]'
								];
							}

/* add the img tag */
						$.markItUp({ replaceWith: this.block.join('') } );
						self.cleanUp();
						}
					else {
						alert(response.msg);
						self.cleanUp();
						}
					}
				};
			$('#'+self.options.form_id).ajaxSubmit(form_data); 
//			console.log($('#'+self.options.form_id).submit());//.loading();
			});


/* init cancel button */
		$('#'+this.options.close).click(self.cleanUp);

/* form response will be sent to the iframe */
		$('#'+this.options.iframe).bind('load', function() {

//			var response = $.secureEvalJSON($(this).contents().find('textarea').text());
// 			if (response.status == 'success') {

			});

/* Finally, display the dialog */
		this.dialog.fadeIn('slow');
		},
	cleanUp: null
	};
