$(document).ready(function() {
  
  
  
  
  /*
   * jQuery Easing v1.3 - http://gsgd.co.uk/sandbox/jquery/easing/
  */

  // t: current time, b: begInnIng value, c: change In value, d: duration
  jQuery.easing['jswing'] = jQuery.easing['swing'];

  jQuery.extend( jQuery.easing,
  {
  	def: 'easeOutQuad',
  	swing: function (x, t, b, c, d) {
  		return jQuery.easing[jQuery.easing.def](x, t, b, c, d);
  	},
  	easeInQuad: function (x, t, b, c, d) {
  		return c*(t/=d)*t + b;
  	},
  	easeOutQuad: function (x, t, b, c, d) {
  		return -c *(t/=d)*(t-2) + b;
  	},
  	easeInOutQuad: function (x, t, b, c, d) {
  		if ((t/=d/2) < 1) return c/2*t*t + b;
  		return -c/2 * ((--t)*(t-2) - 1) + b;
  	},
  	easeInCubic: function (x, t, b, c, d) {
  		return c*(t/=d)*t*t + b;
  	},
  	easeOutCubic: function (x, t, b, c, d) {
  		return c*((t=t/d-1)*t*t + 1) + b;
  	},
  	easeInOutCubic: function (x, t, b, c, d) {
  		if ((t/=d/2) < 1) return c/2*t*t*t + b;
  		return c/2*((t-=2)*t*t + 2) + b;
  	},
  	easeInQuart: function (x, t, b, c, d) {
  		return c*(t/=d)*t*t*t + b;
  	},
  	easeOutQuart: function (x, t, b, c, d) {
  		return -c * ((t=t/d-1)*t*t*t - 1) + b;
  	},
  	easeInOutQuart: function (x, t, b, c, d) {
  		if ((t/=d/2) < 1) return c/2*t*t*t*t + b;
  		return -c/2 * ((t-=2)*t*t*t - 2) + b;
  	},
  	easeInQuint: function (x, t, b, c, d) {
  		return c*(t/=d)*t*t*t*t + b;
  	},
  	easeOutQuint: function (x, t, b, c, d) {
  		return c*((t=t/d-1)*t*t*t*t + 1) + b;
  	},
  	easeInOutQuint: function (x, t, b, c, d) {
  		if ((t/=d/2) < 1) return c/2*t*t*t*t*t + b;
  		return c/2*((t-=2)*t*t*t*t + 2) + b;
  	},
  	easeInSine: function (x, t, b, c, d) {
  		return -c * Math.cos(t/d * (Math.PI/2)) + c + b;
  	},
  	easeOutSine: function (x, t, b, c, d) {
  		return c * Math.sin(t/d * (Math.PI/2)) + b;
  	},
  	easeInOutSine: function (x, t, b, c, d) {
  		return -c/2 * (Math.cos(Math.PI*t/d) - 1) + b;
  	},
  	easeInExpo: function (x, t, b, c, d) {
  		return (t==0) ? b : c * Math.pow(2, 10 * (t/d - 1)) + b;
  	},
  	easeOutExpo: function (x, t, b, c, d) {
  		return (t==d) ? b+c : c * (-Math.pow(2, -10 * t/d) + 1) + b;
  	},
  	easeInOutExpo: function (x, t, b, c, d) {
  		if (t==0) return b;
  		if (t==d) return b+c;
  		if ((t/=d/2) < 1) return c/2 * Math.pow(2, 10 * (t - 1)) + b;
  		return c/2 * (-Math.pow(2, -10 * --t) + 2) + b;
  	},
  	easeInCirc: function (x, t, b, c, d) {
  		return -c * (Math.sqrt(1 - (t/=d)*t) - 1) + b;
  	},
  	easeOutCirc: function (x, t, b, c, d) {
  		return c * Math.sqrt(1 - (t=t/d-1)*t) + b;
  	},
  	easeInOutCirc: function (x, t, b, c, d) {
  		if ((t/=d/2) < 1) return -c/2 * (Math.sqrt(1 - t*t) - 1) + b;
  		return c/2 * (Math.sqrt(1 - (t-=2)*t) + 1) + b;
  	},
  	easeInElastic: function (x, t, b, c, d) {
  		var s=1.70158;var p=0;var a=c;
  		if (t==0) return b;  if ((t/=d)==1) return b+c;  if (!p) p=d*.3;
  		if (a < Math.abs(c)) { a=c; var s=p/4; }
  		else var s = p/(2*Math.PI) * Math.asin (c/a);
  		return -(a*Math.pow(2,10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )) + b;
  	},
  	easeOutElastic: function (x, t, b, c, d) {
  		var s=1.70158;var p=0;var a=c;
  		if (t==0) return b;  if ((t/=d)==1) return b+c;  if (!p) p=d*.3;
  		if (a < Math.abs(c)) { a=c; var s=p/4; }
  		else var s = p/(2*Math.PI) * Math.asin (c/a);
  		return a*Math.pow(2,-10*t) * Math.sin( (t*d-s)*(2*Math.PI)/p ) + c + b;
  	},
  	easeInOutElastic: function (x, t, b, c, d) {
  		var s=1.70158;var p=0;var a=c;
  		if (t==0) return b;  if ((t/=d/2)==2) return b+c;  if (!p) p=d*(.3*1.5);
  		if (a < Math.abs(c)) { a=c; var s=p/4; }
  		else var s = p/(2*Math.PI) * Math.asin (c/a);
  		if (t < 1) return -.5*(a*Math.pow(2,10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )) + b;
  		return a*Math.pow(2,-10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )*.5 + c + b;
  	},
  	easeInBack: function (x, t, b, c, d, s) {
  		if (s == undefined) s = 1.70158;
  		return c*(t/=d)*t*((s+1)*t - s) + b;
  	},
  	easeOutBack: function (x, t, b, c, d, s) {
  		if (s == undefined) s = 1.70158;
  		return c*((t=t/d-1)*t*((s+1)*t + s) + 1) + b;
  	},
  	easeInOutBack: function (x, t, b, c, d, s) {
  		if (s == undefined) s = 1.70158; 
  		if ((t/=d/2) < 1) return c/2*(t*t*(((s*=(1.525))+1)*t - s)) + b;
  		return c/2*((t-=2)*t*(((s*=(1.525))+1)*t + s) + 2) + b;
  	},
  	easeInBounce: function (x, t, b, c, d) {
  		return c - jQuery.easing.easeOutBounce (x, d-t, 0, c, d) + b;
  	},
  	easeOutBounce: function (x, t, b, c, d) {
  		if ((t/=d) < (1/2.75)) {
  			return c*(7.5625*t*t) + b;
  		} else if (t < (2/2.75)) {
  			return c*(7.5625*(t-=(1.5/2.75))*t + .75) + b;
  		} else if (t < (2.5/2.75)) {
  			return c*(7.5625*(t-=(2.25/2.75))*t + .9375) + b;
  		} else {
  			return c*(7.5625*(t-=(2.625/2.75))*t + .984375) + b;
  		}
  	},
  	easeInOutBounce: function (x, t, b, c, d) {
  		if (t < d/2) return jQuery.easing.easeInBounce (x, t*2, 0, c, d) * .5 + b;
  		return jQuery.easing.easeOutBounce (x, t*2-d, 0, c, d) * .5 + c*.5 + b;
  	}
  });
   
  
  
  
  
  

  // Left sub menu
  $(window).load(function() {
    
    if($('.left-sub-menu').length == 1) {
    
      b = $('.left-sub-menu');

      var _offset_top = $(b).offset().top - 40;
      var _scroll_timeout = false;

      $(window).scroll(function(e) {
      
        var _body_height = $('.body').height();
        var _menu_height = $(b).height();
    
        if(_menu_height < _body_height) {
      
          var _new_top = 0;
          var _window_scrolltop = $(window).scrollTop();
          if(_window_scrolltop > _offset_top) {
            _new_top = _window_scrolltop - _offset_top;
            if(!((_new_top + _menu_height) < _body_height)) {
              _new_top = _body_height - _menu_height;
            }
          }
          if(_scroll_timeout) window.clearTimeout(_scroll_timeout);
          _scroll_timeout = window.setTimeout(function() {
            $(b).animate({ top: _new_top }, 'slow', 'easeInOutQuart');
          }, 100);
    
        }
    
      }).scroll();
    
    }
    
  });
  
  
  
  
  
  
  
  
  // Slideshow
  $('#butz-slider').each(function(a, b) {
    
    var _items = $(b).find('li.slide');
    var _item_length = _items.length;
    var _item_width = 970;
    var _current = 0;
    var _is_sliding = false;
    var _slide_interval = false;
    
    if(_item_length < 2) return false;
    
    $(b).find('.slide-nav').each(function(c, d) {
      $(d).append('<a href="#" class="btn-prev"></a><a href="#" class="btn-next"></a>');
    });
    
    $(b).find('ul').width(_item_length * _item_width);
    
    $(b).find('.btn-prev').click(function(e, is_auto) {
      e.preventDefault();
      if(!is_auto) { window.clearInterval(_slide_interval); _slide_interval = false; }
      if(!_is_sliding) {
        _is_sliding = true;
        var _new_current = _current-1;
        if(_new_current < 0) {
          $(b).find('ul').prepend($(b).find('ul').find('li.slide').last());
          $(b).find('ul').animate({ left: '-=' + _item_width }, 0);
          _new_current = 0;
        }
        _current = _new_current;
        $(b).find('ul').animate({ left: '+=' + _item_width }, 2000, function() {
          _is_sliding = false;
        });
      }
    });
    
    $(b).find('.btn-next').click(function(e, is_auto) {
      e.preventDefault();
      if(!is_auto) { window.clearInterval(_slide_interval); _slide_interval = false; }
      if(!_is_sliding) {
        _is_sliding = true;
        var _new_current = _current+1;
        if(_new_current >= _item_length) {
          $(b).find('ul').append($(b).find('ul').find('li.slide').first());
          $(b).find('ul').animate({ left: '+=' + _item_width }, 0);
          _new_current = _item_length-1;
        }
        _current = _new_current;
        $(b).find('ul').animate({ left: '-=' + _item_width }, 2000, function() {
          _is_sliding = false;
        });
      }
    });
    
    _slide_interval = window.setInterval(function() {
      $(b).find('.btn-next').first().trigger('click', [true]);
    }, 9000);
    
  });
  
  
  
  
  
  
  
  
  
  // Accordion
  $('.cms-block.accordion').wrapAll('<div class="cms-accordion">');
  $('.cms-accordion').each(function(a, b) {
    
    $(b).find('.cms-block-copy').each(function(c, d) {
      $(d).find('> h3:first-child').addClass('cms-accordion-headline');
      $(d).find('> *:not(h3:first-child)').wrapAll('<div class="cms-accordion-content"><div class="cms-accordion-content-inner">');
    });
    
    $(b).find('.cms-accordion-content-inner').hide();
    
    $(b).find('.cms-accordion-headline').click(function(e) {
      $(this).next().find('.cms-accordion-content-inner').stop(true, true).slideToggle();
      $(this).closest('.cms-block.accordion').toggleClass('open');
    });
    
  });
  
  
  
  
  
  
  
  
  // Accordion 2
  $('.cms-block-item.accordion2').wrapAll('<div class="cms-accordion2">');
  $('.cms-accordion2').each(function(a, b) {
    
    $(b).find('.cms-block-item-headline h3:first-child').addClass('cms-accordion-headline');
    $(b).find('.cms-block-item-body').wrap('<div class="cms-accordion-content"><div class="cms-accordion-content-inner">');
    
    $(b).find('.cms-accordion-content-inner').hide();
    
    $(b).find('.cms-accordion-headline').click(function(e) {
      $(this).closest('.cms-block-item.accordion2').find('.cms-accordion-content-inner').stop(true, true).slideToggle();
      $(this).closest('.cms-block-item.accordion2').toggleClass('open');
    });
    
  });
  
  
  
  
  
  
  // News
  $('body.category-aktuelles').each(function(a, b) {
    $(b).addClass('category-news');
  });
  $('body.category-news').each(function(a, b) {
    $(b).find('.cms-block-copy').each(function(c, d) {
      $(d).find('h3:first-child').wrap('<div class="news-date">');
      $(d).find('.news-date').nextAll().wrapAll('<div class="news-copy">');
    });
  });
  
  
  
  
  
  // Lightbox
  $('a[rel=lightbox]').click(function(e) {
    
    e.preventDefault(); 
    $('body').prepend('<div id="lightbox-wrapper">');
    $('#lightbox-wrapper').html('<a href="#" class="btn-close">&times;</a><span class="loading"></span><div id="lightbox-content"><img src="'+ $(this).attr('href') +'?'+ Math.floor(Math.random()*1000) +'" alt="" style="display: none;" /></div>');
    $('#lightbox-wrapper .btn-close').click(function(e) {
      e.preventDefault();
      $('#lightbox-wrapper').remove();
      $(window).unbind('resize');
    });
    
    $(window).resize(function() {
      $('#lightbox-wrapper img').css({
        'max-height': $(window).height() - 100
      }).css({
        'margin-top': ($(window).height() - $('#lightbox-wrapper img').height()) / 2
      });
    }).resize();
    
    $('#lightbox-wrapper img').load(function() { $(window).resize(); $(this).show(); $('#lightbox-wrapper .loading').remove(); })
    
  });
  
  
  
  
  
  
  // Network
  $('.block-unsere-partner-und-netzwerke .cms-block-item').each(function(a, b) {
    var _link = $(b).find('.cms-block-copy a').first();
    if(_link.length == 1) {
      $(b).find('.cms-block-item-img img').wrap('<a href="'+ _link.attr('href') +'">');
    }
  });
  
  
  
  
  
  
  
  
  // Search
  $('#searchterm').focus(function() {
    if($(this).val() == $(this).prev('label').text()) { $(this).val(''); }
  });
  $('#searchterm').blur(function() {
    if($(this).val() == '') { $(this).val($(this).prev('label').text()) }
  }).blur();
  
  
  
  
  
  
  
  
  
  // Search highlighting
  jQuery.fn.extend({
    highlight: function(search, insensitive){
      var regex = new RegExp("(<[^>]*>)|(\\b"+ search.replace(/([-.*+?^${}()|[\]\/\\])/g,"\\$1") +")", insensitive ? "ig" : "g");
      return this.html(this.html().replace(regex, function(a, b, c){
        return (a.charAt(0) == "<") ? a : "<span class=\"highlight\">" + c + "</span>";
      }));
    }
  });
  if($('ul.search-results').length == 1) {
    $('ul.search-results').highlight($('ul.search-results').data('searchterm'), 1);
  }
  
  
  
  
  
  
  
  
  
  
  
  
  // Contact

  insert_map = function(c, id) {
    
    // Krefeld
    if(c == 1) {
      ll1 = 51.3488;
      ll2 = 6.5743;
      
    // Leipzig
    } else if(c == 3) {
      ll1 = 51.3453;
      ll2 = 12.3920;
    
    // Berlin
    } else {
      ll1 = 52.5801;
      ll2 = 13.2931;
    }

    var latlng = new google.maps.LatLng(ll1, ll2);
    var myOptions = {
      zoom: 14,
      center: latlng,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    var map = new google.maps.Map(document.getElementById(id), myOptions);

		var infowindow = new google.maps.InfoWindow({
			content: '<p>Test</p>'
		});

		var marker = new google.maps.Marker({
			position: new google.maps.LatLng(ll1, ll2),
			map: map
		});

		google.maps.event.addListener(marker, 'click', function() {
			//infowindow.open(map,marker);
		});
    
  }
  
  $('body.category-kontakt').each(function(a, b) {
    $(b).addClass('category-contact');
  });
  $('body.category-contact').each(function(a, b) {
    $('.cms-block.contact').wrapAll('<div class="contact-col-set">');
    $('.cms-block.contact').each(function(c, d) {
      $(d).append('<div class="contact-map" id="contact-map-'+(c+1)+'">');
      insert_map((c+1), 'contact-map-'+(c+1));
    });
    if($('.wpcf7-mail-sent-ok').length == 1) {
      $('.wpcf7-mail-sent-ok').closest('form').addClass('success');
    }
  });
  
  
  
  
  
  
  
  
  
  
  
  
  
  // Misc
  $('.btn-network-alias a').attr('href', $('.btn-network a').attr('href'));


  
  
  
  
  
  
});
