/*
Filename: bm.js
Created:
  2008/01/14 - HM

Description:
  JavaScript functions used on most pages in THIS application
  
Revisions:
*/

/**************************
    Menu-hover for IE (http://www.htmldog.com/articles/suckerfish/dropdowns/)
**************************/
sfHover = function()
{   try
	{	var i, j;
		//allSubMenus = new Array();
		var allSubMenus = getElementsByClassName("submenu");
		var mainSubMenu = document.getElementById("submenu");
		
		if (mainSubMenu)
			allSubMenus.push(mainSubMenu);
		
		sfEls = new Array();
		
		for (i=0; i<allSubMenus.length; i++)
		{	//sfEls = sfEls.concat(allSubMenus[i].getElementsByTagName("li"));
			var liElems = allSubMenus[i].getElementsByTagName("li");
			for (j=0; j<liElems.length; j++)
				sfEls.push(liElems[j]);
		}
				
		// var sfEls = document.getElementById("submenu").getElementsByTagName("li");
		
	    for (i=0; i<sfEls.length; i++)
	    {   sfEls[i].onmouseover = function()
	        {   this.className += " sfhover";
	        	// drawing error on IE7 if other javascripts change something somewhere
	        	if (isIE7())
	        	{	//redrawPage();
	        		var hoverMenu = this.getElementsByTagName("ul");
					if (hoverMenu && hoverMenu[0])
						hoverMenu[0].style.left = "auto";
				}
	        }
	        sfEls[i].onmouseout = function()
	        {   this.className = this.className.replace(new RegExp(" sfhover\\b"), "");
				// drawing error on IE7 if other javascripts change something somewhere
	        	if (isIE7())
	        	{	//redrawPage();
	        		var hoverMenu = this.getElementsByTagName("ul");
					if (hoverMenu && hoverMenu[0])
						hoverMenu[0].style.left = "-999em";
				}
	        }
	    }
	 }
	 catch(e) {}
}
if (window.attachEvent) // MS event handling
    window.attachEvent("onload", sfHover);
    
/**************************
 MIN-HEIGHT/WIDTH FOR IE 6
***************************/
function checkSubMenuForIE(submenu)
{	var hoverMenus;
    var currentHoverMenu;

	if (submenu)
	{	if (!submenu.style)
			submenu.style = "";
		// sub menu min width
		if (submenu.offsetHeight < 29)
	       submenu.style.height = "29px";
	    // hover menu min width
		hoverMenus = getElementsByClassName("hoverMenu", submenu);
        for (var i=0; i<hoverMenus.length; i++)
        {   currentHoverMenu = hoverMenus[i];
        	if (currentHoverMenu.offsetWidth < 130)
        		currentHoverMenu.style.width = "130px";
        }
    }
}

function checkMinHeightAndWidth()
{	checkSubMenuForIE(document.getElementById("submenu"));
	
	var subMenuClasses = getElementsByClassName("submenu");
	for (i=0; i<subMenuClasses.length; i++)
		checkSubMenuForIE(subMenuClasses[i]);

	/*var submenu = document.getElementById("submenu");
    var hoverMenus;
    var currentHoverMenu;
    
	if (submenu)
	{	// sub menu min width
		if (submenu.offsetHeight < 28)
	       submenu.style.height = "28px";
	    // hover menu min width
		hoverMenus = getElementsByClassName("hoverMenu", submenu);
        for (var i=0; i<hoverMenus.length; i++)
        {   currentHoverMenu = hoverMenus[i];
        	if (currentHoverMenu.offsetWidth < 130)
        		currentHoverMenu.style.width = "130px";
        }
    }*/
}
if (window.attachEvent && isIELess7())
   window.attachEvent("onload", checkMinHeightAndWidth);
    
/******************************************
    switch images on and off for print mode
*******************************************/
var display_img_print = false;
function switchDisplayImages()
{
	try
	{
		//go through the domtree of article and set display to none
	    divs = document.getElementById("content").getElementsByTagName("div");
	    for (i=0; i<divs.length; i++)
	    {
	        switch (divs[i].className)
	        {
	            case "imageCenter":
	            case "imageContainer":
	            case "imageGallery":
	            case "imageHeadline":
	            case "imageLeft":
	            case "imageRight":
	            case "inlineGallery":
	                divs[i].style.display = (display_img_print ? "block" : "none");
	
	            default: break;
	        }
	    }
	}
	catch(e){}
    display_img_print = !display_img_print;
}

/***************************
	TAB MANAGMENT
****************************/
function switchTab(tabArray, activeTab)
{	try
	{	for (var i=0; i<tabArray.length; i++)
		{	var element = document.getElementById(tabArray[i]);
			if (!element)
				continue;
			setElementVisible( element, element.id == activeTab );
		}
	}
	catch(e){}
}
// onclick="switchTab(new Array('prio1TabPanelsContent_8','prio1TabPanelsContent_15'),'prio1TabPanelsContent_15')"


function switchOneTab( currentId, nextId )
{
	var currentEl = document.getElementById( currentId );
	var nextEl = document.getElementById( nextId );

	setElementVisible( currentEl, false );
	setElementVisible( nextEl, true );
  
} // switchOneTab


function setElementVisible( element, visible )
{
	if( element )
	{
		var classes = element.className;
		classes = classes.replace("hidden","");
		if( visible === false )
			classes += " hidden";
		classes = trim(classes);
		element.className = classes;
	} // if
} // setElementVisible


