/**
 * Anpassungen für EHFK
 * MainRotation, Foto-/Video-Lightbox und Facebook-Button
 */
var de = de || {};
de.ehfk = de.ehfk || {};
de.ehfk.main = (function($){
    // Zeit nach der die Rotation erneut startet in ms
    var restartTime = 5000;
  
    function initRotations(){
        var $mainRotation = $("#maincol .mainRotation");
        $mainRotation.each(function(){
            var $this = $(this);
            var $mainRotTab = $this.find("ul li");
            var $mainRotTeaser = $this.find(".ateaser-content");
            var $activeTeaser = $this.find(".ateaser-content.active");
          
            // Zeit aulesen
            var timeParse = $this[0].className.match(/data-mRotTime-\d+/);
            var time;
            if (timeParse) {
                time = timeParse[0].replace(/data-mRotTime-/, "");
            }
            else {
                time = 0;
            }
          
            var rotation;
            if (time == undefined || time <= 0) {
                rotation = false;
            }
            else {
                rotation = true;
            }
          
            var restartTimer;
            var rotaTimer;
          
            // Teaser initialisieren
            var mainRotIndex = $mainRotTeaser.index($activeTeaser);
            $mainRotTeaser.eq(mainRotIndex).css("display", "block");
            if (rotation) {
                rotaTimer = setInterval(nextTeaser, time);
            }
          
            // Click auf einen der Tabs
            $mainRotTab.click(function(){
                var index = $mainRotTab.index(this);
              
                if (index != mainRotIndex) {
                    $mainRotTab.removeClass("active");
                    $(this).addClass("active");
                    $mainRotTeaser.fadeOut(400, function(){
                        $(this).removeClass("active");
                    });
                    $mainRotTeaser.eq(index).fadeIn(400, function(){
                        $(this).addClass("active");
                    });
                    mainRotIndex = index;
                }
              
                if (rotation) {
                    clearTimeout(restartTimer);
                    clearInterval(rotaTimer);
                    restartTimer = setTimeout(function(){
                        rotaTimer = setInterval(nextTeaser, time);
                    }, restartTime);
                }
              
                return false;
            });
          
            // N�chsten Teaser anzeigen
            function nextTeaser(){
                clearTimeout(restartTimer);
                clearInterval(rotaTimer);
                var $activeTab = $this.find("ul li.active");
                var size = $mainRotTab.size();
                var index = $mainRotTab.index($activeTab);
                var neuIndex;
                if (index + 1 >= size) {
                    neuIndex = 0;
                }
                else {
                    neuIndex = index + 1;
                }
                $mainRotTab.eq(index).removeClass("active");
                $mainRotTeaser.eq(index).fadeOut(400, function(){
                    $(this).removeClass("active");
                });
              
                $mainRotTab.eq(neuIndex).addClass("active");
                $mainRotTeaser.eq(neuIndex).fadeIn(400, function(){
                    $(this).addClass("active");
                    mainRotIndex = neuIndex;
                    rotaTimer = setInterval(nextTeaser, time - 800);
                });
              
              
            }
        });
    }
  
     function initAjaxLinks(area){
  if (area) {
   $ajaxLinks = $(area); 
  }
  else{
   $ajaxLinks = $('.hfk-fg a, .imgsmall a, .mediateaserimgleft a, .mainteaser a');
  }
        // Content will be loaded into the specified target via innerHTML
        $ajaxLinks.live('click', function(e){
          if ($(this).attr('href').indexOf("#") != -1) {
     e.preventDefault();
     var obj = this;
     de.bild.httpFist.load({
      'obj': obj,
      'callback': function(data){
       data = $(data);
       //if Lightbox else FG
       if (data.hasClass("lightbox")) {
        handleLightbox(data);
       }
       else {
        var fg = $(obj).parents('.hfk-fg').html(data);
       }
      }
     });
    }
  });
    }
  
    //positioniert die LB und belegt den closeButton
    function handleLightbox(data){
        $('body').prepend($('<div class="overlay"></div>').css("opacity", "0").animate({"opacity" : "0.5"}, 400));
        $('body').prepend($(data).hide());
        $('.lightbox').fadeIn();
        initAjaxLinks(data);
        $('.lightbox .close a').live('click', function(e){
   e.preventDefault();
            $(this).parents('div.lightbox').fadeOut(function(){
                $(this).remove();
            });
            $('.overlay').fadeOut(function(e){
                $(this).remove();
            });
        });
    }
  
    // Facebook-Button einbinden
    function initFB() {
        // Template für den Likebutton
        var template = '<iframe scrolling="no" frameborder="0" allowtransparency="true" src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.ein-herz-fuer-kinder.de&amp;send=false&amp;layout=button_count&amp;width=216&amp;show_faces=false&amp;action=like&amp;colorscheme=light&amp;font=verdana&amp;height=26"></iframe>';
        $("#languageSelect").append(template);
    }
  
    return {
        'init': function(){
            initRotations();
            initAjaxLinks();
            initFB();
        }
    };
  
}(jQuery));
// Rotation nach laden des DOMs starten
jQuery(document).ready(function(){
    de.ehfk.main.init();
});

