/*  */
/*  */

function plugin_regexrep_setup(editor_id, node) {
  node.innerHTML = plugin_regexrep_replace(node.innerHTML, 'setupcontent_regex');
}
function plugin_regexrep_save(editor_id, content, node) {
  return plugin_regexrep_replace(content, 'save_regex');
}
function plugin_regexrep_replace(content, tiny_param) {
  var re_string = tinyMCE.activeEditor.getParam(tiny_param);
  if (typeof(re_string) == "undefined" || re_string == "" || re_string == null) {
    return;
  }
  var re_array = re_string.split(/[*][*]/g);
  for (i=0; i<re_array.length; i++) {
    if (typeof(re_array[i]) == "undefined" || re_array[i] == "" || re_array[i] == null) {
      continue;
    }
    var tokens = re_array[i].split(/\+\+/g);
    if (tokens[0] == null) { return; }
    var replace = ((tokens[1] == null || typeof tokens[1] == "undefined") ? '' : tokens[1] );
    var flags = ((tokens[2] == null || typeof tokens[2] == "undefined") ? 'g' : tokens[2] );
    var re = new RegExp(tokens[0], flags);
    content = content.replace(re, replace);
  }
  return content;
}

function GetButtonWidth(button) {
	var result = 23;	// default width
	switch (button) {
		case 'formatselect':
			result = 88;
			break;
		case 'styleselect':
			result = 88;
			break;
		case 'forecolor':
			result = 32;
			break;
		case 'backcolor':
			result = 32;
			break;
		case 'tablecontrols':
			result = 285;
			break;
	}
	return result;
}

function GetBlockformats () {
	var blockformats = "Heading|h2;Subheading|h3;Literal|pre;Paragraph|p;".split(";");
	var result = new Array();
	var field;
	
	for (var i=0; i < blockformats.length; i++) {
		if (blockformats[i].length > 0) {
			field = blockformats[i].split("|")
			result.push (field[0] + "=" + field[1]);
		}
	}
	
	return result.join (";");
}

function GetStyles () {
	var styles = "Discreet|discreet;Pull-quote|pullquote;Call-out|callout;Highlight|visualHighlight;".split(";");
	var result = new Array();
	var field;
	
	for (var i=0; i < styles.length; i++) {
		if (styles[i].length > 0) {
			field = styles[i].split("|")
			result.push (field[0] + "=" + field[1]);
		}
	}
	
	return result.join (";");
}

function GetTableStyles () {
	var tablestyles = "Subdued grid|plain;Invisible grid|invisible;Fancy listing|listing;Fancy grid listing|grid listing;Fancy vertical listing|vertical listing;".split(";");
	var result = new Array();
	var field;
	
	for (var i=0; i < tablestyles.length; i++) {
		if (tablestyles[i].length > 0) {
			field = tablestyles[i].split("|")
			result.push (field[0] + "=" + field[1]);
		}
	}
	
	return result.join (";");
}

function GetRowStyles () {
	var rowstyles = "Odd row|odd;Even row|even;".split(";");
	var result = new Array();
	var field;
	
	for (var i=0; i < rowstyles.length; i++) {
		if (rowstyles[i].length > 0) {
			field = rowstyles[i].split("|")
			result.push (field[0] + "=" + field[1]);
		}
	}
	
	return result.join (";");
}

function GetCellStyles () {
	var cellstyles = "Plain|;".split(";");
	var result = new Array();
	var field;
	
	for (var i=0; i < cellstyles.length; i++) {
		if (cellstyles[i].length > 0) {
			field = cellstyles[i].split("|")
			result.push (field[0] + "=" + field[1]);
		}
	}
	
	return result.join (";");
}

var toolbars = []

