// Accordionmenu
jQuery().ready(function(){
  
	jQuery('.accordion').accordion({
		header: 'div.title',
		active: false,
		alwaysOpen: false,
		animated: false,
		autoheight: false
	});
	initLytebox();  
});


$(function() {
  var imageCounter = 0;  
  $('.box_zweispaltig img, .infogroup img').each(function(index){
    imageCounter++;
    var image = $(this).attr('rel', 'over_'+imageCounter);
    var imageName = image.attr('src');
    imageName.match(/^(.*)(\.\w+)$/);
    var imageNameMouseover = RegExp.$1 + '_mittel' + RegExp.$2;
    var imageNameGross = RegExp.$1 + '_gross' + RegExp.$2;
    jQuery("<img>").attr("src", imageNameMouseover).load((function(i) {
      return function(){             
        $('<div class="bild_mouseover" style="display:none;" id="over_'+ i +'"><div class="img_links">&nbsp;</div><div class="img_mitte"><a href="'+ imageNameGross +'" rel="lytebox[bildreihe]" title=""><img src="'+ imageNameMouseover +'" alt="" title="" height="237" width="316" /></a></div><div class="img_rechts">&nbsp;</div></div>').insertAfter(image);
        initLytebox();        
      }      
    })(imageCounter));
    
    $(image).mouseenter(function(){      
      $('#'+$(this).attr('rel')).show().mouseleave(function() {
        $(this).hide();
      });    
    });       
  });
  
});
 
/**
* Function is using two hidden fields which define values of default and maximal height.
* Size of the iframe tries to adjust to its content.
* If source of the iframe is placed under different domain then we don't adjust anything - security reasons.
* It is used by RedDot content class 300 - 125 Iframe
**/
function reloadResize(name) {
  // console.log('reloadResize - start: ' + name);
  document.domain = 'transmission-it.de';
  
  // this function is caled form iframe so we need to go to parent window
  var frame = this.parent.document.getElementById(name);
  var max = this.parent.document.getElementById(name + '_MAX');
  var def = this.parent.document.getElementById(name + '_DEF');

  if (frame && max && def) {
    var defVal = def.value;
    var maxVal = max.value;

    // console.log('defVal: ' + defVal + ', maxVal: ' + maxVal);

    var framebody = null;
    // assign the default height - will be used if an error occurs
    var newHeight = def; 

    // getting the body of this window must be done in try/catch block because
    // if the iframe content will be in other domain than the parent window then 
    // exception will be thrown
    try {
      framebody = frame.contentWindow.document.documentElement;
      // set the height to 1 - otherwise if content of the iframe will be
      // smaller than the iframe
      // you will get wrong heightscroll value
      frame.height = 1;
      newHeight = framebody.scrollHeight;

      // for some pages scrollHeight from framebody is returned as window size
      var backupHeight = frame.contentWindow.document.body.scrollHeight;
      if (newHeight == 1) {
        newHeight = backupHeight;
      }
    } catch (err) {
      // application is placed in other domain than the parent page
      // console.error('domains confilict');
      newHeight = defVal;
    }

    // console.log('newHeight: ' + newHeight);

    if (newHeight > maxVal) { // check if it's not larger than the max height
      // console.log('use max height');
      frame.height = maxVal; // assign new height
    } else {
      // console.log('use calculated height');
      frame.height = newHeight; // assign new height

      // check if there is a scrollbar on the bottom
      var scrolbarHeight = parseInt(framebody.offsetHeight) - parseInt(framebody.clientHeight);

      // in IE7 scrolbarHeight is negative number,
      // so it creates smaller IFrame (scrollbars are visible). We need to get
      // positive number.
      if (scrolbarHeight < 0) {
        scrolbarHeight = -scrolbarHeight;
      }

      // if page contains only plain text then sometimes clientHeight is
      // returned as zero and then correction will be wrong
      if (parseInt(framebody.clientHeight) == 0) {
        scrolbarHeight = 0;
      }

      if ((scrolbarHeight != 0) && (newHeight != defVal)) {
        // if it's equal to def then don't resize
        // correct the height of this frame by scrollbar size, otherwise you
        // will see right scrollbar
        // console.log('adjust height because of scrollbars: ' + scrolbarHeight);
        newHeight = parseInt(frame.height) + parseInt(scrolbarHeight);
        frame.height = newHeight;
      }
    }
  } else {
    // console.error('unable to fetch elements' + frame + "-" + max + "-" + def);
  }
}
