/*CVS Add-ins*/
/* 
$Revision: 1.20 $
$RCSfile: behaviour.js,v $
author: JDE
Copyright (c) A&B.face2net GmbH
*/

/* + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + */
/*
    behaviour.js fuer DTMB
    08.05.2007 JDE
*/
/* + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + */
var agt = navigator.userAgent.toLowerCase();
var ie  = (agt.indexOf("msie") != -1);
var ff  = (agt.indexOf("firefox") != -1);
var op = (agt.indexOf("opera") != -1);
var sf    = ((agt.indexOf('applewebkit') != -1) || (navigator.vendor == "Apple Computer, Inc."));

/**
* executes functions when the page loads.
* @param func_name name of function
*/

function addLoadEvent(func_name) {
	var lastonload = window.onload;
	if (typeof window.onload != 'function') 
		{ window.onload = func_name; } 
	else {	window.onload = function() {
      			lastonload(); func_name(); }
  	}
}



var a = new Array("bg_more.gif","bg_more_hover.gif");
var b = new Array();
var i;
for (i = 0; i < a.length; i++) {
  b[i] = new Image();
  b[i].src = '/fileadmin/templates/css/styleimages/'+a[i];
}


/**
* sets an attribute to a node.
* @param elem name of node
* @param attr kind of attribute
* @param value attribute value
*/
function set_attr(elem, attr, value) {
  var myNode = elem; myNode.setAttribute(attr, value ); }
/**
* sets an URL to a node.
* @param elem name of node
* @param url url
*/
function set_href(elem, url) {
  var myNode = elem; myNode.setAttribute("href",url );  }
  
  
/**
* returns the list of objects with a certain class name and tagname
* @param class_name name of class
* @param tag_name name of tag
*/  
function get_elementsByClassName(class_name, tag_name){
  var all_elements; var class_elements=new Array();
  var j=0; var i; var teststr;
  if(document.getElementsByTagName){ 
    all_elements=document.getElementsByTagName(tag_name);   
  }
  
  for(i=0;i<all_elements.length;i++){
    if(all_elements[i].className.indexOf(class_name)!=-1) {
	  teststr=","+all_elements[i].className.split(" ").join(",")+",";
	  if(teststr.indexOf(","+class_name+",")!=-1){
        class_elements[j]=all_elements[i];
		j++;
      }
    }
  }
  
  return class_elements;
  
}  

function get_childbyTag(boxparent, tagname){
   var inner = boxparent.getElementsByTagName(tagname);
   if(inner.length > 0) return inner[0];
   else return false;
}

function get_parentbyTag(boxchild, tagname){
   var outer = boxchild.parentNode;
   if(outer.tagName!=tagname) return false;
   else if (outer.length > 0) return outer[0];
   else return false;
}


/**
* sets a class to a certaion element.
* @obj element object
* @param new_class class name
*/
function set_class(obj, new_class  ){
    obj.setAttribute("class", new_class);
     if(ie) obj.className = new_class;
}

  
  
/**
* sets a title to a node.
* @param elem name of node
* @param title node title
*/
function set_title(elem, title) {
  var myNode = elem; myNode.setAttribute("title", title ); }
/**
* sets a textnode to a parent node.
* @param par name of parent
* @param text value of title
*/
function set_textNode(par, text) {
  if (!document.createTextNode) return false;
  var myNode = par; myNode = document.createTextNode(text);
  return true; }
  
/**
* creates a link with link text, title and url.
* @param txt link text
* @param ltitle link title
* @param url  link destination
* @return link element (a);
*/
function create_link(txt, ltitle, url ) {
  if (!document.createElement || !document.createTextNode) return false;
  var myNode = document.createElement("a");
  var myNode_text = document.createTextNode(txt);
  myNode.appendChild(myNode_text);
  set_title(myNode, ltitle);
  set_href(myNode, url) ;
  return myNode;}
  

/**
* creates a print link using create_link(txt, ltitle, url ) and set_attr(elem, attr, value)
*/
function create_printLink() {
  var printlink = create_link("Drucken", "Diese Seite drucken", "javascript:window.print();" );
  set_attr(printlink, "onclick", "window.print();return false;");
  /*do not use onkeypress otherwise in FF the tab will get stuck in the printlink */ }


