////////////
// jQuery //
////////////

jQuery.ajaxSetup({dataFilter:function(data,type)
{
  if(type=="json" && typeof data=="string" && data.length>=4 && data.substring(0,2)=="/*" && data.substring(data.length-2,data.length)=="*/")
    return data.substring(2,data.length-2);
  return data;
}});

$(function()
{
  $("a").focus(function(event)
  {
    $(this).blur();
  });
  $("input[type='image']").focus(function(event)
  {
    $(this).blur();
  });
});

/////////
// FAQ //
/////////

var faqdivnode;
var faqimgnode;
var plusimage;
var minusimage;

plusimage=new Image();
plusimage.src="/media/common/icon_bullet_plus.gif";
minusimage=new Image();
minusimage.src="/media/common/icon_bullet_minus.gif";

function showFAQItem(h3node) {
  // Hide current item
  if(faqdivnode!=null)
  {
    faqdivnode.style.display="none";
    faqimgnode.src=plusimage.src;
    if(h3node.nextSibling==faqdivnode)
    {
      faqdivnode=null;
      faqimgnode=null;
      return;
    }
  }
  // Show new item
  faqdivnode=h3node.nextSibling;
  faqdivnode.style.display="block";
  faqimgnode=h3node.firstChild.firstChild;
  faqimgnode.src=minusimage.src;
}

///////////
// PopUp //
///////////

var IE6;
if(navigator.appName=="Microsoft Internet Explorer" && navigator.appVersion.indexOf("MSIE 6.")!=-1)
  IE6=true;

function showPopUp(e,url) {
  var cx,cy,cw,ch,px,py,pw,ph,popup;
  
  // Calculate position and size of the client area
  cx=e.screenX-e.clientX;
  cy=e.screenY-e.clientY;
  cw=$(window).width();
  ch=$(window).height();
  // Popup size and position
  pw=670;
  ph=400;
  px=Math.round(cx+(cw-pw)/2);
  py=cy+100;
  // Open popup
  popup=window.open(url,"_blank","width="+pw+",height="+ph+",left="+px+",top="+py+",resizable=yes,scrollbars=yes,status=yes");
  popup.resizeTo(pw,ph);
  popup.moveTo(px,py);
  return false;
}

/////////////
// GreyBox //
/////////////

var greyboxcontentid;
var greymagnifierimage;
var whitemagnifierimage;
var orangemagnifierimage;

greymagnifierimage=new Image();
greymagnifierimage.src="/media/common/overlay_magnifier.gif";
orangemagnifierimage=new Image();
orangemagnifierimage.src="/media/common/overlay_magnifier_orange.gif";
whitemagnifierimage=new Image();
whitemagnifierimage.src="/media/common/overlay_magnifier_white.gif";

function inCorner(e,element,border)
{
  var x,y,ex,ey,mx,my,ep;

  // Calculate element position (border-box)
  // Note: 
  // 1. The body must not have a border.
  // 2. There must not be a "position: relative" element in the chain to the body.
  ex=0;
  ey=0;
  ep=element;
  while(ep.offsetParent!=null)
  {
    ex+=ep.offsetLeft;
    ey+=ep.offsetTop;
    ep=ep.offsetParent;
    if(ep.clientLeft)
    {
      ex+=ep.clientLeft;
      ey+=ep.clientTop;
    }
  }
  if(document.documentElement.clientLeft)
  {
    ex+=document.documentElement.clientLeft;
    ey+=document.documentElement.clientTop;
  }
  // Calculate mouse position
  mx=$(window).scrollLeft()+e.clientX;
  my=$(window).scrollTop()+e.clientY;
  // Calculate mouse position relative to element: (0,0) ist bottom-right
  x=element.clientWidth+2*border-1-(mx-ex);
  y=element.clientHeight+2*border-1-(my-ey);
  return x>=0 && y>=0 && x+y<30+border;
}

function setGreyBoxSizeIE6()
{
  document.getElementById("greyboxbackground").style.position="absolute";
  document.getElementById("greyboxbackground").style.width=document.body.offsetWidth+"px";
  document.getElementById("greyboxbackground").style.height=document.body.offsetHeight+"px";
  document.getElementById("greyboxbody").style.width=document.body.offsetWidth+"px";
}

