
function markdown(method, target) {
	
	var element = document.getElementById( 'newsContent' );

	
	var text = $('#'+target).val();
	var txtarea = document.getElementById(target);
	
	if (document.selection) {
		//alert('Please use another browser...');
		txtarea.focus();
		var sel1 = document.selection.createRange();
		var sel = sel1.text;
        //alert(sel);
		
		switch (method) {
			case 'code':
				new_txt = '\n    '+sel+'\n';
				break;
			case 'quote':
				new_txt = '\n> '+sel.replace('\n', '\n> ')+'\n';
				break;
			case 'b':
				new_txt = '**'+sel+'**';
				break;
			case 'i':
				new_txt = '*'+sel+'*';
				break;
			case 'c':
				new_txt = '\n%'+sel+'%\n';
				break;
			case 'h1':
				new_txt = '\n# '+sel+' #\n';
				break;
			case 'h2':
				new_txt = '\n## '+sel+'##\n';
				break;
			case 'h3':
				new_txt = '\n### '+sel+'###\n';
				break;
			case 'more':
				new_txt = sel+'<!--more-->\n';
				break;
			case 'link':
				if (url = prompt('Type the complete url:','http://'))
					new_txt = '['+sel+']('+url+')';
				else
					return;
				break;
			default:
				break;
		}
		sel1.text = new_txt;
	} else {
		var selected = (txtarea.value).substring(txtarea.selectionStart,txtarea.selectionEnd);
		posStart = txtarea.selectionStart;
		posEnd = txtarea.selectionEnd;
		charsBefore = text.substring(0,posStart).length;
		charsAfter = text.substring(posEnd,text.length).length;
		new_txt = '';
		switch (method) {
			case 'b':
				new_txt = text.substring(0,posStart) + '**'+selected+'**' + text.substring(posEnd, text.length);
				break;
			case 'i':
				new_txt = text.substring(0,posStart) + '*'+selected+'*' + text.substring(posEnd, text.length);
				break;
			case 'c':
				new_txt = text.substring(0,posStart) + '\n%'+selected+'%\n' + text.substring(posEnd, text.length);
				break;
			case 'link':
				if (url = prompt('Type the complete url:', 'http://'))
					new_txt = text.substring(0,posStart) + '['+selected+']('+url+')' + text.substring(posEnd, text.length);
				else
					return;
				break;
			case 'code':
				if (charsBefore > 0) new_txt += text.substring(0,posStart) + '\n';
				new_txt += '    '+selected;
				if (text.substring(posEnd, posEnd+1) == " ") posEnd++;
				if (charsAfter > 0) new_txt += '\n' + text.substring(posEnd, text.length);
				break;
			case 'quote':
				if (charsBefore > 0) new_txt += text.substring(0,posStart) + '\n';
				new_txt += '> '+selected.replace('\n', '\n> ');
				if (text.substring(posEnd, posEnd+1) == " ") posEnd++;
				if (charsAfter > 0) new_txt += '\n' + text.substring(posEnd, text.length);
				break;
			case 'h1':
				if (text.substring(posStart-1,posStart) == " ") posStart--;
				if (charsBefore > 0) new_txt += text.substring(0,posStart) + '\n';
				new_txt += '# '+selected;
				if (text.substring(posEnd, posEnd+1) == " ") posEnd++;
				if (charsAfter > 0) new_txt += '\n' + text.substring(posEnd, text.length);
				break;
			case 'h2':
				if (text.substring(posStart-1,posStart) == " ") posStart--;
				if (charsBefore > 0) new_txt += text.substring(0,posStart) + '\n';
				new_txt += '## '+selected;
				if (text.substring(posEnd, posEnd+1) == " ") posEnd++;
				if (charsAfter > 0) new_txt += '\n' + text.substring(posEnd, text.length);
				break;
			case 'h3':
				if (text.substring(posStart-1,posStart) == " ") posStart--;
				if (charsBefore > 0) new_txt += text.substring(0,posStart) + '\n';
				new_txt += '### '+selected;
				if (text.substring(posEnd, posEnd+1) == " ") posEnd++;
				if (charsAfter > 0) new_txt += '\n' + text.substring(posEnd, text.length);
				break;
			case 'more':
				if (text.substring(posStart-1,posStart) == " ") posStart--;
				if (charsBefore > 0) new_txt += text.substring(0,posStart) + '\n';
				new_txt += selected + '<!--more-->';
				if (text.substring(posEnd, posEnd+1) == " ") posEnd++;
				if (charsAfter > 0) new_txt += '\n' + text.substring(posEnd, text.length);
				break;
			default:
				new_txt = '';
				break;
		}
		$('#'+target).val(new_txt);
	}
	
	$('#'+target).focus();
}