/* DBAG JavaScript Library */
var ns4 = (document.layers)?1:0;
var dom = (document.getElementById && !document.all);
var ie4 = (document.all)?1:0;
var opr = (navigator.userAgent.indexOf("Opera") == 0) && (!ie6);
var moz = navigator.appName=="Netscape" && navigator.appCodeName=="Mozilla" && !document.all;
var mac = navigator.userAgent.indexOf("Mac")!= -1;
var ie6 =(navigator.userAgent.indexOf("Opera") < 0) && (!moz);

var hoehe = 0;

window.status = "Die Bahn"; // writes Javascript default message in browser status bar

// calculate the height of a layer
function getDivsHeight() {
    var iDivsHeight = 0;
    for(var i=0; i < arguments.length; i++) {
        if (document.getElementById(arguments[i])) {
            iDivsHeight += document.getElementById(arguments[i]).offsetHeight;
        }
    }
    return iDivsHeight;
}

// set the height of the back main layer in order to move the footer
function setDivContentMinHeight() {
    var oDivcontent, iWinInnerHeight, tmpHeight;
    var mozCorrection = moz ? -1 : 0;
    oDivcontent = document.getElementById(arguments[0]); //assumes first passed object as name for the content area layer
    oStopperDiv = document.getElementById(arguments[1]); // retrieve stopper div object
    oAddColumn = document.getElementById(arguments[4]);

    iWinInnerHeight = document.body.clientHeight; // store window height of the inner browser area temporarily

    // calculate the height of the page elements except the content area; using the getDivsHeight function
    if (arguments.length > 5) {
        // called from initPopup.
        tmpHeight = getDivsHeight(arguments[2], arguments[3], arguments[4]);
        mozCorrection = moz ? arguments[5] : 0;
    } else if (arguments.length == 5) {
        tmpHeight = getDivsHeight(arguments[2], arguments[3]); // header, footer
    } else if (arguments.length == 4) {
        tmpHeight = getDivsHeight(arguments[2]);
    }

    //if there is more space on the page than the header and footer height, than the main layer is set to a new height
    if(oStopperDiv) {
        if (iWinInnerHeight > (tmpHeight + oStopperDiv.offsetTop + oStopperDiv.offsetHeight)) {
            if (oAddColumn) {
                if ((iWinInnerHeight - tmpHeight + mozCorrection) < oAddColumn.offsetHeight) {
                    oDivcontent.style.height = oAddColumn.offsetHeight + oStopperDiv.offsetHeight + mozCorrection;
                } else {
                    oDivcontent.style.height = iWinInnerHeight - tmpHeight + mozCorrection;
                }
            } else {
                oDivcontent.style.height = iWinInnerHeight - tmpHeight + mozCorrection;
            }
        } else {
            if (oAddColumn) {
                if ((oStopperDiv.offsetTop + oStopperDiv.offsetHeight + mozCorrection) < oAddColumn.offsetHeight) {
                    oDivcontent.style.height = oAddColumn.offsetHeight + oStopperDiv.offsetHeight + mozCorrection;
                } else {
                    oDivcontent.style.height = oStopperDiv.offsetTop + oStopperDiv.offsetHeight + mozCorrection;
                }
            } else {
                oDivcontent.style.height = oStopperDiv.offsetTop + oStopperDiv.offsetHeight + mozCorrection;
            }
        }
    }
}

