// -------------------------------------------------------------------
// markItUp!
// -------------------------------------------------------------------
// Copyright (C) 2008 Jay Salvat
// http://markitup.jaysalvat.com/
// -------------------------------------------------------------------
// MarkDown tags example
// http://en.wikipedia.org/wiki/Markdown
// http://daringfireball.net/projects/markdown/
// -------------------------------------------------------------------
// Feel free to add more tags
// -------------------------------------------------------------------
myMarkdownSettings = {
//   nameSpace:          'markdown', // Useful to prevent multi-instances CSS conflict
   previewParserPath:  '~/sets/markdown/preview.php',
	onShiftEnter: {keepDefault:false, openWith:'\n\n'},
	onTab:        {keepDefault:false, replaceWith: function(m) { return miu.multilineStartWith(m, "\t");} },
	markupSet: [
/*
		{name:'First Level Heading', key:'1', placeHolder:'Your title here...', closeWith:function(markItUp) { return miu.markdownTitle(markItUp, '=') } },
		{name:'Second Level Heading', key:'2', placeHolder:'Your title here...', closeWith:function(markItUp) { return miu.markdownTitle(markItUp, '-') } },
		{name:'Heading 3', key:'3', openWith:'### ', placeHolder:'Your title here...' },

		{name:'Titre de niveau 1', key:'1', openWith:'#### ', placeHolder:'Votre titre de niveau 1 ici' },
		{name:'Titre de niveau 2', key:'2', openWith:'##### ', placeHolder:'Votre titre de niveau 2 ici' },
*/
/*		{name:'Heading 6', key:'6', openWith:'###### ', placeHolder:'Your title here...' },*/
		{name:'Gras', key:'G', openWith:'**', closeWith:'**'},
		{name:'Italique', key:'I', openWith:'_', closeWith:'_'},
		{separator:'---------------' },		
		{name:'Liste non-ordonnée', placeHolder: 'Votre élément de liste non-ordonnée ici', replaceWith: function(m) { return miu.multilineStartWith(m, " - ");} },
		{name:'Liste ordonnée', placeHolder: 'Votre élément de liste ordonnée ici', replaceWith:function(markItUp) {
			var lines = (markItUp.selection||markItUp.placeHolder).split(/\n/);
			var md_lines = jQuery.map(lines, function(l, i) { return (i+1) + ". " + l; });
			return md_lines.join("\n");
		}},
		{separator:'---------------' },
/*		{name:'Picture', key:'P', replaceWith:'![[![Alternative text]!]]([![Url:!:http://]!] "[![Title]!]")'},*/
		{name:'Lien', key:'L', openWith:'[', closeWith:']([![L’adresse de votre lien:!:http://]!])', placeHolder:'Le texte de votre lien' },
		{name:'Citation', replaceWith: function(m) { return miu.multilineStartWith(m, "> ");}, placeHolder: 'Votre citation ici' },
		{name:'Code', replaceWith: function(m) { 
			if ((m.selection||m.placeHolder).split(/\n/).length > 1) 
				return miu.multilineStartWith(m, "\t");
			else
				return '`' + (m.selection||m.placeHolder) + '`';
			}, placeHolder: 'Votre code ici' },
		{separator:'---------------'},	
		{name:'Prévisualiser', call:'preview', className:"preview"}
	]
}

// mIu nameSpace to avoid conflict.
miu = {
	markdownTitle: function(markItUp, char) {
		heading = '';
		n = jQuery.trim(markItUp.selection||markItUp.placeHolder).length;
		for(i = 0; i < n; i++) {
			heading += char;
		}
		return '\n'+heading;
	},
	multilineStartWith: function(markItUp, char) {
		var lines = (markItUp.selection||markItUp.placeHolder).split(/\n/);
		var md_lines = jQuery.map(lines, function(l, i) { return char + l; });
		return md_lines.join("\n");

	}
}