/*
 * insert a bbcode tag around the currently selected text
 */
function foto_insert_bbcode_tag(options) {
	var content = $$('textarea[name=' + options['input'] + ']');
	// INFO simple tag
	if(options['tag'] == 'BR'
		|| options['tag'] == 'READMORE'
	) {
		content.insertAtCursor('[' + options['tag'] + '/]', false);
	}
	// INFO double tag
	if(options['tag'] == 'B'
		|| options['tag'] == 'i'
		|| options['tag'] == 'P'
		|| options['tag'] == 'U'
		|| options['tag'] == 'S'
		|| options['tag'] == 'QUOTE'
		|| options['tag'] == 'LIST'
		|| options['tag'] == 'STEP'
		|| options['tag'] == 'COVER'
		|| options['tag'] == 'ANCHOR'
		|| options['tag'] == 'TITLE1'
		|| options['tag'] == 'TITLE2'
		|| options['tag'] == 'TITLE3'
	) {
		content.insertAroundCursor({
			before: '[' + options['tag'] + ']',
			after: '[/' + options['tag'] + ']'
		});
		// TODO position cursor after insertion
	}
	// INFO complex tag with param and text
	if(options['tag'] == 'IMGL'
		|| options['tag'] == 'IMG'
		|| options['tag'] == 'IMGR'
		|| options['tag'] == 'FILEL'
		|| options['tag'] == 'FILE'
		|| options['tag'] == 'FILER'
		|| options['tag'] == 'URL'
		|| options['tag'] == 'COLOR'
		|| options['tag'] == 'SIZE'
	) {
		content.insertAroundCursor({
			before: '[' + options['tag'] + '=' + options['param'] + ']',
			after: '[/' + options['tag'] + ']',
			defaultMiddle: options['text']
		});
		// TODO position cursor after insertion
	}
}

/*
 * html entity encoder
 */