// set the height of the back main layer in order to move the footer -- Popup version
function setDivContentMinHeightPopup() {
    var hoehe = window.opener.hoehe;
    var oDivcontent, iWinInnerHeight, tmpHeight;
    var mozCorrection = moz ? -1 : 0;

    oDivcontent = document.getElementById(arguments[0]); //assumes first passed object as name for the content area layer
    tmpHeight = oDivcontent.offsetHeight;
    oStopperDiv = document.getElementById(arguments[1]); // retrieve stopper div object
    // wenn Video, dann nimm Element 'videoBrd', wenn Bildergallerie 'galleryBrd', sonst nichts.
    oBrdDiv = document.getElementById(arguments[6]);
    if(!oBrdDiv) oBrdDiv = document.getElementById(arguments[7]);

    iWinInnerHeight = document.body.clientHeight; // store window height of the inner browser area temporarily

    // calculate the height of the page elements except the content area; using the getDivsHeight function
    // called from initPopup.
    mozCorrection = moz ? arguments[5] : 0;
    headerHeight = getDivsHeight(arguments[2]);
    buttonHeight = getDivsHeight(arguments[3]);
    footerHeight = getDivsHeight(arguments[4]);
    //if (tmpHeight > hoehe) {
    //  tmpHeight = hoehe - headerHeight - footerHeight - buttonHeight;
    //}

    //if there is more space on the page than the header and footer height, than the main layer is set to a new height
    if(oStopperDiv) {
        //if (iWinInnerHeight > (footerHeight + buttonHeight + oStopperDiv.offsetTop + oStopperDiv.offsetHeight)) {
        if (iWinInnerHeight > (footerHeight + buttonHeight + headerHeight + oDivcontent.offsetHeight)) {
            // Fenster größer als Contentbereich, Footer wird runter geschoben
            if(ie6) {
                oDivcontent.style.height = iWinInnerHeight - buttonHeight - footerHeight - headerHeight - 13;
            }
            if(moz) {
                oDivcontent.style.height = iWinInnerHeight - buttonHeight - footerHeight - headerHeight - 20;
            }
            if(opr) {
                oDivcontent.style.height = iWinInnerHeight - buttonHeight - footerHeight - headerHeight - 13;
                if(oBrdDiv) {
                    oBrdDiv.style.height = iWinInnerHeight - footerHeight - headerHeight;
                }
                if(!oBrdDiv) {
                    if (ie6) {
                        //oDivcontent.style.height = iWinInnerHeight - buttonHeight - footerHeight - headerHeight -13;
                    } else {
                        //oDivcontent.style.height = iWinInnerHeight - buttonHeight - footerHeight - headerHeight -20;
                    }
                }
            } else {
                if(oBrdDiv) {
                    oBrdDiv.style.height = iWinInnerHeight - footerHeight - headerHeight;
                }
                if(!oBrdDiv) {
                    //oDivcontent.style.height = iWinInnerHeight - buttonHeight - footerHeight - headerHeight;
                }
            }
        } else {
            if (!ie6) mozCorrection = 7;
            if (hoehe <= 455) {
                if (opr) {
                    oDivcontent.style.height = iWinInnerHeight - buttonHeight - footerHeight - headerHeight - 7;
                }
                if (moz) {
                    if (iWinInnerHeight < hoehe) {
                        oDivcontent.style.height = hoehe - buttonHeight - footerHeight - headerHeight - 20;
                    } else {
                        oDivcontent.style.height = iWinInnerHeight - buttonHeight - footerHeight - headerHeight - 20;
                    }
                }
                if (ie6) {
                    oDivcontent.style.height = iWinInnerHeight - buttonHeight - footerHeight - headerHeight - 20;
                }
            } else {
                if(moz) {
                    if (iWinInnerHeight < hoehe) {
                        //oDivcontent.style.height = hoehe - headerHeight - 20;
                        oDivcontent.style.height = tmpHeight;
                    } else {
                        //oDivcontent.style.height = iWinInnerHeight - headerHeight;
                        oDivcontent.style.height = tmpHeight - headerheight;
                    }
                } else {
                    oDivcontent.style.height = iWinInnerHeight - buttonHeight - footerHeight - headerHeight - 20;
                }
            }
            if(oBrdDiv) {
                if(moz) {
                    oBrdDiv.style.height = iWinInnerHeight - footerHeight - headerHeight;
                }
                if(ie6) {
                    oBrdDiv.style.height = hoehe - headerHeight - footerHeight;
                }
                if(opr) {
                    oBrdDiv.style.height = hoehe - footerHeight - headerHeight + 13;
                }
            } else {
                if (hoehe <= 455) {
                    if (opr) {
                        oDivcontent.style.height = iWinInnerHeight - buttonHeight - footerHeight - headerHeight - 13;
                    }
                }
            }
        }
    }
}

// Wrapper function to apply the changes to the footer / main layers if DOM browser detected
// loadEvent triggers a bugfix for MSIE 5.0 and footer bar placement, "true" is for function calls in onLoad events,
// "false" is default and for onResize events

