var setReload = setInterval("UpdateHTML()", 300);

function UpdateHTML() {	
	$('#Html').focus(function() {   	   
		$('#Send').val(getContentBack());
	});
}

tinyMCE.init({
	mode : "exact",
	elements : "Editor",
	theme : "advanced",
	skin : "o2k7",
	cleanup_on_startup : true,
	cleanup: true,
	plugins : "advcode,safari,pagebreak,style,advhr,advimage,advlink,emotions,preview,media,fullscreen,template,inlinepopups,advimage,media,paste",                
	theme_advanced_buttons1 : "bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,|,bullist,numlist,|,outdent,indent,|,link,unlink,|,image,advcode,|,forecolor,|,charmap,|,pastetext,pasteword,pastetext,fullscreen,pagebreak,preview",
	theme_advanced_buttons2 : "",
	theme_advanced_buttons3 : "",
	theme_advanced_toolbar_location : "top",
	theme_advanced_toolbar_align : "left",
	theme_advanced_statusbar_location : "bottom",
	theme_advanced_resizing : false,
	convert_urls : false,                        
	content_css : "css/content.css",                
	external_link_list_url : "lists/link_list.js",
	external_image_list_url : "lists/image_list.js",
	media_external_list_url : "lists/media_list.js"		
}); 

function toggleEditor(id) {
	if(!tinyMCE.get(id)) tinyMCE.execCommand('mceAddControl', false, id);
	else tinyMCE.execCommand('mceRemoveControl', false, id);
}

function getContent() {
	Content = new String('');
	Content = tinyMCE.get('Editor').getContent();
	P = Content.split('</p>');
	X = P.length;
	
	if(X > 1) {
		Content = "";
		for(i = 0; i < X; i++) {
			if(i == X - 1) Content = Content + P[i] + "\n";
			else Content = Content + P[i] + "</p>\n";		
		}
	}
	
	Br = Content.split('<br /><br />');
	R = Br.length;
	
	if(R > 1) {
		Content = "";
		for(i = 0; i < R; i++) {
			if(i == R - 1) Content = Content + Br[i] + "\n";
			else Content = Content + Br[i] + "<br /><br />\n\n";		
		}
	}	
	
	Ul = Content.split('</ul>');
	Y = Ul.length;
	
	if(Y > 1) {
		Content = "";
		for(i = 0; i < Y; i++) {
			if(i == Y - 1) Content = Content + Ul[i] + "\n";
			else Content = Content + Ul[i] + "</ul>\n";		
		}
	}
	
	Ol = Content.split('</ol>');
	O = Ol.length;
	
	if(O > 1) {
		Content = "";
		for(i = 0; i < O; i++) {
			if(i == O - 1) Content = Content + Ol[i] + "\n";
			else Content = Content + Ol[i] + "</ol>\n";		
		}
	}	
	
	Li = Content.split('<li>');
	Z = Li.length;
	
	if(Z > 1) {
		Content = "";
		for(i = 0; i < Z; i++) {	
			if(i == Z - 1) Content = Content + Li[i] + "\n";
			else Content = Content + Li[i] + "\t<li>";	
		}
	}

	return Content;
}

function getContentBack() {
	var Content = $('#Html').val();	
	return Content;
}

$(document).ready(function() {
	
	$('#Code').click(function() {
	    toggleEditor('Editor');
		tinyMCE.get('Editor').setContent(getContent());
	});
	
	$('#Back').click(function() {
		$('.HTML').hide();
		tinyMCE.get('Editor').setContent(getContentBack());
		$('.Editor').show();
	});
	
	
});
