/* 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");
var moz = navigator.appName=="Netscape" && navigator.appCodeName=="Mozilla" && !document.all;
var mac = navigator.userAgent.indexOf("Mac")!= -1;

window.status = "DB Carsharing"; // 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;
	}
    // iDivsHeight = iDivsHeight+150;
    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
	
	iWinInnerHeight = document.body.clientHeight; // store window height of the inner browser area temporarily
    //alert('iWinInnerHeight: '+iWinInnerHeight+' - ');
	if (arguments.length == 4) {
		tmpHeight = getDivsHeight(arguments[2], arguments[3]);
	}
	else if (arguments.length == 3) {
		tmpHeight = getDivsHeight(arguments[2]);
	} // calculate the height of the page elements except the content area; using the getDivsHeight function
	
	if(oStopperDiv) {
      if (iWinInnerHeight > (tmpHeight + oStopperDiv.offsetTop + oStopperDiv.offsetHeight)) {
        oDivcontent.style.height = iWinInnerHeight - tmpHeight + mozCorrection;
      } else {
        oDivcontent.style.height = oStopperDiv.offsetTop + oStopperDiv.offsetHeight + mozCorrection;
      }
	}
    //if there is more space on the page than the header and footer height, than the main layer is set to a new height
}

// 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');
		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
}

// 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('content', 'stopper', 'header', 'footer');
		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) {
	var winWidth = width + marginWidth;
	var winHeight = height + marginHeight;
		
	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();
	}
}

// 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();
	}	
}

// recommend view opener
var recommendPopup;
var recommendPopupHeight=510;
var recommendPopupWidth=401;

function recommendOpen(src) {
	if (recommendPopup!=null && !recommendPopup.closed) {
		recommendPopup.location.href=src;
		recommendPopup.focus();
	} else {
		eval('recommendPopup = window.open(src, "_blank", "width=' + recommendPopupWidth + ',height=' + recommendPopupHeight + ',top=' + top + ',left=' + left + ',toolbar=no,scrollbars=yes,resizable=yes,menubar=no,location=no,dependent=no");');
		recommendPopup.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';
		}
}
// 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;
	}
}

// Text ein- und ausklappen

var angezeigt = false;

function versteckt() 
{ 
	if (angezeigt)
	{
		document.getElementById('ghost').style.display = 'none';
		angezeigt = false;
	}
	else
	{
		document.getElementById('ghost').style.display = 'block';
		angezeigt = true;
	}
}

// Accordion-Effekt

var Accordion = Class.create();

Accordion.prototype = {
	initialize: function(id, tag, name) {
		this.id = id;
		this.headerTag = tag.toUpperCase();
		this.instance = name;
		this.headingClassName = (arguments[3] || "panel");
		this.contentClassName = (arguments[4] || "panelBody");
		this.panels = new Array();

		var tags = $(id).getElementsByTagName('*');		
		for ( var i = 0; i < tags.length; i++) {
			switch(tags.item(i).tagName) {
				case this.headerTag:
					tags.item(i).style.cursor = "pointer";
					tags.item(i).onclick = this._returnEvalCode(this.instance);
					break;

				default:
					if (tags.item(i).className == this.headingClassName) {
						tags[i]._index = this._returnIndex(this.panels.length);
						this.panels[this.panels.length] = tags.item(i);
						//the line above is same meaning as "this.panels.push(tags.item(i));"
						
						if (this.panels.length == 1) {
							tags.item(i).id = "visible";
						}
					}

					if (tags.item(i).className == this.contentClassName) {
						tags.item(i).style.display = "none";
					}
					break;

			}
		}
		this.length = this.panels.length;
		this.show(0, true);
	},

	show: function(index, force) {
		if ( (index >= this.length) || (index < 0) ) {
			//alert("index out of range");
			return;
		}

		if ( $('visible') == this.panels[index] ){
			if (force) {
				//alert("force to show the visible element.");
				for(var i = 0; i < this.length; i++) {
					if(this._body(this.panels[i]).style.display != "none") {
						new Effect.SlideUp(this._body(this.panels[i]));
					}
				}
				new Effect.SlideDown(this._body(this.panels[index]));
				return;
			}
			
			//alert("it's already shown now.");
			return;
		}

		//alert("show another element.");
		new Effect.Parallel(
			[
				new Effect.SlideUp( this._body($('visible')) ),
				new Effect.SlideDown( this._body(this.panels[index]) )
			], {
				duration: 0.2
			}
		);
	
		$('visible').id = "";
		this.panels[index].id = "visible";
		return;
	},

	_body: function(e) {
		var tags = e.getElementsByTagName('*');
		for( var i=0; i<tags.length; i++) {
			if (tags.item(i).className == this.contentClassName) {
				return tags.item(i);
			}
		}
	},

	_returnIndex: function(i) {
		return function() {
			return i;
		}
	},

	_returnEvalCode: function(s) {
		return function(){
			eval(s + ".show(" + this.parentNode._index() + ");");
		}
	}
};