function init(loadEvent) {
    if (!ns4) {
        setDivContentMinHeight('main', 'stopper', 'header', 'footer', 'addColumnStartPage');
        footero=document.getElementById('footer');
        if (footero)
            footero.style.visibility='visible';
    }
    if (loadEvent && ie4 && !ns4 && ((navigator.appVersion.indexOf("MSIE 5.0")!= -1) || (mac))) {
        window.resizeBy(1,1);
        window.resizeBy(-1,-1);
    }
    if (loadEvent){
        initImages(); // preload all portalnavigation images
        if(doHighlight){
            highlightSearchWords();
        }
    }
}

function initPopup(loadEvent) {
    if (!ns4){
        setDivContentMinHeightPopup('innerMain', 'stopper', 'header', 'buttonBar', 'footerBar', -8, 'videoBrd', 'galleryBrd');
    }
}

// init() function for popup forms.
// resizes the window to the correct size. Normally the resizing is done by
// the popup opener javascript, but if you follow a direct link (e. g. from
// a newsletter mail) the popup opener is not involved.
function pforminit(loadEvent){
    resizeToInnerSize(385, 510);
    init(loadEvent);
}
// Wrapper function to apply the changes to the footer on the startpage
// only used in startpage because of different div-container structure
function initStart(loadEvent) {
    if (!ns4) {
        setDivContentMinHeight('maincontainer', 'stopper', 'header', 'footer', 'addColumnStartPage');
        footero=document.getElementById('footer');
        footero.style.visibility='visible';
    }
    if (loadEvent && ie4 && !ns4 && ((navigator.appVersion.indexOf("MSIE 5.0")!= -1) || (mac))) {
        window.resizeBy(1,1);
        window.resizeBy(-1,-1);
    }
}

// onMouseOvers for the PortalNavigation
var initSet = false;

function initImages() {
    if (numPortalImages && imgInactNames && imgActNames) {
        for (var i=0; i< numPortalImages; i++) {
            var tempImage = new Image();
            tempImage.src = imgInactNames[i];
            tempImage.src = imgActNames[i];
        }
    }
}

function actImg(obj, num) {
    if (document.images && !ns4 && imgActNames) {
        document.getElementById(obj.id).src = imgActNames[num];
    }
}

function inActImg(obj, num) {
    if (document.images && !ns4 && imgInactNames) {
        document.getElementById(obj.id).src = imgInactNames[num];
    }
}

// popup window opener service function for standard content pages and large view on pictures
var marginWidth = 50;
var marginHeight = 300;
var top = 60;
var left = 80;
var picLarge;

function picLrgOpn(src, width, height) {
	// default width and height (content + margin)
    var winWidth  = width  + marginWidth;
    var winHeight = height + marginHeight;
	
	var args = arguments;
	// if window width or height is given and greater 0 (arguments 3+4), take them
	if(args[3] != null && args[3] > 0) winWidth = args[3];
	if(args[4] != null && args[4] > 0) winHeight = args[4];
	

    //if (height > 300) winHeight = winHeight + height - 300;

    hoehe = winHeight - top;

    if (picLarge!=null && !picLarge.closed) {
        picLarge.resizeTo(winWidth, winHeight);
        picLarge.location.href=src;
        picLarge.focus();
    } else {
        eval('picLarge = window.open(src, "_blank", "width=' + winWidth + ',height=' + winHeight + ',top=' + top + ',left=' + left + ',toolbar=no,scrollbars=yes,resizable=yes,menubar=no,location=no,dependent=no");');
        picLarge.resizeTo(winWidth, winHeight);
        picLarge.focus();
    }
}

// picture gallery opener
var galleryPopup;
var galleryPopupHeight=422;
var galleryPopupWidth=596;

function picGalOpn(src) {
    hoehe = galleryPopupHeight;
    if (galleryPopup!=null && !galleryPopup.closed) {
        galleryPopup.location.href=src;
        galleryPopup.focus();
    } else {
        eval('galleryPopup = window.open(src, "_blank", "width=' + galleryPopupWidth + ',height=' + galleryPopupHeight + ',top=' + top + ',left=' + left + ',toolbar=no,scrollbars=yes,resizable=yes,menubar=no,location=no,dependent=no");');
        galleryPopup.focus();
    }
}

// video popup opener
var videoPopup;
var videoPopupHeight=441;
var videoPopupWidth=573;

