$(document).ready(function () {
  $('#coda-slider-1').codaSlider({
    dynamicArrows: false,
    dynamicTabs: false,
    autoSlide: false
  });
  $("div#maincontent-design div.infoslider a.bottomlink").each(function() {
    var p = $(this).parent();
    if ($(this).offset().top < ($(p).offset().top + $(p).height())) {
      $(p).css('height', $(p).height() + $(this).height() + 9);
    }
  });
  $('.faderbox .fader div.faderblock').each(function() {
    $(this).parent().height(Math.max($(this).parent().height(), $(this).height()));
  });
  $('.faderbox table.selector td, .faderbox ul.selector li').each(function() {
    var c = $(this).attr('class').split(' ');
    var f = $(this).parents('.faderbox').children('.fader');
    var e = false;
    for(var i in c) {
      if (c[i].substr(0,4) == 'sel_') {
        e = c[i].substr(4);
        break;
      }
    }
    $(this).click(function() {
      $(this).siblings().each(function() {
        $(this).removeClass('active');
      });
      $(this).addClass('active');
      $(f).children('div.faderblock').each(function() {
        if ($(this).css('display') != 'none') $(this).fadeOut(600);
      });
      $(f).children('div.'+e).fadeIn(600);
      if (!$(this).hasClass('no-hash')) window.location.hash = e;
    });
  });

  var href = window.location.href.split('#');
  if (href.length > 1) {
    var f = href[1];
    $('#maincontent .faderbox table.selector td').removeClass('active');
    $('#maincontent .faderbox table.selector td.sel_'+f).addClass('active');
    $('#maincontent .faderbox .fader div.faderblock').css('display', 'none');
    $('#maincontent .faderbox .fader div.faderblock.'+f).css('display', 'block');
  }
  $('.faderbox .fader div.zudaten table td:nth-child(1)').addClass('firstrow');
  $('.faderbox .fader div.zubehoer table td:nth-child(1)').addClass('firstrow');
  $('.faderbox .fader div.zubehoer table td:nth-child(2)').addClass('middlerow');
  $('.faderbox .fader div.zubehoer table td:nth-child(3)').addClass('lastrow');
  $('.faderbox .fader div.produkt table td:nth-child(1)').addClass('firstrow');
  var row = {elems: []};
  var newrow = false;
  $('div.content_teaser_2rows div.infoslider').each(function() {
    var ih = $(this).children('.topleftimg').height()+20;
    if (ih > $(this).height()) $(this).css('height', ih);
    var c = $(this).attr('class').split(' ');
    newrow = false;
    for(var i in c) {
      if (c[i] == 'infosliderleft') {
        if (row.elems.length) {
          for(var j in row.elems) $(row.elems[j]).css('height', row.maxh);
        }
        newrow = true;
        row = {maxh: $(this).height(), elems: [$(this)]};
        break;
      }
    }
    if (!newrow) {
      row.elems.push($(this));
      row.maxh = Math.max(row.maxh, $(this).height());
    }
  });
  if (!newrow && row.elems.length) {
    for(var j in row.elems) $(row.elems[j]).css('height', row.maxh);
  }
  $("div.akkosplit div.textsection").hide();
  $("div.akkosplit div.textsection:first").show();
  $("div.akkosplit h4:first").addClass('open');
  $("div.akkosplit h4").bind("click", function() {
    if (!$(this).hasClass('open')) {
      $(this).siblings('div.textsection').each(function(){
        if ($(this).css('display') != 'none') $(this).slideUp(500);
      });
      $(this).next().slideDown(500);
      $(this).siblings('h4').removeClass('open');
      $(this).addClass('open');
    }
  });
  if ($('.faderbox .fader div.faderblock.produkt div.productlink').size()) {
    $('.faderbox .fader div.faderblock.produkt div.productlink').clone().appendTo('.faderbox .fader div.faderblock.zubehoer');
    $('.faderbox .fader div.faderblock.produkt div.productlink').clone().appendTo('.faderbox .fader div.faderblock.anleitungen');
    $('.faderbox .fader div.faderblock.produkt div.productlink').clone().appendTo('.faderbox .fader div.faderblock.faq');
  }
  var pinum = $('div.#maincontent div.prod_images div.image').size();
  if (pinum > 1) {
    $('div.#maincontent div.prod_images').append('<div class="bnavi bnavil"></div>');
    $('div.#maincontent div.prod_images').append('<div class="bnavi bnavir"></div>');
    $('div.#maincontent div.prod_images div.bnavir').click(function() {
       nimgnavi.back();
    });
    $('div.#maincontent div.prod_images div.bnavil').click(function() {
       nimgnavi.forward();
    });
    nimgnavi.init(pinum);
  }
  $('div.brochures div.brochure.brochure-line').each(function() {
    var ih = $(this).find('.image img').attr('height')+40;
    // alert ("Hoehe img: " + ih + " und Hoehe css: " + $(this).css('height'));
    if (ih > $(this).height()) $(this).css('height', ih);
    // alert("Hoehe img: " + ih + " und Hoehe css: " + $(this).css('height'));
  });
  $('div.brochures div.brochure.brochure-line .image .zoomtrigger').click(function() {
    $('div.brochures div.brochure.brochure-line .image .zoomimg').fadeOut(300);
    var a = $(this).siblings('.zoomimg').children('a');
    if ($(a).length) {
      $(this).siblings('.zoomimg').prepend('<img src="'+$(a).attr('href')+'" alt="" />');
      $(a).detach();
      $(this).siblings('.zoomimg').children('img').load(function() {
        imdiak_cust.reposimg($(this).parent());
      });
    }
    $(this).siblings('.zoomimg').fadeIn(500);
    $(this).parent().parent().siblings().css('z-index', 1);
    $(this).parent().parent().css('z-index', 9);
    imdiak_cust.reposimg($(this).siblings('.zoomimg'));
    $(this).siblings('.zoomimg').click(function() {
      $(this).fadeOut(300);
    });
  });
  $('div#rightcontent .pagerelements').each(function() {
    var epp = 3; var pages = [[]]; var c = 0; var p = 0;
    $(this).children('.pagerelement').each(function() {
      if (c > 0 && c / epp == Math.round(c/epp)) {
        p++;
        pages[p] = [];
      }
      pages[p].push($(this));
      c++;
    });
    if (pages.length > 1) {
      var hpages = "";
      for(var i in pages) {
        hpages += "<div class=\"pagerpage pagerpage"+i+"\">";
        for(var j in pages[i]) hpages += "<div class=\"pagerelement\">"+$(pages[i][j]).html()+"</div>";
        hpages += "</div>";
      }
      var hnavi = "<div class=\"pager\">mehr</div>";
      $(this).html(hpages+hnavi);
      $(this).children('.pagerpage').css('display', 'none');
      $(this).children('.pagerpage0').css('display', 'block');
      $(this).children('.pager').addClass('active0');
      $(this).children('.pager').addClass('total'+pages.length);
      $(this).children('.pager').click(function() {
        var classes = $(this).attr('class').split(' ');
        var a; var t;
        for(var i in classes) {
          if (classes[i].substr(0,6) == 'active') {
            a = Number(classes[i].substr(6));
          } else if (classes[i].substr(0,5) == 'total') {
            t = Number(classes[i].substr(5));
          }
        }
        if (++a >= t) a = 0;
        $(this).attr('class', 'pager total'+t+' active'+a);
        $(this).siblings('.pagerpage').css('display', 'none');
        $(this).siblings('.pagerpage'+a).fadeIn(500);
      });
    } else {
      $(this).children('.pagerelement').css('display', 'block');
    }
  });
});



