/*******************************************************************************
	Filename		: lib.js

	Created			: 4 Sep 2006 (15:09:25)
	Created by		: Manfred Schneiderbauer, manfred.schneiderbauer@world-direct.at

	Last Updated	: 08 AUG 2008 (25:60:60)
	Updated by		: Manfred Schneiderbauer, manfred.schneiderbauer@world-direct.at

	Comments		:
*******************************************************************************/

/*
lib.js for KRAL

Author:			  manfred.schneiderbauer@world-direct.at
Version:      1.342
*/

// redirect to English language if needed
if(navigator.cookieEnabled)
  {
  var l1=navigator.language; if(!l1||l1==undefined){l1='en';}
  var l2=navigator.systemLanguage; if(!l2||l2==undefined){l2='en';}
  var l3=navigator.userLanguage; if(!l3||l3==undefined){l3='en';}
  if((l1.indexOf('de')==-1&&l2.indexOf('de')==-1&&l3.indexOf('de')==-1))
    {
    if(location.href.toLowerCase().indexOf('l=')==-1&&getCookie('languageSwitched')==null)
      {
      setCookie('languageSwitched',true);
      location.href=location.href+'?L=2';
      }
    }
  if((location.href.indexOf('kral-usa.int')!=-1||location.href.indexOf('kral-usa.com')!=-1)&&getCookie('languageSwitched')==null)
    {
    setCookie('languageSwitched',true);
    //location.href=location.href+'us/';
    location.href=location.href+'?L=2';
    }
  }

// deactivate 2nd and 3rd top menu levels on start page
// currently deactivated; only necessary when the Flash Slideshow is in use
function modifyTopmenu(){
  var allDivs=d('clib_MenuWrapper').getElementsByTagName('div');
  for(var i=0;i<allDivs.length;i++)
    {
      if(allDivs[i].className.indexOf('menu_dropdown')!=-1)
        {
          allDivs[i].style.display='none';
        }
    }
}
//clib_onload("if(window.isStartpage){modifyTopmenu();}");

// Change start page's document title
function removeHomeFromTitle(){
if(document.title.indexOf('Home')!=-1){document.title=document.title.replace('Home','');}
}
clib_onload('removeHomeFromTitle();');


// Create simple imagemap dynamics on start page
// (imagemap dynamics on Kontakt are generated inline in default_navigation.ts!)
function createImageMapHover(){
if(d('imCountriesStart')){
  var im=d('imCountriesStart');
  var map=d('mpWorld');
  im.onmouseout=function(){createImageMapHover_swapImage();}
  var areasArr=new Array(); areasArr=map.getElementsByTagName('area');
  for(var i=0;i<areasArr.length;i++)
    {
     areasArr[i].onmouseover=function(){createImageMapHover_swapImage(this.href);}
     areasArr[i].onmouseout=function(){createImageMapHover_swapImage();}
    }
}
}
function createImageMapHover_swapImage(continent){
 var im=d('imCountriesStart');
 if(continent)
  {
    im.src='fileadmin/dev/img/map/map_('+continent.substring(continent.length-2,continent.length-1)+').gif';
  }else{
    im.src='fileadmin/dev/img/map/map_world.gif';
  }
}
clib_onload('window.setTimeout("createImageMapHover();",2000)');

// Superscript all (R)s in H4s
function superscriptRs(){
var allH4s=document.getElementsByTagName('h4');
for(var i=0;i<allH4s.length;i++)
  {
    if(escape(allH4s[i].innerHTML).indexOf('%AE')!=-1)
      {
       allH4s[i].innerHTML=unescape(escape(allH4s[i].innerHTML).replace('%AE','%3Csup%3E%AE%3C/sup%3E'));
      }
    try{
    if(escape(allH4s[i].nextSibling.innerHTML).indexOf('%AE')!=-1)
      {
       allH4s[i].nextSibling.innerHTML=unescape(escape(allH4s[i].nextSibling.innerHTML).replace('%AE','<span style=font-weight:normal;>%3Csup%3E%AE%3C/sup%3E</span>'));
      //allH4s[i].nextSibling.style.fontSize='11px';
      //allH4s[i].nextSibling.style.lineHeight='11pt';
      }
    }catch(error){}
  }
}
clib_onload("superscriptRs();");

