$(document).ready(function(){
  // handle the page_lead animation
  $('.page_lead').each(function(){
    var $this = $(this);
    var $h2 = $this.find('h2');
    var $h3 = $this.find('h3');
    var $imagegallery = $('.imagegallery').css('opacity', 0);
    var $main_content_text = $('.main_content_text').css('opacity', 0);
    $h2.data('origleft', $h2.css('left')).css('left', '-500px');
    $h3.data('origleft', $h3.css('left')).css('left', '-500px');
    $this.hide().css('overflow', 'hidden').fadeIn(1000, function(){
      $h2.animate({left: $h2.data('origleft')}, 500, function(){
        $h3.animate({left: $h3.data('origleft')}, 500, function(){
          $imagegallery.animate({opacity: 1}, 500);
          $main_content_text.animate({opacity: 1}, 500);
        });
      });
    });
  });

  if ( $('body#frontpage').length > 0 ) {
    // handle the frontpageslider
    var frontpageslider_effectduration = 500;
    var frontpageslider_interval = 4000;
    
    // add some JavaScript only elements
    $('div.frontpagecontent').filter(':first').before('<div id="frontpageslider_outer"><a id="arrow_slider_left"> </a><div id="frontpageslider"><div id="fontpageslider_mask_left"></div><div id="frontpageslider_inner"></div><div id="fontpageslider_mask_right"></div></div><a id="arrow_slider_right"> </a></div>');
    var $slider = $('#frontpageslider_inner');
    //var $slider = $('<div id="frontpageslider_inner"></div>');
    
    var frontpagecontent_maxheight = 0;
    
    // copy the slider images from the content to the slider twice and hide all unused content boxes
    $('div.frontpagecontent')
      .each(function(){
        $(this).find('.fortheslider').clone().addClass('intheslider').append('<br>'+$(this).find('h2').text()).appendTo($slider);
      })
      .each(function(index){
        $(this).find('.fortheslider').addClass('intheslider').append('<br>'+$(this).find('h2').text()).appendTo($slider);
        $(this).find('h2').remove();
        
        if ($(this).height() > frontpagecontent_maxheight){
          frontpagecontent_maxheight = $(this).height();
        }
      })
      .css('height', frontpagecontent_maxheight)
      .not(':first')
      .hide();
      
    // bring the right image to the current position
    $slider.find('.intheslider:last').prependTo($slider);
    $slider.find('.intheslider:last').prependTo($slider);
    $slider.find('.intheslider:last').prependTo($slider);
    
    // handle the clicks on the right arrow
    $('#arrow_slider_right').click(function(){
      var $sliderimages = $slider.find('.intheslider');
      var stepwidth = $sliderimages.first().outerWidth();
      var i = 1;
      $slider.find('.intheslider').animate({left: '-='+stepwidth}, frontpageslider_effectduration, function(){
        if ( i >= $sliderimages.length ){
          $slider.find('.intheslider').css('left', '+='+stepwidth);
          $slider.find('.intheslider:first').appendTo($slider);
        }
        i++;
      });
      $('div.frontpagecontent').filter(':visible').fadeOut(frontpageslider_effectduration/2, function(){
        var $this = $(this);
        if ( $this.next('div.frontpagecontent').length > 0 ){
          $(this).next('div.frontpagecontent').fadeTo(frontpageslider_effectduration/2, 1);
        } else {
          $(this).siblings('div.frontpagecontent').filter(':first').fadeTo(frontpageslider_effectduration/2, 1);
        }
      });
    });
    // handle the clicks on the right straight reverse
    $('#arrow_slider_left').click(function(){
      var $sliderimages = $slider.find('.intheslider');
      var stepwidth = $sliderimages.first().outerWidth();
      var i = 1;
      $slider.find('.intheslider').animate({left: '+='+stepwidth}, frontpageslider_effectduration, function(){
        if ( i >= $sliderimages.length ){
          $slider.find('.intheslider').css('left', '-='+stepwidth);
          $slider.find('.intheslider:last').prependTo($slider);
        }
        i++;
      });
      $('div.frontpagecontent').filter(':visible').fadeOut(frontpageslider_effectduration/2, function(){
        var $this = $(this);
        if ( $this.prev('div.frontpagecontent').length > 0 ){
          $(this).prev('div.frontpagecontent').fadeTo(frontpageslider_effectduration/2, 1);
        } else {
          $(this).siblings('div.frontpagecontent').filter(':last').fadeTo(frontpageslider_effectduration/2, 1);
        }
      });
    });
    // trigger the slideshow
    var slideshowinterval = setInterval(function(){
      $('#arrow_slider_right').trigger('click');
    }, frontpageslider_interval);
    // stop the slideshow
    $('#arrow_slider_right, #arrow_slider_right, #frontpageslider').mouseover(function(){
      clearInterval(slideshowinterval);
    });
  }
  
  // handle the flashoverlay
  if ( $('a.download[href$=".swf"]').length > 0 ){
    $('<div id="flashoverlay"></div>').appendTo($('body'));
    $('a.download[href$=".swf"]').overlay({
      target: '#flashoverlay',
      expose: '#000000',
      onLoad: function(){
        $('<iframe width="'+this.getOverlay().width()+'" height="'+this.getOverlay().height()+'" src="'+this.getTrigger().attr('href')+'" id="flashoverlay_iframe" />').appendTo(this.getOverlay());
      },
      onClose: function(){
        $('#flashoverlay_iframe').remove();
      }
    });
  }
  
  // handle the lightbox
  if ( $('a[rel="#lightbox"]').length > 0 ){
    // handle the overlay
    $('<div id="lightbox"></div>').appendTo($('body'));
    $('a[rel="#lightbox"]').overlay({
      expose: '#000000',
      onBeforeLoad: function(){
        var $overlay = this.getOverlay();
        var href = this.getTrigger().attr('href');
        if ( href.match(/.mp4$/i) ) {
          var $item = $('<div id="lightbox_item" style="width: 640px; height: 360px;"><object width="640" height="360" type="application/x-shockwave-flash" data="/fileadmin/downloads/video/build/flashmediaelement.swf"><param name="movie" value="/fileadmin/downloads/video/build/flashmediaelement.swf"><param name="flashvars" value="controls=true&file=/'+href+'"><embed src="/fileadmin/downloads/video/build/flashmediaelement.swf" quality="high" width="640" height="360" type="application/x-shockwave-flash"  pluginspage="http://www.macromedia.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash" flashvars="controls=true&file=/'+href+'"></embed></object></div>');
        } else {
          var $item = $('<img src="'+href+'" id="lightbox_item">');
        }
        $item
          .appendTo($overlay)
          .load(function(){
            $overlay.css('left', $(window).width()/2 - $item.width()/2+'px');
          });
      },
      onClose: function(){
        $('#lightbox_item').remove();
      }
    });
  }
    
  // handle the foldables
  $('.foldertrigger').each(function(){
    var $this = $(this);
    var $folder = $this.nextAll('.foldable').eq(0);
    $folder.hide();
    $this.addClass('closed');
    $this.toggle(function(){
      if ( $this.hasClass('exclusive') ){
        $('.foldertrigger.open').trigger('click');
      }
      $folder.slideDown();
      $this.removeClass('closed').addClass('open');
    },function(){
      $folder.slideUp();
      $this.removeClass('open').addClass('closed');
    });
  });
});
