var buttonsnap_markers = new Array(
"contact form"
);
var buttonsnap_classes = new Array(
"wpcf_marker"
);

function TinyMCE_buttonsnap_initInstance(inst) {
	tinyMCE.importCSS(inst.getDoc(), "http://www.riad107.com/marrakech/wp-content/plugins/wp-contact-form/buttonsnap.php?docss=true");
}

function TinyMCE_buttonsnap_parseAttributes(attribute_string) {
	var attributeName = "";
	var attributeValue = "";
	var withInName;
	var withInValue;
	var attributes = new Array();
	var whiteSpaceRegExp = new RegExp('^[ \n\r\t]+', 'g');
	var titleText = tinyMCE.getLang('lang_buttonsnap_more');
	var titleTextPage = tinyMCE.getLang('lang_buttonsnap_page');

	if (attribute_string == null || attribute_string.length < 2)
		return null;

	withInName = withInValue = false;

	for (var i=0; i<attribute_string.length; i++) {
		var chr = attribute_string.charAt(i);

		if ((chr == '"' || chr == "'") && !withInValue)
			withInValue = true;
		else if ((chr == '"' || chr == "'") && withInValue) {
			withInValue = false;

			var pos = attributeName.lastIndexOf(' ');
			if (pos != -1)
				attributeName = attributeName.substring(pos+1);

			attributes[attributeName.toLowerCase()] = attributeValue.substring(1).toLowerCase();

			attributeName = "";
			attributeValue = "";
		} else if (!whiteSpaceRegExp.test(chr) && !withInName && !withInValue)
			withInName = true;

		if (chr == '=' && withInName)
			withInName = false;

		if (withInName)
			attributeName += chr;

		if (withInValue)
			attributeValue += chr;
	}

	return attributes;
}

function TinyMCE_buttonsnap_cleanup(type, content) {
	switch (type) {
		case "initial_editor_insert":
			content = TinyMCE_buttonsnap_cleanup("insert_to_editor", content);
			alert('foo');
			
			break;
	
		case "insert_to_editor":
			var startPos = 0;

			for(z=0;z<buttonsnap_markers.length;z++) {
				var startPos = 0;
				while ((startPos = content.indexOf('<!--' + buttonsnap_markers[z] + '-->', startPos)) != -1) {
					// Insert image
					var contentAfter = content.substring(startPos + 7 + buttonsnap_markers[z].length);
					content = content.substring(0, startPos);
					content += '<img src="' + (tinyMCE.getParam("theme_href") + "/images/spacer.gif") + '" ';
					content += ' width="100%" height="10px" ';
					content += 'alt="" class="' + buttonsnap_classes[z] + '" />';
					content += contentAfter;
	
					startPos++;
				}
			}
			break;

		case "get_from_editor":
			var startPos = -1;
			while ((startPos = content.indexOf('<img', startPos+1)) != -1) {
				var endPos = content.indexOf('/>', startPos);
				var attribs = TinyMCE_buttonsnap_parseAttributes(content.substring(startPos + 4, endPos));

				for(z=0;z<buttonsnap_classes.length;z++) {
					if (attribs['class'] == buttonsnap_classes[z]) {
						endPos += 2;
		
						var embedHTML = '<!--' + buttonsnap_markers[z] + '-->';
		
						// Insert embed/object chunk
						chunkBefore = content.substring(0, startPos);
						chunkAfter = content.substring(endPos);
						content = chunkBefore + embedHTML + chunkAfter;
						break;
					}
				}
			}
			break;
	}

	return content;
}


initArray = {
	mode : "specific_textareas",
	editor_selector : "mceEditor",
	width : "100%",
	theme : "advanced",
	theme_advanced_buttons1 : "bold,italic,strikethrough,separator,bullist,numlist,outdent,indent,separator,justifyleft,justifycenter,justifyright,separator,link,unlink,image,wp_more,separator,spellchecker,separator,wp_help,wp_adv,wp_adv_start,formatselect,underline,justifyfull,forecolor,separator,pastetext,pasteword,separator,removeformat,cleanup,separator,charmap,separator,undo,redo,wp_adv_end",
	theme_advanced_buttons2 : "",
	theme_advanced_buttons3 : "",
	language : "fr_fr",
	theme_advanced_toolbar_location : "top",
	theme_advanced_toolbar_align : "left",
	theme_advanced_path_location : "bottom",
	theme_advanced_resizing : true,
	browsers : "msie,gecko,opera,safari",
	dialog_type : "modal",
	theme_advanced_resize_horizontal : false,
	convert_urls : false,
	relative_urls : false,
	remove_script_host : false,
	force_p_newlines : true,
	force_br_newlines : false,
	convert_newlines_to_brs : false,
	remove_linebreaks : false,
	fix_list_elements : true,
	gecko_spellcheck : true,
	entities : "38,amp,60,lt,62,gt",
	button_tile_map : true,
	content_css : "http://www.riad107.com/marrakech/wp-includes/js/tinymce/plugins/wordpress/wordpress.css",
	valid_elements : "p/-div[*],-strong/-b[*],-em/-i[*],-font[*],-ul[*],-ol[*],-li[*],*[*]",
	save_callback : 'TinyMCE_wordpressPlugin.saveCallback',
	imp_version : "0",
	plugins : "inlinepopups,autosave,spellchecker,paste,wordpress,buttonsnap"
};


tinyMCE.init(initArray);

