function trace (s) {
	if (!document.getElementById('debug')) {
		var o = document.createElement('pre');
		o.id = 'debug';
		o.style.color = 'black';
		o.style.fontSize = '10pt';
		o.style.position = 'absolute';
		o.style.top = '0px';
		o.style.right = '0px';
		o.style.textAlign = 'left';
		document.getElementsByTagName('body')[0].appendChild(o);
	}
	if (typeof console == 'undefined') {
		document.getElementById('debug').innerHTML += s + '<br />';
	}
	else {
		console.log(s);
	}
}

var Difference = {
	called: false,
	timer: 0,
	init: function () {
		// quit if this function has already been called
		if (Difference.called) {
			return;
		}
	
		// flag this function so we don't do the same thing twice
		Difference.called = true;
		if (Difference.timer) {
			clearInterval(Difference.timer);
		}

		var ids = ['demos1', 'demos2'];
		var demoLinks = [];
		ids.each (function (e) {
			e = $(e);
			$A(e.getElementsByTagName('a')).each(function (link) {
				link = $(link);
				demoLinks.push(link);
			})
		});
	
		demoLinks.each(function (e) {
			e = $(e);
			if (e.href.indexOf('register.php') != -1) {
				return;
			}
			if (e.href.indexOf('mailto') != -1) {
				return;
			}
			e.observe('click', function (evt) {
				Event.stop(evt);
				openDemo(e);
			});
		});
	},
	addEvent: function (obj, type, fn) {
		if (obj.addEventListener) {
			obj.addEventListener(type, fn, false);
		}
		else if (obj.attachEvent) {
			obj['e' + type + fn] = fn;
			obj[type + fn] = function() {
				obj['e' + type + fn](window.event);
			};
			obj.attachEvent('on' + type, obj[type + fn]);
		}
	}
}


/* for Mozilla/Opera9 */
if (document.addEventListener) {
	document.addEventListener('DOMContentLoaded', Difference.init, false);
}

/* for Internet Explorer */
/*@cc_on @*/
/*@if (@_win32)
	document.write('<script id=__ie_onload defer src=javascript:void(0)><\/script>');
	var script = document.getElementById('__ie_onload');
	script.onreadystatechange = function() {
		if (this.readyState == 'complete') {
			Difference.init(); // call the onload handler
		}
	};
/*@end @*/

/* for Safari */
if (/WebKit/i.test(navigator.userAgent)) { // sniff
	Difference.timer = setInterval(function() {
		if (/loaded|complete/.test(document.readyState)) {
			Difference.init(); // call the onload handler
		}
	}, 50);
}

Difference.addEvent(window, 'load', function() { Difference.init() });

function openDemo(e) {
	e = $(e);
	/*
	var paths = e.href.split('/');
	var demoName = paths.pop().replace(/\.html/, '');
	var path = '';
	while ((p = paths.pop()) != 'difference') {
		path = p + '/' + path;
	}
	var extensions = ['_config.xml', '_controller.swf', '_preload.swf'];
	*/

	var regex = /<object .*?height="(.*?)" width="(.*?)"/gim;

	Popup.load(e.href, {
		contentLoaded: function (str) {
			/*
			extensions.each(function (s) {
				str = str.replace(new RegExp(demoName + s, 'gi'), path + demoName + s);			
			});
			return '<div style="border: 1px solid #000; width: 802px">' + str + '</div>';
			*/

			// attempt to get get the sizes from the flash movie
			var r = new RegExp('<object\\s[\\s\\S]*?height\\s*=\\s*"\\s*(\\d+?)\\s*"[\\s\\S]*?width\\s*=\\s*"\\s*(\\d+?)\\s*"', 'mgi').exec(str);
			//trace(r);
			var height = parseInt(r[1]);
			var width = parseInt(r[2]);

			var outerWidth = width + 2; 
			if (Popup.isIE && !Popup.isIE7) {
				outerWidth += 2;
			}

			return '<div style="width: ' + outerWidth + 'px;"><iframe style="border: 1px solid #000; width: ' + width + 'px; height: ' + height + 'px" src="' + e.href + '" scrolling="no" frameborder="0"></iframe><a class="popupClose" href="javascript:;" onclick="Popup.hide()">Close</a></div>';
		} 
	});
}

