bgneal@312: var ImageDialog = {
bgneal@312: 	preInit : function() {
bgneal@312: 		var url;
bgneal@312: 
bgneal@312: 		tinyMCEPopup.requireLangPack();
bgneal@312: 
bgneal@312: 		if (url = tinyMCEPopup.getParam("external_image_list_url"))
bgneal@312: 			document.write('<script language="javascript" type="text/javascript" src="' + tinyMCEPopup.editor.documentBaseURI.toAbsolute(url) + '"></script>');
bgneal@312: 	},
bgneal@312: 
bgneal@312: 	init : function(ed) {
bgneal@312: 		var f = document.forms[0], nl = f.elements, ed = tinyMCEPopup.editor, dom = ed.dom, n = ed.selection.getNode();
bgneal@312: 
bgneal@312: 		tinyMCEPopup.resizeToInnerSize();
bgneal@312: 		this.fillClassList('class_list');
bgneal@312: 		this.fillFileList('src_list', 'tinyMCEImageList');
bgneal@312: 		this.fillFileList('over_list', 'tinyMCEImageList');
bgneal@312: 		this.fillFileList('out_list', 'tinyMCEImageList');
bgneal@312: 		TinyMCE_EditableSelects.init();
bgneal@312: 
bgneal@312: 		if (n.nodeName == 'IMG') {
bgneal@312: 			nl.src.value = dom.getAttrib(n, 'src');
bgneal@312: 			nl.width.value = dom.getAttrib(n, 'width');
bgneal@312: 			nl.height.value = dom.getAttrib(n, 'height');
bgneal@312: 			nl.alt.value = dom.getAttrib(n, 'alt');
bgneal@312: 			nl.title.value = dom.getAttrib(n, 'title');
bgneal@312: 			nl.vspace.value = this.getAttrib(n, 'vspace');
bgneal@312: 			nl.hspace.value = this.getAttrib(n, 'hspace');
bgneal@312: 			nl.border.value = this.getAttrib(n, 'border');
bgneal@312: 			selectByValue(f, 'align', this.getAttrib(n, 'align'));
bgneal@312: 			selectByValue(f, 'class_list', dom.getAttrib(n, 'class'), true, true);
bgneal@312: 			nl.style.value = dom.getAttrib(n, 'style');
bgneal@312: 			nl.id.value = dom.getAttrib(n, 'id');
bgneal@312: 			nl.dir.value = dom.getAttrib(n, 'dir');
bgneal@312: 			nl.lang.value = dom.getAttrib(n, 'lang');
bgneal@312: 			nl.usemap.value = dom.getAttrib(n, 'usemap');
bgneal@312: 			nl.longdesc.value = dom.getAttrib(n, 'longdesc');
bgneal@312: 			nl.insert.value = ed.getLang('update');
bgneal@312: 
bgneal@312: 			if (/^\s*this.src\s*=\s*\'([^\']+)\';?\s*$/.test(dom.getAttrib(n, 'onmouseover')))
bgneal@312: 				nl.onmouseoversrc.value = dom.getAttrib(n, 'onmouseover').replace(/^\s*this.src\s*=\s*\'([^\']+)\';?\s*$/, '$1');
bgneal@312: 
bgneal@312: 			if (/^\s*this.src\s*=\s*\'([^\']+)\';?\s*$/.test(dom.getAttrib(n, 'onmouseout')))
bgneal@312: 				nl.onmouseoutsrc.value = dom.getAttrib(n, 'onmouseout').replace(/^\s*this.src\s*=\s*\'([^\']+)\';?\s*$/, '$1');
bgneal@312: 
bgneal@312: 			if (ed.settings.inline_styles) {
bgneal@312: 				// Move attribs to styles
bgneal@312: 				if (dom.getAttrib(n, 'align'))
bgneal@312: 					this.updateStyle('align');
bgneal@312: 
bgneal@312: 				if (dom.getAttrib(n, 'hspace'))
bgneal@312: 					this.updateStyle('hspace');
bgneal@312: 
bgneal@312: 				if (dom.getAttrib(n, 'border'))
bgneal@312: 					this.updateStyle('border');
bgneal@312: 
bgneal@312: 				if (dom.getAttrib(n, 'vspace'))
bgneal@312: 					this.updateStyle('vspace');
bgneal@312: 			}
bgneal@312: 		}
bgneal@312: 
bgneal@312: 		// Setup browse button
bgneal@312: 		document.getElementById('srcbrowsercontainer').innerHTML = getBrowserHTML('srcbrowser','src','image','theme_advanced_image');
bgneal@312: 		if (isVisible('srcbrowser'))
bgneal@312: 			document.getElementById('src').style.width = '260px';
bgneal@312: 
bgneal@312: 		// Setup browse button
bgneal@312: 		document.getElementById('onmouseoversrccontainer').innerHTML = getBrowserHTML('overbrowser','onmouseoversrc','image','theme_advanced_image');
bgneal@312: 		if (isVisible('overbrowser'))
bgneal@312: 			document.getElementById('onmouseoversrc').style.width = '260px';
bgneal@312: 
bgneal@312: 		// Setup browse button
bgneal@312: 		document.getElementById('onmouseoutsrccontainer').innerHTML = getBrowserHTML('outbrowser','onmouseoutsrc','image','theme_advanced_image');
bgneal@312: 		if (isVisible('outbrowser'))
bgneal@312: 			document.getElementById('onmouseoutsrc').style.width = '260px';
bgneal@312: 
bgneal@312: 		// If option enabled default contrain proportions to checked
bgneal@312: 		if (ed.getParam("advimage_constrain_proportions", true))
bgneal@312: 			f.constrain.checked = true;
bgneal@312: 
bgneal@312: 		// Check swap image if valid data
bgneal@312: 		if (nl.onmouseoversrc.value || nl.onmouseoutsrc.value)
bgneal@312: 			this.setSwapImage(true);
bgneal@312: 		else
bgneal@312: 			this.setSwapImage(false);
bgneal@312: 
bgneal@312: 		this.changeAppearance();
bgneal@312: 		this.showPreviewImage(nl.src.value, 1);
bgneal@312: 	},
bgneal@312: 
bgneal@312: 	insert : function(file, title) {
bgneal@312: 		var ed = tinyMCEPopup.editor, t = this, f = document.forms[0];
bgneal@312: 
bgneal@312: 		if (f.src.value === '') {
bgneal@312: 			if (ed.selection.getNode().nodeName == 'IMG') {
bgneal@312: 				ed.dom.remove(ed.selection.getNode());
bgneal@312: 				ed.execCommand('mceRepaint');
bgneal@312: 			}
bgneal@312: 
bgneal@312: 			tinyMCEPopup.close();
bgneal@312: 			return;
bgneal@312: 		}
bgneal@312: 
bgneal@312: 		if (tinyMCEPopup.getParam("accessibility_warnings", 1)) {
bgneal@312: 			if (!f.alt.value) {
bgneal@312: 				tinyMCEPopup.confirm(tinyMCEPopup.getLang('advimage_dlg.missing_alt'), function(s) {
bgneal@312: 					if (s)
bgneal@312: 						t.insertAndClose();
bgneal@312: 				});
bgneal@312: 
bgneal@312: 				return;
bgneal@312: 			}
bgneal@312: 		}
bgneal@312: 
bgneal@312: 		t.insertAndClose();
bgneal@312: 	},
bgneal@312: 
bgneal@312: 	insertAndClose : function() {
bgneal@312: 		var ed = tinyMCEPopup.editor, f = document.forms[0], nl = f.elements, v, args = {}, el;
bgneal@312: 
bgneal@312: 		tinyMCEPopup.restoreSelection();
bgneal@312: 
bgneal@312: 		// Fixes crash in Safari
bgneal@312: 		if (tinymce.isWebKit)
bgneal@312: 			ed.getWin().focus();
bgneal@312: 
bgneal@312: 		if (!ed.settings.inline_styles) {
bgneal@312: 			args = {
bgneal@312: 				vspace : nl.vspace.value,
bgneal@312: 				hspace : nl.hspace.value,
bgneal@312: 				border : nl.border.value,
bgneal@312: 				align : getSelectValue(f, 'align')
bgneal@312: 			};
bgneal@312: 		} else {
bgneal@312: 			// Remove deprecated values
bgneal@312: 			args = {
bgneal@312: 				vspace : '',
bgneal@312: 				hspace : '',
bgneal@312: 				border : '',
bgneal@312: 				align : ''
bgneal@312: 			};
bgneal@312: 		}
bgneal@312: 
bgneal@312: 		tinymce.extend(args, {
bgneal@442: 			src : nl.src.value.replace(/ /g, '%20'),
bgneal@312: 			width : nl.width.value,
bgneal@312: 			height : nl.height.value,
bgneal@312: 			alt : nl.alt.value,
bgneal@312: 			title : nl.title.value,
bgneal@312: 			'class' : getSelectValue(f, 'class_list'),
bgneal@312: 			style : nl.style.value,
bgneal@312: 			id : nl.id.value,
bgneal@312: 			dir : nl.dir.value,
bgneal@312: 			lang : nl.lang.value,
bgneal@312: 			usemap : nl.usemap.value,
bgneal@312: 			longdesc : nl.longdesc.value
bgneal@312: 		});
bgneal@312: 
bgneal@312: 		args.onmouseover = args.onmouseout = '';
bgneal@312: 
bgneal@312: 		if (f.onmousemovecheck.checked) {
bgneal@312: 			if (nl.onmouseoversrc.value)
bgneal@312: 				args.onmouseover = "this.src='" + nl.onmouseoversrc.value + "';";
bgneal@312: 
bgneal@312: 			if (nl.onmouseoutsrc.value)
bgneal@312: 				args.onmouseout = "this.src='" + nl.onmouseoutsrc.value + "';";
bgneal@312: 		}
bgneal@312: 
bgneal@312: 		el = ed.selection.getNode();
bgneal@312: 
bgneal@312: 		if (el && el.nodeName == 'IMG') {
bgneal@312: 			ed.dom.setAttribs(el, args);
bgneal@312: 		} else {
bgneal@312: 			ed.execCommand('mceInsertContent', false, '<img id="__mce_tmp" />', {skip_undo : 1});
bgneal@312: 			ed.dom.setAttribs('__mce_tmp', args);
bgneal@312: 			ed.dom.setAttrib('__mce_tmp', 'id', '');
bgneal@312: 			ed.undoManager.add();
bgneal@312: 		}
bgneal@312: 
bgneal@442: 		tinyMCEPopup.editor.execCommand('mceRepaint');
bgneal@442: 		tinyMCEPopup.editor.focus();
bgneal@312: 		tinyMCEPopup.close();
bgneal@312: 	},
bgneal@312: 
bgneal@312: 	getAttrib : function(e, at) {
bgneal@312: 		var ed = tinyMCEPopup.editor, dom = ed.dom, v, v2;
bgneal@312: 
bgneal@312: 		if (ed.settings.inline_styles) {
bgneal@312: 			switch (at) {
bgneal@312: 				case 'align':
bgneal@312: 					if (v = dom.getStyle(e, 'float'))
bgneal@312: 						return v;
bgneal@312: 
bgneal@312: 					if (v = dom.getStyle(e, 'vertical-align'))
bgneal@312: 						return v;
bgneal@312: 
bgneal@312: 					break;
bgneal@312: 
bgneal@312: 				case 'hspace':
bgneal@312: 					v = dom.getStyle(e, 'margin-left')
bgneal@312: 					v2 = dom.getStyle(e, 'margin-right');
bgneal@312: 
bgneal@312: 					if (v && v == v2)
bgneal@312: 						return parseInt(v.replace(/[^0-9]/g, ''));
bgneal@312: 
bgneal@312: 					break;
bgneal@312: 
bgneal@312: 				case 'vspace':
bgneal@312: 					v = dom.getStyle(e, 'margin-top')
bgneal@312: 					v2 = dom.getStyle(e, 'margin-bottom');
bgneal@312: 					if (v && v == v2)
bgneal@312: 						return parseInt(v.replace(/[^0-9]/g, ''));
bgneal@312: 
bgneal@312: 					break;
bgneal@312: 
bgneal@312: 				case 'border':
bgneal@312: 					v = 0;
bgneal@312: 
bgneal@312: 					tinymce.each(['top', 'right', 'bottom', 'left'], function(sv) {
bgneal@312: 						sv = dom.getStyle(e, 'border-' + sv + '-width');
bgneal@312: 
bgneal@312: 						// False or not the same as prev
bgneal@312: 						if (!sv || (sv != v && v !== 0)) {
bgneal@312: 							v = 0;
bgneal@312: 							return false;
bgneal@312: 						}
bgneal@312: 
bgneal@312: 						if (sv)
bgneal@312: 							v = sv;
bgneal@312: 					});
bgneal@312: 
bgneal@312: 					if (v)
bgneal@312: 						return parseInt(v.replace(/[^0-9]/g, ''));
bgneal@312: 
bgneal@312: 					break;
bgneal@312: 			}
bgneal@312: 		}
bgneal@312: 
bgneal@312: 		if (v = dom.getAttrib(e, at))
bgneal@312: 			return v;
bgneal@312: 
bgneal@312: 		return '';
bgneal@312: 	},
bgneal@312: 
bgneal@312: 	setSwapImage : function(st) {
bgneal@312: 		var f = document.forms[0];
bgneal@312: 
bgneal@312: 		f.onmousemovecheck.checked = st;
bgneal@312: 		setBrowserDisabled('overbrowser', !st);
bgneal@312: 		setBrowserDisabled('outbrowser', !st);
bgneal@312: 
bgneal@312: 		if (f.over_list)
bgneal@312: 			f.over_list.disabled = !st;
bgneal@312: 
bgneal@312: 		if (f.out_list)
bgneal@312: 			f.out_list.disabled = !st;
bgneal@312: 
bgneal@312: 		f.onmouseoversrc.disabled = !st;
bgneal@312: 		f.onmouseoutsrc.disabled  = !st;
bgneal@312: 	},
bgneal@312: 
bgneal@312: 	fillClassList : function(id) {
bgneal@312: 		var dom = tinyMCEPopup.dom, lst = dom.get(id), v, cl;
bgneal@312: 
bgneal@312: 		if (v = tinyMCEPopup.getParam('theme_advanced_styles')) {
bgneal@312: 			cl = [];
bgneal@312: 
bgneal@312: 			tinymce.each(v.split(';'), function(v) {
bgneal@312: 				var p = v.split('=');
bgneal@312: 
bgneal@312: 				cl.push({'title' : p[0], 'class' : p[1]});
bgneal@312: 			});
bgneal@312: 		} else
bgneal@312: 			cl = tinyMCEPopup.editor.dom.getClasses();
bgneal@312: 
bgneal@312: 		if (cl.length > 0) {
bgneal@312: 			lst.options.length = 0;
bgneal@312: 			lst.options[lst.options.length] = new Option(tinyMCEPopup.getLang('not_set'), '');
bgneal@312: 
bgneal@312: 			tinymce.each(cl, function(o) {
bgneal@312: 				lst.options[lst.options.length] = new Option(o.title || o['class'], o['class']);
bgneal@312: 			});
bgneal@312: 		} else
bgneal@312: 			dom.remove(dom.getParent(id, 'tr'));
bgneal@312: 	},
bgneal@312: 
bgneal@312: 	fillFileList : function(id, l) {
bgneal@312: 		var dom = tinyMCEPopup.dom, lst = dom.get(id), v, cl;
bgneal@312: 
bgneal@312: 		l = window[l];
bgneal@312: 		lst.options.length = 0;
bgneal@312: 
bgneal@312: 		if (l && l.length > 0) {
bgneal@312: 			lst.options[lst.options.length] = new Option('', '');
bgneal@312: 
bgneal@312: 			tinymce.each(l, function(o) {
bgneal@312: 				lst.options[lst.options.length] = new Option(o[0], o[1]);
bgneal@312: 			});
bgneal@312: 		} else
bgneal@312: 			dom.remove(dom.getParent(id, 'tr'));
bgneal@312: 	},
bgneal@312: 
bgneal@312: 	resetImageData : function() {
bgneal@312: 		var f = document.forms[0];
bgneal@312: 
bgneal@312: 		f.elements.width.value = f.elements.height.value = '';
bgneal@312: 	},
bgneal@312: 
bgneal@312: 	updateImageData : function(img, st) {
bgneal@312: 		var f = document.forms[0];
bgneal@312: 
bgneal@312: 		if (!st) {
bgneal@312: 			f.elements.width.value = img.width;
bgneal@312: 			f.elements.height.value = img.height;
bgneal@312: 		}
bgneal@312: 
bgneal@312: 		this.preloadImg = img;
bgneal@312: 	},
bgneal@312: 
bgneal@312: 	changeAppearance : function() {
bgneal@312: 		var ed = tinyMCEPopup.editor, f = document.forms[0], img = document.getElementById('alignSampleImg');
bgneal@312: 
bgneal@312: 		if (img) {
bgneal@312: 			if (ed.getParam('inline_styles')) {
bgneal@312: 				ed.dom.setAttrib(img, 'style', f.style.value);
bgneal@312: 			} else {
bgneal@312: 				img.align = f.align.value;
bgneal@312: 				img.border = f.border.value;
bgneal@312: 				img.hspace = f.hspace.value;
bgneal@312: 				img.vspace = f.vspace.value;
bgneal@312: 			}
bgneal@312: 		}
bgneal@312: 	},
bgneal@312: 
bgneal@312: 	changeHeight : function() {
bgneal@312: 		var f = document.forms[0], tp, t = this;
bgneal@312: 
bgneal@312: 		if (!f.constrain.checked || !t.preloadImg) {
bgneal@312: 			return;
bgneal@312: 		}
bgneal@312: 
bgneal@312: 		if (f.width.value == "" || f.height.value == "")
bgneal@312: 			return;
bgneal@312: 
bgneal@312: 		tp = (parseInt(f.width.value) / parseInt(t.preloadImg.width)) * t.preloadImg.height;
bgneal@312: 		f.height.value = tp.toFixed(0);
bgneal@312: 	},
bgneal@312: 
bgneal@312: 	changeWidth : function() {
bgneal@312: 		var f = document.forms[0], tp, t = this;
bgneal@312: 
bgneal@312: 		if (!f.constrain.checked || !t.preloadImg) {
bgneal@312: 			return;
bgneal@312: 		}
bgneal@312: 
bgneal@312: 		if (f.width.value == "" || f.height.value == "")
bgneal@312: 			return;
bgneal@312: 
bgneal@312: 		tp = (parseInt(f.height.value) / parseInt(t.preloadImg.height)) * t.preloadImg.width;
bgneal@312: 		f.width.value = tp.toFixed(0);
bgneal@312: 	},
bgneal@312: 
bgneal@312: 	updateStyle : function(ty) {
bgneal@442: 		var dom = tinyMCEPopup.dom, b, bStyle, bColor, v, isIE = tinymce.isIE, f = document.forms[0], img = dom.create('img', {style : dom.get('style').value});
bgneal@312: 
bgneal@312: 		if (tinyMCEPopup.editor.settings.inline_styles) {
bgneal@312: 			// Handle align
bgneal@312: 			if (ty == 'align') {
bgneal@312: 				dom.setStyle(img, 'float', '');
bgneal@312: 				dom.setStyle(img, 'vertical-align', '');
bgneal@312: 
bgneal@312: 				v = getSelectValue(f, 'align');
bgneal@312: 				if (v) {
bgneal@312: 					if (v == 'left' || v == 'right')
bgneal@312: 						dom.setStyle(img, 'float', v);
bgneal@312: 					else
bgneal@312: 						img.style.verticalAlign = v;
bgneal@312: 				}
bgneal@312: 			}
bgneal@312: 
bgneal@312: 			// Handle border
bgneal@312: 			if (ty == 'border') {
bgneal@442: 				b = img.style.border ? img.style.border.split(' ') : [];
bgneal@442: 				bStyle = dom.getStyle(img, 'border-style');
bgneal@442: 				bColor = dom.getStyle(img, 'border-color');
bgneal@442: 
bgneal@312: 				dom.setStyle(img, 'border', '');
bgneal@312: 
bgneal@312: 				v = f.border.value;
bgneal@312: 				if (v || v == '0') {
bgneal@312: 					if (v == '0')
bgneal@442: 						img.style.border = isIE ? '0' : '0 none none';
bgneal@442: 					else {
bgneal@442: 						if (b.length == 3 && b[isIE ? 2 : 1])
bgneal@442: 							bStyle = b[isIE ? 2 : 1];
bgneal@442: 						else if (!bStyle || bStyle == 'none')
bgneal@442: 							bStyle = 'solid';
bgneal@442: 						if (b.length == 3 && b[isIE ? 0 : 2])
bgneal@442: 							bColor = b[isIE ? 0 : 2];
bgneal@442: 						else if (!bColor || bColor == 'none')
bgneal@442: 							bColor = 'black';
bgneal@442: 						img.style.border = v + 'px ' + bStyle + ' ' + bColor;
bgneal@442: 					}
bgneal@312: 				}
bgneal@312: 			}
bgneal@312: 
bgneal@312: 			// Handle hspace
bgneal@312: 			if (ty == 'hspace') {
bgneal@312: 				dom.setStyle(img, 'marginLeft', '');
bgneal@312: 				dom.setStyle(img, 'marginRight', '');
bgneal@312: 
bgneal@312: 				v = f.hspace.value;
bgneal@312: 				if (v) {
bgneal@312: 					img.style.marginLeft = v + 'px';
bgneal@312: 					img.style.marginRight = v + 'px';
bgneal@312: 				}
bgneal@312: 			}
bgneal@312: 
bgneal@312: 			// Handle vspace
bgneal@312: 			if (ty == 'vspace') {
bgneal@312: 				dom.setStyle(img, 'marginTop', '');
bgneal@312: 				dom.setStyle(img, 'marginBottom', '');
bgneal@312: 
bgneal@312: 				v = f.vspace.value;
bgneal@312: 				if (v) {
bgneal@312: 					img.style.marginTop = v + 'px';
bgneal@312: 					img.style.marginBottom = v + 'px';
bgneal@312: 				}
bgneal@312: 			}
bgneal@312: 
bgneal@312: 			// Merge
bgneal@312: 			dom.get('style').value = dom.serializeStyle(dom.parseStyle(img.style.cssText), 'img');
bgneal@312: 		}
bgneal@312: 	},
bgneal@312: 
bgneal@312: 	changeMouseMove : function() {
bgneal@312: 	},
bgneal@312: 
bgneal@312: 	showPreviewImage : function(u, st) {
bgneal@312: 		if (!u) {
bgneal@312: 			tinyMCEPopup.dom.setHTML('prev', '');
bgneal@312: 			return;
bgneal@312: 		}
bgneal@312: 
bgneal@312: 		if (!st && tinyMCEPopup.getParam("advimage_update_dimensions_onchange", true))
bgneal@312: 			this.resetImageData();
bgneal@312: 
bgneal@312: 		u = tinyMCEPopup.editor.documentBaseURI.toAbsolute(u);
bgneal@312: 
bgneal@312: 		if (!st)
bgneal@312: 			tinyMCEPopup.dom.setHTML('prev', '<img id="previewImg" src="' + u + '" border="0" onload="ImageDialog.updateImageData(this);" onerror="ImageDialog.resetImageData();" />');
bgneal@312: 		else
bgneal@312: 			tinyMCEPopup.dom.setHTML('prev', '<img id="previewImg" src="' + u + '" border="0" onload="ImageDialog.updateImageData(this, 1);" />');
bgneal@312: 	}
bgneal@312: };
bgneal@312: 
bgneal@312: ImageDialog.preInit();
bgneal@312: tinyMCEPopup.onInit.add(ImageDialog.init, ImageDialog);