function GetToolbars(sId) {
	var buttons = eval('(' + document.getElementById (sId).title + ')').buttons;

//	var kek = document.getElementById ('text');
//	var kektitle = kek.title;
//	var kekobject = eval('(' + kektitle + ')');
//	alert (kekobject.toolbar_bold);

	var toolbar_temp = [[],[],[],[]];

	var toolbar_width = parseInt('440');
	var cur_toolbar = 0;
	var cur_x = 0;
	var button_width;

	for (var i = 0; i < buttons.length; i++) {
		button_width = GetButtonWidth(buttons[i]);
		if (cur_x + button_width > toolbar_width) {
			cur_x = button_width;
			cur_toolbar++;
		} else {
			cur_x += button_width;
		}
		if (cur_toolbar <= 3) {
			toolbar_temp[cur_toolbar].push (buttons[i]);
		}
	}

	toolbars = [toolbar_temp[0].join(','), toolbar_temp[1].join(','), toolbar_temp[2].join(','), toolbar_temp[3].join(',')];
}

function GetBase() {
	var href_string = document.location.href;
	var href_array = href_string.split('/');
	if (href_string.indexOf('portal_factory') != -1) {
		while (href_array[href_array.length-1] != 'portal_factory') {
			href_array.pop();
		}
		href_array.pop();
	} else {
		if (href_array.length > 4) {
			href_array.pop();
		}
		if (href_array.length > 4) {
			href_array.pop();
		}
	}
	return href_array.join('/') + '/';
}
var realBase = document.location.href.substring(0,document.location.href.length-5);

/* Table Regular Expression (Mangles tables under IE)
**(?:<(?!t[dhr]|a\\b)[^>]*?>)*(?=(?:(?!<t[dhr]\\b).)*?<\\/t[dh]>)++++ig
*/
var regexes="class\\s*=?\\s*\"?(?:Mso[^\"]*?)\"?++++gi**<![-]-.*?--\\s*>++++g";

//jq(document).ready (function () {
//	GetToolbars ('text');