var Popup = Class.create();
Popup = {
	// moo.fx version,
	bodyPadding: 20,
	overlay: 'overlay',
	outerCont: 'popouter',
	innerCont: 'popinner',
	intrans:	false,
	fadeDuration:	300, // milliseconds
	resizeDuration:	600, // milliseconds
	created:	false,
	content:	null,
	iframe: null, // required to block out windowed controls in IE, see http://dotnetjunkies.com/WebLog/jking/archive/2003/07/21/488.aspx
	isIE: /MSIE/gi.test(navigator.userAgent),
	ieIE7: /MSIE\s7/gi.test(navigator.userAgent),
	create:	function () {
		var overlay = document.createElement('div');
		overlay.setAttribute('id', Popup.overlay);
		overlay = $(overlay);
		overlay.onclick = function (event) {
			Popup.hide();
		}
		overlay.setStyle({
			position: 'absolute',
			left: '0px',
			top: '0px',
			width: '100%',
			height: '100%',
			display: 'none',
			zIndex: '1000'
		});

		var outer = document.createElement('div');
		outer.setAttribute('id', Popup.outerCont);
		outer = $(outer);
		outer.onclick = function (event) {
			Popup.hide();
		}
		outer.setStyle({
			position: 'absolute',
			top: '30px',
			width: '100%',
			textAlign: 'center',
			left: '0px',
			display: 'none',
			zIndex: '1000'
		});

		var inner = document.createElement('div');
		inner.setAttribute('id', Popup.innerCont);
		inner = $(inner);
		inner.onclick = function (event) {
			Popup.cancelBubble(event);
		}
		inner.setStyle({
			marginRight: 'auto',
			marginLeft: 'auto',
			textAlign: 'left'
		});

		var sizes = Popup.getSizes();
		overlay.style.height = sizes.page.height + 'px';
		//if (/msie/gi.test(navigator.userAgent.toLowerCase())) {
		if (Popup.isIE) {
			overlay.style.width = sizes.page.width + 'px';
		}
		outer.appendChild(inner);


		if (Popup.isIE && !Popup.isIE7) {
			// create the iframe
			var iframe = document.createElement('iframe');
			iframe = $(iframe);
			iframe.setStyle({
				position: 'absolute',
				left: '0px',
				top: '0px',
				width: overlay.style.width,
				height: overlay.style.height,
				visibility: 'visible',
				display: 'none',
				background: '#fff',
				opacity: '0'
			});
			document.getElementsByTagName('body')[0].appendChild(iframe);
			Popup.iframe = iframe;
		}

		document.getElementsByTagName('body')[0].appendChild(overlay);
		document.getElementsByTagName('body')[0].appendChild(outer);

		Popup.overlay = document.getElementById(Popup.overlay);
		Popup.outerCont = document.getElementById(Popup.outerCont);
		Popup.innerCont = document.getElementById(Popup.innerCont);

		Popup.created = true;

		Event.observe(window, 'resize', function (evt) {
			setTimeout('Popup.resized()', 10); 
		});
	},
	resized: function () {
		Popup.overlay.setStyle({display: 'none'});
		if (Popup.iframe) {
			Popup.iframe.setStyle({display: 'none'});
		}

		var sizes = Popup.getSizes();

		Popup.overlay.style.height = sizes.page.height + 'px';
		//if (/msie/gi.test(navigator.userAgent.toLowerCase())) {
		if (Popup.isIE) {
			Popup.overlay.style.width = sizes.page.width + 'px';
		}
		if (Popup.iframe) {
			Popup.iframe.setStyle({
				width: Popup.overlay.style.width,
				height: Popup.overlay.style.height,
				display: ''
			});
		}
		Popup.overlay.setStyle({display: ''});
	},
	fadeInContent: function () {
		var content = Popup.innerCont.getElementsByTagName('div')[0];
		content = $(content);
		content.setStyle({
			position: '',
			visibility: 'visible',
			opacity: '0'
		});
	},
	showContent: function() {
		// resize the box

		var content = document.createElement('div');
		content = $(content);
		content.setStyle({
			position: 'absolute',
			visibility: 'hidden'
		});

		var str = Popup.oXML.responseText;
		//var m = /<body.*?>([\s\S]*)<\/body>/mgi.exec(str);
		var m = new RegExp('<body.*?>([\\s\\S]*)</body>', 'mgi').exec(str);
		str = m ? m[1] : '<p style="margin: 0px; padding: 20px">An error has occured</p>';
		//var path = (window.location.href.indexOf('/digitaldictation/') != -1) ? '../' : '';
		//str = str.replace(/images\//, path + 'images/');
		content.innerHTML = Popup.options.contentLoaded(str);

		// append the content to the document and get it's size
		document.getElementsByTagName('body')[0].appendChild(content);

		var oldDims = Popup.getDimensions(Popup.innerCont);
		var newDims = Popup.getDimensions(content);

		// remove the content from the document
		document.getElementsByTagName('body')[0].removeChild(content);

		// change the boxes height
		var heightFx = new Fx.Style(Popup.innerCont, 'height', {
			duration: Popup.resizeDuration,
			onComplete: function () {
				var widthFx = new Fx.Style(Popup.innerCont, 'width', {
					duration: Popup.resizeDuration,
					onComplete: function () {
						content.setStyle({
							position: '',
							visibility: 'hidden',
							width: newDims.width + 'px',
							height: newDims.height + 'px'
						});
						// append the content
						Popup.innerCont.appendChild(content);

						var fadeFx = new Fx.Style(content, 'opacity', {
							duration: Popup.fadeDuration,
							onComplete: function () {
								Popup.intrans = false;
							}
						});
						fadeFx.custom(0, 1);
					}
				});
				widthFx.custom(oldDims.width, newDims.width);
			}
		});
		Popup.delContent();
		heightFx.custom(oldDims.height, newDims.height);
	},
	showLoading: function () {
	    var con = Popup.xhcon();
	    if (!con) {
			return;
		}

		// show the loading gif
		//var img = document.createElement('img');
		//img = $(img);
		//img.setStyle({margin: '30px'});
		//img.setAttribute('src', Popup.path + 'images/loading.gif');
		Popup.delContent();
		Popup.innerCont.setStyle({
			width: '150px',
			height: '150px',
			//background: '#fff'
			background: Popup.options.innerBackground
		});
		//Popup.innerCont.appendChild(img);
		Popup.outerCont.setStyle({visibility: 'visible', display: 'block'});

		con.connect(Popup.href, 'GET', Math.random(), function (oXML) {
			Popup.oXML = oXML;
			setTimeout('Popup.showContent()', 0); // delay by 1 second, this is done on purpose
		});
	},
	showOverlay: function () {
		Popup.overlay.setStyle({
			display: 'block',
			visibility: 'hidden',
			background: Popup.options.overlayBackground
		});
		var overlayFx = new Fx.Style(Popup.overlay, 'opacity', {
			duration: Popup.fadeDuration,
			onComplete: function () {
				Popup.showLoading();
				//overlayFx.custom(Popup.options.opacity, 0);
			}
		});
		overlayFx.custom(0, Popup.options.opacity);
	},
	show:	function () {
		if (!Popup.created) {
			Popup.create();
		}

		if (Popup.intrans) {
			return false;
		}

		var path = (window.location.href.indexOf('/html/') != -1) ? '../' : '';
		Popup.path = path;

		Popup.intrans = true;
		if (Popup.iframe) {
			Popup.iframe.setStyle({
				display: 'block'
			});
		}
		Popup.showOverlay();
	},
	hide:		function () {
		if (Popup.intrans) {
			return;
		}

		Popup.intrans = true;
		var overlayFx = new Fx.Style(Popup.overlay, 'opacity', {
			duration: Popup.fadeDuration,
			onComplete: function () {
				if (Popup.iframe) {
					Popup.iframe.setStyle({
						display: 'none'
					});
				}
				Popup.intrans = false;
			}
		});
		var contentFx = new Fx.Style(Popup.innerCont, 'opacity', {
			duration: Popup.fadeDuration,
			onComplete: function () {
				Popup.outerCont.setStyle({display: 'none'});
				Popup.innerCont.setStyle({visibility: 'visible', opacity: '1'});
				overlayFx.custom(Popup.options.opacity, 0);
			}
		});
		Popup.delContent();
		contentFx.custom(1, 0);
		return false;
	},
	delContent:	function () {
		Popup.innerCont.innerHTML = '';
	},
	load:	function (href, options) {
		// move to the top of the page
		window.scrollTo(0, 0);		

		Popup.oXML = null;
		Popup.href = href;		
		Popup.options = Object.extend({
			innerBackground: '#fff',
			overlayBackground: '#fff',
			opacity: 0.7,
			contentLoaded: function (s) { return s; }
		}, options || {});
		Popup.show();
	},
	/*
	resized:	function () {
		//if (Popup.created && /msie/gi.test(navigator.userAgent.toLowerCase())) {
		//if (Popup.created && new RegExp('msie', 'gi').test(navigator.userAgent.toLowerCase())) {
		if (Popup.isIE) {
			var sizes = Popup.getSizes();
			overlay.style.height = sizes.page.height + 'px';
			overlay.style.width = sizes.page.width + 'px';
		}
	},
	*/
	cancelBubble:	function (eObj) {
		if (!eObj) var eObj = window.event;
		if (!eObj) return;
		eObj.cancelBubble = true;
		if (eObj.stopPropagation) {
			eObj.stopPropagation();
		}
	},
	getSizes: function () {
		var xScroll, yScroll;

		if (window.innerHeight && window.scrollMaxY) {	
			xScroll = document.body.scrollWidth;
			yScroll = window.innerHeight + window.scrollMaxY;
		}
		/*
		else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
			xScroll = document.body.scrollWidth;
			yScroll = document.body.scrollHeight;
		}
		else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
			xScroll = document.body.offsetWidth;
			yScroll = document.body.offsetHeight;
		}
		*/
		else {
			xScroll = document.body.scrollWidth;
			yScroll = document.body.scrollHeight;
		}
		
		var windowWidth, windowHeight;
		if (self.innerHeight) {	// all except Explorer
			windowWidth = self.innerWidth;
			windowHeight = self.innerHeight;
		}
		else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
			windowWidth = document.documentElement.clientWidth;
			windowHeight = document.documentElement.clientHeight;
		}
		else if (document.body) { // other Explorers
			windowWidth = document.body.clientWidth;
			windowHeight = document.body.clientHeight;
		}	
		
		// for small pages with total height less then height of the viewport
		if (yScroll < windowHeight) {
			pageHeight = windowHeight;
		}
		else {
			pageHeight = yScroll;
		}
	
		// for small pages with total width less then width of the viewport
		if (xScroll < windowWidth) {	
			pageWidth = windowWidth;
		}
		else {
			pageWidth = xScroll;
		}

		return { page: {width: pageWidth, height: pageHeight}, window: {width: windowWidth, height: windowHeight }};
	},
	getDimensions: function(element) {
		if (!element) {
			return false;
		}
		if (element.style.display != 'none') {
			return {width: element.offsetWidth, height: element.offsetHeight};
		}

		// All *Width and *Height properties give 0 on elements with display none,
		// so enable the element temporarily
		var els = element.style;
		var originalVisibility = els.visibility;
		var originalPosition = els.position;
		els.visibility = 'hidden';
		els.position = 'absolute';
		els.display = '';
		var originalWidth = element.clientWidth;
		var originalHeight = element.clientHeight;
		els.display = 'none';
		els.position = originalPosition;
		els.visibility = originalVisibility;
		return {width: originalWidth, height: originalHeight};
	},
	xhcon:		function () {
		var xmlhttp, bComplete = false;
		try { xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); }
		catch (e) { try { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); }
		catch (e) { try { xmlhttp = new XMLHttpRequest(); }
		catch (e) { xmlhttp = false; }}}
		if (!xmlhttp) {
			return null;
		}
		this.connect = function(sURL, sMethod, sVars, fnDone) {
			if (!xmlhttp) {
				return false;
			}
			bComplete = false;
			sMethod = sMethod.toUpperCase();
	
			try {
				if (sMethod == "GET") {
					xmlhttp.open(sMethod, sURL+"?"+sVars, true);
					sVars = "";
				}
				else {
					xmlhttp.open(sMethod, sURL, true);
					xmlhttp.setRequestHeader("Method", "POST "+sURL+" HTTP/1.1");
					xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
				}
				xmlhttp.onreadystatechange = function() {
					if (xmlhttp.readyState == 4 && !bComplete) {
						bComplete = true;
						fnDone(xmlhttp);
					}
				};
				xmlhttp.send(sVars);
			}
			catch(z) { return false; }
			return true;
		};
		return this;
	}
}