/**
* creates a list item with a print link.
*/
function create_printLinkItem() {
  if (!document.getElementById) return false;
  var antecessorId = "f_impress";
  var myAntecessor = document.getElementById(antecessorId);
  
   if(myAntecessor){
     var myList = myAntecessor.parentNode;
     if(myList.tagName.indexOf("ul")){
       var myItem = document.createElement("li");
       var myseperator = document.createElement("span");
       var spacer = document.createTextNode(" ")
       myseperator.appendChild(spacer);
       var printlink = create_link("Drucken", "Diese Seite drucken", "javascript:window.print();" );
       set_attr(myItem, "id", "f_print");
       myItem.appendChild(myseperator);
       myItem.appendChild(printlink);
       set_attr(printlink, "onclick", "window.print();return false;");
       myList.insertBefore(myItem,myAntecessor );
       //myList.appendChild(myItem);
     }
  } return true;
}

/* + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
 Lupe fuer verlinkte kleine Contentbilder
 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + */

function create_magnifier_behaviour(){
   var my_imglist_left = get_elementsByClassName("img_left","div");
   var my_imglist_right = get_elementsByClassName("img_right","div");
   for( var i = 0; i< my_imglist_left.length; i++) {
     var img = get_childbyTag(my_imglist_left[i], "img");
     var caption = get_childbyTag(my_imglist_left[i], "p");
     if(img.parentNode){
      if(img.parentNode.tagName=="A"){
        var mylink = img.parentNode;
        set_magnifierlink(mylink,img,caption);
      }
     }
   }
   
   for( var j = 0; j< my_imglist_right.length; j++) {
     var img = get_childbyTag(my_imglist_right[j], "img");
     var caption = get_childbyTag(my_imglist_right[j], "p");
     if(img.parentNode.tagName=="A") {
       var mylink = img.parentNode;
       set_magnifierlink(mylink,img,caption);
       }
   }
}

function set_magnifierlink(mylink,img,caption){
  var title = mylink.getAttribute("title");
  var url = mylink.getAttribute("href");
  var target = "_blank";
  if(url !=null ){
    var magnifierlink = document.createElement("a");
    set_href(magnifierlink, url);
    set_attr(magnifierlink, "target", target)
  
    if(title != null){
      set_title(magnifierlink, title);
    }
    if(caption.firstChild!=null){
       var caption_txt = caption.firstChild;
       magnifierlink.appendChild(caption_txt);
     
       caption.appendChild(magnifierlink);
       caption.className = "magnifier";
     }
  }
}

/* + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
 font-scaling based structure changes: based on a script by L. Carvalho
 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + */
 
var TextResizeDetector = function() { 
  var el  = null;
  var iIntervalDelay  = 200;
  var iInterval = null;
  var iCurrSize = -1;
  var iBase = -1;
  var aListeners = [];
 	
  var createControlElement = function() {
    el = document.createElement('span');
    el.id='textResizeControl';
    el.innerHTML='&nbsp;';
    el.style.position="absolute";
    el.style.left="-9999px";
    el.style.top="-9999px";
      
    var elC = document.getElementById(TextResizeDetector.TARGET_ELEMENT_ID);
    if(elC){ elC.insertBefore(el,elC.firstChild); }
	  iBase = iCurrSize = TextResizeDetector.getSize(); 
    };
    
 	function _stopDetector() {
      window.clearInterval(iInterval); iInterval=null;
	};
    
	function _startDetector() {
      if(!iInterval) { iInterval = window.setInterval('TextResizeDetector.detect()',iIntervalDelay); }
	};
 	
    function _detect() {
      var iNewSize = TextResizeDetector.getSize();
      if(iNewSize!== iCurrSize) {
        for(var i=0; i<aListeners.length;i++) {
          var aListnr = aListeners[i];
          var oArgs = {  iBase: iBase,iDelta:((iCurrSize!=-1) ? iNewSize - iCurrSize + 'px' : "0px"),iSize:iCurrSize = iNewSize};
          if(!aListnr.obj) {
            aListnr.fn('textSizeChanged',[oArgs]);
		  }else{
            aListnr.fn.apply(aListnr.obj,['textSizeChanged',[oArgs]]);
          }//else
		}//for
      } //if
 	  return iCurrSize;
 	};
    
	var onAvailable = function() {
      if(!TextResizeDetector.onAvailableCount_i ) { TextResizeDetector.onAvailableCount_i =0; }
      
      if(document.getElementById(TextResizeDetector.TARGET_ELEMENT_ID)) {
        TextResizeDetector.init();
        if (TextResizeDetector.USER_INIT_FUNC){ TextResizeDetector.USER_INIT_FUNC(); }
        TextResizeDetector.onAvailableCount_i = null;
      }else{
        if (TextResizeDetector.onAvailableCount_i<600) { 
          TextResizeDetector.onAvailableCount_i++;
          setTimeout(onAvailable,200)
		}
	  }
	};
    
    setTimeout(onAvailable,500);

 	return {
      init: function() {
        createControlElement();		
		_startDetector();
      },
      
      addEventListener: function(fn,obj,bScope) {
        aListeners[aListeners.length] = {
		  fn: fn,
		  obj: obj
	    }
        return iBase;
	  },
      
      detect: function() { 
        return _detect();
      },
      
      getSize: function() {
        var iSize;
		return el.offsetHeight;
	  },
 			
      stopDetector: function() {
        return _stopDetector();
      },
      
      startDetector: function() {
        return _startDetector();
	  }
 	}
}();

