/**
 * $Id: editor_plugin_src.js 201 2007-02-12 15:56:56Z spocke $
 *
 * @author Moxiecode
 * @copyright Copyright � 2004-2008, Moxiecode Systems AB, All rights reserved.
 */

(function() {
	tinymce.create('tinymce.plugins.BBCodePlugin', {
		init : function(ed, url) {
			var t = this, dialect = ed.getParam('bbcode_dialect', 'punbb').toLowerCase();

			ed.onBeforeSetContent.add(function(ed, o) {
				o.content = t['_' + dialect + '_bbcode2html'](o.content);
			});

			ed.onSaveContent.add(function(ed, o) {
				o.content = t['_' + dialect + '_html2bbcode'](o.content);
			});

			ed.onPostProcess.add(function(ed, o) {
				if (o.set)
					o.content = t['_' + dialect + '_bbcode2html'](o.content);

// 				if (o.get)
// 					o.content = t['_' + dialect + '_html2bbcode'](o.content);
			});
		},

		getInfo : function() {
			return {
				longname : 'BBCode Plugin',
				author : 'Moxiecode Systems AB',
				authorurl : 'http://tinymce.moxiecode.com',
				infourl : 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/bbcode',
				version : tinymce.majorVersion + "." + tinymce.minorVersion
			};
		},

		// Private methods

		// HTML -> BBCode in PunBB dialect
		_punbb_html2bbcode : function(s) {
// alert("html2bbcode");
			s = tinymce.trim(s);

			function rep(re, str) {
				s = s.replace(re, str);
			};

// 			rep(/(.*?>)[\n|\r]/g,"$1");
			rep(/[\n\r]{0,1}(<(ol|ul).*?>)[\n\r]{0,1}/g,"$1");
                        rep(/(<\/(ul|ol|li)>)[\n\r]/g,"$1");

			// bbcode of img for sn article
			while(/(<img class=\"(.+?)\" .+?>)/.test(s)){
			    var _res = RegExp.$1;
			    var _typ = RegExp.$2;
			    var _rpl = "";

			    if(_typ == 'snVideo'){
				var _w = /width=\"(.*?)\"/.test(_res) ? RegExp.$1+';':'';
				var _h = /height=\"(.*?)\"/.test(_res) ? RegExp.$1+';':'';
				var _p = /alt=\"(.*?)\"/.test(_res) ? RegExp.$1:'';
				var _v = /title=\"(.*?)\"/.test(_res) ? RegExp.$1+';':'';
				_rpl = '[video='+_v+_w+_h+_p+']';
			    } else{
				var _p = (/src=\"(.*?)\"/.test(_res) ? RegExp.$1:'').split('/').pop();
				var _f = (/alt=\"(.*?)\"/.test(_res) ? RegExp.$1:'').split('/').pop();
				var _s = (/style=\"float:(.*?);\"/.test(_res) ? RegExp.$1:'').replace(/ /g,'');
				var _i = 'img';
				switch(_s){
				      case 'left':
					  _i = 'imgl';
				      break;
				      case 'right':
					  _i = 'imgr';
				      break;

				}
				_rpl = '['+_i+'='+_f+']'+_p+'[/'+_i+']';
			    }

			    s = s.replace(_res,_rpl);
			}

			//////

			// replace list html-codeStyle
			  var rl = new Array();
			  rl[0] = {o:"[list]",c:"[/list]",f:/<ul>/};
			  rl[1] = {o:"[list=d]",c:"[/list]",f:/<ul style="list-style-type: disc;">/};
			  rl[2] = {o:"[list=c]",c:"[/list]",f:/<ul style="list-style-type: circle;">/};
			  rl[3] = {o:"[list=s]",c:"[/list]",f:/<ul style="list-style-type: square;">/};
			  
			  rl[4] = {o:"[list=i]",c:"[/list]",f:/<ol style="list-style-type: lower-roman;">/};
			  rl[5] = {o:"[list=I]",c:"[/list]",f:/<ol style="list-style-type: upper-roman;">/};
			  rl[6] = {o:"[list=a]",c:"[/list]",f:/<ol style="list-style-type: lower-alpha;">/};
			  rl[7] = {o:"[list=A]",c:"[/list]",f:/<ol style="list-style-type: upper-alpha;">/};
			  rl[8] = {o:"[list=1]",c:"[/list]",f:/<ol style="list-style-type: decimal;">/};
			  rl[9] = {o:"[list=1]",c:"[/list]",f:/<ol>/};


			  var cl = new Array();
			  do{
			    var p = s.search(/<\/ol>|<\/ul>/);
			    var re = "";
			    
			    for(var i = 0; i < rl.length; i++){
			      var np = s.search(rl[i].f);

			      if(np != -1 && p > np){
				  p = np;
				  cl[cl.length] = rl[i];
				  re = rl[i].f;
			      }
			    }

			    if(cl.length > 0){
			      if(re == ""){
				s = s.replace(/<\/ol>|<\/ul>/,cl.pop().c);
			      }else{
				s = s.replace(re, cl[cl.length-1].o);
			      }
			    }
			  }while(cl.length > 0 || s.search(/<(ol|ul).*?>/) != -1 );

// 			  s = s.replace(/<li>(.*?)<\/li>/g,"[*]$1[/*]");
			  
			  s = s.replace(/<li>/g,"[*]");
			  s = s.replace(/<\/li>/g,"[/*]");
			
			while(s.match(/<a(.*?)>(.*?)<\/a>/i)){
			  var lnkh = RegExp.$1;
			  var lnkt = RegExp.$2;
			  lnkh.match(/title=\"(.+?)\"/);
			  var title = RegExp.$1;
			  lnkh.match(/href=\"(.+?)\"/);
			  var href = RegExp.$1;

			  if(title!=href){
			    url = "[xurl="+href+"|"+title+"]"+lnkt+"[/url]";
			  }else{
			    url = "[url="+href+"]"+lnkt+"[/url]";
			  }
			  s = s.replace(/<a(.*?)>(.*?)<\/a>/i, url);
			}

			rep(/<a.*?href=\"(.*?)\".*?>(.*?)<\/a>/gi,"[url=$1]$2[/url]");
			

			rep(/<(h[1|2|3])>(.*?)<\/(h[1|2|3])>/gi,"[$1]$2[/$3]");
			
			rep(/<sup>(.*?)<\/sup>/gi,"[sup]$1[/sup]");
			rep(/<sub>(.*?)<\/sub>/gi,"[sub]$1[/sub]");
			
			
			
			
// 			rep(/<font.*?color=\"(.*?)\".*?class=\"codeStyle\".*?>(.*?)<\/font>/gi,"[code][color=$1]$2[/color][/code]");
// 			rep(/<font.*?color=\"(.*?)\".*?class=\"quoteStyle\".*?>(.*?)<\/font>/gi,"[quote][color=$1]$2[/color][/quote]");
// 			rep(/<font.*?class=\"codeStyle\".*?color=\"(.*?)\".*?>(.*?)<\/font>/gi,"[code][color=$1]$2[/color][/code]");
// 			rep(/<font.*?class=\"quoteStyle\".*?color=\"(.*?)\".*?>(.*?)<\/font>/gi,"[quote][color=$1]$2[/color][/quote]");
// 			rep(/<span style=\"color: ?(.*?);\">(.*?)<\/span>/gi,"[color=$1]$2[/color]");
// // 			rep(/<font.*?color=\"(.*?)\".*?>(.*?)<\/font>/gi,"[color=$1]$2[/color]");
// 			rep(/<span style=\"font-size:(.*?);\">(.*?)<\/span>/gi,"[size=$1]$2[/size]");
// 			rep(/<font>(.*?)<\/font>/gi,"$1");
    
			s = s.replace(/<span class=\"snInfoBoxHead\".*?>(.+?)<\/span>/g,"[boxhead]$1[/boxhead]");

			while(s.match(/<span class=\"snInfoBox\" style=\"(.+?)\">(.+?)<\/span>/)){
			  var l = RegExp.$1; var c = RegExp.$2;
			  var b = l.indexOf("float: left") != -1 ? "infol":"infor";
			  var w = "";

			  if(l.match(/width:(.+?);.+/)){
			    var _cwn = parseInt(RegExp.$1);
			    if(!isNaN(_cwn)){
				w = "="+_cwn;
			    }
			  }

			  s = s.replace(/<span class=\"snInfoBox\" style=\"(.+?)\">(.+?)<\/span>/,"["+b+w+"]"+c+"[/"+b+"]");
			}


			

			rep(/<code>(.*?)<\/code>/gi,"[code]$1[/code]");
			rep(/<blockquote>(.*?)<\/blockquote>/gi,"[quote]$1[/quote]");
			rep(/<\/(strong|b)>/gi,"[/b]");
			rep(/<(strong|b)>/gi,"[b]");
			rep(/<\/(em|i)>/gi,"[/i]");
			rep(/<(em|i)>/gi,"[i]");
			rep(/<\/u>/gi,"[/u]");
			rep(/<span style=\"text-decoration: ?underline;\">(.*?)<\/span>/gi,"[u]$1[/u]");
			rep(/<u>/gi,"[u]");
			rep(/<blockquote[^>]*>/gi,"[quote]");
			rep(/<\/blockquote>/gi,"[/quote]");
			rep(/<br \/>/gi,"\n");
			rep(/<br\/>/gi,"\n");
			rep(/<br>/gi,"\n");
			rep(/<p(.*?)>/gi,"");
			rep(/<\/p>/gi,"\n");
			rep(/&nbsp;/gi," ");
			rep(/&quot;/gi,"\"");
			rep(/&lt;/gi,"<");
			rep(/&gt;/gi,">");
			rep(/&amp;/gi,"&");
		
			rep(/\[\/(tr|td|table)\]\n/gi,"[/$1]");
			rep(/\[(tr|table|td)\]\n/gi,"[$1]");


			return s; 
		},

		// BBCode -> HTML from PunBB dialect
		_punbb_bbcode2html : function(s) {
			s = tinymce.trim(s);
// alert("bbcode2html");
			function rep(re, str) {
				s = s.replace(re, str);
			};

			var img_path = (tinyMCE.activeEditor.getParam("snstuff_img_url_image") || "/articleimage/{artikelid}/").replace(/\{artikelid\}/g,parent.entryid);
			img_path = img_path.replace(/\{imgname\}/g,"");

			rep(/\[imgl=(.*?)\](.*?)\[\/imgl\]/gi,"<img class=\"noresize\" style=\"float:left;\" src=\""+img_path+"$2\" alt=\"$1\"/>");
			rep(/\[imgr=(.*?)\](.*?)\[\/imgr\]/gi,"<img class=\"noresize\" style=\"float:right;\" src=\""+img_path+"$2\" alt=\"$1\"/>");
			rep(/\[img=(.*?)\](.*?)\[\/img\]/gi,"<img class=\"noresize\" src=\""+img_path+"$2\" alt=\"$1\"/>");


			while(/(\[video=(.*?)\])/.test(s)){
				var _res = RegExp.$1;
				var _l = RegExp.$2.split(';');
				var _rpl = '<img class="snVideo" src="/editor_stuff/plugin/snstuff/images/trans.gif" title="'+_l[0]+'" width="'+parseInt(_l[1])+'" height="'+parseInt(_l[2])+'" ';
				_rpl += _l[3]?'alt="'+_l[3]+'" />':'/>';
				s = s.replace(_res,_rpl);
			}

                        // replace list bbcode
			var rl = new Array();
			  rl[0] = {f:/\[list\]/, o:"<ul style=\"list-style-type: disc;\">",c:"</ul>"};
			  rl[1] = {f:/\[list=d\]/, o:"<ul style=\"list-style-type: disc;\">",c:"</ul>"};
			  rl[2] = {f:/\[list=s\]/, o:"<ul style=\"list-style-type: square;\">",c:"</ul>"};
			  rl[3] = {f:/\[list=c\]/, o:"<ul style=\"list-style-type: circle;\">",c:"</ul>"};
			  
			  rl[4] = {f:/\[list=1\]/, o:"<ol style=\"list-style-type: decimal;\">",c:"</ol>"};
			  rl[5] = {f:/\[list=i\]/, o:"<ol style=\"list-style-type: lower-roman;\">",c:"</ol>"};
			  rl[6] = {f:/\[list=I\]/, o:"<ol style=\"list-style-type: upper-roman;\">",c:"</ol>"};
			  rl[7] = {f:/\[list=a\]/, o:"<ol style=\"list-style-type: lower-alpha;\">",c:"</ol>"};
			  rl[8] = {f:/\[list=A\]/, o:"<ol style=\"list-style-type: upper-alpha;\">",c:"</ol>"};

			  var cl = new Array();
			  do{
			    var p = s.search(/\[\/list\]/);
			    var re = "";
			    
			    for(var i = 0; i < rl.length; i++){
			      var np = s.search(rl[i].f);

			      if(np != -1 && p > np){
				  p = np;
				  cl[cl.length] = rl[i];
				  re = rl[i].f;
			      }
			    }
			    if(cl.length > 0){
			      if(re == ""){
				s = s.replace(/\[\/list\]/,cl.pop().c);
			      }else{
				s = s.replace(re, cl[cl.length-1].o);
			      }
			    }
			  }while(cl.length > 0 || s.search(/\[list.*?\]/) != -1);
			  

			s = s.replace(/\[\/\*\]/g,"</li>");
			s = s.replace(/\[\*\]/g,"<li>");


			s = s.replace(/\[infor\]/mg,"<span class=\"snInfoBox\" style=\"width:100px; float: right;\">");
			s = s.replace(/\[infol\]/mg,"<span class=\"snInfoBox\" style=\"width:100px; float: left;\">");
			
			s = s.replace(/\[infor=(.+?){0,1}\]/mg,"<span class=\"snInfoBox\" style=\"width:$1px; float: right;\">");
			s = s.replace(/\[infol=(.+?){0,1}\]/mg,"<span class=\"snInfoBox\" style=\"width:$1px; float: left;\">");
			
			s = s.replace(/\[\/(infor|infol)\]/g,"</span>");

			s = s.replace(/\[boxhead\]/g,"<span class=\"snInfoBoxHead\">");
			s = s.replace(/\[\/boxhead\]/g,"</span>");


			// example: [b] to <strong>
			
			rep(/\[b\]/gi,"<strong>");
			rep(/\[\/b\]/gi,"</strong>");
			rep(/\[i\]/gi,"<em>");
			rep(/\[\/i\]/gi,"</em>");
			rep(/\[u\]/gi,"<u>");
			rep(/\[\/u\]/gi,"</u>");
			rep(/\[url=([^\]]+)\](.*?)\[\/url\]/gi,"<a href=\"$1\" title=\"$1\">$2</a>");
			rep(/\[xurl=([^\]]+)\|(.*?)\](.*?)\[\/url\]/gi,"<a href=\"$1\" title=\"$2\">$3</a>");
			
			rep(/\[url\](.*?)\[\/url\]/gi,"<a href=\"$1\" title=\"$1\">$1</a>");
// 			rep(/\[img\](.*?)\[\/img\]/gi,"<img src=\"$1\" />");
			rep(/\[color=(.*?)\](.*?)\[\/color\]/gi,"<font color=\"$1\">$2</font>");
			rep(/\[code\](.*?)\[\/code\]/gi,"<code>$1</code>");
			rep(/\[quote.*?\](.*?)\[\/quote\]/gi,"<blockquote>$1</blockquote>");
			
			
			rep(/\[(h[1|2|3])\](.*?)\[\/(h[1|2|3])\]/gi,"<$1>$2</$3>");
			rep(/(<\/h[1|2|3]>)\n/gi,"$1");
			rep(/\n(<h[1|2|3]>)/gi,"$1");
			
			
			
			rep(/\[sup\](.*?)\[\/sup\]/gi,"<sup>$1</sup>");
			rep(/\[sub\](.*?)\[\/sub\]/gi,"<sub>$1</sub>");
			
/*
			rep(/\[\/(tr|td|table)\]/gi,"[/$1]");
			rep(/\[(tr|table)\]/gi,"[$1]");*/
			
			
			rep(/\n/gi,"<br />");
			

			
			return s; 
		}
	});

	// Register plugin
	tinymce.PluginManager.add('bbcode', tinymce.plugins.BBCodePlugin);
})();