/**
 * $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.PluginManager.requireLangPack('magnify_publisher');
	
	function makeOpaque(bool) {	
		var hh = document.getElementsByTagName('head')[0];
		var removeOpacity = document.getElementById('removeOpacity');
		if ( bool ) {
			var ss = document.createElement('style');
			ss.id = "removeOpacity";
			var def = 'html, body, div, span, applet, object, iframe, ' +
			'h1, h2, h3, h4, h5, h6, p, blockquote, pre, ' +
			'a, abbr, acronym, address, big, cite, code, ' +
			'del, dfn, em, font, img, ins, kbd, q, s, samp, ' +
			'small, strike, strong, sub, sup, tt, var, ' +
			'dl, dt, dd, ol, ul, li, ' +
			'fieldset, form, label, legend, ' +
			'table, caption, tbody, tfoot, thead, tr, th, td, .defaultSkin .mceButtonDisabled .icon{opacity:1.0; filter:alpha(opacity=100)}';
			ss.setAttribute("type", "text/css");
			if (ss.styleSheet) {   // IE
				ss.styleSheet.cssText = def;
			} else {                // the world
				var tt = document.createTextNode(def);
				ss.appendChild(tt);
			}
			hh.appendChild(ss);
		} else {
			if ( removeOpacity ) {
				hh.removeChild(removeOpacity);
			}
		}
	}
	
	tinymce.create('tinymce.plugins.MagnifyPublisher', {
	
		init : function(ed, url) {
			var t = this;

			t.editor = ed;
		
			ed.addCommand('mceMagnifyPublisher', function() {
				makeOpaque(true);
				ed.windowManager.open({
					file : url + '/publisher.html',
					width : 650 + parseInt(ed.getLang('magnify_publisher.delta_width', 0)),
					height : 550 + parseInt(ed.getLang('magnify_publisher.delta_height', 0)),
					inline : 1
				}, {
					plugin_url : url,
					magnify_publisher_url : ed.getParam('magnify_publisher_url'),
					magnify_publisher_iframe : t.mvp_iframe
				});
			});
		
			// Register button
			ed.addButton('magnify_publisher', {
				title : 'magnify_publisher.desc',
				cmd : 'mceMagnifyPublisher',
				image : url + '/img/embedvideo_icon.png'
			});
			
			ed.onBeforeSetContent.add(t._setContent, t);
			
			
			ed.onBeforeSetContent.add(function(ed, o) {
				var iframeRegex = /(<iframe [^>]*?)\brel="([^">]*?img[^">]+?)"([^>]*?(?:\/>|>\s*<\/iframe>))/ig;
				o.content = o.content.replace(iframeRegex, t._convert_to_img);
			});

			ed.onPostProcess.add(function(ed, o) {
				if (o.get)
					var imgRegex = /(<img [^>]*?)\brel="([^">]*?iframe[^">]+?)"([^>]*?>)/ig;
					o.content = o.content.replace(imgRegex, t._convert_to_iframe);
			});
		},
					
		getInfo : function() {
			return {
				longname : 'Magnify Publisher plugin',
				author : 'Magnify.net',
				authorurl : 'http://www.magnify.net',
				infourl : 'http://www.magnify.net',
				version : "1.0"
			};
		},
		
		_setContent : function(ed, o) {
			var t = this, sp, ep, c = o.content;
			var callback_url = ed.baseURI.toAbsolute('plugins/magnify_publisher/embed.html');
			t.mvp_iframe = '<iframe width="650" height="550" frameborder="0" scrolling="no" marginwidth="0" marginheight="0" src="' + ed.getParam('magnify_publisher_url') + '/embed/publisher?callback=' + callback_url + '&callback_type=GetFrame"></iframe>'
		},
		
		
		_convert_to_iframe : function(input, m1, m2, m3) {
			var iframe = decodeURIComponent(m2);
			var placeholder = encodeURIComponent(m1 + m3);
			var newIframe = iframe.replace(/(<iframe)/i, "$1" + " rel=\"" + placeholder + "\"" );
			return newIframe;
		},
		
		_convert_to_img : function(input, m1, m2, m3) {
			var image = decodeURIComponent(m2);
			var placeholder = encodeURIComponent(m1 + m3);
			var newImg = image.replace(/([<]img)/i, "$1" + " rel=\"" + placeholder + "\"" );
			return newImg;
		}

		
	});

	// Register plugin
	tinymce.PluginManager.add('magnify_publisher', tinymce.plugins.MagnifyPublisher);
})();