// title images slideshow
function titleSlideshow(){
var imgs=d('dvFlashSlideshow');
// define display
var allImgsArr=imgs.getElementsByTagName('img');
var imgContainer='<div style="position:absolute;left:0px;top:0px;background-color:#ffffff;" id="dvSlide_0"><a><img width="480" height="250" src="/fileadmin/dev/img/s.gif"></a></div>';
var ic=1;
var today=d('dv_slideshowDate').innerHTML.toLowerCase().replace('. ','.');
// make day two-digit
today=today.replace(' 1.',' 01.');
today=today.replace(' 2.',' 02.');
today=today.replace(' 3.',' 03.');
today=today.replace(' 4.',' 04.');
today=today.replace(' 5.',' 05.');
today=today.replace(' 6.',' 06.');
today=today.replace(' 7.',' 07.');
today=today.replace(' 8.',' 08.');
today=today.replace(' 9.',' 09.');
// collect relevant images
var thisI=1; var thisTitleArr=''; var thisTitle=''; var t=0; var showThis=false; var dontShowThis=false;
for(var i=0;i<allImgsArr.length;i++) // go thru all images
  {
   if(allImgsArr[i].src.indexOf('upload')!=-1)
    {
    thisTitleArr=allImgsArr[i].title.split(',');
    showThis=false; dontShowThis=false; // reset flags
    for(t=0;t<thisTitleArr.length;t++) // go thru all date commands
      {
      thisTitle=thisTitleArr[t].replace(',','').toLowerCase();
      thisTitle=Trim(thisTitle); // remove bordering whtspcs
      thisTitle=thisTitle.replace('. ','.'); // remove whtspc after dot, if there
      if(thisTitle.indexOf('.')==1) // if day is one-digit, make it two-digit
        {
        thisTitle=thisTitle.replace('1.','01.');
        thisTitle=thisTitle.replace('2.','02.');
        thisTitle=thisTitle.replace('3.','03.');
        thisTitle=thisTitle.replace('4.','04.');
        thisTitle=thisTitle.replace('5.','05.');
        thisTitle=thisTitle.replace('6.','06.');
        thisTitle=thisTitle.replace('7.','07.');
        thisTitle=thisTitle.replace('8.','08.');
        thisTitle=thisTitle.replace('9.','09.');
      }
      if(today.indexOf(thisTitle)!=-1||today.indexOf(thisTitle.replace('! ',''))!=-1||today.indexOf(thisTitle.replace('!',''))!=-1||today.indexOf(thisTitle.replace('nicht ',''))!=-1||today.indexOf(thisTitle.replace('not ',''))!=-1||showThis)
        { // if the date command is valid, or a valid has been found...
         showThis=true;
         if(thisTitle.indexOf('!')!=-1||thisTitle.indexOf('nicht')!=-1||thisTitle.indexOf('not')!=-1) // if this is a NOT command...
          {
           if(today.indexOf(thisTitle.replace('! ',''))!=-1||today.indexOf(thisTitle.replace('!',''))!=-1||today.indexOf(thisTitle.replace('nicht ',''))!=-1||today.indexOf(thisTitle.replace('not ',''))!=-1)
            {
            showThis=false;
            dontShowThis=true;
            }
          } else {
          dontShowThis=false;
          showThis=true;
          }
         }
      }
      if(showThis&&!dontShowThis) // if image matches date command and no match had a NOT...
          {
          imgContainer+= '<div id="dvSlide_'+thisI+'" style="position:absolute;top:0px;left:0px;">';
          thisI++;
          if(allImgsArr[i].parentNode.tagName.toLowerCase()=='a') // conect link
            {
            imgContainer+='<a href="'+allImgsArr[i].parentNode.href+'" target="'+allImgsArr[i].parentNode.target+'">';
            }
          imgContainer+='<img ';
          if(allImgsArr[i].alt) // connect alt
            {
            imgContainer+=' alt="'+allImgsArr[i].alt+'"';
            }
          imgContainer+=' title="'+allImgsArr[i].alt+'"'; // connect title to alt
          imgContainer+=' src="'+allImgsArr[i].src+'" border="0" width="480" height="250">';
          allImgsArr[i].valid=true; // also indicator for text appender!
          allImgsArr[i].hreflink=allImgsArr[i].parentNode.href;
          if(allImgsArr[i].parentNode.tagName.toLowerCase()=='a')
            {
            imgContainer+='</a>';
            }
          imgContainer+='</div>';
          }
    ic++;
    }
  }
titleSlideshow_appendTexts();
imgs.innerHTML=imgContainer; // write images block
window.slidesLoaded=0;
try{
// attach onload handlers to first 2 slideshow images
imgs.getElementsByTagName('img')[1].onload=function(){window.slidesLoaded++;}
if(imgs.getElementsByTagName('img')[2])
  {
   // attach only if there is a second image; otherwise, mark it as loaded at once
   imgs.getElementsByTagName('img')[2].onload=function(){window.slidesLoaded++;}
  } else {
   window.slidesLoaded++;
  }
titleSlideshow_Wait();
}catch(error){}
}
// wait for the first two images to be loaded
function titleSlideshow_Wait(){
  if(window.slidesLoaded<2)
    {
     window.setTimeout("titleSlideshow_Wait();",250);
    } else {
     d('dvFlashSlideshow').style.visibility='visible';
     titleSlideshow_Run(0);
    }
}

