// JavaScript Document
var App = {
	// setable Properties of the application
	width: 0,
	height: 0,
	top: 0,
	left: 0,
	minWidth: 0,
	maxWidth: 0,
	minHeight:0,
	maxHeight: 0,
	screenX: 0,
	screenY: 0,
	align: '',
	containerId:'',
	lastWNDId: 0,
	
	//placeholder for userdefined settings
	define: function() {},
	
	// makes the initialization of the application, 
	// runs the function define and loads all the userdefined properties
	init: function(id) {
		// check the Agent
		App.getAgent();
		
		// gets the measures of the visible screen
		App.H_IMG = document.getElementById('I_00');
		App.screenX = App.H_IMG.offsetWidth;
		App.screenY = App.H_IMG.offsetHeight;
		
		// saves the instance of the element given bei containerId
		App.containerId = id;
		if (App.containerId != '') {
			App.H_CON = document.getElementById(App.containerId);
		
			// runs predefined Settings
			App.define();
			
			// makes the allign of the given application container
			//App.setAlign();
			//App.setValign();
			//App.H_CON.style.visibility = 'visible';
		}
	},
	//gets the current Browsername (this is a very simple Function without checking the version)
	getAgent: function() {
		var str = navigator.appName;
		if (str.indexOf('Explorer') != -1) 		{App.agent = 'IE';}
		else if (str.indexOf('Netscape') != -1) {App.agent = 'NS';}
		else if (str.indexOf('Microsoft') != -1) {App.agent = 'IE';}
	},
	// sets the horizontal alignment of the element defined by containerId
	setAlign: function () {		
		if (App.align == 'left') {
			App.left = 0;
		} else if (App.align == 'center') {
			var d = App.screenX - App.width;
			if (d >= 0) App.left = d / 2;
			else d = 0;
		} else if (App.align == 'right') {
			App.left = App.screenX - App.width;
		}
		App.H_CON.style.left = String(App.left) + 'px';
	},
	
	// sets the horizontal alignment of the element defined by containerId
	setValign: function () {
		if (App.valign == 'top') {
			App.top = 0;
		} else if (App.valign == 'middle') {
			var d = App.screenY - App.height;
			if (d >= 0) App.top = d / 2;
			else d = 0;
		} else if (App.valign == 'bottom') {
			App.top = App.screenY - App.height;
		}
		App.H_CON.style.top = String(App.top) + 'px';
	},
	
	//fits the application to the visible windows, and resizes all child elements
	resize: function () {
		// saves changes
		App.screenX = App.H_IMG.offsetWidth;
		App.screenY = App.H_IMG.offsetHeight;
		
		//makes the alignment with delay
		window.setTimeout("App.setAlign()", 50);
		App.setValign();
	},
	
	//register scrollable Content
	registerContent: function(ID, scrollBar, maxHeight) {
		App.HND = document.getElementById(ID);
		var P_HND = App.HND.parentNode;
		if (scrollBar == 'auto') {
			if (App.HND.offsetHeight > maxHeight) {
				bt_1 = document.createElement('img');
				bt_1.src = 'images/scroll_up.gif';
				bt_1.style.position = 'absolute';
				bt_1.style.top = String(App.HND.offsetTop) + 'px';
				bt_1.style.left = String(App.HND.offsetLeft + App.HND.offsetWidth + 3) + 'px';
				bt_2 = document.createElement('img');
				bt_2.src = 'images/scroll_down.gif';
				bt_2.style.position = 'absolute';
				bt_2.style.top = String(App.HND.offsetTop + maxHeight - 22) + 'px';
				bt_2.style.left = bt_1.style.left;
				P_HND.appendChild(bt_1);
				P_HND.appendChild(bt_2);
				App.clipWidth = String(App.HND.offsetWidth) + 'px';
				App.clipHeight = String(maxHeight) + 'px';
				App.clipTop = '0px';
				App.HND.style.clip = 'rect('+App.clipTop+', '+App.clipWidth+', '+App.clipHeight+', 0px)';
				if (App.agent == 'IE') {
					bt_1.onmousedown =  new Function('App.scrollUpStart()');
					bt_1.onmouseup = new Function('App.scrollUpEnd()');
					bt_1.onmouseover = new Function('App.setOverCursor()');
					bt_1.onmouseout = new Function('App.setNormalCursor()');
					bt_2.onmousedown =  new Function('App.scrollDownStart()');
					bt_2.onmouseup = new Function('App.scrollDownEnd()');
					bt_2.onmouseover = new Function('App.setOverCursor()');
					bt_2.onmouseout = new Function('App.setNormalCursor()');
				} else {
					bt_1.addEventListener('mousedown', App.scrollUpStart, true);
					bt_1.addEventListener('mouseup', App.scrollUpEnd, true);
					bt_1.addEventListener('mouseover', App.setOverCursor, true);
					bt_1.addEventListener('mouseout', App.setNormalCursor, true);
					bt_2.addEventListener('mousedown', App.scrollDownStart, true);
					bt_2.addEventListener('mouseup', App.scrollDownEnd, true);
					bt_2.addEventListener('mouseover', App.setOverCursor, true);
					bt_2.addEventListener('mouseout', App.setNormalCursor, true);
				}
				App.scrollStep = 10;
			}
		}
	},
	
	setOverCursor: function() {
		App.H_CON.style.cursor = 'pointer';
	},
	setNormalCursor: function() {
		App.H_CON.style.cursor = 'default';
	},
	
	// scrollUp
	scrollUpStart: function() {
		if (parseInt(App.clipTop.slice(0, - 2)) > 0) {
			App.HND.style.top = String(App.HND.offsetTop + App.scrollStep) + 'px';
			App.clipTop = String(parseInt(App.clipTop.slice(0, - 2)) - App.scrollStep) + 'px';
			App.clipHeight = String(parseInt(App.clipHeight.slice(0, - 2)) - App.scrollStep) + 'px';
			App.HND.style.clip = 'rect('+App.clipTop+', '+App.clipWidth+', '+App.clipHeight+', 0px)';
			App.interval = window.setTimeout('App.scrollUpStart()', 50);
		}
	},
	scrollUpEnd: function() {
		window.clearTimeout(App.interval);
	},
	// scrollDown
	scrollDownStart: function() {
		if (App.HND.offsetHeight > parseInt(App.clipHeight.slice(0, - 2))) {
			App.HND.style.top = String(App.HND.offsetTop - App.scrollStep) + 'px';
			App.clipTop = String(parseInt(App.clipTop.slice(0, - 2)) + App.scrollStep) + 'px';
			App.clipHeight = String(parseInt(App.clipHeight.slice(0, - 2)) + App.scrollStep) + 'px';
			App.HND.style.clip = 'rect('+App.clipTop+', '+App.clipWidth+', '+App.clipHeight+', 0px)';
			App.interval = window.setTimeout('App.scrollDownStart()', 50);
		}
	},
	scrollDownEnd: function() {
		window.clearTimeout(App.interval);
	}
}