tinymce.dom.Event.add (window, 'load', function () {

var aInstances = tinymce.DOM.select('textarea');

for (var i = 0; i < aInstances.length; i++) {

if (tinymce.DOM.hasClass(aInstances[i], 'mce_editable')) {

GetToolbars(aInstances[i].id);

tinyMCE.init({
  // General options
  mode:"exact",
  elements:aInstances[i].id,
  theme:"advanced",
  skin:"plone",
  inlinepopups_skin : "plonepopup",
  plugins : "safari,pagebreak,table,save,advhr,advimage,advlink,emotions,iespell,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,inlinepopups",

  theme_advanced_styles: GetStyles(),
  theme_advanced_blockformats: GetBlockformats(),
  theme_advanced_buttons1 : toolbars[0],
  theme_advanced_buttons2 : toolbars[1],
  theme_advanced_buttons3 : toolbars[2],
  theme_advanced_buttons4 : toolbars[3],
  theme_advanced_toolbar_location:"top",
  theme_advanced_toolbar_align:"left",
  theme_advanced_path_location:"bottom",
  theme_advanced_path : false,
  theme_advanced_resizing: true,
  theme_advanced_resizing_use_cookie: true,
  theme_advanced_resize_horizontal : false,

  theme_advanced_source_editor_width: parseInt('100%'),
  theme_advanced_source_editor_height: parseInt('400'),
  
  auto_resize : false,
  
  table_styles: GetTableStyles(),
  table_row_styles: GetRowStyles(),
  table_cell_styles: GetCellStyles(),

  directionality: "ltr",

  content_css: "http://www.comune.sestoeduniti.cr.it/portal_tinymce/tinymce-getstyle",
  body_class: "documentContent",

  // Drop lists for link/image/media/template dialogs
  // template_external_list_url : "lists/template_list.js",
  // external_link_list_url : "lists/link_list.js",
  // external_image_list_url : "lists/image_list.js",
  // media_external_list_url : "lists/media_list.js",

  // Replace values for the template plugin
  template_replace_values : {
    username : "Some User",
    staffid : "991234"
  },

  document_base_url:GetBase(),
  remove_linebreaks:true,
  setupcontent_callback : "plugin_regexrep_setup",
  save_callback : "plugin_regexrep_save",
  setupcontent_regex : regexes,
  save_regex : regexes,
  valid_elements:""
  +"a[class|href|id|name|tabindex|title|target],"
  +"abbr[class|id|title],"
  +"acronym[class|id|title],"
  +"address[class|align|id|title],"
  +"bdo[class|id|title]," 
  +"big[class|id|title]," 
  +"blockquote[dir|cite|class|id|title]," 
  +"br[class|clear<all?left?none?right|id|title],"
  +"button[accesskey|class|disabled<disabled|id|name|tabindex|title|type|value]," 
  +"caption," 
  +"center[class|id|title]," 
  +"cite[class|id|title]," 
  +"code[class|id|title]," 
  +"col[off|class|id|span|title|valign<baseline?bottom?middle?top|width]," 
  +"colgroup[off|class|id|span|title|valign<baseline?bottom?middle?top|width]," 
  +"dd[class|id|title],"
  +"del[cite|class|datetime|id|title],"
  +"dfn[class|id|title],"
  +"dir[class|compact<compact|id|title],"
  +"div[align<center?right?justify|class|id|style|title]," 
  +"dl[class|compact<compact|id|title]," 
  +"dt[class|id|title]," 
  +"em/i[class|id|title],"
  +"fieldset[class|id|title]," 
  +"form[accept|accept-charset|action|class|enctype|id|method<get?post|name|title|target]," 
  +"h1[class|id|title]," 
  +"h2[class|id|title]," 
  +"h3[class|id|title]," 
  +"h4[class|id|title]," 
  +"h5[class|id|title]," 
  +"h6[class|id|title]," 
  +"hr[align<center?right|class|id|noshade<noshade|size|title|width]," 
  +"iframe[align<bottom?left?middle?right?top|class|frameborder|height|id|longdesc|marginheight|marginwidth|name|scrolling<auto?no?yes|src|title|width]," 
  +"img[alt|border|class|height|hspace|id|longdesc|name|src|title|width]," 
  +"input[accept|accesskey|alt|checked<checked|class|disabled<disabled|id|maxlength|name|readonly<readonly|size|src|tabindex|title|type<button?checkbox?file?hidden?image?password?radio?reset?submit?text|value]," 
  +"ins[cite|class|datetime|id|title],"
  +"isindex[class|id|prompt|title],"
  +"kbd[class|id|title],"
  +"label[accesskey|class|for|id|title],"
  +"legend[accesskey|class|id|title],"
  +"li[class|id|title|type|value|align],"
  +"link[class|href|hreflang|id|media|rel|rev|title|target|type],"
  +"map[class|id|name|title],"
  +"menu[class|compact<compact|id|title],"
  +"noframes[class|id|title],"
  +"noscript[class|id|title],"
  +"ol[class|compact<compact|id|start|title|type|align],"
  +"optgroup[class|disabled<disabled|id|label|title],"
  +"option[class|disabled<disabled|id|label|selected<selected|title|value],"
  +"p[align<center?right?justify|class|id|style|title],"
  +"param[id|name|type|value|valuetype<DATA?OBJECT?REF],"
  +"pre/listing/plaintext/xmp[align<center?right?justify|class|id|title|width],"
  +"q[cite|class|id|title],"
  +"s[class|id|title],"
  +"samp[class|id|title],"
  +"select[class|disabled<disabled|id|multiple<multiple|name|size|tabindex|title],"
  +"small[class|id|title],"
  +"span[align<center?right?justify|class|class|id|style|title],"
  +"strike[class|class|id|title],"
  +"strong/b,"
  +"sub[class|id|title]," 
  +"sup[class|id|title]," 
  +"table[class|id|rules|summary|title]," 
  +"tbody[class|id|title],"
  +"td[class|colspan|id|rowspan|title|nowrap|valign|align|style],"
  +"textarea[accesskey|class|cols|disabled<disabled|id|name|readonly<readonly|rows|tabindex|title]," 
  +"tfoot[off|class|id|title|valign<baseline?bottom?middle?top],"
  +"th[class|id|title|nowrap|align],"
  +"thead[class|id|title],"
  +"title[dir<ltr?rtl],"
  +"tr[class|id|title],"
  +"tt[class|id|title],"
  +"u[class|id|title],"
  +"ul[class|compact<compact|id|title|type|align]"
});
}
}

});
/*  */
