/* behavior.js - last changed 2008-10-27 */

jQuery.noConflict();

(function($) {
	
	function callOnDomReady() {
		prepareInputButtons;
	};
	
	callOnDomReady();
})(jQuery);


if (!document.getElementById) {
    document.getElementById = function() {
        return null;
    }
}

function getRawObject(obj) {
    var theObj;
    if (typeof obj == "string") {
        theObj = document.getElementById(obj);
    } else {
        // pass through object reference
        theObj = obj;
    }
    return theObj;
}

function getElementStyle(obj, IEStyleProp, CSSStyleProp) {
    var elem = getRawObject(obj);
    if (elem) {
        if (elem.currentStyle) {
            return elem.currentStyle[IEStyleProp];
        } else if (window.getComputedStyle) {
            var compStyle = window.getComputedStyle(elem, "");
            return compStyle.getPropertyValue(CSSStyleProp);
        }
    }
    return "";
}

function toggleBgColor(elem) {
    var bgColor = getElementStyle(elem, "backgroundColor", "background-color");
    bgColor = (bgColor == "" ? "transparent" : bgColor);
    if (bgColor == "transparent") {
        elem.style.backgroundColor = bgColor;
    } else {
        elem.style.backgroundColor = "transparent";
    }
}

function toggleTextDecoration(elem) {
    if (elem.style.textDecoration == "underline") {
        elem.style.textDecoration = "none";
    } else {
        elem.style.textDecoration = "underline";
    }
}

function openNewWindow(url, windowName, windowFeatures) {
	var windowWidth = getWinWidth(500);
	var windowHeight = getWinHeight(700);
    var attributes = (windowFeatures && windowFeatures != "") ? windowFeatures : "width=" + windowWidth + ",height=" + windowHeight + ",menubar,resizable,scrollbars,status";
    var newWindow = window.open(url, windowName, attributes);
    if (window.focus) {
        newWindow.focus();
    }
}

function openNewPicWindow(url, windowName, intWindoscrWidth) { // Querformat
	if (!intWindoscrWidth) { intWindoscrWidth = 470; }
	var windowWidth = getWinWidth(intWindoscrWidth);
	var windowHeight = getWinHeight(560);
    var attributes = "width=" + windowWidth + ",height=" + windowHeight + ",menubar,resizable,scrollbars,status";
    openNewWindow(url, windowName, attributes);
}

function openNewXXLWindow(url, windowName, intWindoscrWidth) { // Querformat
	if (!intWindoscrWidth) { intWindoscrWidth = 760; }
	var windowWidth = getWinWidth(intWindoscrWidth);
	var windowHeight = getWinHeight(560);
    var attributes = "width=" + windowWidth + ",height=" + windowHeight + ",menubar,resizable,scrollbars,status";
    openNewWindow(url, windowName, attributes);
}

function openNewPanelPicWindow(url, windowName, intWindoscrWidth) { // Hochformat    
	if (!intWindoscrWidth) { intWindoscrWidth = 470; }
	var windowWidth = getWinWidth(intWindoscrWidth);
	var windowHeight = getWinHeight(600);
    var attributes = "width=" + windowWidth + ",height=" + windowHeight + ",menubar,resizable,scrollbars,status";
    openNewWindow(url, windowName, attributes);
}

function openNewMapWindow(url, windowName) { // Karte
	var windowWidth = getWinWidth(840); // enlarged font size, bigger size needed for interactive maps
	var windowHeight = getWinHeight(620);
    var attributes = "width=" + windowWidth + ",height=" + windowHeight + ",menubar,resizable,scrollbars,status";
    openNewWindow(url, windowName, attributes);
}

function openNewMovieWindow(url, windowName) { // Flash
	var windowWidth = getWinWidth(485);
	var windowHeight = getWinHeight(550);
    var attributes = "width=" + windowWidth + ",height=" + windowHeight + ",menubar,resizable,scrollbars,status";
    openNewWindow(url, windowName, attributes);
}

function openNewMicrositeWindow(url, windowName) { // Flash for FM microsite
	var windowWidth = getWinWidth(960);
	var windowHeight = getWinHeight(670);
    var attributes = "width=" + windowWidth + ",height=" + windowHeight + ",menubar,resizable,scrollbars,status";
    openNewWindow(url, windowName, attributes);
}
    
function focusElem(elemId) {
    var elem = document.getElementById(elemId);
    if (elem && elem.focus) {
        elem.focus();
    }
}

// Global variable for subwindow reference
var printWindow;

function openPrintWindow(url, target) {
    var windowUrl = url ? url : window.location.href;
    var windowName = target ? target : "_blank";
	var windowWidth = getWinWidth(730);
	var windowHeight = getWinHeight(600);
    printWindow = window.open(windowUrl, windowName, "width=" + windowWidth + ",height=" + windowHeight + ",scrollbars,resizable,menubar,toolbar,left=0,top=0");
    printWindow.focus();    
}