var nimgnavi = {
  aimg: null,
  totalimgs: null,
  init: function(ti) {
    this.aimg = 1;
    this.totalimgs = ti;
  },
  back: function() {
    if (this.aimg == 1) {
      this.aimg = this.totalimgs;
    } else {
      this.aimg--;
    }
    this.show();
  },
  forward: function() {
    if (this.aimg == this.totalimgs) {
      this.aimg = 1;
    } else {
      this.aimg++;
    }
    this.show();
  },
  show: function() {
    $('div.#maincontent div.prod_images div.image').fadeOut(500);
    $('div.#maincontent div.prod_images div.image'+this.aimg).fadeIn(500);
  }
}
var imdiak_cust = {
  reposimg: function(zoomimg) {
    $(zoomimg).css('top', "0px");
    var i = {y: $(zoomimg).offset().top, height: $(zoomimg).height()};
    var scrollPos = (document.defaultView ? document.defaultView.pageYOffset : document.documentElement.scrollTop);
    var w = {height: $(window).height(), scrollPos: scrollPos};
    var d = (i.y+i.height+40) - (w.scrollPos+w.height);
    if (i.y-d < w.scrollPos) d = i.y-w.scrollPos;
    if (d > 0) $(zoomimg).css('top', -d + "px");
  }
}