function write_headstyles(selector, declarations) {
 if (!document.styleSheets || document.styleSheets.length == 0) {
  return false;
 }
 var stylesheet = document.styleSheets[document.styleSheets.length-1];
 if (stylesheet.insertRule) {
   //FF
   var new_rule_number = stylesheet.insertRule(selector + " { " + declarations + " }", stylesheet.cssRules.length);
   if (stylesheet.cssRules[new_rule_number].cssText.length > 0) {
    return true;
   }
 } else if (stylesheet.addRule) {
  // IE
  stylesheet.addRule(selector, declarations);
  return true;
 }
 
 return false;
}

function remove_headstyles () {
  if (!document.styleSheets || document.styleSheets.length == 0) {
    return false;
  }
 
  var stylesheet = document.styleSheets[document.styleSheets.length-1];
  
  if (stylesheet.deleteRule) {
    while(stylesheet.cssRules.length >= 1) stylesheet.deleteRule(stylesheet.cssRules.length-1);
    return true;
  } 
 return false;
}


function init()  {
  if (document.getElementById){
    var iBase = TextResizeDetector.addEventListener(onFontResize,null);
    var check = document.getElementById('navService');
    var stiftung = false;
    if(get_elementsByClassName("sti", "body").length == 0 && check){
      switch_websites(iBase);
      
    }else if(get_elementsByClassName("sti", "body").length == 1 && document.getElementById("start")){
      scale_boxteaser( false );
    }
  }
}
	
TextResizeDetector.TARGET_ELEMENT_ID = 'navService';
TextResizeDetector.USER_INIT_FUNC = init;

function onFontResize(e,args) {
  var smaller = false;
 
  if((args[0].iDelta).indexOf("-") != -1){
    var smaller = true;
	
  } 
  //nur fuer Firefox engines und IE
  var check = document.getElementById('navService');
  var stiftung = false;
  
  if(get_elementsByClassName("sti", "body").length == 0 && check){
    if(ie||ff) check_onFontResize(smaller, args[0].iSize);
    
  }else if(get_elementsByClassName("sti", "body").length == 1 && document.getElementById("start")){
      scale_boxteaser( smaller );
  }
 
  
}	

function check_onFontResize(smaller, font_size){

 if (!document.getElementById) return false;
 if( !document.getElementById("header") || !document.getElementById("headerbox")) return false;

 
 //alert(height_header+" : "+height_headerbox);
 if(!smaller){
   var height_header = document.getElementById("header").clientHeight;
   var height_headerbox = document.getElementById("headerbox").clientHeight;
   if( height_header > (height_headerbox)){
     remove_headstyles();
     scaleup_headerbox(height_header, height_headerbox);
   }
   switch_websites(font_size);
 } else{
    var height_header = document.getElementById("header").clientHeight;
    var height_headerbox = document.getElementById("headerbox").clientHeight;
    if( height_header < height_headerbox){
     remove_headstyles();
     scaledown_headerbox(height_header, height_headerbox); 
   }
   switch_websites(font_size);
 }
 return true;

}

