/**
 * Desk v0.1
 * @copyright (C) 2005, Daniel Ecer
 * Note: this code is not free (yet). You may not use it without prior permissions.
 */
var desk_copyright = '@copyright (C) 2005, Daniel Ecer; this code is NOT free (yet). You may not use it without prior permissions.';

function deskCheckOnLoad() {
	if ((!document.createElement) || (!document.body) || (!document.body.innerHTML)) {
		// browser is not supported
		alert('the browser is not supported');
		return;
	}
	window.setTimeout('doDeskAutoStart();', 3000);
}

function deskOpenDefaultLink() {
	var s	= new String(window.location.pathname);
	var i	= s.lastIndexOf('/');
	if (i >= 0) {
		s	= s.substring(i + 1, s.length);
	}
	openLink(baseUrl + '/print/' + s);
}

function doDeskAutoStart() {
	var slash	= '/';
	slash	+= '';
	var div	= document.createElement('div');
	div.className		= 'desk-start';
	var anchor			= document.createElement('a');
	anchor.className	= 'desk-start';
	anchor.setAttribute('title', 'Start Desk');
	anchor.innerHTML	= 'start desk';
	anchor.onclick		= function() {
		window.startDeskImmediately	= true;
		window.ondeskload	= function() {
			window.setTimeout('deskOpenDefaultLink();', 2000);
		};
		var elements		=  document.body.childNodes;
		for (var i = elements.length - 1; i >= 0; i--) {
			document.body.removeChild(elements[i]);
		}
		var baseUrl	= window.location.protocol + '//' + window.location.hostname;
		var s	= new String(window.location.pathname);
		var i	= s.lastIndexOf(slash);
		if (i >= 0) {
			s	= s.substring(0, i);
		}
		i	= s.lastIndexOf(slash);
		if (i >= 0) {
			var folder	= s.substring(i + 1, s.length);
			if ((folder == 'desimple') || (folder == 'detree') || (folder == 'detab') || (folder == 'debl') ||
				(folder == 'debw') || (folder == 'degr') || (folder == 'depl') || (folder == 'print')) {
				s	= s.substring(0, i);
			}
		}
		if ((s != '') && (s != slash)) {
			baseUrl	+= s;
		}
		var scriptUrl		= baseUrl + '/de/js/desk.js';
		var styleUrl		= baseUrl + '/de/css/desk.css';
		var styleUrl2		= baseUrl + '/templates/detree/css/template_css.css';
		var script			= document.createElement('script');
		script.type			= 'text/javascript';
		script.language		= 'JavaScript';
		script.src			= scriptUrl;
		var a	= document.getElementsByTagName('head');
		var head	= (a.length > 0 ? a[0] : document.body);
		if (head != null) {
			var a	= document.getElementsByTagName('style');
			for (i = a.length - 1; i >= 0; i--) {
				a[i].disabled	= true;
				//a[i].parentNode.removeChild(a[i]);
				//head.removeChild(a[i]);
			}
		}
		if (document.createStyleSheet) {
			var s	= '';
			if (document.styleSheets.length > 0) {
				// the IE does not override styles
				document.styleSheets[0].addRule('#main_body', 'margin: 0;');
				document.styleSheets[0].addRule('.toc', 'border: 0;	padding: 0;	margin: 0; float: none;');
				document.styleSheets[0].addRule('.toc div', 'background-color: #F8F8F8;');
				document.styleSheets[0].addRule('.toc-header', 'display: none;');
				for (i = 0; i < document.styleSheets.length; i++) {
					document.styleSheets[i].disabled	= true;
				}
			}
			document.createStyleSheet(styleUrl);
			document.createStyleSheet(styleUrl2);
		} else {
			var style			= document.createElement('link');
			style.type			= 'text/css';
			style.rel			= 'stylesheet';
			style.href			= styleUrl2;
			head.appendChild(style);
			style			= document.createElement('link');
			style.type			= 'text/css';
			style.rel			= 'stylesheet';
			style.href			= styleUrl;
			head.appendChild(style);
		}
		head.appendChild(script);
	};
	div.appendChild(anchor);
	var anchor2			= document.createElement('a');
	anchor2.className	= 'desk-start-close';
	anchor2.setAttribute('title', 'Close');
	anchor2.innerHTML	= 'close';
	anchor2.onclick		= function() {
		document.body.removeChild(div);
	};
	div.appendChild(anchor);
	div.appendChild(anchor2);
	var y				= -1000;
	div.style.position	= 'absolute';
	div.style.top		= y + 'px';
	document.body.appendChild(div);
	
//	alert('div.style.right=' + getStyleSheetProperty('div', 'desk-start', 'right'));

//	div.style.left		= '10px';
//	div.style.top		= '10px';

	var width			= parseInt(div.style.width, 10);
	var height			= parseInt(div.style.height, 10);
	if (isNaN(width)) {
		width = div.offsetWidth;
	}
	if (isNaN(height)) {
		height  = div.offsetHeight;
	}
	if ((isNaN(width)) || (isNaN(height)) || (width < 1) || (height < 1)) {
		width	= 50;
		height	= 50;
		div.style.width		= width + 'px';
		div.style.height	= height + 'px';
	}
	y	= 0-height;
	div.style.top		= y + 'px';

	window.deskStartDiv		= div;
	window.deskStartTimer	= window.setInterval('doDeskStartTimer();', 100);
}

/*
function getStyleSheetProperty(t, c, p) {
	var sheets = document.styleSheets;
	if(sheets.length > 0) {
		// loop over each sheet
		for(var x = 0; x < sheets.length; x++) {
			// grab stylesheet rules
			var rules = sheets[x].cssRules;
			if(rules.length > 0) {
				// check each rule
				for(var y = 0; y < rules.length; y++) {
					var z = rules[y].style;
					// use the native selectorText and style stuff
					if(((z[p] != "") && (z[p] != null)) &&
					   ((rules[y].selectorText == c) ||
					    (rules[y].selectorText == (t + "." + c)))) {
						return z[p];
					}
				}
			}
		}
	}

}
*/

function doDeskStartTimer() {
	var div	= window.deskStartDiv;
	var y	= parseInt(div.style.top, 10);
	if (y >= 0) {
		window.clearInterval(window.deskStartTimer);
	}
	y	= Math.min(0, y + Math.max(1, Math.abs(y / 8)));
	div.style.top		= y + 'px';
}

if (window.addEventListener) {
	window.addEventListener("load", deskCheckOnLoad, false);
} else if (window.attachEvent) {
	window.attachEvent("onload", deskCheckOnLoad);
} else {
	// dont bother to run it on older browsers (single quote here not webfileshrinker-friendly)
}