function showGreyBoxScroll(contentid,scroll) {
  if(greyboxcontentid!=null)
    document.getElementById(greyboxcontentid).style.display="none";
  document.getElementById(contentid).style.display="inline";
  greyboxcontentid=contentid;
  if(IE6)
  {
    setGreyBoxSizeIE6();
    window.onresize=setGreyBoxSizeIE6;
  }
  document.getElementById("greyboxbackground").style.display="block";
  document.getElementById("greyboxbody").style.display="block";
  if(scroll)
    window.scrollTo(0,0);
  return true;
}

function showGreyBoxNoTrack(contentid) {
  document.getElementById("greyboxlinks").style.marginTop="77px";
  document.getElementById("greyboxlinks").style.marginLeft="36px";
  document.getElementById("greyboxlinks").style.width="674px";
  $("#greyboxbutton-select").hide();
  return showGreyBoxScroll("greyboxcontent-"+contentid);
}

function showGreyBox(contentid) {
  trackPage(window.location.pathname.replace(/;jsessionid=.*/,"")+"/greybox-info-"+contentid);
  $("#greyboxbutton-close").text("schließen").attr("href","javascript:closeGreyBox()");
  showGreyBoxNoTrack(contentid);
}

function showGreyBoxFileUpload() {
  var gh,ch,cy,top;
  
  // greyboxlinks padding: 5px ... 5px ...; and greyboxlink height: 18px
  gh=5+18+5 + 1+27 + 12+10+2+10+12 + 27+1;
  ch=$(window).height();
  cy=$(window).scrollTop();
  top=Math.max(0,Math.round((ch-gh)/2))+cy;

  document.getElementById("greyboxlinks").style.marginTop=top+"px";
  document.getElementById("greyboxlinks").style.marginLeft="136px";
  document.getElementById("greyboxlinks").style.width="474px";
  $("#greyboxbutton-select").hide();
  $("#greyboxbutton-close").text("abbrechen").attr("href","javascript:cancelFileUpload()");
  return showGreyBoxScroll("greyboxcontent-fileupload",false);
}

function setImageSrcIE6() {
  var img;

  img=document.getElementById("greyboxcontent-image").firstChild;
  img.src=img.src;
}

function showGreyBoxImage(e,element,imageid,linkurl,imageurl,alt,gw,gh,border) {
  var gy,ch,cy,top,left,image,contentimage,close;
  
  if(inCorner(e,element,border) || linkurl=='')
  {
    trackPage(window.location.pathname.replace(/;jsessionid=.*/,"")+"/greybox-image-"+imageid);
    // greyboxlinks padding: 5px ... 5px ...; and greyboxlink height: 18px
    gy=5;
    ch=$(window).height();
    cy=$(window).scrollTop();
    top=Math.max(0,Math.max(0,Math.round((ch-gh)/2)-8-5-18-5)+cy-gy+5);
    left=Math.floor((762-gw)/2)-8;
  
    image=new Image();
    image.src=imageurl;
    contentimage=document.getElementById("greyboxcontent-image");
    contentimage.firstChild.src=image.src;
    if(IE6)
      window.setTimeout("setImageSrcIE6()",1);
    contentimage.firstChild.alt=alt;
    contentimage.firstChild.style.width=gw+"px";
    contentimage.firstChild.style.height=gh+"px";
    contentimage.style.marginLeft=left+"px";
    document.getElementById("greyboxlinks").style.marginTop=top+"px";
    document.getElementById("greyboxlinks").style.marginLeft=left+"px";
    document.getElementById("greyboxlinks").style.width=gw+"px";
    if(linkurl!='')
      $("#greyboxbutton-select").attr("href",linkurl).show();
    else
      $("#greyboxbutton-select").hide();
    $("#greyboxbutton-close").text("schließen").attr("href","javascript:closeGreyBox()");
    return showGreyBoxScroll("greyboxcontent-image",false);
  }
  else
  {
    window.location.href=linkurl;
    return false;
  }
}

function closeGreyBox() {
  document.getElementById("greyboxbackground").style.display="none";
  document.getElementById("greyboxbody").style.display="none";
}

function mouseMoveMagnifier(e,element,border) {
  if(inCorner(e,element,border))
    mouseOverMagnifier(element);
  else
  {
    if(element.src!=whitemagnifierimage.src)
      element.src=whitemagnifierimage.src;
  }
}

function mouseOverMagnifier(element) {
  if(element.src!=orangemagnifierimage.src)
    element.src=orangemagnifierimage.src;
}

function mouseOutMagnifier(element) {
  if(element.src!=greymagnifierimage.src)
    element.src=greymagnifierimage.src;
}