/************* SCALE FONTS ***************/
function startScaleFonts()
{	var newCSSClass = getCookie("scaledSize", "scaledNormal");
//alert(newCSSClass);
	checkReplaceFonts(newCSSClass, "scaledNormal");
}

function scaleElements(objects2Scale, oldClass, newClass)
{	for (i=0; i<objects2Scale.length; i++)
	{	var className = objects2Scale[i].className;
		className = className.replace(oldClass, newClass);
		className = trim(className);
		objects2Scale[i].className = className;
	}
}

function checkReplaceFonts(newClass, oldClass)
{	try
	{	var scaler = document.getElementById("scaler");
		var activeScalerItem = null;
		var objects2Scale = getElementsByClassName(oldClass, null, "div");
		var iFramesToScale = document.getElementsByTagName("iframe");
		
		if (scaler!=null)
		{	scaler.style.visibility = "visible";
			// reset all subclasses - no active class
			scaler.innerHTML = scaler.innerHTML.replace("active",""); 
			activeScalerItem = document.getElementById(newClass);
			if (activeScalerItem != null)
				activeScalerItem.className += " " + "active";
		}
		
		//alert(objects2Scale.length);
		//alert(iFramesToScale.length);
		
		// scale within this document
		scaleElements(objects2Scale, oldClass, newClass);
		
		// scale within iframes
		for (i=0; i<iFramesToScale.length; i++)
		{	var iFrameDoc = getIFrameDocument(iFramesToScale[i]);

			if (iFrameDoc)
			{	objects2Scale = getElementsByClassName(oldClass, iFrameDoc);
				scaleElements(objects2Scale, oldClass, newClass);
			}
		}

		// drawing error on Opera - force redraw
		if (window.opera)
			redrawPage();
	}
	catch(e)
	{	//alert(e);
	}
}

// the cookie value for cookie "scaledSize" is always the CSS class name
// the id of the scaler elements is always the CSS class name
function setScaleSize(newClass)
{	var oldClass = getCookie("scaledSize", "scaledNormal");
	setCookie("scaledSize", newClass, 365);
	//alert(newClass);
	checkReplaceFonts(newClass, oldClass);
}

function generateSectionList(list)
{	if (!list)
		return;
		
	var sectionsAnker = getElementsByClassName("sectionAnkerName");
	var sectionsDisplay = getElementsByClassName("sectionHeadline");
	var ankerName;
	var newHTML = "";
	
	for (var i=0; i<sectionsDisplay.length; i++)
	{	if (sectionsAnker.length > i)
			newHTML += '<li><a href="#' + sectionsAnker[i].name + '">' + sectionsDisplay[i].innerHTML + "</a></li>";
	}
	
	list.innerHTML = newHTML;
}


// check for empty advertisement places
function checkAdvertisements()
{
	checkAdvertisementPlace( "advertisementHeaderTop" );
	checkAdvertisementPlace( "advertisementHeaderRight" );
	checkAdvertisementPlace( "advertisementPrio2" );
	// right column
	for( var index = 0; index < 10; index++ )
	{
		checkAdvertisementPlace( "advertisementRightcolumn"+index );
	}
} // checkAdvertisements

function checkAdvertisementPlace( id )
{
	var el = document.getElementById( id );
	if( el && el.innerHTML && el.innerHTML.indexOf("leer2.gif") > -1 )
	{
		el.className += " hidden";
	}
} // checkAdvertisementPlace


// open print version page
function openPrintWindow( url )
{
	url = appendUrlParameter( url, "print", "yes" );
	
	// add slideshow image page parameter
	if( slideshowPage != null )
	{
		url = appendUrlParameter( url, "slideshowpage", slideshowPage );
	}

	window.open( 
		url,
		'printversion',
		'menubar=no,toolbar=no,status=no,width=870,height=500,scrollbars=yes,resizable=yes'
		);
		
	return false;
} // openPrintWindow


// opens any popup window as programmed below
// the url is the only important parameter
// used for instance online in article ID 1092871
// could be generalized by giving the height and with as parameter
function openGeneralPopup(objectId)
{ var url= objectId + "&amp;rand=" + Math.random();
  var wname = "";
  var wheight=570;
  var wwidth=808;
  
  var windowtop = ((screen.height - wheight) /2) -20;
  var windowleft = (screen.width - wwidth) /2;
  
  var browserName=navigator.appName;
  var browserVer=parseInt(navigator.appVersion);
  
  if (browserName=="Netscape")
  { if (browserVer>4)
    { wwidth = wwidth+15;
          wheight = wheight+5;
      }
  }
  
  // create unique window name from the given url -> IE does not like all characters from an url as window name
  try
  { wname = "";
    
    for (i=0; i<objectId.length; i++)
    { var ch = objectId.charAt(i); 
      var charCode = objectId.charCodeAt(i);
      
      // only numbers and simple characters are accepted
      if ((charCode>=48 && charCode<=57) // digits
        || (charCode>=65 && charCode<=90) // capital letters
        || (charCode>=97 && charCode<=122) // small letters
        )
      { wname += ch;
      }
    }
  }
  catch (e)
  { wname = "";
    //alert(e);
  }
  
  openwin = window.open(url,wname,"toolbar=0,width=" + wwidth + ",height=" + wheight + ",location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=1,copyhistory=0,left=" + windowleft + ",top=" + windowtop);
  openwin.focus();
}