//
// $Id: main.js 32 2007-01-05 19:37:51Z claudio.felber $
//
// Javascript code used throughout the website.
//
// Author: Claudio Felber (claudio.felber@perron2.ch)
// Created: 2006-12-20
//
// Copyright (c) 2006, Perron2 GmbH, All Rights Reserved.
//

var isIE5x = /MSIE 5/.test(navigator.userAgent) && !/Opera/.test(navigator.userAgent) && !/Mac/.test(navigator.userAgent);
var isIE6 = /MSIE 6/.test(navigator.userAgent) && !/Opera/.test(navigator.userAgent) && !/Mac/.test(navigator.userAgent);
var isIEMac = /MSIE 5/.test(navigator.userAgent) && /Mac/.test(navigator.userAgent);
var isOpera = /Opera/.test(navigator.userAgent);
var isSafari = /Safari/.test(navigator.userAgent);
var lang = document.getElementsByTagName('html')[0].lang;

window.onload = function() {
    if (isIE5x || isIE6 || isOpera) {
        enhanceSections();
    }

	if (!isIEMac) {
		enhanceForms();
	}
	
    prepareButtons();

    if (isIE5x || isIE6 || isIEMac) {
        enhanceButtons();
    }
	
	if (!isIEMac) {
		activatePopups();
	}

	// Hack to prevent IE5/Mac from hiding the first helpers item
	if (isIEMac) {
		var helpers = document.getElementById('helpers');
		var item = document.createElement('li');
		helpers.insertBefore(item, helpers.firstChild);
	}
}

function enhanceSections() {
    var content = document.getElementById('content');
    var blocks = content.getElementsByTagName('div');

    for (var i = 0; i < blocks.length; i++) {
        var block = blocks[i];
        if (block.className == 'section') {
            var div = document.createElement('div');
            div.className = 'section_footer';
            div.style.top = block.offsetTop + block.offsetHeight - 1 + 'px';
            block.parentNode.appendChild(div);
        } else if (/\bblock\b/.test(block.className)) {
            var div = document.createElement('div');
            div.className = 'block_footer';
            div.style.left = block.offsetLeft + 'px';
            div.style.top = block.offsetTop + block.offsetHeight - 1 + 'px';
            block.insertBefore(div, block.firstChild);
        }
    }
}

function enhanceForms() {
	forms = document.forms;
	for (var i = 0; i < forms.length - 1; i++) {
		var form = forms[i];
		for (var j = 0; j < form.elements.length; j++) {
			var elem = form.elements[j];
			if (elem.parentNode.tagName != 'TD') {
				continue;
			}
			elem.style.width = 275 + (275 - elem.offsetWidth) + 'px';			
		}
	}
}

function prepareButtons() {
    var buttons = findButtons();
	for (var i = 0; i < buttons.length; i++) {
        var button = buttons[i];
        var id = button.className;
		if (id.substring(0, 6) == 'button') {
			id = id.substring(7);
		}
		var blockExpr = /\bblock\b/;

		var type = 'normal';
        if (blockExpr.test(button.parentNode.className) || blockExpr.test(button.parentNode.parentNode.className)) {
            type = 'block';
        } else if (button.parentNode.parentNode.parentNode.tagName == 'DD') {
            type = 'popup';
        }

        button.image_on = '/pictures/buttons/' + type + '/' + id + '_' + lang + '_on.gif';
        button.image_off = '/pictures/buttons/' + type + '/' + id + '_' + lang + '_off.gif';

        var image = new Image();
        image.src = button.image_on;
    }
}

function enhanceButtons() {
    var button = document.getElementById('submit');
    button.onmouseover = function() {
        this.style.backgroundImage = 'url(/pictures/frame/search/submit_on.gif)';
    }
    button.onmouseout = function() {
        this.style.backgroundImage = 'url(/pictures/frame/search/submit_off.gif)';
    }

    var buttons = findButtons();
    for (var i = 0; i < buttons.length; i++) {
        var button = buttons[i];
        button.onmouseover = function() {
            this.style.backgroundImage = 'url(' + this.image_on + ')';
        }
        button.onmouseout = function() {
            var id = this.className;
            this.style.backgroundImage = 'url(' + this.image_off + ')';
        }
    }
}

