// filename: bvb_gen.js
// version 1.1 - 2006-03-02 - mkr
//
// martin dot krause at gpm dot de
//

var hideDropdownTimeout;

//config	
var objDropdownId = 'dropdown';
var objNavigationId = 'mainnavigation'
var maxDropdowns = 10;
// set window name
	window.name = 'bvb';

// escape from framesets
//if (parent.frames && parent.frames.length != 0) top.location.href = document.location.href;
//else self.focus();

// pageInit
function pageInit() {
	//browsercheck
	is = new BrowserCheck();
	
	//create eventhandlers for link elements
	//createNavFocus() ;

	//visibleWidth
	var myBrowser = new thisBrowserObject();
	
	//center mainnavigation
	//centerNavigation();

	// initialize reload
	//reloadPage(true);
	
}

function showDropdown (dropdownNo) {
	//reset 
	hideDropdown();

	var objDropdownFullId = objDropdownId+dropdownNo;
	var objNavigationFullId = objNavigationId+dropdownNo;

	var objDropdownXPos = 0;
	var objDropdownYPos = 0;

	var objDropdown = document.getElementById(objDropdownFullId);
	if (objDropdown!=null) {
		var objNavigation = document.getElementById(objNavigationFullId);
	
		// compare width of dropdown / mainnav, adjust width if necessary
		if ( objNavigation.offsetWidth > (parseInt(objDropdown.offsetWidth)) ){objDropdown.style.width = parseInt(objNavigation.offsetWidth) + 20+'px';}
	
		// get the x-position of every element surrounding the link, add them
		if (objNavigation && typeof objNavigation.offsetParent != "undefined") 
		{
			while (objNavigation && typeof objNavigation.offsetLeft == "number") 
			{
				objDropdownXPos += objNavigation.offsetLeft;
				objNavigation = objNavigation.offsetParent;
			}
		}
		
		//adjust ff1.5
		if (is.ff15) { objDropdownXPos = parseInt(objDropdownXPos) +1 ; }
			
		// calculate the y-position
		objDropdownYPos = document.getElementById(objNavigationFullId).offsetHeight + 67; // 68px is the visual height
		
		//set positions, fold out dropdown
		objDropdown.style.position = 'absolute';
		objDropdown.style.left = objDropdownXPos + 'px';
		objDropdown.style.top = objDropdownYPos + 'px';
		objDropdown.style.visibility = 'visible';
		
	}
	//fix background
	var obj = document.getElementById(objNavigationFullId);
	//keep class="active"
	if (obj.className.indexOf('active') != -1) {obj.className = 'keepactive';}
	obj.className += " hover ";//needed for opera / ie
	//obj.style.background = 'transparent url(/img/gen_navigation_hover.gif) 100% 100% no-repeat ! important; '; //doesn't work with opera /ie
}

// hide subnavigation pulldown on a click anywhere on the page
function hideDropdown() {
	
	// clear timer
	if (hideDropdownTimeout) {window.clearTimeout(hideDropdownTimeout);}
	
	for (var i = 1; i < maxDropdowns; i++ ) 
	{
		//reset background
		var objNavigationFullId = objNavigationId+i;

		if (document.getElementById(objNavigationFullId)) 
		{ 
			obj = document.getElementById(objNavigationFullId);
			//obj.style.background = 'transparent url(/img/gen_navigation_border.gif) bottom right no-repeat;';
			//restore class="active"
			if (obj.className.indexOf('keepactive') != -1) {obj.className = 'active';}
			if (obj.className.indexOf('hover') != -1) {obj.className = '';} //needed for opera
		}

		//unfold dropdown
		var objDropdownFullId = objDropdownId+i;
		if (document.getElementById(objDropdownFullId)) 
		{
			var obj = document.getElementById(objDropdownFullId);
			obj.style.visibility = 'hidden';
			obj.style.position = 'fixed'; //fix ie text-selection bug
		}
	}

}

function centerNavigation() {
	var obj = document.getElementById(objNavigationId+'wrapper');
	obj.style.position = 'relative';
	obj.style.left = '50%';
	obj.style.marginLeft =  (-1 * (myBrowser.visibleWidth / 4))+'px';
}

function reloadPage(firstcall) {
	if (firstcall == true) 
	{
		//add eventhandler on first call
		window.onresize = reloadPage
	}
	// check if window has changed
	compareBrowser = new thisBrowserObject();
	if (myBrowser.visibleWidth != compareBrowser.visibleWidth) {
		//reload page
		setTimeout('document.location.reload()',250);
	}
}

//hides dropdown after 2secs on mouseout
function hideDropdownOnMouseOut(eventhandler) {

	switch (eventhandler) {
	
		// clear timer
		case 'onmouseover':
			if (hideDropdownTimeout) {window.clearTimeout(hideDropdownTimeout);}
		break;
	
		// set timer
		case 'onmouseout':
			hideDropdownTimeout = window.setTimeout("hideDropdown()",500);
		break;
	}
}

// hide subnavigation pulldown on a click anywhere on the page
// if (is.ns4) document.captureEvents(Event.MOUSEUP)
if (hideDropdown) document.onmouseup = hideDropdown

// general functions 
/*----------------------------------------------------------------------------*/

// removes/restore default text, using obj.name if no defaultText is given
function removeDefault(obj, eventhandler, defaultText) {
	switch (eventhandler) {
		
		case 'onfocus' :
			if ( obj.value == firstLetterUpperCase(obj.name) ) { obj.value = '';}
			else if ( (typeof(defaultText) != 'undefined') && obj.value == defaultText ) { obj.value = '';}
		break;
		
		case 'onblur' :
			if ( obj.value == '' && (typeof(defaultText) == 'undefined') ) { obj.value = firstLetterUpperCase(obj.name) ;}
			else if ( obj.value == '' && (typeof(defaultText) != 'undefined') ) { obj.value = defaultText;}
		break; 
	
	}
}

// returns obj as Obj; used by removeDefault
function firstLetterUpperCase(obj) {
	var formatedString = obj.slice(0,1).toUpperCase() + obj.slice(1) ;
	return formatedString;
}

// disables Button to prevent more than one click and enhance usabillity
function disableButton(objId){
	objButton = document.getElementById(objId);
	objButton.disabled = true;
	objButton.value = 'Bitte warten';
	objButton.style.cursor = 'wait'
}

function thisBrowserObject () {
	// visible width
	if (window.innerWidth) { this.visibleWidth = window.innerWidth;}
	else if (document.body && document.body.clientWidth) {this.visibleWidth = document.body.clientWidth;}
	else {this.visibleWidth = false;}
}


