/**
 * $Id: tinymce.js 464 2007-11-30 15:21:14Z spocke $
 *
 * @author Moxiecode
 * @copyright Copyright © 2004-2007, Moxiecode Systems AB, All rights reserved.
 */

if (!window.mce)
	window.mce = {};

mce.Loader = {
	init : function() {
		var d = document, t = this, ua, ti;

		// Make sure that the window is focused
		window.focus();

		// Browser check
		ua = navigator.userAgent;
		t.isOpera = window.opera && opera.buildNumber;
		t.isWebKit = /WebKit/.test(ua);
		t.isOldWebKit = t.isWebKit && !window.getSelection().getRangeAt;
		t.isIE = !t.isWebKit && !t.isOpera && (/MSIE/gi).test(ua) && (/Explorer/gi).test(navigator.appName);
		t.isIE6 = t.isIE && /MSIE [56]/.test(ua);
		t.isGecko = !t.isWebKit && /Gecko/.test(ua);
		t.isMac = ua.indexOf('Mac') != -1;
		t.isCMS = !!top.mce.ContentManager;
		t.count = 0;

		// Handle DOM content loaded
		if (t.isCMS) {
			if (t.isIE && d.location.protocol != 'https:') {
				// Fake DOMContentLoaded on IE
				d.write('<script id=__ie_onload defer src=\'javascript:""\';><\/script>');
				d.getElementById("__ie_onload").onreadystatechange = function() {
					if (this.readyState == "complete") {
						mce.Loader.pageLoad();
						d.getElementById("__ie_onload").onreadystatechange = d = null; // Prevent leak
					}
				};
			} else {
				window.addEventListener('DOMContentLoaded', mce.Loader.pageLoad, false);

				if (t.isIE || t.isWebKit) {
					ti = setInterval(function() {
						if (/loaded|complete/.test(d.readyState)) {
							clearInterval(ti);
							mce.Loader.pageLoad();
						}
					}, 10);
				}
			}

			// Handle page unload
			t.addEvent(window, 'beforeunload', mce.Loader.fakeUnload);
			t.addEvent(window, 'unload', mce.Loader.pageUnload);
		} else
			t.addEvent(document, 'keydown', mce.Loader.keyHandler);
	},

	fakeUnload : function() {
		// Is there things still loading, then do some magic
		if (document.readyState == 'interactive') {
			function stop() {
				// Prevent memory leak
				document.detachEvent('onstop', stop);

				// Call unload handler
				mce.Loader.pageUnload();
			};

			// Fire unload when the currently loading page is stopped
			document.attachEvent('onstop', stop);

			// Remove onstop listener after a while to prevent the unload function
			// to execute if the user presses cancel in an onbeforeunload
			// confirm dialog and then presses the browser stop button
			window.setTimeout(function() {
				document.detachEvent('onstop', stop);
			}, 0);
		}
	},

	pageLoad : function() {
		if (!mce.Loader.done) {
			window.setTimeout(function() {
				top.mce.ContentManager.addPage(window);
			}, 0);

			mce.Loader.done = 1;
		}
	},

	pageUnload : function() {
		if (!mce.Loader.unloaded) {
			top.mce.ContentManager.removePage(window);
			mce.Loader.unloaded = 1;
		}
	},

	addEvent : function(o, n, f) {
		if (o.attachEvent)
			o.attachEvent('on' + n, f);
		else if (o.addEventListener)
			o.addEventListener(n, f, false);
		else
			o['on' + n] = f;
	},

	keyHandler : function(e) {
		var t = mce.Loader;

		e = e || window.e;

		if ((e.keyCode == 27 || e.keyCode == 17) && (!t.oldKeyCode || e.keyCode == t.oldKeyCode)) {
			if (++t.count == 3) {
				t.showLogin();

				if (e.preventDefault)
					e.preventDefault();
				else
					e.returnValue = false;
			}
		} else
			t.count = 0;

		t.oldKeyCode = e.keyCode;
	},

	showLogin : function() {
		var t = mce.Loader, vp = t.getViewPort(), ifr, d = document, s, w, h;

		// Window size
		w = 250;
		h = 180;

		// Create iframe
		ifr = d.createElement('iframe');
		ifr.id = 'mceLoginBox';
		ifr.src = t.getBaseURL() + '/../pages/login.html';
		ifr.frameBorder = 'no';
		s = ifr.style;
		s.position = 'absolute';
		s.left = ((vp.x + vp.w / 2) - (w / 2)) + 'px';
		s.top = ((vp.y + vp.h / 2) - (h / 2)) + 'px';
		s.width = w + 'px';
		s.height = h + 'px';
		s.border = '1px solid black';
		s.zIndex = 100000;

		d.body.appendChild(ifr);
	},

	hideLogin : function() {
		var e = document.getElementById('mceLoginBox');

		e.parentNode.removeChild(e);
		this.count = 0;
	},

	getViewPort : function(w) {
		var d, b;

		w = !w ? window : w;
		d = w.document;
		b = (!this.isIE || d.compatMode == "CSS1Compat") ? d.documentElement : d.body;

		// Returns viewport size excluding scrollbars
		return {
			x : w.pageXOffset || b.scrollLeft,
			y : w.pageYOffset || b.scrollTop,
			w : w.innerWidth || b.clientWidth,
			h : w.innerHeight || b.clientHeight
		};
	},

	getBaseURL : function() {
		return this.baseURL || this.findBaseURL(/mce\/js/);
	},

	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;
	}
};

mce.Loader.init();