if (!window.moozcms)window.moozcms = {baseURL : '/moozcms'};/*
 * $Id: Loader.js 787 2009-09-17 12:20:52Z spocke $
 *
 * @author Moxiecode
 * @copyright Copyright 2004-2007, Moxiecode Systems AB, All rights reserved.
 */

(function() {
	var c = 0;

	moozcms.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;

			// Fix for Google Translate
			try {
				t.isCMS = top.moozcms && !!top.moozcms.ContentManager;
			}
			catch (ex) {
				// Ignore, not in CMS.
			}
	
			t.count = 0;

			// Handle DOM content loaded
			if (t.isCMS) {
				// Check if the document is loaded into the edit view
				// Todo: Fix a loop here for frames support later
				if (top.document.getElementById('editView').contentWindow != window)
					return;

				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") {
							moozcms.Loader.pageLoad();
							d.getElementById("__ie_onload").onreadystatechange = d = null; // Prevent leak
						}
					};
				} else {
					window.addEventListener('DOMContentLoaded', moozcms.Loader.pageLoad, false);

					if (t.isIE || t.isWebKit) {
						ti = setInterval(function() {
							if (/loaded|complete/.test(d.readyState)) {
								clearInterval(ti);
								moozcms.Loader.pageLoad();
							}
						}, 10);
					}
				}

				// Handle page unload
				t.addEvent(window, 'beforeunload', moozcms.Loader.fakeUnload);
				t.addEvent(window, 'unload', moozcms.Loader.pageUnload);

				// Add page level css and script
				document.write(
					'<link rel="stylesheet" type="text/css" media="all" href="' + moozcms.baseURL + '/core/css/?package=core.page" />' +
					'<script type="text/javascript" src="' + moozcms.baseURL + '/core/js/?package=core.page"></script>'
				);
			} else
				t.addEvent(document, 'keyup', moozcms.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
					moozcms.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() {
			var t = moozcms.Loader, i, d = document, bl, s = d.styleSheets;

			// Fire ones
			if (t.done)
				return;

			function get(n) {
				return document.getElementsByTagName(n);
			};

			for (i = 0; i < s.length; i++) {
				// Gecko check
				if (t.isGecko) {
					try {
						s[i].cssRules[0];
					} catch (ex) {
						if (ex.code == 15) {
							window.setTimeout(t.pageLoad, 0);
							return;
						}
					}
				}

				// Opera check
				if (s[i].disabled) {
					window.setTimeout(t.pageLoad, 0);
					return;
				}
			}

			// WebKit
	/*		if (get('link').length + get('style').length != s.length) {
				window.setTimeout(t.pageLoad, 0);
				return;
			}*/

			// Fire page done
			if (!t.done) {
				//t.addEvent(d, 'mousemove', moozcms.Loader.mouseMove);
				t.addEvent(window, 'scroll', moozcms.Loader.hideTools);
				t.addEvent(window, 'resize', moozcms.Loader.hideTools);
				t.addEvent(d, 'mouseover', moozcms.Loader.mouseOver);

				// Create blocker
				bl = d.createElement('div');
				bl.setAttribute('id', 'moozBlocker');
				d.body.appendChild(bl);

				t.addEvent(bl, 'click', moozcms.Loader.blurArea);

				bl = d = null; // Fix IE leaks

				window.setTimeout(function() {
					top.moozcms.ContentManager.addPage(window);
				}, 0);

				// Add moozCMS class to body
				document.body.className += ' moozCMS';

				t.done = 1;
			}
		},

		pageUnload : function() {
			if (!moozcms.Loader.unloaded) {
				top.moozcms.ContentManager.removePage(window);
				moozcms.Loader.unloaded = 1;
			}
		},

		blurArea : function() {
			top.moozcms.ContentManager.activePage.blurActiveArea();
		},

		mouseOver : function(e) {
			var n = e.target, p, cms;

			for (; n; n = n.parentNode) {
				if (n.nodeType === 1 && n.getAttributeNode('moozcms:type')) {
					if (n.id != moozcms.Loader._lastId && (cms = top.moozcms.ContentManager)) {
						p = cms.findPage(window);

						if (p) {
							p.enterArea(n);
							moozcms.Loader._lastId = n.id;
						}
					}

					return;
				}
			}

			moozcms.Loader.hideTools();
		},

		hideTools : function() {
			var p, cms;

			if (cms = top.moozcms.ContentManager) {
				p = cms.findPage(window);
				if (p) {
					p.hideTools();
					moozcms.Loader._lastId = 0;
				}
			}
		},

		addEvent : function(o, n, f) {
			var id, tf;

			if (o.attachEvent) {
				// Prevent cicular references leak in IE
				id = c++;
				tf = moozcms.Loader.addEvent;

				if (!tf.events)
					tf.events = {};

				tf.events[id] = f;

				o.attachEvent('on' + n, function() {
					var e = window.event;

					if (!e.target)
						e.target = e.srcElement;

					e.preventDefault = function() {
						e.returnValue = false;
					};

					e.stopPropagation = function() {
						e.cancelBubble = true;
					}; 

					moozcms.Loader.addEvent.events[id](e);
					e = 0;
				});
			} else if (o.addEventListener)
				o.addEventListener(n, f, false);
			else
				o['on' + n] = f;

			o = f = tf = 0; // Prevent IE leak
		},

		keyHandler : function(e) {
			var t = moozcms.Loader;

			e = e || window.event;

			if ((e.keyCode == 17 || e.keyCode == 27) && (!t.oldKeyCode || e.keyCode == t.oldKeyCode)) {
				if (++t.count == 3) {
					t.showLogin();

					if (e.preventDefault)
						e.preventDefault();
					else
						e.returnValue = false;

					t.count = 0;
				}
			} else
				t.count = 0;

			t.oldKeyCode = e.keyCode;
		},

		showLogin : function() {
			var t = moozcms.Loader, vp = t.getViewPort(), ifr, d = document, s, w, h;

			if (t.shown)
				return;

			// Window size
			w = 340;
			h = 420;

			// Create iframe
			ifr = d.createElement('iframe');
			ifr.id = 'mooz_login_box';
			ifr.src = moozcms.baseURL + '/core/pages/login.html';
			ifr.frameBorder = 'no';
			ifr.allowTransparency = true;
			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.zIndex = 100000;
			t.shown = 1;

			d.body.appendChild(ifr);
		},

		hideLogin : function() {
			var e = document.getElementById('mooz_login_box');

			e.parentNode.removeChild(e);
			this.count = 0;
			this.shown = 0;

			window.focus();
		},

		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
			};
		}
	};

	moozcms.Loader.init();
})();
