
var NEToolbox = new function() {
	
	this.eventHandler = function(e) {
		if (window.ne && ne.designMode.designModeEnabled) return;
		var target;
		if (!e) var e = window.event;
		if (e.target) {
			target = e.target;
		}
		else if (e.srcElement) {
			target = e.srcElement;
		}
		
		if (target instanceof HTMLImageElement &&
			(target = target.parentNode) && 
			target instanceof HTMLAnchorElement && 
			target.className &&
			target.className.indexOf('neoeditImagePreview') != -1 &&
			target.href) {
			
			if (!e) var e = window.event;
			if (e.preventDefault) {
				e.preventDefault();
			}
			else {
				e.returnValue = false;
			}
			
			var container = document.createElement('div');
			container.id = 'neoeditImagePreviewOverlay'
			container.setAttribute('style', 'position:absolute;z-index:9999;');
			document.body.insertBefore(container, document.body.firstChild);
			document.body.style.overflow = 'hidden';
			
			var bg = document.createElement('div');
			bg.setAttribute('style', 'position:fixed;top:0px;left:0px;width:100%;height:100%;background-color:#000;opacity:0;')
			container.appendChild(bg)

			var frame = document.createElement('div');
			frame.setAttribute('style', 'position:fixed;top:0px;left:0px;width:100%;height:100%;overflow:auto;')
			container.appendChild(frame)
			
			var table = document.createElement('div');
			table.setAttribute('style', 'position:absolute;display:table;width:100%;height:100%;padding-top:0px;padding-bottom:0px')
			frame.appendChild(table);
			
			var cell = document.createElement('div');
			cell.setAttribute('style', 'position:relative;display:table-cell;vertical-align:middle;text-align:center;')
			table.appendChild(cell);
			
			var innerBlock = document.createElement('div');
			innerBlock.setAttribute('style', 'position:relative;display:inline-block;padding:0px;');
			cell.appendChild(innerBlock)

			var close = document.createElement('div');
			close.setAttribute('style', 'position:absolute;top:0px;right:-14px;width:30px;height:30px;z-index: 1;background: url(\'/toolbox/close.png\') no-repeat;')
			close.onclick = function() {
				document.body.removeChild(document.getElementById('neoeditImagePreviewOverlay'));
				document.body.style.overflow = 'auto';
			}
			innerBlock.appendChild(close)
			
			var innerTable = document.createElement('div');
			innerTable.setAttribute('style','display:table;width:100%;margin-top:12px;');
			innerBlock.appendChild(innerTable);
			
			var innerTableCell = document.createElement('div');
			innerTableCell.setAttribute('style','display:table-cell;width:9px;height:9px;background:url(\'/toolbox/corner.png\') no-repeat;');
			innerTable.appendChild(innerTableCell);
			
			innerTableCell = document.createElement('div');
			innerTableCell.setAttribute('style','display:table-cell;width:auto;height:9px;line-height:9px;background-color:#fff;');
			innerTableCell.appendChild(document.createTextNode('\u00a0'))
			innerTable.appendChild(innerTableCell);
			
			innerTableCell = document.createElement('div');
			innerTableCell.setAttribute('style','display:table-cell;width:9px;height:9px;background:url(\'/toolbox/corner.png\') no-repeat top right;');
			innerTable.appendChild(innerTableCell);
			
			var content = document.createElement('div');
			content.setAttribute('style','background-color:#fff;padding-left:9px;;padding-right:9px;');
			innerBlock.appendChild(content);
			
			innerTable = document.createElement('div');
			innerTable.setAttribute('style','display:table;width:100%;margin-bottom:10px;');
			innerBlock.appendChild(innerTable);
			
			innerTableCell = document.createElement('div');
			innerTableCell.setAttribute('style','display:table-cell;width:9px;height:9px;background:url(\'/toolbox/corner.png\') no-repeat bottom left');
			innerTable.appendChild(innerTableCell);
			
			innerTableCell = document.createElement('div');
			innerTableCell.setAttribute('style','display:table-cell;width:auto;height:9px;line-height:9px;background-color:#fff;');
			innerTableCell.appendChild(document.createTextNode('\u00a0'))
			innerTable.appendChild(innerTableCell);
			
			innerTableCell = document.createElement('div');
			innerTableCell.setAttribute('style','display:table-cell;width:9px;height:9px;background:url(\'/toolbox/corner.png\') no-repeat bottom right');
			innerTable.appendChild(innerTableCell);

			var spinner = new Image();
			spinner.src = 'spinner.gif';
			spinner.setAttribute('style','display:block;border:0px;padding:70px 100px;');
			content.appendChild(spinner);

			function imageCallback(e) {
				content.replaceChild(this, content.firstChild)
			}

			var img = new Image();
			img.onload = imageCallback;
			img.setAttribute('style','display:block;border:0px;');
			img.src = target.href;

			var i = 0;
			function fadeUp() {
				i = Number((i+0.2).toFixed(2));
				if (i>1) i = 1;
				innerBlock.style.opacity = i;
				bg.style.opacity = (i-(i/4)).toFixed(2);
				if (i < 1) {
					window.setTimeout(arguments.callee, 10);
				}
			}
			fadeUp();

			return false;
		}
	}
	
	this.embedFlash = function(id, file, flashVars, width, height, bgcolor) {
		if (typeof flashObject == 'undefined') {
			var script = document.createElement('script');
			document.head.appendChild(script);
			script.type = 'text/javascript';
			script.src = '/toolbox/flashObject.js';
			script.onload = function() { 
				flashObject.embedFlash(id, file, flashVars, width, height, bgcolor);
			}
		}
	}

	this.embedFlashVideoPlayer = function(id, flashVars, width, height, bgcolor) {
		this.embedFlash(id, '/toolbox/player.swf', flashVars, width, height, bgcolor)
	}
	
	this.embedUnity = function(id, file, width, height) {
		if (typeof unityObject == 'undefined') {
			var script = document.createElement('script');
			document.head.appendChild(script);
			script.type = 'text/javascript';
			script.src = '/toolbox/UnityObject.js';
			script.onload = function() { 
				unityObject.embedUnity(id, file, width, height);
			}
		}
	}
}

if (document.addEventListener) {
	document.addEventListener('click', NEToolbox.eventHandler, false);
} 
else if (document.attachEvent) {
	document.attachEvent('onclick', NEToolbox.eventHandler);
}