function picVidOpn(src) {
    hoehe = videoPopupHeight;
    var opr = navigator.userAgent.indexOf("Opera") != -1;
    if (videoPopup!=null && !videoPopup.closed) {
        videoPopup.location.href=src;
        videoPopup.focus();
    } else {
        if (opr) videoPopupHeight = videoPopupHeight + 10;
        eval('videoPopup = window.open(src, "_blank", "width=' + videoPopupWidth + ',height=' + videoPopupHeight + ',top=' + top + ',left=' + left + ',toolbar=no,scrollbars=yes,resizable=yes,menubar=no,location=no,dependent=no");');
        videoPopup.focus();
    }
}

// print view opener
var printPopup;
var printPopupHeight=510;
var printPopupWidth=603;

function printOpen(src) {
    if (printPopup!=null && !printPopup.closed) {
        printPopup.location.href=src;
        printPopup.focus();
    } else {
        eval('printPopup = window.open(src, "_blank", "width=' + printPopupWidth + ',height=' + printPopupHeight + ',top=' + top + ',left=' + left + ',toolbar=no,scrollbars=yes,resizable=yes,menubar=no,location=no,dependent=no");');
        printPopup.focus();
    }
}

// formPopup view opener
var formPopupHeight=510;
var formPopupWidth=401;

function formPopupOpen(src) {
    var formPopup = null;
    eval('formPopup = window.open(src, "_blank", "width=' + formPopupWidth + ',height=' + formPopupHeight + ',top=' + top + ',left=' + left + ',toolbar=no,scrollbars=yes,resizable=yes,menubar=no,location=no,dependent=no");');
    formPopup.focus();
}

// switch Element to visible when javascript is enabled, example: button bar in popup
function showElement(elemName) {
        if (!ns4) {
                elem=document.getElementById(elemName);
                if (elem != null)
                    elem.style.visibility='visible';
        }
}
function simplifySpecialChars(s) {
    s = s.replace(/\xe4/, "ae");
    s = s.replace(/\xf6/, "oe");
    s = s.replace(/\xfc/, "ue");
    s = s.replace(/\xc4/, "Ae");
    s = s.replace(/\xd6/, "Oe");
    s = s.replace(/\xdc/, "Ue");
    s = s.replace(/\xdf/, "ss");
    return s;
}
function simplifyTimetable(formName, fromName, toName) {
    formular = document.forms[formName];
    from = formular.elements[fromName];
    from.value = simplifySpecialChars(from.value);

    to = formular.elements[toName];
    to.value = simplifySpecialChars(to.value);
}
// Set time and date as default for the ticketinfo-form on startpage
// Examples for 'strFormatDate':
// + dd.mm.yy --> 01.03.03 and 12.10.03
// + d.m.yyyy --> 1.3.2003 and 12.10.2003
// + yyyy/mm/dd --> 2003/03/01 and 2003/10/12
// ddd -- > dd , mmm --> mm, y --> yy, yyy --> yyyy
// Examples for 'strFormatTime':
// + hh:mm --> 06:09 and 13:32
// + h:m --> 6:09 and 13:32
function setTimeAndDate(strFormatDate,strFormatTime,strFormName,strElementNameDate,strElementNameTime)
{
    var oToday = new Date();
    var day = oToday.getDate();
    var month = oToday.getMonth()+1;
    var year = oToday.getFullYear()+"";
    var hours = oToday.getHours();
    var minutes = oToday.getMinutes();

    strFormatDate = strFormatDate.toLowerCase();
    strFormatTime = strFormatTime.toLowerCase();

    regTemp = strFormatDate.match(/d/g);
    if(regTemp.length>=2)if(day<10)day="0"+day;
    strFormatDate = strFormatDate.replace(/d+/,day);

    regTemp = strFormatDate.match(/m/g);
    if(regTemp.length>=2)if(month<10)month="0"+month;
    strFormatDate = strFormatDate.replace(/m+/,month);

    regTemp = strFormatDate.match(/y/g);
    if(regTemp.length<3)year= year.slice(2);
    strFormatDate = strFormatDate.replace(/y+/,year);

    regTemp = strFormatTime.match(/h/g);
    if(regTemp.length>=2)if(hours<10)hours="0"+hours;
    strFormatTime = strFormatTime.replace(/h+/,hours);

    if(minutes<10)minutes="0"+minutes;
    strFormatTime = strFormatTime.replace(/m+/,minutes);

    if(dom)
    {
        oFormElementDate = document.getElementById(strElementNameDate);
        oFormElementTime = document.getElementById(strElementNameTime);
        oFormElementDate.value = strFormatDate;
        oFormElementTime.value = strFormatTime;
    }
    else
    {
        document.forms[strFormName].elements[strElementNameDate].value=strFormatDate;
        document.forms[strFormName].elements[strElementNameTime].value=strFormatTime;
    }
}