function findButtons() {
	if (isIE5x || isIE6) {
	    var input = document.getElementById('content').getElementsByTagName('input');
		var buttons = [];
		for (var i = 0; i < input.length; i++) {
			if (input[i].className.substring(0, 6) == 'button') {
				buttons[buttons.length] = input[i];
			}
		}
		return buttons;
	} else {
	    return document.getElementById('content').getElementsByTagName('button');
	}
}

function activatePopups() {
    var lists = document.getElementsByTagName('dl');
	var content = document.getElementById('content');

    for (var i = 0; i < lists.length; i++) {
        var list = lists[i];
        if (list.className == 'publications') {
            topics = list.getElementsByTagName('dt');
            for (var j = 0; j < topics.length; j++) {
                var topic = topics[j];
                topic.popup = topic.nextSibling;
                while (topic.popup && topic.popup.nodeName != 'DD') {
                    topic.popup = topic.popup.nextSibling;
                }

				topic.popup.parentNode.removeChild(topic.popup);
				document.getElementById('page').appendChild(topic.popup);

				// position the popup element
				var pos = getElemPos(topic);
				topic.popup.className = 'publications';
				topic.popup.style.left = pos.left + topic.offsetWidth / 2 + 'px';
				topic.popup.top = pos.top + topic.offsetHeight / 2;

                if (topic.popup) {
                    topic.onmouseover = function() {
						this.popup.style.top = '-1000px';
                        this.popup.style.display = 'block';
						this.popup.style.top = this.popup.top - this.popup.offsetHeight + 'px';
					}
                    topic.onmouseout = function() {
                        this.popup.style.display = 'none';
                    }

                    elems = [ topic.popup ];
                    if (isSafari) {
                        var buttons = topic.popup.getElementsByTagName('button');
                        for (k = 0; k < buttons.length; k++) {
                            elems[elems.length] = buttons[k];
                        }
                    }

                    for (k = 0; k < elems.length; k++) {
                        elems[k].popup = topic.popup;
                        elems[k].onmouseover = topic.onmouseover;
                        elems[k].onmouseout = topic.onmouseout;
                    }
                }
            }
        }
    }
}

function getElemPos(elem) {
	var left = 0;
	var top = 0;
	while (elem.id != 'page' && elem.parentNode != null) {
		left += elem.offsetLeft;
		top += elem.offsetTop;
		elem = elem.parentNode;
	}
	return { left: left, top: top };
}

function picture(name, width, height, title) {
    var features = 'width=' + width + ',height=' + height + ',left=10,top=10';

    win = window.open('', 'popup', features);

    win.document.open();
    win.document.write('<html><head>');
    if (title) {
        win.document.write('<title>' + title + '</title>');
    }
    win.document.write('</head>');
    win.document.write('<body bgcolor="#FFFFFF" leftmargin="0" topmargin="0" marginheight="0" marginwidth="0" onBlur="self.close()">');
    win.document.write('<img src="' + name + '" width="' + width + '" height="' + height + '" onClick="self.close()">');
    win.document.write('</body>');
    win.document.write('</html>');
    win.document.close();

    win.focus();
    setTimeout('win.focus();', 250);
}

function focusForm(control, form) {
	if (form != null) {
		form = document.forms[form];
	} else {
		form = document.forms[0];
	}
	
	if (control != null) {
		control = form.elements[control];
	} else {
		for (var i = 0; i < form.elements.length; i++) {
			var elem = form.elements[i];
			if (elem.type == 'text' || elem.type == 'select-one' || elem.type == 'textarea') {
				control = elem;
				break;
			}
		}
	}
	
	if (control != null) {
		if (control.type == "text") {
			control.focus();
			control.select();
		} else {
			control.focus();
		}
    }
}