var curvyCornersVerbose = false;

function curvy_redraw(obj) {
    $(obj).parent().append('<div class="rounded highlight curvyRedraw"><a class="small_button" id="'+obj.id+'" title="'+obj.title+'" href="">'+obj.value+'</a></div>');
    $(obj).remove();
}

var init_shortlink_stuff = function(data) {
   var portal_url = $('#portal_url').attr('href');
   $('#shortlink').val(portal_url + data);
   $('#shortlink').show();
   $('#shortlink_label').show();
   var remember_lastvis = $('#remember_lastvisited');
   if (remember_lastvis.length == 0) {
       return;
   }
   var last_visited = $.cookie('dgb.last_visited');
   if (last_visited == null) {
      last_visited = '';
   }
   var already_there = last_visited.indexOf(data.substring(3));
   if (already_there > -1) {
       var regex = new RegExp(data.substring(3)+',');
       last_visited = last_visited.replace(regex, '');
   }
   last_visited = last_visited + data.substring(3)+',';
   if (last_visited.split(',').length > 8) {
       last_visited = last_visited.substring(last_visited.indexOf(',')+1);
   }
   $.cookie('dgb.last_visited', last_visited, {path:'/', expires:999});
}

$().ready(function() {
    var context_url = $('#context_url').attr('href');
    $.getJSON(context_url + '/@@shortlink.json', init_shortlink_stuff);
});

var mouseover_highlight = function (event) {
    $('.highlight').unbind('mouseenter', mouseover_highlight);
    $('.highlight').unbind('mouseout', mouseout_highlight);
    curvyCorners.adjust(
        event.currentTarget, 'style.backgroundColor', highlight_color);
    curvyCorners.redraw();
    $('.highlight').bind('mouseover', mouseover_highlight);
    $('.highlight').bind('mouseout', mouseout_highlight);
    event.stopPropagation();
}

var mouseout_highlight = function (event) {
    $('.highlight').unbind('mouseout', mouseout_highlight);
    $('.highlight').unbind('mouseover', mouseover_highlight);
    curvyCorners.adjust(
        event.currentTarget, 'style.backgroundColor', "#666666");
    curvyCorners.redraw();
    $('.highlight').bind('mouseout', mouseout_highlight);
    $('.highlight').bind('mouseenter', mouseover_highlight);
    event.stopPropagation();
}

$().ready(function() {
    $('input.curvyReplace').each(function() {
        curvy_redraw(this);
    });

    $('.highlight').not('#main_nav ul li').bind('mouseenter', mouseover_highlight);
    $('.highlight').not('#main_nav ul li').bind('mouseout', mouseout_highlight);

});

$(document).ready(function() {
    $('a[href^="http"]').filter(
        function (i) {
            if ($(this).attr("href").indexOf(location.host) == -1) {
                if (!$(this).children('img').length && !$(this).parents('#meta_nav').length) {
                    $(this).addClass('extern');
                }
            }
        });
    $('#dgb a.send').filter(
        function (i) {
            if (!$.browser.msie) {
                this.href = utf8_link;
            }
        });
});