function foto_htmlentities(text) {
	text = text.replace(/"/g,'&quot;');
	text = text.replace(/&/g,'&amp;');
	text = text.replace(/\'/g,'&#039;');
	text = text.replace(/</g,'&lt;');
	text = text.replace(/>/g,'&gt;');
	text = text.replace(/\^/g,'&circ;');
	text = text.replace(/‘/g,'&lsquo;');
	text = text.replace(/’/g,'&rsquo;');
	text = text.replace(/“/g,'&ldquo;');
	text = text.replace(/”/g,'&rdquo;');
	text = text.replace(/•/g,'&bull;');
	text = text.replace(/–/g,'&ndash;');
	text = text.replace(/—/g,'&mdash;');
	text = text.replace(/˜/g,'&tilde;');
	text = text.replace(/™/g,'&trade;');
	text = text.replace(/š/g,'&scaron;');
	text = text.replace(/›/g,'&rsaquo;');
	text = text.replace(/œ/g,'&oelig;');
	text = text.replace(//g,'&#357;');
	text = text.replace(/ž/g,'&#382;');
	text = text.replace(/Ÿ/g,'&Yuml;');
	text = text.replace(/ /g,'&nbsp;');
	text = text.replace(/¡/g,'&iexcl;');
	text = text.replace(/¢/g,'&cent;');
	text = text.replace(/£/g,'&pound;');
	text = text.replace(/ /g,'&curren;');
	text = text.replace(/¥/g,'&yen;');
	text = text.replace(/¦/g,'&brvbar;');
	text = text.replace(/§/g,'&sect;');
	text = text.replace(/¨/g,'&uml;');
	text = text.replace(/©/g,'&copy;');
	text = text.replace(/ª/g,'&ordf;');
	text = text.replace(/«/g,'&laquo;');
	text = text.replace(/¬/g,'&not;');
	text = text.replace(/­/g,'&shy;');
	text = text.replace(/®/g,'&reg;');
	text = text.replace(/¯/g,'&macr;');
	text = text.replace(/°/g,'&deg;');
	text = text.replace(/±/g,'&plusmn;');
	text = text.replace(/²/g,'&sup2;');
	text = text.replace(/³/g,'&sup3;');
	text = text.replace(/´/g,'&acute;');
	text = text.replace(/µ/g,'&micro;');
	text = text.replace(/¶/g,'&para');
	text = text.replace(/·/g,'&middot;');
	text = text.replace(/¸/g,'&cedil;');
	text = text.replace(/¹/g,'&sup1;');
	text = text.replace(/º/g,'&ordm;');
	text = text.replace(/»/g,'&raquo;');
	text = text.replace(/¼/g,'&frac14;');
	text = text.replace(/½/g,'&frac12;');
	text = text.replace(/¾/g,'&frac34;');
	text = text.replace(/¿/g,'&iquest;');
	text = text.replace(/À/g,'&Agrave;');
	text = text.replace(/Á/g,'&Aacute;');
	text = text.replace(/Â/g,'&Acirc;');
	text = text.replace(/Ã/g,'&Atilde;');
	text = text.replace(/Ä/g,'&Auml;');
	text = text.replace(/Å/g,'&Aring;');
	text = text.replace(/Æ/g,'&AElig;');
	text = text.replace(/Ç/g,'&Ccedil;');
	text = text.replace(/È/g,'&Egrave;');
	text = text.replace(/É/g,'&Eacute;');
	text = text.replace(/Ê/g,'&Ecirc;');
	text = text.replace(/Ë/g,'&Euml;');
	text = text.replace(/Ì/g,'&Igrave;');
	text = text.replace(/Í/g,'&Iacute;');
	text = text.replace(/Î/g,'&Icirc;');
	text = text.replace(/Ï/g,'&Iuml;');
	text = text.replace(/Ð/g,'&ETH;');
	text = text.replace(/Ñ/g,'&Ntilde;');
	text = text.replace(/Ò/g,'&Ograve;');
	text = text.replace(/Ó/g,'&Oacute;');
	text = text.replace(/Ô/g,'&Ocirc;');
	text = text.replace(/Õ/g,'&Otilde;');
	text = text.replace(/Ö/g,'&Ouml;');
	text = text.replace(/×/g,'&times;');
	text = text.replace(/Ø/g,'&Oslash;');
	text = text.replace(/Ù/g,'&Ugrave;');
	text = text.replace(/Ú/g,'&Uacute;');
	text = text.replace(/Û/g,'&Ucirc;');
	text = text.replace(/Ü/g,'&Uuml;');
	text = text.replace(/Ý/g,'&Yacute;');
	text = text.replace(/Þ/g,'&THORN;');
	text = text.replace(/ß/g,'&szlig;');
	text = text.replace(/à/g,'&agrave;');
	text = text.replace(/á/g,'&aacute;');
	text = text.replace(/â/g,'&acirc;');
	text = text.replace(/ã/g,'&atilde;');
	text = text.replace(/ä/g,'&auml;');
	text = text.replace(/å/g,'&aring;');
	text = text.replace(/æ/g,'&aelig;');
	text = text.replace(/ç/g,'&ccedil;');
	text = text.replace(/è/g,'&egrave;');
	text = text.replace(/é/g,'&eacute;');
	text = text.replace(/ê/g,'&ecirc;');
	text = text.replace(/ë/g,'&euml;');
	text = text.replace(/ì/g,'&igrave;');
	text = text.replace(/í/g,'&iacute;');
	text = text.replace(/î/g,'&icirc;');
	text = text.replace(/ï/g,'&iuml;');
	text = text.replace(/ð/g,'&eth;');
	text = text.replace(/ñ/g,'&ntilde;');
	text = text.replace(/ò/g,'&ograve;');
	text = text.replace(/ó/g,'&oacute;');
	text = text.replace(/ô/g,'&ocirc;');
	text = text.replace(/õ/g,'&otilde;');
	text = text.replace(/ö/g,'&ouml;');
	text = text.replace(/÷/g,'&divide;');
	text = text.replace(/ø/g,'&oslash;');
	text = text.replace(/ù/g,'&ugrave;');
	text = text.replace(/ú/g,'&uacute;');
	text = text.replace(/û/g,'&ucirc;');
	text = text.replace(/ü/g,'&uuml;');
	text = text.replace(/ý/g,'&yacute;');
	text = text.replace(/þ/g,'&thorn;');
	text = text.replace(/ÿ/g,'&yuml;');
	return text;
}

/*
 * html entity decoder
 */
function foto_html_entity_decode(text) {
	text = text.replace(/&quot;/g,'"');
	text = text.replace(/&amp;/g,'&');
	text = text.replace(/&#039;/g,"'");
	text = text.replace(/&apos;/g,"'");
	text = text.replace(/&lt;/g,'<');
	text = text.replace(/&gt;/g,'>');
	text = text.replace(/&circ;/g,'^');
	text = text.replace(/&lsquo;/g,'‘');
	text = text.replace(/&rsquo;/g,'’');
	text = text.replace(/&ldquo;/g,'“');
	text = text.replace(/&rdquo;/g,'”');
	text = text.replace(/&bull;/g,'•');
	text = text.replace(/&ndash;/g,'–');
	text = text.replace(/&mdash;/g,'—');
	text = text.replace(/&tilde;/g,'˜');
	text = text.replace(/&trade;/g,'™');
	text = text.replace(/&scaron;/g,'š');
	text = text.replace(/&rsaquo;/g,'›');
	text = text.replace(/&oelig;/g,'œ');
	text = text.replace(/&#357;/g,'');
	text = text.replace(/&#382;/g,'ž');
	text = text.replace(/&Yuml;/g,'Ÿ');
	text = text.replace(/&nbsp;/g,' ');
	text = text.replace(/&iexcl;/g,'¡');
	text = text.replace(/&cent;/g,'¢');
	text = text.replace(/&pound;/g,'£');
	text = text.replace(/&curren;/g,' ');
	text = text.replace(/&yen;/g,'¥');
	text = text.replace(/&brvbar;/g,'¦');
	text = text.replace(/&sect;/g,'§');
	text = text.replace(/&uml;/g,'¨');
	text = text.replace(/&copy;/g,'©');
	text = text.replace(/&ordf;/g,'ª');
	text = text.replace(/&laquo;/g,'«');
	text = text.replace(/&not;/g,'¬');
	text = text.replace(/&shy;/g,'­');
	text = text.replace(/&reg;/g,'®');
	text = text.replace(/&macr;/g,'¯');
	text = text.replace(/&deg;/g,'°');
	text = text.replace(/&plusmn;/g,'±');
	text = text.replace(/&sup2;/g,'²');
	text = text.replace(/&sup3;/g,'³');
	text = text.replace(/&acute;/g,'´');
	text = text.replace(/&micro;/g,'µ');
	text = text.replace(/&para/g,'¶');
	text = text.replace(/&middot;/g,'·');
	text = text.replace(/&cedil;/g,'¸');
	text = text.replace(/&sup1;/g,'¹');
	text = text.replace(/&ordm;/g,'º');
	text = text.replace(/&raquo;/g,'»');
	text = text.replace(/&frac14;/g,'¼');
	text = text.replace(/&frac12;/g,'½');
	text = text.replace(/&frac34;/g,'¾');
	text = text.replace(/&iquest;/g,'¿');
	text = text.replace(/&Agrave;/g,'À');
	text = text.replace(/&Aacute;/g,'Á');
	text = text.replace(/&Acirc;/g,'Â');
	text = text.replace(/&Atilde;/g,'Ã');
	text = text.replace(/&Auml;/g,'Ä');
	text = text.replace(/&Aring;/g,'Å');
	text = text.replace(/&AElig;/g,'Æ');
	text = text.replace(/&Ccedil;/g,'Ç');
	text = text.replace(/&Egrave;/g,'È');
	text = text.replace(/&Eacute;/g,'É');
	text = text.replace(/&Ecirc;/g,'Ê');
	text = text.replace(/&Euml;/g,'Ë');
	text = text.replace(/&Igrave;/g,'Ì');
	text = text.replace(/&Iacute;/g,'Í');
	text = text.replace(/&Icirc;/g,'Î');
	text = text.replace(/&Iuml;/g,'Ï');
	text = text.replace(/&ETH;/g,'Ð');
	text = text.replace(/&Ntilde;/g,'Ñ');
	text = text.replace(/&Ograve;/g,'Ò');
	text = text.replace(/&Oacute;/g,'Ó');
	text = text.replace(/&Ocirc;/g,'Ô');
	text = text.replace(/&Otilde;/g,'Õ');
	text = text.replace(/&Ouml;/g,'Ö');
	text = text.replace(/&times;/g,'×');
	text = text.replace(/&Oslash;/g,'Ø');
	text = text.replace(/&Ugrave;/g,'Ù');
	text = text.replace(/&Uacute;/g,'Ú');
	text = text.replace(/&Ucirc;/g,'Û');
	text = text.replace(/&Uuml;/g,'Ü');
	text = text.replace(/&Yacute;/g,'Ý');
	text = text.replace(/&THORN;/g,'Þ');
	text = text.replace(/&szlig;/g,'ß');
	text = text.replace(/&agrave;/g,'à');
	text = text.replace(/&aacute;/g,'á');
	text = text.replace(/&acirc;/g,'â');
	text = text.replace(/&atilde;/g,'ã');
	text = text.replace(/&auml;/g,'ä');
	text = text.replace(/&aring;/g,'å');
	text = text.replace(/&aelig;/g,'æ');
	text = text.replace(/&ccedil;/g,'ç');
	text = text.replace(/&egrave;/g,'è');
	text = text.replace(/&eacute;/g,'é');
	text = text.replace(/&ecirc;/g,'ê');
	text = text.replace(/&euml;/g,'ë');
	text = text.replace(/&igrave;/g,'ì');
	text = text.replace(/&iacute;/g,'í');
	text = text.replace(/&icirc;/g,'î');
	text = text.replace(/&iuml;/g,'ï');
	text = text.replace(/&eth;/g,'ð');
	text = text.replace(/&ntilde;/g,'ñ');
	text = text.replace(/&ograve;/g,'ò');
	text = text.replace(/&oacute;/g,'ó');
	text = text.replace(/&ocirc;/g,'ô');
	text = text.replace(/&otilde;/g,'õ');
	text = text.replace(/&ouml;/g,'ö');
	text = text.replace(/&divide;/g,'÷');
	text = text.replace(/&oslash;/g,'ø');
	text = text.replace(/&ugrave;/g,'ù');
	text = text.replace(/&uacute;/g,'ú');
	text = text.replace(/&ucirc;/g,'û');
	text = text.replace(/&uuml;/g,'ü');
	text = text.replace(/&yacute;/g,'ý');
	text = text.replace(/&thorn;/g,'þ');
	text = text.replace(/&yuml;/g,'ÿ');
	return text;
}

/*
 * get the dirname of a path
 */
function foto_dirname(path) {
	return path.replace(/\\/g,'/').replace(/\/[^\/]*\/?$/, '');
}

/*
 * get the basename of a path
 */
function foto_basename(path) {
	return path.replace(/\\/g,'/').replace( /.*\//, '');
}

