/**
 * @author paul
 */
var UI = {
	Version: {
		version: '0.0.1',
		build: '$Rev$'
	}
};

UI.Overlay = {
	create: function(title, dialogDiv) {
		this.overlay = document.createElement('div');
		this.overlay.id = 'dialog_overlay';
		Object.extend(this.overlay.style, {
			position: 'absolute',
			top: 0,
			left: 0,
			zIndex: 50,
			width: '100%',
			backgroundColor: '#000',
			display: 'none'
		});
		document.body.insertBefore(this.overlay, document.body.childNodes[0]);
		
		this.overlay.style.height = this.documentHeight() + 'px';
		new Effect.Appear(this.overlay, {duration: 0.3, from: 0.0, to: 0.5});
		
		this.store = {
			title: title,
			div: dialogDiv,
			innerHTML: dialogDiv.innerHTML
		};
		this.show();
	},
	show: function() {
		this.dialog = document.createElement('div');
		this.dialog.id = 'dialog';
		Object.extend(this.dialog.style, {
			position: 'fixed',
			top: 0,
			left: 0,
			height: '300px',
			width: '70%',
			zIndex: 200,
			padding: '12px',
			backgroundColor: '#fff'
		});
		
		if (UI.BrowserDetect.browser == 'Explorer' && UI.BrowserDetect.version <= 6) {
			this.dialog.style.position = 'absolute';
		}
		
		var html = '';
		html += '<div style="float: right"><a href="javascript:UI.Overlay.close()">sluiten</a></div>'
		html += '<h3 style="margin-top: 0">' + this.store.title + '</h3>';
		html += this.store.div.innerHTML;
		this.store.div.innerHTML = '';	// Empty it so the id's stay unique
		
		this.dialog.innerHTML = html;
		
		this.dialog.style.top	= ((document.viewport.getHeight() / 2) - (parseInt(this.dialog.style.height, 10) / 2)) + 'px';
		this.dialog.style.left	= ((document.viewport.getWidth() / 2) - ((parseInt(this.dialog.style.width, 10) / 100) * document.viewport.getWidth()) / 2) + 'px';
		
		//document.body.insertBefore(this.dialog, document.body.childNodes[0]);
		this.overlay.parentNode.insertBefore(this.dialog, this.overlay);
		
		this.selectBoxes('hide');
	},
	center: function() {
		this.dialog.style.top	= ((document.viewport.getHeight() / 2) - (parseInt(this.dialog.style.height, 10) / 2)) + 'px';
		this.dialog.style.left	= parseInt(document.viewport.getWidth() / 2, 10) - parseInt(parseInt(this.dialog.style.width, 10) / 2) + 'px';
	},
	close: function() {
		this.store.div.innerHTML = this.store.innerHTML;	// Reset the innerHTML of the original (hidden) div
		Element.remove(this.dialog);
		//new Effect.Fade(this.overlay, {duration: 0.3, from: 0.5, to: 0.0});
		Element.remove(this.overlay);
		
		this.selectBoxes('show');
	},

	/**
	 * Get the height of the document or the height of the window if the document is smaller
	 * @return {Number}
	 */
	documentHeight: function() {
		var htmlHeight = document.body.parentNode.scrollHeight; 
		var windowHeight = window.innerHeight; 
		if ( htmlHeight < windowHeight ) {
			return windowHeight;
		} else {
			return htmlHeight;
		}
	},
	
	selectBoxes: function(what) {
		$A(document.getElementsByTagName('select')).each(function(select) {
			Element[what](select);
		});
		
		if(what == 'hide') {
			$A(this.dialog.getElementsByTagName('select')).each(function(select) {
				Element.show(select)
			});
		}
	}
};

UI.BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]
};

UI.BrowserDetect.init();