$().ready(function(){
    $("form.contact div.form-controls a").click(function(){
        $("form.contact .invisible input").click();
        return false;
    });
    $("form.memberform div.form-controls a").click(function(){
        $("form.memberform .invisible input").click();
        return false;
    });

    $(".jqmClose").mouseover(function() {
        this.src = "/++resource++dgb/img/icon_close_hi.gif";
    });
    $(".jqmClose").mouseout(function() {
        this.src = "/++resource++dgb/img/icon_close.gif";
    });

    // set default text and toggle its view of the top searchbox
    $('#search_text').val($('#search_text').prev().text());
    toggleInputLabel('#search_text');

    // style check- and selectboxes
    $('input:checkbox').checkbox();
    $('select').not('.no-style').sSelect();


    var startFlagBgPos = 7;
    var newPos = 0;
    $('#meta_nav ul.newList li').each(function(i){
       if(i>0){
           newPos = startFlagBgPos - (i*26);
           $(this).children('a').css('background-position', '0 '+newPos+'px');
       }
    });

    /*sndNavConnector*/
    // Set width of red connector between mannavi item and subnavibox. So all borders fit together
    // Get width of mainnavi item and add 6 cause of the containing image for the round corner
    $('.sndNaviConnectBox:visible .sndNaviConnect').width($('.sndNaviConnectBox:visible .sndNaviConnect').parent().parent().outerWidth()+6);

    /*sndNaviBox*/
   if ($('.sndNaviConnectBox:visible').parent().length) {
       var sndNavConPos = $('.sndNaviConnectBox:visible').parent().position(); //default left top position of connector to set the second navigation box
       var newWidth = sndNavConPos.left + $('.sndNaviConnectBox:visible').parent().width() + 270; //default width for second navigation box
       $('.sndNaviConnectBox:visible').next().children('.secondary-box').width(newWidth); // if right border of second navigation box is left of the mein navi item right border set its width with default value
       var boxWidth = 0;
       $('#main_nav ul li.highlight .secondary-box ul.secondary').children('li').each(function(){boxWidth += $(this).outerWidth() + 10})
       $('div.pos-secbox').width(boxWidth);
       $('div.secondary-box').width(boxWidth);
       var sndBoxW = $('.sndNaviConnectBox:visible').next().children('.secondary-box').outerWidth(); // width (incl. padding and margin) of second navi box
       var sndBoxH = $('.sndNaviConnectBox:visible').next().children('.secondary-box').height(); // default height of second navi box
       $('#main_nav ul li.highlight .secondary-box ul.secondary').children('li').children('ul').each(function(){
           // get all list items in second navi box to get the needed height and width of the second navi box
           hL = 0;
           wL = 0;
           hR = 0;
           wR = 0;
           $(this).children('li.fleft').each(function(){
               hL += $(this).outerHeight();
               wL = (wL<$(this).outerWidth())?$(this).outerWidth():wL;
           });
           $(this).children('li.fright').each(function(){
               hR += $(this).outerHeight();
               wR = (wR<$(this).outerWidth())?$(this).outerWidth():wR;
           });
           $(this).children('li.fleft').width(wL+14);
           $(this).children('li.fright').width(wR+14);
           $(this).width(wL+wR+38);
       });

       $(".secondary-box ul.secondary").children("li").hover(function(){
               var min_width = boxWidth;
               var parent_width = $(this).parent('ul').outerWidth();
               if($(this).children('ul').length){
                   var pObj = $('.sndNaviConnectBox:visible').next().children('.secondary-box'); // get second/third navigation box of the active main navigation item
                   pObj.height(sndBoxH + $(this).children('ul').outerHeight()+8); // set new height of the second/third navigation box with offset of 5px
                   var children_width = $(this).children('ul').outerWidth();
                   maxW = (children_width <= parent_width) ? parent_width : children_width;
                   maxW = (maxW < min_width) ? min_width : maxW;
                   $(this).children('ul').width(maxW);
                   pObj.width(maxW); //set new width of the second/third navigation box with offset of 50px
               }
               var secondary_width = $('div.secondary-box').outerWidth();
               if (secondary_width > parent_width) {
                   $('div.pos-secbox').width(secondary_width);
               }
               $('.secondary-box ul.secondary li.active ul').addClass('hidden'); // add to all third navigation list of active second navigation css class "hidden"
               $(this).children('ul.hidden').removeClass('hidden'); // remove css class "hidden" of the third navi list on mouseover of the second navi item
       }, function(){
               if ($(this).children('ul').length) {
                   var pObj = $('.sndNaviConnectBox:visible').next().children('.secondary-box');
                    pObj.height(sndBoxH); // set default height of secon navigation box
                    maxW = ($('.secondary-box ul.secondary li.active ul').outerWidth() <= sndBoxW) ? sndBoxW : $('.secondary-box ul.secondary li.active ul').outerWidth();
                    pObj.width(sndBoxW); // set default width of secodan navigation box
                    $(this).children('ul').addClass('hidden');
                }
       });

       if ($('.secondary-box ul.secondary li.active').length) {
           // set width and height of active second navigation box including third
           var hLa = 0;
           var wLa = 0;
           var hRa = 0;
           var wRa = 0;
           $('.secondary-box ul.secondary li.active ul li.fleft').each(function(){
               // get max width and height for left col by iteration over all list of items for left col
               hLa += $(this).outerHeight();
               wLa = (wLa<$(this).outerWidth())?$(this).outerWidth():wLa;
           });
           $('.secondary-box ul.secondary li.active ul li.fright').each(function(){
               // get max width and height for right col  by iteration over all list items for right col
               hRa += $(this).outerHeight();
               wRa = (wRa<$(this).outerWidth())?$(this).outerWidth():wRa;
           });
           var maxWa = wLa + wRa;
           var maxHa = (hLa>=hRa)?hLa:hRa;
           var pObj = $('.secondary-box ul.secondary').children('li.active').parent().parent(); // get second/third navigation box of the active secondary navigation item
           pObj.height(pObj.height() + maxHa +5); // set height of the second/third navigation box
           pObj.height(sndBoxH);
           sW = (newWidth<=maxWa+17)?maxWa+17:newWidth; // build width of second/third navigation width
           pObj.width(sndBoxW); // set width of the second/thrd navigation box
           //pObj.width(sW);
           $('.secondary-box ul.secondary li.active ul li.fleft').width(wLa); // set width of left col with max width for left col
           $('.secondary-box ul.secondary li.active ul li.fright').width(wRa); // set width of right col with max width for right col
           $('.secondary-box ul.secondary li.active ul').width(wLa+wRa+14); // set width of list conatining left and right col
           //$('.secondary-box ul.secondary li.active ul').removeClass('hidden'); // show list after all settings are done to prevent flicker
       }
       var border = RUZEE.ShadedBorder.create({
           corner: 3,
           shadow: 8
       });
       border.render($('.secondary-box'));
       $('.pos-secbox, .sndNaviConnectBox').css('visibility', 'visible');
   }

    if ($.browser.msie) {
        var version = $.browser.version.split(".");
        if (new Number(version[0]) < 8) {
        $('.selectedTxt').bind('click', function() {
            var list = $(this).parents('.newListSelected').children('ul');
            if (!$(this).parents('#searchresult_filter').length && !$(this).parents('#change_language').length && (list[0].style.top[0] != '-')) {
                // move list to the top
                list[0].style.top = 0 - list.height() + 'px';
            }
        });
        }
    }


});