/* Switch actual stylesheet to print and trigger print dialog if supported */
function setupPrint() {
    // hide whole body to avoid flickering while disabling and enabling the stylesheets
    document.body.style.display = "none";
    if (document.getElementsByTagName) {
        var elem;
        for(var i = 0; (elem = document.getElementsByTagName("link")[i]); i++) {
            if (elem.getAttribute("rel").indexOf("style") != -1 && elem.getAttribute("media") != "print") {
                elem.disabled = true;
                // if link is an alternate stylesheet and has both screen and print media type
                if (elem.getAttribute("rel").indexOf("alt") != -1 && elem.getAttribute("title").indexOf("Druckvorschau") != -1) {
                    // enable stylesheet
                    elem.disabled = false;
                }
            }
        }
    }
    // show body again
    document.body.style.display = "block";
    if (window.print) {
        window.setTimeout("window.print()", 1000);
    }
}

function validateChoice(elem) {
    var isValid = true;
    if (elem && elem.type == "select-one") {
        if (elem.options[elem.selectedIndex].value == "") {
            isValid = false;
        }
    }
    return isValid;
}

function prepareInputButtons() {
    if (document.getElementsByTagName) {
        var inputElems = document.body.getElementsByTagName("INPUT");
        for (var i = 0; i < inputElems.length; i++) {
            var inputElem = inputElems[i];
            if ((inputElem.type == "submit" || inputElem.type == "reset" || inputElem.type == "button") && inputElem.className == "subbtn") {
        		// set toggleTextDecoration for suitable events
        		// existing events are saved
                var cachedOnMouseOver = inputElem.onmouseover;
                var cachedOnMouseOut = inputElem.onmouseout;
                var cachedOnFocus = inputElem.onfocus;
                var cachedOnBlur = inputElem.onblur;
                
                inputElem.onmouseover = function() {
                    toggleTextDecoration(this);
                    if (cachedOnMouseOver) {
                        return cachedOnMouseOver();
                    }
                }
                inputElem.onmouseout = function() {
                    toggleTextDecoration(this);
                    if (cachedOnMouseOut) {
                        return cachedOnMouseOut();
                    }
                }
                inputElem.onfocus = function() {
                    toggleTextDecoration(this);
                    if (cachedOnFocus) {
                        return cachedOnFocus();
                    }
                }
                inputElem.onblur = function() {
                    toggleTextDecoration(this);
                    if (cachedOnBlur) {
                        return cachedOnBlur();
                    }
                }
            }
        }
    }
}

function getWinWidth(favWidth) {
	var sh;
	if (!favWidth) { favWidth = 600 } // assume a window that fits in smallest common screen
    if (screen.width) {
        sw = screen.width;
    } else {
		sw = 640; // assume smallest common screen resolution
	}
	if (favWidth < sw) { // if window fits in screen
		return favWidth; // return the preferred width
	} else {
		return sw - 50; // return screen width minus a padding to screen border
	}
}

function getWinHeight(favHeight) {
	var sh;
	if (!favHeight) { favHeight = 400 } // assume a window that fits in smallest common screen
    if (screen.height) {
        sh = screen.height;
    } else {
		sh = 480; // assume smallest common screen resolution
	}
	if (favHeight < sh) { // if window fits in screen
		return favHeight; // return the preferred height
	} else {
		return sh - 50; // return screen height minus a padding for windows taskbar etc.
	}
}
var my_webtrekkString = "";
function send_webtrekk(webtrekkString, mode) {
	var debug = false;
	var delay = 1200;
	var mode_suffix = "."+mode+".debug";
	if (debug) alert("start "+webtrekkString);
	if ((mode == 'popup') || (mode == 'click') || (mode == 'link_newWin') || (mode == 'link_dload') || (mode == 'link_dload_newWin')) {
		if (debug) {
			webtrekkString += mode_suffix;
			if (mode == 'link_dload') webtrekkString += ".delay"+delay;
		}
		if (typeof(wt_sendinfo) != "undefined") {
			if (debug) alert(mode+"="+webtrekkString);
			wt_sendinfo(webtrekkString,'click');
			if (mode == 'link_dload') {
				var date = new Date();
				var curDate = null;
				do {
					curDate = new Date();
				}
				while(curDate-date < delay);
			}
		}
	}
	else if (mode == 'link') {
		if (debug) webtrekkString += mode_suffix;
		my_webtrekkString = webtrekkString;
		if (typeof(wt_registerEvent) != "undefined") {
			if (debug) alert(mode+"="+webtrekkString);
			wt_registerEvent(window, (wt_browserNameIE && wt_typeof(window.onbeforeunload)) ? "beforeunload" : "unload", send_webtrekk);
		}
	}
	else {
		// unload
		if (my_webtrekkString != "") {
			if (debug) alert("onUnload="+my_webtrekkString);
			if (typeof(wt_sendinfo) != "undefined") {
				if (debug) alert("go");
				wt_sendinfo(my_webtrekkString);
			}
		}
	}
}

function popup_banner(myAddress, myParams, webtrekkString) {
	if (webtrekkString) send_webtrekk(webtrekkString, 'popup');
	bannerPopup = window.open(myAddress, "bannerPopup", myParams);
	bannerPopup.focus();	
	return false;
}