function initiate_scalebehaviour(){
  if (!document.getElementById) return false;
  if( !document.getElementById("header") || !document.getElementById("headerbox")) return false;
  
  var height_header = document.getElementById("header").clientHeight;
  var height_headerbox = document.getElementById("headerbox").clientHeight;
  
  if( height_header > (height_headerbox)){
    
    scaleup_headerbox(height_header, height_headerbox);
  }
  return true;
}

function switch_websites(font_size){
   if(document.getElementById("websites")){
      //var width_websites = document.getElementById("websites").clientWidth;
      
        (font_size>=38)? document.getElementById("websites").setAttribute("id", "websites_scale"): document.getElementById("websites").setAttribute("id", "websites");
        if(font_size<=38 && font_size>=26) {
          write_headstyles("#websites ul li a", "min-height: 2.3em;");
          write_headstyles("#websites p.sti a", "min-height: 2.3em;");
          write_headstyles("#websites ul li.active span.active strong","border: 1px solid #fff; min-height: 2.45em;");
        }
       
    }else if(document.getElementById("websites_scale") ){
      var width_websites = document.getElementById("websites_scale").clientWidth;
      
       write_headstyles("#websites ul li a", "min-height: 2.3em;");
       write_headstyles("#websites ul li.active span.active strong ", "border: 1px solid #fff; min-height: 2.45em;");
       (font_size<38)? document.getElementById("websites_scale").setAttribute("id", "websites"): document.getElementById("websites_scale").setAttribute("id", "websites_scale");
    
        
   }//else if
}

function scaleup_headerbox(height_header, height_headerbox){
  if(ie) var declaration_headerbox = "height:".concat(height_header+6+"px");
  else   var declaration_headerbox = "height:".concat(height_header+1+"px");
  
  if(ie) var declaration_infobox = "height:".concat(height_header+"px");
  else   var declaration_infobox = "height:".concat(height_header-4+"px");
  
  var declaration_navSub = "min-height:".concat(height_header-13+"px");
  write_headstyles("#header #headerbox", declaration_headerbox);
  write_headstyles("#header #headerbox .infobox", declaration_infobox);
  write_headstyles("#header #headerbox .infobox", declaration_infobox);
  write_headstyles("#header #headerbox #navSub ul", declaration_navSub);
  write_headstyles("#header #headerbox #navSub ul ul", "min-height: 1em");
}

function scaledown_headerbox(height_header, height_headerbox){
  if(ie) var declaration_headerbox = "height:".concat(height_header+6+"px");
     else   var declaration_headerbox = "height:".concat(height_header+1+"px");
     
     if(ie) var declaration_infobox = "height:".concat(height_header+"px");
     else   var declaration_infobox = "height:".concat(height_header-4+"px");
     write_headstyles("#header #headerbox", declaration_headerbox);
     write_headstyles("#header #headerbox .infobox", declaration_infobox);
}

/*Boxteaser fuer Startseite*/
function scale_boxteaser(smaller){
  remove_headstyles();

  var boxes = get_elementsByClassName("box_teaser", "div");
  var default_height_box = 258;
  var default_height_div = 240;
  var height_div = 242;
  
  for (var i=0; i<boxes.length; i++){
    var current_height_box = boxes[i].clientHeight;
    var current_link = get_childbyTag(boxes[i], "a");
    if (!smaller) {
      var current_innerdiv = get_childbyTag(boxes[i], "div");
      var current_height = current_innerdiv.clientHeight;
      if(current_height > height_div){
        height_div = current_height;
      }//if
      
    }else if(smaller){
      var current_innerdiv = get_childbyTag(boxes[i], "div");
      var current_height = current_innerdiv.clientHeight;
      if(current_height > height_div){
       height_div = current_height;
      }
      
    }
    var url = current_link.getAttribute("href");
    var title = current_link.getAttribute("title");
    var morebox = get_parentbyTag(current_link, "span");
    current_link.className = "button";
    set_links_toBoxelements(url, boxes[i],current_innerdiv, title, morebox);
  }//for  
  
  
  
  if(ie||ff) set_boxteaserstyles_ieff(height_div);
  else if(sf||op) set_boxteaserstyles_sfop(boxes,height_div );
}