/* jQuery custom checkboxes |  @version 1.3.0 Beta 1 */
(function($){var i=function(e){if(!e)var e=window.event;e.cancelBubble=true;if(e.stopPropagation)e.stopPropagation()};$.fn.checkbox=function(f){try{document.execCommand('BackgroundImageCache',false,true)}catch(e){}var g={cls:'jquery-checkbox',empty:'/portal/dgb/++resource++internet/img/blank.gif'};g=$.extend(g,f||{});var h=function(a){var b=a.checked;var c=a.disabled;var d=$(a);if(a.stateInterval)clearInterval(a.stateInterval);a.stateInterval=setInterval(function(){if(a.disabled!=c)d.trigger((c=!!a.disabled)?'disable':'enable');if(a.checked!=b)d.trigger((b=!!a.checked)?'check':'uncheck')},10);return d};return this.each(function(){var a=this;var b=h(a);if(a.wrapper)a.wrapper.remove();a.wrapper=$('<span class="'+g.cls+'"><span class="mark"><img src="'+g.empty+'" /></span></span>');a.wrapperInner=a.wrapper.children('span:eq(0)');a.wrapper.hover(function(e){a.wrapperInner.addClass(g.cls+'-hover');i(e)},function(e){a.wrapperInner.removeClass(g.cls+'-hover');i(e)});b.css({position:'absolute',zIndex:-1,visibility:'hidden'}).after(a.wrapper);var c=false;if(b.attr('id')){c=$('label[for='+b.attr('id')+']');if(!c.length)c=false}if(!c){c=b.closest?b.closest('label'):b.parents('label:eq(0)');if(!c.length)c=false}if(c){c.hover(function(e){a.wrapper.trigger('mouseover',[e])},function(e){a.wrapper.trigger('mouseout',[e])});c.click(function(e){b.trigger('click',[e]);i(e);return false})}a.wrapper.click(function(e){b.trigger('click',[e]);i(e);return false});b.click(function(e){i(e)});b.bind('disable',function(){a.wrapperInner.addClass(g.cls+'-disabled')}).bind('enable',function(){a.wrapperInner.removeClass(g.cls+'-disabled')});b.bind('check',function(){a.wrapper.addClass(g.cls+'-checked')}).bind('uncheck',function(){a.wrapper.removeClass(g.cls+'-checked')});$('img',a.wrapper).bind('dragstart',function(){return false}).bind('mousedown',function(){return false});if(window.getSelection)a.wrapper.css('MozUserSelect','none');if(a.checked)a.wrapper.addClass(g.cls+'-checked');if(a.disabled)a.wrapperInner.addClass(g.cls+'-disabled')})}})(jQuery);


