(function() {
	window.mcImageManager = {
		callSettings : null,
		args : null,
//		baseURL : null,
		settings : {
			document_base_url : null,
			relative_urls : false,
			remove_script_host : false,
			use_url_path : true,
			path : null,
			rootpath : null,
			remember_last_path : 'auto',
			custom_data : null,
			target_elements : '',
			target_form : '',
			handle : 'image,media'
		},
		setup : function() {
			var t = this, o, d = document, cp = [];

			// Find document_base_url
			o = d.location.href;

			if (o.indexOf('?') != -1)
				o = o.substring(0, o.indexOf('?'));

			o = o.substring(0, o.lastIndexOf('/') + 1);

			t.settings.default_base_url = unescape(o);

			// Find script base URL
			function get(nl) {
				var i, n;

				for (i=0; i<nl.length; i++) {
					n = nl[i];

					cp.push(n);

					if (n.src && /mcimagemanager\.js/g.test(n.src))
						return n.src.substring(0, n.src.lastIndexOf('/'));
				}
			};

			o = d.documentElement;
			if (o && (o = get(o.getElementsByTagName('script'))))
				return t.baseURL = o;

			o = d.getElementsByTagName('script');
			if (o && (o = get(o)))
				return t.baseURL = o;

			o = d.getElementsByTagName('head')[0];
			if (o && (o = get(o.getElementsByTagName('script'))))
				return t.baseURL = o;
		},
		relaxDomain : function() {
			var t = this, p = /(http|https):\/\/([^\/:]+)\/?/.exec(t.baseURL);

			// Use tinymce relaxed domain
			if (window.tinymce && tinymce.relaxedDomain && tinymce.relaxedDomain != document.location.hostname) {
				t.relaxedDomain = tinymce.relaxedDomain;
				return;
			}

			// Relax domain
			if (p && p[2] != document.location.hostname)
				document.domain = t.relaxedDomain = p[2].replace(/.*\.(.+\..+)$/, '$1');
		},

		init : function(s) {
			this.settings = this.merge(s, this.settings);
		},

		preInit : function() {
			var baseDir = document.location.href;

			if (baseDir.indexOf('?') != -1)
				baseDir = baseDir.substring(0, baseDir.indexOf('?'));

			baseDir = baseDir.substring(0, baseDir.lastIndexOf('/') + 1);

			this.settings.document_base_url = unescape(baseDir);
		},

		merge : function(s, a) {
			var n, o = {};

			for (n in a)
				o[n] = a[n];

			if (s) {
				for (n in s)
					o[n] = s[n];
			}

			return o;
		},
		browse : function(s) {
			var t = this;

			s = s || {};

			if (s.fields) {
				s.oninsert = function(o) {
					t.each(s.fields.replace(/\s+/g, '').split(/,/), function(v) {
						var n;

						if (n = document.getElementById(v))
							t._setVal(n, o.focusedFile.url);
					});
				};
			}

			this.openWin({page : 'index.html', scrollbars : 'yes'}, s);
		},
		
		edit : function(s) {
			this.openWin({page : 'edit.html', width : 800, height : 500}, s);
		},

		upload : function(s) {
			this.openWin({page : 'upload.html', width : 550, height : 350}, s);
		},

		view : function(s) {
			this.openWin({page : 'view.html', width : 800, height : 500}, s);
		},

		createDir : function(s) {
			this.openWin({page : 'createdir.html', width : 450, height : 280}, s);
		},

		openWin : function(f, a) {
			var t = this, w, v;

			t.windowArgs = a = t.extend({}, t.settings, a);
			f = t.extend({
				x : -1,
				y : -1,
				width : 800,
				height : 500,
				inline : 1
			}, f);

			if (f.x == -1)
				f.x = parseInt(screen.width / 2.0) - (f.width / 2.0);

			if (f.y == -1)
				f.y = parseInt(screen.height / 2.0) - (f.height / 2.0);

			if (f.page)
//				f.url = t.baseURL + '/../tinymce31/jscripts/tiny_mce/plugins/imagemanager/pages/im/index.html?type=im&page=' + f.page;
				f.url = t.baseURL + '/../index.php?type=im&page=' + f.page;

			if (a.session_id)
				f.url += '&sessionid=' + a.session_id;

			if (a.custom_data)
				f.url += '&custom_data=' + escape(a.custom_data);

			if (t.relaxedDomain)
				f.url += '&domain=' + escape(t.relaxedDomain);

			// Open in specified frame
			if (a.target_frame) {
				if (v = frames[a.target_frame])
					v.document.location = f.url;

				if (v = document.getElementById(a.target_frame))
					v.src = f.url;

				return;
			}

			// Use TinyMCE window API
			if (window.tinymce && tinyMCE.activeEditor)
				return tinyMCE.activeEditor.windowManager.open(f, a);

			// Use jQuery WindowManager
			if (window.jQuery && jQuery.WindowManager)
				return jQuery.WindowManager.open(f, a);

			// Use native dialogs
			w = window.open(f.url, 'mcImageManagerWin', 'left=' + f.x + 
				',top=' + f.y + ',width=' + f.width + ',height=' + 
				f.height + ',scrollbars=' + (f.scrollbars ? 'yes' : 'no') + 
				',resizable=' + (f.resizable ? 'yes' : 'no') + 
				',statusbar=' + (f.statusbar ? 'yes' : 'no')
			);

			try {
				w.focus();
			} catch (ex) {
				// Ignore
			}
		},
		each : function(o, f, s) {
			var n, l;

			if (o) {
				s = s || o;

				if (o.length !== undefined) {
					for (n = 0, l = o.length; n < l; n++)
						f.call(s, o[n], n, o);
				} else {
					for (n in o) {
						if (o.hasOwnProperty(n))
							f.call(s, o[n], n, o);
					}
				}
			}
		},

		extend : function() {
			var k, a = arguments, t = a[0], i, v;

			for (i = 1; i < a.length; i++) {
				if (v = a[i]) {
					for (k in v)
						t[k] = v[k];
				}
			}

			return t;
		},

		// Legacy functions
		open : function(fn, en, url, cb, s) {
			var t = this, el;

			s = s || {};

			// Use input value if it was found
			if (!s.url && document.forms[fn] && (el = document.forms[fn].elements[en.split(',')[0]]))
				s.url = el.value;

			if (!cb) {
				s.oninsert = function(o) {
					var e, i, v, f = o.focusedFile;

					v = en.replace(/\s+/g, '').split(',');

					for (i = 0; i < v.length; i++) {
						if (e = document.forms[fn][v[i]])
							t._setVal(e, f.url);
					}
				};
			} else {
				if (typeof(cb) == 'string')
					cb = window[cb];

				s.oninsert = function(o) {
					cb(o.focusedFile.url, o);
				};
			}

			t.browse(s);
		},


		filebrowserCallBack : function(field_name, url, type, win, asked) {
			var s, i, hl, fo;

			function getParam(n, d) {
				if (tinyMCE.getParam)
					return tinyMCE.getParam(n, d);
				else
					return tinyMCE.activeEditor.getParam(n, d);
			};

			// Is file manager included, ask it first
			if (window.mcFileManager && !asked) {
				hl = mcFileManager.settings.handle;

				hl = hl.split(',');
				for (i=0; i<hl.length; i++) {
					if (type == hl[i])
						fo = 1;
				}

				if (fo && mcFileManager.filebrowserCallBack(field_name, url, type, win, 1))
					return;
			}

			// Save away
			this.field = field_name;
			this.callerWindow = win;
			this.inTinyMCE = true;

			// Setup instance specific settings
			s = {
				path : getParam("imagemanager_path"),
				rootpath : getParam("imagemanager_rootpath"),
				remember_last_path : getParam("imagemanager_remember_last_path"),
				custom_data : getParam("imagemanager_custom_data"),
				insert_filter : getParam("imagemanager_insert_filter"),
				document_base_url : getParam("document_base_url"),
				is_callback : true
			};

			// Open browser
			this.open(0, field_name, url, function(url, info) {
				mcImageManager.insertFileToTinyMCE(url, info);
			}, s);

			return true;
		},

		insertFileToTinyMCE : function(url, info) {
			var url, f = this.callerWindow.document.forms[0], names = ['alt', 'title', 'linktitle'], i;

			// Handle old and new style
			if (typeof(TinyMCE_convertURL) != "undefined")
				url = TinyMCE_convertURL(url, null, true);
			else if (tinyMCE.convertURL)
				url = tinyMCE.convertURL(url, null, true);
			else
				url = tinyMCE.activeEditor.convertURL(url, null, true);

			// Set URL
			f.elements[this.field].value = url;

			// Set alt and title info
			if (info.custom && info.custom.description) {
				for (i=0; i<names.length; i++) {
					if (f.elements[names[i]])
						f.elements[names[i]].value = info.custom.description;
				}
			}

			// Try to fire the onchange event
			try {
				f.elements[this.field].onchange();
			} catch (e) {
				// Skip it
			}
		},

		selectFile : function(d) {
			this.insertFileToForm(d);
		},

		insertFileToForm : function(d) {
			var t = this, s = this.merge(t.callSettings, t.settings), elements, i, elm, base, f, url;

			if (s.insert_filter)
				s.insert_filter(d);

			url = d.url;

			// Convert to relative
			if (s.relative_urls) {
				url = this.parseURL(url);
				base = this.parseURL(s.document_base_url);
				url = this.absToRel(base.path, url.path) + (url.query ? "?" + url.query : '') + (url.hash ? "#" + url.hash : '');
			}

			// Remove proto and host
			if (s.remove_script_host)
				url = this.removeHost(url);

			if (s.insert_callback) {
				s.insert_callback(url, d);
				return;
			}

			// Set URL to all form fields
			if (s.target_elements) {
				elements = s.target_elements.split(',');

				for (i=0; i<elements.length; i++) {
					f = document.forms[s.target_form];

					if (f)
						elm = f.elements[elements[i]];
					else
						elm = document.getElementById(elements[i]);

					if (!elm)
						continue;

					if (elm && typeof elm != "undefined")
						elm.value = url;

					// Try to fire the onchange event
					try {
						elm.onchange();
					} catch (e) {
						// Skip it
					}
				}
			}
		},

		removeHost : function(url) {
			return url.replace(/^(\w+:\/\/)([^\/]+)/, '');
		},

		parseURL : function(u, b) {
			var l = document.location;

			// Force absolute
			if (u.indexOf('://') == -1) {
				if (!b)
					b = l.pathname;

				if (u.charAt(0) != '/')
					u = this.relToAbs(b.replace(/\/[^\/]+$/, ''), u);

				u = l.protocol + '//' + l.host + (l.port ? ':' + l.port : '') + u;
			}

			u = /^((\w+):\/\/)?((\w+):?(\w+)?@)?([^\/\?:]+):?(\d+)?(\/?[^\?#]+)?\??([^#]+)?#?(\w*)/.exec(u);

			return {
				protocol : u[2],
				username : u[3],
				password : u[4],
				host : u[5],
				port : u[6],
				domain : u[7],
				path : u[8],
				query : u[9],
				hash : u[10]
			};
		},

		absToRel : function(base_url, url_to_relative) {
			var strTok1, strTok2, breakPoint = 0, outputString = "", i;

			// Crop away last path part
			base_url = base_url.substring(0, base_url.lastIndexOf('/'));
			strTok1 = base_url.split('/');
			strTok2 = url_to_relative.split('/');

			if (strTok1.length >= strTok2.length) {
				for (i=0; i<strTok1.length; i++) {
					if (i >= strTok2.length || strTok1[i] != strTok2[i]) {
						breakPoint = i + 1;
						break;
					}
				}
			}

			if (strTok1.length < strTok2.length) {
				for (i=0; i<strTok2.length; i++) {
					if (i >= strTok1.length || strTok1[i] != strTok2[i]) {
						breakPoint = i + 1;
						break;
					}
				}
			}

			if (breakPoint == 1)
				return url_to_relative;

			for (i=0; i<(strTok1.length-(breakPoint-1)); i++)
				outputString += "../";

			for (i=breakPoint-1; i<strTok2.length; i++) {
				if (i != (breakPoint-1))
					outputString += "/" + strTok2[i];
				else
					outputString += strTok2[i];
			}

			return outputString;
		},

		relToAbs : function(base_path, rel_path) {
			var end = '', i, newBaseURLParts = [], newRelURLParts = [], numBack, len, absPath;

			rel_path = rel_path.replace(/[?#].*$/, function(a) {
				end = a;

				return '';
			});

			// Split parts
			baseURLParts = base_path.split('/');
			relURLParts = rel_path.split('/');

			// Remove empty chunks
			for (i=baseURLParts.length-1; i>=0; i--) {
				if (baseURLParts[i].length == 0)
					continue;

				newBaseURLParts[newBaseURLParts.length] = baseURLParts[i];
			}

			baseURLParts = newBaseURLParts.reverse();

			// Merge relURLParts chunks
			numBack = 0;
			for (i=relURLParts.length-1; i>=0; i--) {
				if (relURLParts[i].length == 0 || relURLParts[i] == ".")
					continue;

				if (relURLParts[i] == '..') {
					numBack++;
					continue;
				}

				if (numBack > 0) {
					numBack--;
					continue;
				}

				newRelURLParts[newRelURLParts.length] = relURLParts[i];
			}

			relURLParts = newRelURLParts.reverse();

			// Remove end from absolute path
			len = baseURLParts.length-numBack;
			absPath = (len <= 0 ? "" : "/") + baseURLParts.slice(0, len).join('/') + "/" + relURLParts.join('/');

			return absPath + end;
		},

		getArgs : function() {
			return this.args;
		},

		/**#@-*/

		openInIframe : function(iframe_id, form_name, element_names, file_url, js, s) {
			this.settings.iframe = iframe_id;
			this.open(form_name, element_names, file_url, js, s);
		},

		open : function(form_name, element_names, file_url, js, s) {
			var x, y, w, h, win, val, pa = '';

			s = this.merge(s, this.settings);

			w = 795;
			h = 500;
			x = parseInt(screen.width / 2.0) - (w / 2.0);
			y = parseInt(screen.height / 2.0) - (h / 2.0);

			if (document.attachEvent) {
				// Pesky MSIE + XP SP2
				w += 15;
				h += 35;
			}

			this.args = {
				path : s.path,
				rootpath : s.rootpath,
				custom_data : s.custom_data,
				remember_last_path : s.remember_last_path
			};

			if (((form_name && element_names) || file_url) && s.use_url_path) {
				val = file_url ? file_url : document.forms[form_name].elements[element_names.split(',')[0]].value;

				if (val)
					this.args.url = this.parseURL(val, this.parseURL(s.document_base_url).path).path;
			}

			s.target_form = form_name;
			s.target_elements = element_names;
			s.insert_callback = typeof(js) == 'function' ? js : eval(js);

			if (s.session_id)
				pa += '&sessionid=' + s.session_id;

			if (s.custom_data)
				pa += '&custom_data=' + escape(s.custom_data);

			this.callSettings = s;
			if (window.mcFileManager)
				mcFileManager.callSettings = s;

			if (s.iframe) {
				win = document.getElementById(s.iframe);
				win.contentWindow.document.location = this.getBaseURL() + '/../index.php?type=im';
				return;
			} else if (window.tinymce && tinyMCE.activeEditor)
				tinyMCE.activeEditor.windowManager.open({url : this.getBaseURL() + '/../index.php?type=im' + pa, width : w, height : h, resizable : 1, scrollbars : 1, statusbar : 0, inline : 1});
			else
				win = window.open(this.getBaseURL() + '/../index.php?type=im' + pa, 'mcImageManagerWin', 'left=' + x + ',top=' + y + ',width=' + w + ',height=' + h + ',scrollbars=yes,resizable=yes,statusbar=no');
		},

		getBaseURL : function() {
			if (this.baseURL)
				return this.baseURL;

			// If loaded using TinyMCE 3.x and XHR requests
			if (window.tinymce && tinymce.PluginManager && tinymce.PluginManager.urls['imagemanager'])
				return this.baseURL = tinymce.PluginManager.urls['imagemanager'] + '/js';

			return this.baseURL = this.findBaseURL(/mcimagemanager\.js/g);
		},

		findBaseURL : function(k) {
			var o, d = document;

			function get(nl) {
				var i, n;

				for (i=0; i<nl.length; i++) {
					n = nl[i];

					if (n.src && k.test(n.src))
						return n.src.substring(0, n.src.lastIndexOf('/'));
				}
			};

			o = d.documentElement;
			if (o && (o = get(o.getElementsByTagName('script'))))
				return o;

			o = d.getElementsByTagName('script');
			if (o && (o = get(o)))
				return o;

			o = d.getElementsByTagName('head')[0];
			if (o && (o = get(o.getElementsByTagName('script'))))
				return o;

			return null;
		}
	};
	mcImageManager.setup();
	mcImageManager.relaxDomain();
	mcImageManager.preInit();
})();