/*********************************************************
 * functions for highlighting the searchwords on a page
 ********************************************************/
// global switch. pages can set this to false, if they don't want their text to be
// highlighted.
var doHighlight = true;
//
function highlightSearchWords(){
    var words = extractSearchWords();
    if(words == null || words.length == 0){
        return;
    }
    var node = document.getElementById("content");
    for(var i = 0; i <= words.length; i++){
        highlightWords(node, words[i], i);
    }
}

// extract the search words from the referrer
function extractSearchWords(){
    var words = new Array();
    var ref = document.referrer;
    if(ref == null || ref == "")
        return;
    if (ref.indexOf('?') == -1)
        return;
    var query = ref.substr(ref.indexOf('?') + 1);
    var params = query.split('&');
    for(var i=0; i < params.length; i++) {
        if(params[i].indexOf("q=") > -1){
            words = words.concat(params[i].substr(params[i].indexOf('=')+1).split('+'));
        }
    }
    for(var i=0; i < words.length; i++) {
        words[i] = decodeURIComponent(words[i].toLowerCase());
    }
    return words;
}

function highlightWords(node, word, numColor){
	// some guards
	if(word == null || word.length == 0)
		return;
	if(node == null)
		return;

	if(node.className == "dlBox")
		return;

	// there are only 10 colors in our stylesheet.
	numColor = numColor % 10;

	// iterate through all child nodes
	if(node.hasChildNodes()) {
		var nodeNum;
		for (nodeNum = 0; nodeNum < node.childNodes.length; nodeNum++) {
			var childNode = node.childNodes[nodeNum];
			var className = childNode.className;
			if(className == null || className.indexOf("searchword") == -1){
				highlightWords(childNode, word, numColor);
			}
		}
	}

	// if a text node contains the search word, split it in three nodes:
	// - text node with text up to the word
	// - <span> with the word
	// - text node with text after the word
	if (node.nodeType == 3) { // only for text nodes
		if (node.nodeValue != null && node.nodeValue.length != 0) {
			var result  = matchWord(node.nodeValue, word);
			if (result != null && result.length != 0) {
				var index = node.nodeValue.toLowerCase().indexOf(result.toLowerCase());
				if(index != -1) {
					nodeText = node.nodeValue;

					before = document.createTextNode(nodeText.substr(0, index));
					wordNode = nodeText.substr(index, result.length);
					after = document.createTextNode(nodeText.substr(index + result.length));

					wordNode = document.createElement("span");
					wordNode.className = "searchword" + numColor;
					wordNode.appendChild(document.createTextNode(nodeText.substr(index, result.length)));

					parentNode = node.parentNode;
					parentNode.insertBefore(before, node);
					parentNode.insertBefore(wordNode, node);
					parentNode.insertBefore(after, node);
					parentNode.removeChild(node);
				}
			}
		}
	}
}

function matchWord(text, word) {
	// convert all predefined search wild-cards of the variable 'word' to a regular expression...
	var expression = word;

	// first of all, clean up the search string
	expression = cleanUpSearchString(expression);

	// after that, convert it to a regexp
	expression = expression.replace(/\*/gi, "[^\\s]*");
	expression = expression.replace(/\?/gi, "[^\\s]");
	expression = expression.replace(/~/gi, "");
	expression = "(" + expression + ")";

	// now, match the expression...
	var search = new RegExp(expression, 'gi');
	search.exec(text);

	var result = RegExp.$1;

	return result;
}


function cleanUpSearchString(expression) {
	expression = expression.replace(/\./gi, "");
	expression = expression.replace(/\$/gi, "");
	expression = expression.replace(/\\/gi, "");
	expression = expression.replace(/\^/gi, "");
	expression = expression.replace(/\+/gi, "");
	return expression;
}


function resizeToInnerSize(newWidth, newHeight){
    curWidth = document.body.clientWidth;
    curHeight = document.body.clientHeight;

    var mozCorrection = moz ? 1 : 0;

    var wDiff = newWidth - curWidth;
    wDiff += mozCorrection;
    var hDiff = newHeight - curHeight;

    resizeBy(wDiff, hDiff);
}