// start slide show
function titleSlideshow_Run(iterator){
var arrows=d('dv_TitleArrowTeasers').getElementsByTagName('img');
window.outDv=d('dvSlide_'+Number(iterator));
if(d('dvSlide_'+Number(iterator+1)))
  {
  window.inDv=d('dvSlide_'+Number(iterator+1));
  window.inArrow=arrows[iterator];
  } else {
  window.inDv=d('dvSlide_1');
  window.inArrow=arrows[0];
  }

iterator++;
if(iterator>d('dvFlashSlideshow').getElementsByTagName('img').length-1){iterator=1;}

// execute transition
titleSlideshow_Transit(0);
// re-run
window.inDv.style.zIndex=2; window.inDv.style.visibility='visible';
window.outDv.style.zIndex=1; window.outDv.style.visibility='visible';
if(d('dvFlashSlideshow').getElementsByTagName('img').length>2)
  {
  window.setTimeout("titleSlideshow_Run("+iterator+");",6000);
  }
}
function titleSlideshow_Transit(step){

if(isIE5up)
  {
  window.inDv.style.filter='progid:DXImageTransform.Microsoft.Alpha(opacity='+step+')';
  if(step>=25)
    {
    window.inArrow.style.filter='progid:DXImageTransform.Microsoft.Alpha(opacity='+step+')';
    }
  window.outDv.style.filter='progid:DXImageTransform.Microsoft.Alpha(opacity='+100-step+')';
  } else {
  window.inDv.style.opacity=step/100;
  window.inDv.style.MozOpacity=step/100;
  window.inDv.style.KhtmlOpacity=step/100;
  if(step>=25)
    {
    window.inArrow.style.opacity=step/100;
    window.inArrow.style.MozOpacity=step/100;
    window.inArrow.style.KhtmlOpacity=step/100;
    }
  window.outDv.style.opacity=(100-step)/100;
  window.outDv.style.MozOpacity=(100-step)/100;
  window.outDv.style.KhtmlOpacity=(100-step)/100;
  }

if(window.CPUSpeed>=2500){step=step+1.5;}
if(window.CPUSpeed<2500&&window.CPUSpeed>=1250){step=step+1;}
if(window.CPUSpeed<1250){step=step+5;}

step=step+2;
// flip z-index in the middle
if(step==50)
  {
   window.inDv.style.zIndex=2;
   window.outDv.style.zIndex=1;
  }
// reset arrows on start
if(step<8)
  {
   var arrows=d('dv_TitleArrowTeasers').getElementsByTagName('img');
   for(var i=0;i<arrows.length;i++)
    {
     if(isIE5up)
      {
      arrows[i].style.filter='progid:DXImageTransform.Microsoft.Alpha(opacity=25)';
      } else {
      arrows[i].style.opacity=.25;
      arrows[i].style.MozOpacity=.25;
      arrows[i].style.KhtmlOpacity=.25;
      }
    }
  }
// continue fading
if(step<=100)
  {
   window.setTimeout("titleSlideshow_Transit("+step+");",25);
  } else {
   window.inDv.style.filter=''; window.inDv.style.opacity=''; window.inDv.style.MozOpacity=''; window.inDv.style.KhtmlOpacity='';
   window.outDv.style.filter=''; window.outDv.style.opacity=''; window.outDv.style.MozOpacity=''; window.outDv.style.KhtmlOpacity='';
   window.outDv.style.visibility='hidden';
  }
}