function set_boxteaserstyles_ieff(height_div){
  if(ie) {
    write_headstyles("body.sti #start .content .box_teaser div", "height:".concat(height_div+"px;"));
    write_headstyles("body.sti #start .content .box_teaser", "background:".concat(" url(/fileadmin/templates/css/styleimages/_sti/bg_boxteaser_bottom.gif) no-repeat bottom -3px;"));
    write_headstyles("body.sti #start .content .box_teaser", "padding-bottom:".concat("8px;"));
    write_headstyles("body.sti #start .content .box_teaser h3", "background:".concat("#fff;"));
    write_headstyles("#start .content .box_teaser span.more", "padding-bottom:".concat("8px;"));
    
    /* height:"+height_link+"px;"*/
  }else if(ff){
    write_headstyles("body.sti #start .content .box_teaser div", "min-height:".concat(height_div+"px;"));
    write_headstyles("body.sti #start .content .box_teaser h3", "background:".concat("#fff;"));
    write_headstyles("body.sti #start .content .box_teaser", "background-image:".concat("url(/fileadmin/templates/css/styleimages/_sti/bg_boxteaser_bottom.gif);"));
    write_headstyles("body.sti #start .content .box_teaser", "background-position:".concat("bottom right;"));
    write_headstyles("#start .content .box_teaser span.more", "padding-bottom:".concat("10px;"));
    write_headstyles("body.sti #start .content .box_teaser", "padding-bottom:".concat("8px;"));
  }
}

function set_boxteaserstyles_sfop(boxes, height_div){
    for (var j=0; j<boxes.length; j++){
      var current_innerdiv = get_childbyTag(boxes[j], "div");
      if(sf) current_innerdiv.style.display = "inline";
      current_innerdiv.style.height = " ".concat(height_div+"px");
      boxes[j].style.background = "url(/fileadmin/templates/css/styleimages/_sti/bg_boxteaser_bottom.gif) no-repeat bottom right";
      boxes[j].style.padding = "0 0 8px 0";
      if(sf) current_innerdiv.style.display = "block";
    }  
  
}

function set_links_toBoxelements(url, box, current_innerdiv, title, morebox){
  var paragraph = get_childbyTag(box, "p");
  var img = get_childbyTag(box, "img");
  
  if(!img){ /*nicht vorhanden*/}else{
   var imglink = create_link("",  title, url );
   imglink.appendChild(img);
   if(!paragraph) current_innerdiv.insertBefore(imglink, morebox);
   else current_innerdiv.insertBefore(imglink, paragraph);
  }
   /*
   //nicht mehr benoetigt
  if(!paragraph) { 
  //nicht vorhanden 
  } 
   
  else{
   

     if(paragraph.hasChildNodes){
       var children = paragraph.childNodes;
       
       if(children.length==1){
        var plink = document.createElement("a");
        set_title(plink, title);
        set_href(plink, url);
        var ptxt = paragraph.firstChild
        plink.appendChild(ptxt);
        paragraph.appendChild(plink);
       }
     } 
	
  }*/ 
}

function switch_breadcrumbstyle(){
    if(document.getElementById && document.getElementById("breadcrumb") && !document.getElementById("start") && !document.getElementById("dontChangeBCS")) {//&& !document.getElementById("dontChangeBCS") eingefügt, widiger,18.5.2009
       var my_dl = document.getElementById("breadcrumb");
       if(my_dl) {
         var my_parent = my_dl.getElementsByTagName("p")[0];
         var my_children 	= my_parent.childNodes;   
         var num_children = my_children.length; 
         for(var i = num_children-1; i > 1; i--) {  
            if(my_children[i].nodeType != 1) {
              var new_child = document.createElement("img");
              set_attr(new_child, "src", "/fileadmin/templates/images/breadcrumb.gif"); 
              set_attr(new_child, "alt", ""); 
              my_parent.replaceChild(new_child, my_children[i]);
            }
         }
       }
    }
}



/* + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + */
/* loaded events  */
if(ie||ff) addLoadEvent(initiate_scalebehaviour);
addLoadEvent(create_printLinkItem);
addLoadEvent(create_magnifier_behaviour);
addLoadEvent(switch_breadcrumbstyle);



