﻿function browserVersion() {
	var ua = window.navigator.userAgent;
	var msie = ua.indexOf("MSIE ");
	if (msie > 0) // If Internet Explorer, return version number
		return parseFloat (ua.substring(msie + 5, ua.indexOf(".", msie) + 2));
	
	// If another browser, return 0
	return 0;
}

//nlbeck: How many times are the dropdowns set to hidden?
var hiddenCount = 0;

//nlbeck: Show/hide dropdowns
function toogleDropdowns(visibility) {
	if (visibility == 'hidden')
		hiddenCount++;
	else //nlbeck: We expect 'visible'
		hiddenCount--;
	//nlbeck: Just a little bit of information for the debugger ;-)
//	window.status = 'Functions.js - hiddenCount: ' + hiddenCount;
	
	//nlbeck: Only if we are totally sure we can hide the dropdown, we want to do it...
	if (visibility == 'visible' && hiddenCount > 0)
		return;

	var ieVersion	= browserVersion();
	if (ieVersion > 4 && ieVersion < 7)
		if (document.getElementsByTagName) {
			elems = document.getElementsByTagName('select');
			for (elem in elems)
				if (elems[elem].style && elems[elem].className != 'stayVisible')
					elems[elem].style.visibility = visibility;
		}
}


var allSelects;
//nlbeck: Register all dropdowns, so we can hide them if necessary
function trackDropdowns() {
	var ieVersion	= browserVersion();
	if (ieVersion > 4 && ieVersion < 7)
		if (document.getElementsByTagName)
			allSelects = document.getElementsByTagName('select');
}

trackDropdowns();

//nlbeck: Finds the correct .top for any element by using .offsetTop
function findTop(elem) {
	if (elem.offsetParent == null)
		return elem.offsetTop;

	return findTop(elem.offsetParent) + elem.offsetTop;
}

//nlbeck: Finds the correct .left for any element by using .offsetLeft
function findLeft(elem) {
	if (elem.offsetParent == null)
		return elem.offsetLeft;

	return findLeft(elem.offsetParent) + elem.offsetLeft;
}

//nlbeck: Makes sure, that if any select-element comes above the header - it becomes invisible
function scrolling() {
	if (allSelects == null || hiddenCount > 0)
		return;
	
	var scrollTop = document.documentElement.scrollTop;

	for (x in allSelects) {
		var elem = allSelects[x];
		if (elem.style) {
			var offsetTop = findTop(elem, -1);
			if (offsetTop - scrollTop < document.all.header.offsetHeight)
				elem.style.visibility = 'hidden';
			else
				elem.style.visibility = 'visible';
		}
	}
}




function showWaitElement() { //nlbeck: Added to support header/footer
	var waitElement = document.getElementById('__AjaxCall_Wait');
	if (waitElement)
		waitElement.style.visibility = 'visible';
	document.getElementById('header').style.borderBottomColor = 'red';
	document.getElementById('footer').style.borderTopColor = 'red';
}

function hideWaitElement() { //nlbeck: Added to support header/footer
	var waitElement = document.getElementById('__AjaxCall_Wait');
	if (waitElement)
		waitElement.style.visibility = 'hidden'; 
	document.getElementById('header').style.borderBottomColor = '#FC0';
	document.getElementById('footer').style.borderTopColor = '#FC0';
	
//	if (document.all)
//		document.recalc();
}


function addEvents() {
	window.onscroll = windowOnScroll;
	document.onmousemove = documentOnMouseMove;
	document.onmousedown = documentOnMouseDown;
	document.onmouseup = documentOnMouseUp;
}

function windowOnScroll() {
	//nlbeck: Functions.js
	scrolling();
}

function documentOnMouseMove() {
	//nlbeck: SmartTip.js
	positiontip();
	//nlbeck: Dragable.js
//	mouseMove();
}

function documentOnMouseDown() {
}

function documentOnMouseUp() {
	//nlbeck: Dragable.js
//	mouseUp();
}

var movedToHash = 0;
//nlbeck: The browser can't scroll to a specific point by using the Url style: #scrollTo
//        This is because of the 'fixed' header. So we have to manually scroll a bit up on the page.
//        We use the 'ScrollTo'-prefix to disable the browsers automatically scrolling.
function moveToHash(moveTo) {
	var hash;
	if (moveTo == null) {
		if (movedToHash >= 2)
			return;

		hash = document.location.hash;
	}
	else
		hash = moveTo;
	if (hash != '') {
		hash = hash.substring('#ScrollTo'.length);
		var objs = document.getElementsByName(hash);
		var top = findTop(objs[0]);
		window.scrollTo(0, top - document.getElementById('header').offsetHeight - 34);
		//nlbeck: For the debugger:
//		window.status = 'moveToHash: hash=' + hash + ', scrolledTo=' + (top - document.getElementById('header').offsetHeight - 10);
	}
	movedToHash++;
}

function showContent() {
	document.getElementById('content').style.display = '';
	document.getElementById('holdContent').style.display = 'none';
}

var log;
function onLoad() {
	//nlbeck: Let's log when something funny happens :-D
	log = new Log4js.getLogger('Ex-Trade');
	log.setLevel(Log4js.Level.ALL);
	//nlbeck: Log using AJAX - we just love that! :-)
	var ajaxAppender = new AjaxAppender(log, baseUrl + 'LogEvent.aspx')
	ajaxAppender.setLayout(new BasicLayout());
	log.addAppender(ajaxAppender);

	addEvents();

	//nlbeck: If using another browser than IE or using IE7, show the content now
	//        else we wait until recalcDone is called
	if (browserVersion() == 0 || browserVersion() == 7)
		showContent();

	moveToHash();

	var tables = document.getElementById('maincontent').getElementsByTagName('table');
	for (var i = 0; i < tables.length; i++)
		//nlbeck: the class attribute: IE: className, FireFox: class
		if (tables[i].getAttribute('className') == 'data' || tables[i].getAttribute('class') == 'data')
			//nlbeck: Make sure, that all data-tables have the fixed header
			new MovingTableHeader(tables[i].id);
}

//nlbeck: Function is run by ie7-standard-p.js, recalc()
function recalcDone() {
	//nlbeck: The page is now redrawn - show the content!
	setTimeout('showContent()', 100);

	//nlbeck: For some reason, we have to wait a moment before IE has finished all drawing of the page
	setTimeout('moveToHash()', 120);
}

window.onload = onLoad;
