JSUtils = new Class({

	l: null,
	strings: {
		erro: {
			pt: 'Erro',
			en: 'Error'
		},
		erros: {
			pt: 'Erros',
			en: 'Errors'
		},
		corrigirPre: {
			pt: 'Corrija-os e tente ',
			en: 'Correct them and try '
		},
		corrigirPos: {
			pt: ' novamente.',
			en: ' again.'
		},
	},

	// ----------------------------------------------------------------------------------------------------
	initialize: function(l) {

		this.l = l;

	},

	// ----------------------------------------------------------------------------------------------------
	mostraErrorArray: function(errorArray, verbo) {

		var titulo, errorStr;
		if(errorArray.length == 1) {
			titulo = this.strings.erro[this.l];
			errorStr = errorArray[0] + '.';
		} else {
			titulo = this.strings.erros[this.l];
			errorStr = '<ul>';
			for(var n = 0; n < errorArray.length; n++) {
				if(n == errorArray.length - 1) errorStr += '<li>' + errorArray[n] + '.</li>';
				else errorStr += '<li>' + errorArray[n] + ';</li>';
			}
			errorStr += '</ul>';
			errorStr += this.strings.corrigirPre[this.l] + verbo + this.strings.corrigirPos[this.l];
		}
		msgr.show({
			tipo: 'error',
			titulo: titulo,
			html: errorStr
		});

	},

	// ----------------------------------------------------------------------------------------------------
	emailCheck: function(emailStr) {

		var emailPat = /^(.+)@(.+)$/;
		var specialChars = "\\(\\)<>@,;:\\\\\\\"\\.\\[\\]";
		var validChars = "\[^\\s" + specialChars + "\]";
		var quotedUser = "(\"[^\"]*\")";
		var ipDomainPat = /^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
		var atom = validChars + '+';
		var word = "(" + atom + "|" + quotedUser + ")";
		var userPat = new RegExp("^" + word + "(\\." + word + ")*$");
		var domainPat = new RegExp("^" + atom + "(\\." + atom + ")*$");
		var matchArray = emailStr.match(emailPat);

		if(matchArray == null) return false;
		var user = matchArray[1];
		var domain = matchArray[2];

		if(user.match(userPat) == null) return false;

		var IPArray = domain.match(ipDomainPat);
		if(IPArray != null) {
			// this is an IP address;
			for(var i = 1;i <= 4; i++) {
				if(IPArray[i] > 255) return false;
			}
			return true;
		}

		var domainArray = domain.match(domainPat);
		if(domainArray == null) return false;

		var atomPat = new RegExp(atom, "g");
		var domArr = domain.match(atomPat);
		var len = domArr.length;
		if(domArr[domArr.length-1].length < 2 || domArr[domArr.length-1].length > 3) return false;

		if(len < 2) return false;

		return true;

	},

	// ----------------------------------------------------------------------------------------------------
	skinInputs: function() {

		// Skin inputs
		var inputArray = $$('input');
		inputArray = inputArray.filter(
			function(item, index) {
				return !item.hasClass('abb_symb') && item.id != 'login' && item.id != 'senha' && item.id != 'q' && (item.type == 'text' || item.type == 'password');
			}
		);
//		var inputArray = inputArray.combine($$('textarea'));
		inputArray.each(
			function(item, index) {
				item.addEvents({
					focus: function() {
						this.setStyle('border', '1px solid #FECD08');
					},
					blur: function() {
						this.setStyle('border', '1px solid #666666');
					}
				});
				if(index == 0 && item != null) item.focus();
			}
		);
		if($chk($('q_depois'))) $('q_depois').focus();

	},

	// ----------------------------------------------------------------------------------------------------
	initTinyMCECompleto: function() {

		var setup = function(ed) {
			// Gets executed after DOM to HTML string serialization
			ed.onPostProcess.add(function(ed, o) {
				// State get is set when contents is extracted from editor
				if(!o.get) return;
				var html = o.content;
				html = html.replace(/<!--(.*?)-->/, '');

				// Tirar spans indesejados
				html = html.replace(/<span class="(.*)">/gi, '');
				html = html.replace(/<\/span>/gi, '');

				html = html.replace(/<[/]?(?:font|span|xml|del|ins|w|[ovwxp]:[^<=>\s]+)[^>]*?>/, '');
//				html = html.replace(/<([a-z][a-z0-9]*)\b (?:(?:class|lang|style|size|face|align)\s*=\s*(?:'[^<']*'|""[^<""]*""|[^\s<>]+)\s+)+.*?>/, '<$1>');
				html = html.replace(/<(p|strong|i|span)\b (?:(?:class|lang|style|size|face)\s*=\s*(?:'[^<']*'|""[^<""]*""|[^\s<>]+)\s+)+.*?>/, '<$1>');
				o.content = html;
			});
		}

		// Conteudo
		tinyMCE.init({
			language: 'pt',
			mode: 'textareas',
			theme: 'advanced',
			skin: 'sucropedia',
			plugins: 'paste, msgr_popups, advimagescale, advhr, table',
			theme_advanced_toolbar_location: 'top',
			theme_advanced_toolbar_align: 'left',
			theme_advanced_buttons1: 'undo, redo, formatselect, bold, italic, strikethrough, |, link, unlink, anchor, |, image, |, bullist, numlist, outdent, indent, |, justifyleft, justifycenter, justifyright, justifyfull',
			theme_advanced_buttons2: Browser.Engine.trident ? 'cut, copy, paste, |, cleanup, removeformat, |, hr, |, tablecontrols' : 'cleanup, removeformat, |, hr, |, tablecontrols',
			theme_advanced_buttons3: null,
			theme_advanced_resizing: true,
			theme_advanced_resize_horizontal: false,
			theme_advanced_statusbar_location: 'bottom',
//			content_css: 'modulos/tiny_mce/jscripts/tiny_mce/sucropedia.css',
			theme_advanced_blockformats : 'h1, h2, h3',
			setup: setup,
//			entities: null,
			entity_encoding: 'named',
			relative_urls: false,
			document_base_url: '/',
			paste_auto_cleanup_on_paste : true,
			paste_text_use_dialog: false,
			paste_remove_spans: true,
			paste_preprocess : function(pl, o) {
				// Content string containing the HTML from the clipboard
				var html = o.content.trim();
				html = html.replace(/<!--(.*?)-->/, '');
//				html = html.replace(/<[/]?(?:font|span|xml|del|ins|w|[ovwxp]:[^<=>\s]+)[^>]*?>/, '');
//				html = html.replace(/<(p|strong|i|span)\b (?:(?:class|lang|style|size|face)\s*=\s*(?:'[^<']*'|""[^<""]*""|[^\s<>]+)\s+)+.*?>/, '<$1>');
				html = html.replace(/<(p|strong|i|span)\b (?:(?:class|lang|style|size|face)\s*=\s*(?:'[^<']*'|""[^<""]*""|[^\s<>]+)\s+)+.*?>/, '<$1>');
				o.content = html;
			},
			paste_postprocess : function(pl, o) {
				// Content DOM node containing the DOM structure of the clipboard
//				o.node.innerHTML = o.node.innerHTML + "\n-: CLEANED :-";
			},

			// Advimage
			cleanup_on_startup: true,
			advimagescale_maintain_aspect_ratio: true, /* this is the default behavior */
			advimagescale_fix_border_glitch: true, /* also the default behavior */
			advimagescale_noresize_all: false, /* set to true to prevent all resizing on images */
			advimagescale_append_to_url: false, /* apply dimensions to image URL on resize */
			advimagescale_max_width:  500,
			advimagescale_min_width:  500 / 3,
			/* call this function when an image is loading */
			advimagescale_loading_callback: function(imgNode) {
			},
			/* call this function when an image is finished loading */
			advimagescale_loaded_callback: function(imgNode) {
			},
			/* call this function when an image has been resized */
			advimagescale_resize_callback: function(editorInstance, imgNode) {
				var el, src, nome, w, h, r, nw, min, dif, dir;
				el = $(imgNode);
				src = el.get('src');
				nome = src.substr(src.lastIndexOf('/') + 1);
				w = Number(el.get('width'));
				h = Number(el.get('height'));
				r = h / w;
				min = Number.MAX_VALUE;
				dif = Math.abs(w - 500);
				if(dif < min) {
					min = dif;
					nw = 500;
					dir = '';
				}
				dif = Math.abs(w - Math.round(500 / 2));
				if(dif < min) {
					min = dif;
					nw = Math.round(500 / 2);
					dir = 'med/';
				}
				dif = Math.abs(w - Math.round(500 / 3));
				if(dif < min) {
					min = dif;
					nw = Math.round(500 / 3);
					dir = 'peq/';
				}
				el.set({
					src: 'admins/' + gestorPaginas.info.idAdmin + '/imagens/' + dir + nome,
					_mce_src: 'admins/' + gestorPaginas.info.idAdmin + '/imagens/' + dir + nome,
					width: nw,
					height: Math.round(nw * r)
				});
			}
		});

		// Tips
//		window.tips.attach($$('span.mceText'));

	},

	// ----------------------------------------------------------------------------------------------------
	initTinyMCESimples: function() {

		var setup = function(ed) {
			// Gets executed after DOM to HTML string serialization
			ed.onPostProcess.add(function(ed, o) {
				// State get is set when contents is extracted from editor
				if(!o.get) return;
				var html = o.content;
				html = html.replace(/<!--(.*?)-->/, '');

				// Tirar spans indesejados
				html = html.replace(/<span class="(.*)">/gi, '');
				html = html.replace(/<\/span>/gi, '');

				html = html.replace(/<[/]?(?:font|span|xml|del|ins|w|[ovwxp]:[^<=>\s]+)[^>]*?>/, '');
//				html = html.replace(/<([a-z][a-z0-9]*)\b (?:(?:class|lang|style|size|face|align)\s*=\s*(?:'[^<']*'|""[^<""]*""|[^\s<>]+)\s+)+.*?>/, '<$1>');
				html = html.replace(/<(p|strong|i|span)\b (?:(?:class|lang|style|size|face)\s*=\s*(?:'[^<']*'|""[^<""]*""|[^\s<>]+)\s+)+.*?>/, '<$1>');
				o.content = html;
			});
		}

		tinyMCE.init({
			mode: 'textareas',
			theme: 'advanced',
			theme_advanced_toolbar_location: 'top',
			theme_advanced_toolbar_align: 'left',
			theme_advanced_buttons1: 'undo,redo, bold, italic, underline, sub, sup, charmap, |, bullist, numlist',
			theme_advanced_buttons2: '',
			theme_simple_statusbar_location: 'bottom',
//			content_css: 'modulos/tiny_mce/jscripts/tiny_mce/sucropedia.css',
			setup: setup
		});

	}

});