function transferTeaserHeadline(){
try
  {
  d('txTeaserHeadTarget').innerHTML=d('txTeaserHeadSource').innerHTML;
  d('txTeaserHeadSource').parentNode.getElementsByTagName('img')[0].style.display='none';
  d('txTeaserHeadSource').parentNode.getElementsByTagName('br')[0].style.display='none';
  d('txTeaserHeadSource').parentNode.getElementsByTagName('br')[1].style.display='none';
  window.txAlleInfos=d('txAlleInfos').innerHTML;
  } catch (error) {
  window.setTimeout("transferTeaserHeadline();",250);
  }
}

// strip whitespaces
function Trim(str){
while(str.substring(str.length-1,str.length)==' '){str=str.substring(0,str.length-1);}
while(str.substring(0,1)==' '){str=str.substring(1,str.length);}
return str;
}

// show matching texts
function titleSlideshow_appendTexts(){
var allImgs=d('dvFlashSlideshow').getElementsByTagName('img'); var validImgs=new Array(); var ic=0;
for(var i=0;i<allImgs.length;i++)
  {
  if(allImgs[i].src.indexOf('/uploads/pics/')!=-1)
    {
    validImgs[ic]=allImgs[i];
    ic++;
    }
  }
var imgTexts=d('dvFlashSlideshow').getElementsByTagName('h4');
var tHTML=''; var actSlides=
tHTML+='<div class="dv_titleteasertext"><h3 class="h3_titlehead"><span id="txTeaserHeadTarget"></span></h3></div><div class="dv_teaserheadunderline">&nbsp;</div><table cellspacing="0" cellpadding="0">';
for(i=0;i<imgTexts.length;i++)
  {
    if(validImgs[i].valid!=undefined)
      {
      tHTML+='<tr class="tr_titleteaser"><td valign="top" class="td_titleteaserarrow"><img src="/fileadmin/dev/img/arrow-right.gif"></td><td valign="top" class="td_titleteaserteaser">';
      tHTML+='<h2>'+imgTexts[i].innerHTML+'</h2>';
      tHTML+=imgTexts[i].nextSibling.innerHTML;
      if(validImgs[i].hreflink!=undefined&&validImgs[i].hreflink.indexOf('http')!=-1)
        {
        tHTML+='<br/><a name="an_alleinfos" href="'+validImgs[i].hreflink+'"></a>';
        }
      tHTML+='</td></tr>';
      }
  }
tHTML+='</table>';
window.tHTML=tHTML;
displayTeasers();
}
/** 20100708 HBR@world-direct.at */
function addHintTarget() {
	try {	
		window.tHTML+=d('txallNews').innerHTML;
		d('txallNews').innerHTML = '';
	} catch(error){}
}
clib_onload("addHintTarget()"); // init
/** end */

// mark the first teaser arrow
function markFirstArrow(){
if(d('dv_TitleArrowTeasers'))
  {
    var fi=d('dv_TitleArrowTeasers').getElementsByTagName('img')[0].style;
  if(isIE5up)
    {
    fi.filter='progid:DXImageTransform.Microsoft.Alpha(opacity=100)';
    } else {
    fi.opacity=1;
    fi.MozOpacity=1;
    fi.KhtmlOpacity=1;
    }
  }else{
  window.setTimeout("markFirstArrow();",250);
  }
}

