//
// $Id$
//
// Javascript code used throughout the website.
//
// Author: Claudio Felber (claudio.felber@perron2.ch)
// Created: 2007-04-23
//
// Copyright (c) 2007, 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);

registerInit(init);

function init() {
	// quit if this function has already been called
    if (arguments.callee.done) {
		return;
	}

    // flag this function so we don't do the same thing twice
    arguments.callee.done = true;

	if (!isIEMac) {
		enhanceForms();
	}

	enhanceZoomImages();
    focusForm();
}

function registerInit(callback) {
    // for Mozilla and Opera 9
    if (document.addEventListener) {
        document.addEventListener("DOMContentLoaded", callback, false);
    }

    // for Internet Explorer
    if (document.getElementById) {
		document.write('<script id="__ie_onload" defer="true" src="javascript:void(0)"><\/script>');
		var deferScript = document.getElementById('__ie_onload');
        if (deferScript) {
            deferScript.onreadystatechange = function() {
                if (this.readyState == 'complete') {
                    callback();
                }
            };

            // check whether script has already completed
            deferScript.onreadystatechange();

            // clear reference to prevent leaks in IE 
            deferScript = null;
        }
    }

	// for Safari
	if (/WebKit/i.test(navigator.userAgent)) { // sniff
		var _timer = setInterval(function() {
			if (/loaded|complete/.test(document.readyState)) {
				clearInterval(_timer);
				callback(); // call the onload handler
			}
		}, 10);
	}	

    // for other browsers
    window.onload = callback;
}

function enhanceForms() {
	forms = document.forms;
	for (var i = 0; i < forms.length; 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;
			}
            if (elem.type == 'select-one' || elem.type == 'text' || elem.type == 'textarea') {
				var classes = elem.className.trim().split('/s+/');
				var width = classes.contains('text2') || classes.contains('select2') ? 100 : 246;
				elem.style.width = width + (width - elem.offsetWidth) + 'px';
            }
        }
	}
}

function enhanceZoomImages() {
	var content = document.getElementById('content');
	if (content) {
		var images = content.getElementsByTagName('img');
		var zoomImages = [];
		for (var i = 0; i < images.length; i++) {
			if (images[i].parentNode.tagName == 'A') {
				zoomImages.push(images[i]);
			}
		}
		for (var i = 0; i < zoomImages.length; i++) {
			var zoom = document.createElement("img");
			zoom.src = 'pictures/zoom.gif';
			zoom.className = 'zoom';
			zoomImages[i].parentNode.appendChild(zoom);
		}
	}
}

function focusForm(control, form) {
	if (form != null) {
		form = document.forms[form];
	} else {
		form = document.forms[0];
	}

    if (form != null) {
        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();
            }
        }
    }
}

function submit(name, value) {
	if (name != null) {
		var field = document.createElement('input');
		field.id = name;
		field.name = name;
        field.type = 'hidden';
		field.value = value == null ? '' : value;
		document.forms[0].appendChild(field);
	}
	document.forms[0].submit();
}

function zoom(image, caption) {
	image = escape(image);
	caption = escape(caption);
	zoomWindow = window.open('zoom.php?image=' + image + '&caption=' + caption,
							 'zoomWindow', 'width=436, height=300, scrollbars=0, resizable=0');
    setTimeout('zoomWindow.focus();', 250);
}

function resizeZoomWindow() {
	var page = document.getElementById('page');
	page.onclick = function() {
		window.close();
	}
	setClientSize(window, page.offsetWidth, page.offsetHeight);
}

function setClientSize(win, width, height) {
	// it is important to resize the window to the
    // wanted values first, even if we won't get them.
	win.resizeTo(width, height);

    // create the checkpoint element
    var cp = win.document.createElement("div");
    cp.style.position = "absolute";
    cp.style.width = "0px";
    cp.style.height = "0px";
    cp.style.right = "0px";
    cp.style.bottom = "0px";

    // we can only read it's position after we
    // insert it into the document
    win.document.body.appendChild(cp);

    // here we get the actual client size
    var currentWidth = cp.offsetLeft;
    var currentHeight = cp.offsetTop;

    // here we find out how much more we need
    // in order to get to the needed W x H size
    // (or in other words, we compute the size of
    // window decorations: border, scroll bars, title)
    var dw = width - currentWidth;
    var dh = height - currentHeight;

    // and _finally_ we get what we need
    win.resizeBy(dw, dh);

    // we can safely delete the checkpoint now
    win.document.body.removeChild(cp);
}

String.prototype.trim = function() {
    return this.replace(/^\s+|\s+$/g, '');
}

if (Array.prototype.push) {
	Array.prototype.push = function(value) {
		this[this.length] = value;
	}
}

Array.prototype.contains = function(value) {
	for (var i = 0; i < this.length; i++) {
		if (this[i] == value) {
			return true;
		}
	}
	return false;
}