/**
 * RUZEE.ShadedBorder 0.6.2
 * (c) 2006 Steffen Rusitschka
 *
 * RUZEE.ShadedBorder is freely distributable under the terms of an MIT-style license.
 * For details, see http://www.ruzee.com/
 */

var RUZEE = window.RUZEE || {};

RUZEE.ShadedBorder = {

create: function(opts) {
  var isie = /msie/i.test(navigator.userAgent) && !window.opera;
  var isie6 = isie && !window.XMLHttpRequest;
  function sty(el, h) {
    for(k in h) {
      if (/ie_/.test(k)) {
        if (isie) el.style[k.substr(3)]=h[k];
      } else el.style[k]=h[k];
    }
  }
  function crdiv(h) {
    var el=document.createElement("div");
    el.className = "sb-gen";
    sty(el, h);
    return el;
  }
  function op(v) {
    v = v<0 ? 0 : v;
    if (v>0.99999) return "";
    return isie ? " filter:alpha(opacity=" + (v*100) + ");" : " opacity:" + v + ';';
  }

  var sr = opts.shadow || 0;
  var r = opts.corner || 0;
  var bor = 0;
  var bow = opts.border || 0;
  var boo = opts.borderOpacity || 1;
  var shadow = sr != 0;
  var lw = r > sr ? r : sr;
  var rw = lw;
  var th = lw;
  var bh = lw;
  if (bow > 0) {
    bor = r;
    r = r - bow;
  }
  var cx = r != 0 && shadow ? Math.round(lw/3) : 0;
  var cy = cx;
  var cs = Math.round(cx/2);
  var iclass = r > 0 ? "sb-inner" : "sb-shadow";
  var sclass = "sb-shadow";
  var bclass = "sb-border";
  var edges = opts.edges || "trlb";
  if (!/t/i.test(edges)) th=0;
  if (!/b/i.test(edges)) bh=0;
  if (!/l/i.test(edges)) lw=0;
  if (!/r/i.test(edges)) rw=0;

  var p = { position:"absolute", left:"0", top:"0", width:lw + "px", height:th + "px",
            ie_fontSize:"1px", overflow:"hidden", margin:"0", padding:"0" }; var tl = crdiv(p);
  delete p.left; p.right="0"; p.width=rw + "px"; var tr = crdiv(p);
  delete p.top; p.bottom="0"; p.height=bh + "px"; var br = crdiv(p);
  delete p.right; p.left="0"; p.width=lw + "px"; var bl = crdiv(p);

  var tw = crdiv({ position:"absolute", width:"100%", height:th + "px", ie_fontSize:"1px",
                   top:"0", left:"0", overflow:"hidden", margin:"0", padding:"0" });
  var t = crdiv({ position:"relative", height:th + "px", ie_fontSize:"1px",
                  margin:"0 "+ rw + "px 0 " + lw + "px", overflow:"hidden", padding:"0" });
  tw.appendChild(t);

  var bw = crdiv({ position:"absolute", left:"0", bottom:"0", width:"100%", height:bh + "px",
                   ie_fontSize:"1px", overflow:"hidden", margin:"0", padding:"0" });

  var b = crdiv({ position:"relative", height:bh + "px", ie_fontSize:"1px",
                  margin:"0 "+ rw + "px 0 " + lw + "px", overflow:"hidden", padding:"0" });

  bw.appendChild(b);

  var mw = crdiv({ position:"absolute", top:(-bh)+"px", left:"0", width:"100%", height:"100%",
                   overflow:"hidden", ie_fontSize:"1px", padding:"0", margin:"0" });

  function corner(el,t,l) {
    var w = l ? lw : rw;
    var h = t ? th : bh;
    var s = t ? cs : -cs;
    var dsb = []; var dsi = []; var dss = [];

    var xp=0; var xd=1; if (l) { xp=w-1; xd=-1; }
    for (var x=0; x<w; ++x) {
      var yp=h-1; var yd=-1; if (t) { yp=0; yd=1; }
      var finished=false;
      for (var y=h-1; y>=0 && !finished; --y) {
        var div = '<div style="position:absolute; top:' + yp + 'px; left:' + xp + 'px; ' +
                  'width:1px; height:1px; overflow:hidden; margin:0; padding:0;';

        var xc = x - cx; var yc = y - cy - s;
        var d = Math.sqrt(xc*xc+yc*yc);
        var doShadow = false;

        if (r > 0) {
          // draw border
          if (xc < 0 && yc < bor && yc >= r || yc < 0 && xc < bor && xc >= r) {
            dsb.push(div + op(boo) + '" class="' + bclass + '"></div>');
          } else
          if (d<bor && d>=r-1 && xc>=0 && yc>=0) {
            var dd = div;
            if (d>=bor-1) {
              dd += op((bor-d)*boo);
              doShadow = true;
            } else dd += op(boo);
            dsb.push(dd + '" class="' + bclass + '"></div>');
          }

          // draw inner
          var dd = div + ' z-index:2;' + (t ? 'background-position:0 -' + (r-yc-1) + 'px;' : 'background-image:none;');
          var finish = function() {
            if (!t) dd = dd.replace(/top\:\d+px/, "top:0px");
            dd = dd.replace(/height\:1px/, "height:" + (y+1) + "px");
            dsi.push(dd + '" class="' + iclass + '"></div>');
            finished = true;
          };
          if (xc < 0 && yc < r || yc < 0 && xc < r) {
            finish();
          } else
          if (d<r && xc>=0 && yc>=0) {
            if (d>=r-1) {
              dd += op(r-d);
              doShadow = true;
              dsi.push(dd + '" class="' + iclass + '"></div>');
            } else {
              finish();
            }
          } else doShadow = true;
        } else doShadow = true;

        // draw shadow
        if (sr > 0 && doShadow) {
          d = Math.sqrt(x*x+y*y);
          if (d<sr) {
            dss.push(div + ' z-index:0; ' + op(1-(d/sr)) + '" class="' + sclass + '"></div>');
          }
        }
        yp += yd;
      }
      xp += xd;
    }
    el.innerHTML = dss.concat(dsb.concat(dsi)).join('');
  }

  function mid(mw) {
    var ds = [];

    ds.push('<div style="position:relative; top:' + (th+bh) + 'px; height:2048px; ' +
            ' margin:0 ' + (rw-r-cx) + 'px 0 ' + (lw-r-cx) + 'px; ' +
            ' padding:0; overflow:hidden;' +
            ' background-position:0 ' + (th > 0 ? -(r+cy+cs) : '0') + 'px;"' +
            ' class="' + iclass + '"></div>');

    var dd = '<div style="position:absolute; width:1px;' +
        ' top:' + (th+bh) + 'px; height:2048px; padding:0; margin:0;';
    if (sr>0) {
      for (var x=0; x<lw-r-cx; ++x) {
        ds.push(dd + ' left:' + x + 'px;' + op((x+1.0)/lw) +
            '" class="' + sclass + '"></div>');
      }

      for (var x=0; x<rw-r-cx; ++x) {
        ds.push(dd + ' right:' + x + 'px;' + op((x+1.0)/rw) +
            '" class="' + sclass + '"></div>');
      }
    }

    if (bow > 0) {
      var su = ' width:' + bow + 'px;' + op(boo) + '" class="' + bclass + '"></div>';
      ds.push(dd + ' left:' + (lw-bor-cx) + 'px;' + su);
      ds.push(dd + ' right:' + (rw-bor-cx) + 'px;' + su);
    }

    mw.innerHTML = ds.join('');
  }

  function tb(el, t) {
    var ds = [];
    var h = t ? th : bh;
    var dd = '<div style="height:1px; overflow:hidden; position:absolute; margin:0; padding:0;' +
        ' width:100%; left:0px; ';
    var s = t ? cs : -cs;
    for (var y=0; y<h-s-cy-r; ++y) {
      if (sr>0) ds.push(dd + (t ? 'top:' : 'bottom:') + y + 'px;' + op((y+1)*1.0/h) +
          '" class="' + sclass + '"></div>');
    }
    if (y >= bow) {
      ds.push(dd + (t ? 'top:' : 'bottom:') + (y - bow) + 'px;' + op(boo) +
          ' height:' + bow + 'px;" class="' + bclass + '"></div>');
    }

    ds.push(dd + (t ? 'background-position-y:0; top:' :
                      'background-image:none; bottom:') + y + 'px;' +
        ' height:' + (r+cy+s) + 'px;" class="' + iclass + '"></div>');

    el.innerHTML = ds.join('');
  }

  corner(tl, true, true); corner(tr, true, false);
  corner(bl, false, true); corner(br, false, false);
  mid(mw); tb(t, true); tb(b, false);
  needsCloning = false;

  return {
    render: function(el) {
      if (typeof el == 'string') el = document.getElementById(el);
      if (el.length != undefined) {
        for (var i=0; i<el.length; ++i) this.render(el[i]);
        return;
      }
      el.className += " sb";
      sty(el, { position:"relative", background:"transparent" });

      // remove generated children
      var node = el.firstChild;
      while (node) {
        var nextNode = node.nextSibling;
        if (node.nodeType == 1 && node.className == 'sb-gen')
          el.removeChild(node);
        node = nextNode;
      }

      var iel = el.firstChild;

      var twc = needsCloning ? tw.cloneNode(true) : tw;
      var mwc = needsCloning ? mw.cloneNode(true) : mw;
      var bwc = needsCloning ? bw.cloneNode(true) : bw;
      var tlc = needsCloning ? tl.cloneNode(true) : tl;
      var trc = needsCloning ? tr.cloneNode(true) : tr;
      var blc = needsCloning ? bl.cloneNode(true) : bl;
      var brc = needsCloning ? br.cloneNode(true) : br;

      el.insertBefore(tlc, iel); el.insertBefore(trc, iel);
      el.insertBefore(blc, iel); el.insertBefore(brc, iel);
      el.insertBefore(twc, iel); el.insertBefore(mwc, iel);
      el.insertBefore(bwc, iel);

      if (isie6) {
        el.onmouseover=function() { this.className += " hover"; }
        el.onmouseout=function() { this.className = this.className.replace(/ hover/,""); }
      }
      if (isie) {
        function resize() {
          twc.style.width = bwc.style.width = mwc.style.width = el.offsetWidth + "px";
          if (isie6) {
            mwc.firstChild.style.height = el.offsetHeight + "px";
          } else {
            for (var i=0; i<mwc.childNodes.length; ++i) {
              try{
                  mwc.childNodes[i].style.height = (el.offsetHeight - bh - th)  + "px";
              }catch(err){

              }
            }
          }
          trc.style.right = brc.style.right = null;
          trc.style.left  = brc.style.left  = (el.offsetWidth - rw) + "px";
        }
        el.onresize=resize;
        resize();
      }
      needsCloning = true;
    }
  };
}
}


function toggleInputLabel(elID){
    $(elID).bind("focus", function(e){
        if($(elID).val()==$(elID).prev().text()){
            $(elID).val('');
        }
    });
    $(elID).bind("blur", function(e){
        if($(elID).val()==''){
            $(elID).val($(elID).prev().text());
        }
    });

}