// display the teaser texts
function displayTeasers(){
if(d('dv_TitleArrowTeasers'))
  {
  d('dv_TitleArrowTeasers').innerHTML=window.tHTML;
  transferTeaserHeadline();
  markFirstArrow();
  var allAs=document.getElementsByTagName('a');
  for(var i=0;i<allAs.length;i++)
    {
    if(allAs[i].name=='an_alleinfos')
      {
      allAs[i].innerHTML=window.txAlleInfos;
      }
    }
  } else {
  window.setTimeout("displayTeasers();",500);
  }
}

// Remove umlauts from Tmailforms obsubmit and add the referrer page
function removeUmlauts(formobj){

// fill and format referrer
try{
var ref=document.referrer;
ref=ref.substring(ref.lastIndexOf('/')+1,ref.length).replace('.html','').replace('.php','');
ref=ref.substring(0,1).toUpperCase()+ref.substring(1,ref.length);
if(ref.indexOf('?')!=-1){ref=ref.substring(0,ref.indexOf('?'));}
while(ref.indexOf('_')!=-1){ref=ref.replace('_',' ');}
if(d('hdRef').value==''){d('hdRef').value=ref;}
}catch(error){}

var allInputs=formobj.getElementsByTagName('input');
var allTextareas=formobj.getElementsByTagName('textarea');
var escapedVal='';
for(var i=0;i<allInputs.length;i++) // loop thru input elements
  {
   escapedVal=escape(allInputs[i].value); // escape value string for find/replace
   while(escapedVal.indexOf('%E4')!=-1){escapedVal=escapedVal.replace('%E4','ae');}
   while(escapedVal.indexOf('%F6')!=-1){escapedVal=escapedVal.replace('%F6','oe');}
   while(escapedVal.indexOf('%FC')!=-1){escapedVal=escapedVal.replace('%FC','ue');}
   while(escapedVal.indexOf('%C4')!=-1){escapedVal=escapedVal.replace('%C4','Ae');}
   while(escapedVal.indexOf('%D6')!=-1){escapedVal=escapedVal.replace('%D6','Oe');}
   while(escapedVal.indexOf('%DC')!=-1){escapedVal=escapedVal.replace('%DC','Ue');}
   while(escapedVal.indexOf('%DF')!=-1){escapedVal=escapedVal.replace('%DF','ss');}
   allInputs[i].value=unescape(escapedVal); // write unescaped back
  }
escapedVal='';
for(var i=0;i<allTextareas.length;i++) // same for textareas
  {
   escapedVal=escape(allTextareas[i].value);
   while(escapedVal.indexOf('%E4')!=-1){escapedVal=escapedVal.replace('%E4','ae');}
   while(escapedVal.indexOf('%F6')!=-1){escapedVal=escapedVal.replace('%F6','oe');}
   while(escapedVal.indexOf('%FC')!=-1){escapedVal=escapedVal.replace('%FC','ue');}
   while(escapedVal.indexOf('%C4')!=-1){escapedVal=escapedVal.replace('%C4','Ae');}
   while(escapedVal.indexOf('%D6')!=-1){escapedVal=escapedVal.replace('%D6','Oe');}
   while(escapedVal.indexOf('%DC')!=-1){escapedVal=escapedVal.replace('%DC','Ue');}
   while(escapedVal.indexOf('%DF')!=-1){escapedVal=escapedVal.replace('%DF','ss');}
   allTextareas[i].value=unescape(escapedVal);
  }
}

// deactivate ENTER key on Tmailform forms
function suppressEnterInTmailforms(){
if(getElementByClass('div','tx-pilmailform-pi1')) // get Tmailform, if there
  {
   // get all input and select elements, but leave textareas alone
   var allInps=getElementByClass('div','tx-pilmailform-pi1').getElementsByTagName('input');
   var allSels=getElementByClass('div','tx-pilmailform-pi1').getElementsByTagName('input');
   // attach handlers
   for(var i=0;i<allInps.length;i++)
    {
    allInps[i].onkeypress=suppressEnterInTmailforms_handle;
    }
   for(i=0;i<allSels.length;i++)
    {
    allSels[i].onkeypress=suppressEnterInTmailforms_handle;
    }
  }
}
clib_onload("suppressEnterInTmailforms()"); // init
function suppressEnterInTmailforms_handle(e){ // handler function
var key='';
if(isIE5up)
  {
   key=window.event.keyCode;
  } else {
   key=e.keyCode ;
  }
if(key==13)
  {
  return false; // suppress if enter
  }
}

