// ----------------------------------------------------------------------------
// 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
// ----------------------------------------------------------------------------
mySettings = {
	onShiftEnter:	{keepDefault:false, replaceWith:'<br />\n'},
	onCtrlEnter:	{keepDefault:false, openWith:'\n<p>', closeWith:'</p>\n'},
	onTab:			{keepDefault:false, openWith:'	 '},
	markupSet: [
		{name:'Heading 2', key:'2', openWith:'<h2(!( class="[![Class]!]")!)>', closeWith:'</h2>', placeHolder:'Your title here...' },
		{name:'Heading 3', key:'3', openWith:'<h3(!( class="[![Class]!]")!)>', closeWith:'</h3>', placeHolder:'Your title here...' },
		{name:'Paragraph', openWith:'<p(!( class="[![Class]!]")!)>', closeWith:'</p>' },
		{separator:'---------------' },
		{name:'Bold', key:'B', openWith:'(!(<strong>|!|<b>)!)', closeWith:'(!(</strong>|!|</b>)!)' },
		{name:'Italic', key:'I', openWith:'(!(<em>|!|<i>)!)', closeWith:'(!(</em>|!|</i>)!)' },
		{separator:'---------------' },
		{name:'Ul', openWith:'<ul>\n', closeWith:'</ul>\n' },
		{name:'Ol', openWith:'<ol>\n', closeWith:'</ol>\n' },
		{name:'Li', openWith:'<li>', closeWith:'</li>' },
		{separator:'---------------' },
		{
			name: 'Picture',
			key: 'P',
			replaceWith: function(markitup) {
				pageCode = randomString(8);
				window.open('/cms/image_mgnt.php?mnu=image&type=misc&code=' + pageCode, "imgMgnt", "status=1,toolbar=0,scrollbars=1,width=600, height=450");
				tmpMarkitup = markitup;
				imgListner();
			}
		},
		{name:'Link', key:'L', openWith:'<a href="[![Link:!:http://]!]"(!( title="[![Title]!]")!)>', closeWith:'</a>', placeHolder:'Your text to link...' },
		{separator:'---------------' },
		{name:'Clean', className:'clean', replaceWith:function(markitup) { return markitup.selection.replace(/<(.*?)>/g, "") } },
		{name:'Preview', className:'preview', call:'previewContent' }
	]
}

	var boolReplace = false;
	var txtImg = "";
	var pageCode = "";
	var tmpMarkitup = null;
	
	var strMarkup = "";
	
	function imgListner() {
		if (boolReplace == true) {
			boolReplace = false;
			for(prop in tmpMarkitup) {
				strMarkup = strMarkup + prop + ":" + tmpMarkitup[prop] + "\n";
			}
	//		alert(strMarkup);
	//		tmpMarkitup.selection
	
			strOrig = tmpMarkitup.textarea.value;
			intSelLen = tmpMarkitup.selection.length;
			intLen = tmpMarkitup.textarea.value.length;
	
			start = tmpMarkitup.caretPosition
			len = txtImg.length;
					
			lines = strOrig.replace(/\r?\n/, "\n ");
			lines = lines.split(/\n/);

			var ie = 0;
			if ($.browser.msie) {
				var c = 0;
				for (var ie=0; ie < lines.length; ie++) {
					c += lines[ie].length;
					if (c >= start) {
						line = ("on line: " + lines[ie]);
						break
					}
				}

				var selLines = null;
				selLines = tmpMarkitup.selection.match(/\r?\n/);
				if (selLines != null || tmpMarkitup.selection.length > 0)
					ie = ie + 1;
			}	
		
			strFront = strOrig.substring(0, (start + ie));
			strBack = strOrig.substring((start + ie) + intSelLen, intLen);
			block = strFront + txtImg + strBack

			tmpMarkitup.textarea.value = block;
	
			if (tmpMarkitup.textarea.createTextRange){
				// quick fix to make it work on Opera 9.5
				if ($.browser.opera && $.browser.version >= 9.5 && len == 0) {
					return false;
				}
				range = tmpMarkitup.textarea.createTextRange();
				range.collapse(true);
				range.moveStart('character', start); 
				range.moveEnd('character', len); 
				range.select();
			} else if (tmpMarkitup.textarea.setSelectionRange ){
				tmpMarkitup.textarea.setSelectionRange(start, start + len);
			}
	
			tmpMarkitup.textarea.scrollTop = tmpMarkitup.scrollPosition;
			tmpMarkitup.textarea.focus();
	//		console.log(tmpMarkitup.selection);
		}
		else {
			setTimeout("imgListner()", 1);
		}
	}

	function previewContent() {
		var id = (arguments[0] != null) ? arguments[0] : $(this).attr("id").replace(/[^0-9]/g, "");;
	
		$.post("/cms/cms_tmp.php", {'content': $("#markItUpT" + id + " textarea").val()}, function(data) {
			$("#C" + id).html(data);
			$("#markItUpT" + id).hide();
			$("#C" + id).show();
			$("#cms_button" + id).show();
	
	
			$("#C" + id).unbind("click").bind("click", function(){
				$("#C" + id).hide();
				$("#markItUpT" + id).show();
			})
			
		});
	}