// cut the top banner down to 452px width to avoid layout disturbtion
function cutTopBanner(){try{
var bdiv=getElementsByClass('td','td_bannertop')[0].getElementsByTagName('div')[0];
bdiv.style.overflow='hidden';
if(!isIE7up)
  {
  bdiv.style.width='455px';
  }else{
  bdiv.style.width='443px';
  }
}catch(error){}}
clib_onload("cutTopBanner()");

// insert dummy stretcher into left menu area if empty
function correctEmptyLeftMenuWidth(){try{
 if(getElementsByClass('td','td_leftmenu')[0].getElementsByTagName('a').length==0)
  {
   var stretcher=document.createElement('img');
   stretcher.style.height='1px';
   stretcher.style.width='237px';
   stretcher.style.visibility='hidden';
   stretcher.src='fileadmin/dev/img/s.gif';
   getElementsByClass('td','td_leftmenu')[0].appendChild(stretcher);
  }
}catch(error){}}
clib_onload("correctEmptyLeftMenuWidth()");

// heavily patch top menu on Safari
function patchTopMenuOnSafari(){if(d('clib_MenuWrapper')){try{
// set new positions
var lis=d('clib_MenuWrapper').getElementsByTagName('li');
var tl=''; var tls=new Array(); var tc=0;
for(var i=0;i<lis.length;i++)
  {
  tl=lis[i];
  if(tl.id.indexOf('1tmenu')!=-1&&i!=0)
    {
    tl.style.position='absolute';
    tl.style.top=0; tl.style.left='100px';
    tls[tc]=tl; tc++;
    }
  }
tls[tc-1].style.left='200px';

// handle sub menu visibility
var allD=document.getElementsByTagName('div'); var thisMain=''; var thisSub='';
for(i=0;i<allD.length;i++)
  {
   if(allD[i].className.indexOf('menu_dropdown')!=-1)
    {
     thisSub=allD[i];
     thisMain=thisSub.parentNode;
     // ensure hovering
     thisMain.style.display='block';
     thisMain.style.width='100px';
     // attach event handlers
     thisMain.onmouseover=patchTopMenuOnSafari_handleEvent_on;
     thisMain.onmouseout=patchTopMenuOnSafari_handleEvent_off;
     thisSub.onmouseover=patchTopMenuOnSafari_handleEvent_on;
     thisSub.onmouseout=patchTopMenuOnSafari_handleEvent_off;
     // correct sub menu positioning
     thisSub.style.position='relative';
     thisSub.style.top='1px';
     thisSub.style.zIndex=i; // prevent sub-sub menu overlaying by next main's sub
     thisMain.id='dvMainMenuItem_'+i; thisSub.id='dvSubMenuContainer_'+i; // make items accessible by event handlers
     thisSub.style.visibility='hidden'; // invisibilize all subs
    }
  }
}catch(error){}}}
if(isSafari){clib_onload("patchTopMenuOnSafari()");}
function patchTopMenuOnSafari_handleEvent_on(e){ // show sub menus
var thisMenuIndex=this.id.replace('dvMainMenuItem_','').replace('dvSubMenuContainer_','');
d('dvSubMenuContainer_'+thisMenuIndex).style.visibility='visible';
e.stopPropagation(); // prevent bubbling
}
function patchTopMenuOnSafari_handleEvent_off(e){ // hide sub menus
var thisMenuIndex=this.id.replace('dvMainMenuItem_','').replace('dvSubMenuContainer_','');
d('dvSubMenuContainer_'+thisMenuIndex).style.visibility='hidden';
var allD=document.getElementsByTagName('div'); var thisMain=''; var thisSub='';
e.stopPropagation(); // prevent bubbling
}
