$(document).ready(function() {
  
  $('.tx-dspocast-pi1').each(function(){
    var papa = $(this);
    var thisoverlayinstance;
    $('.ds_pt_overlay',papa).each(function(){   
      thisoverlayinstance = $(this);
      $(this).find('.ds_pt_kleinvideo_fenster').scrollable({
        next: thisoverlayinstance.find('.ds_pt_slider_right'),
        prev: thisoverlayinstance.find('.ds_pt_slider_left')
      }).navigator({
        navi: thisoverlayinstance.find('.ds_pt_slidernavigation'),
        activeClass: 'ds_pt_slidernavigation_el_active',
        indexed: true
      });
      $(this).find('.ds_pt_kleinvideo_fenster').data('scrollable').seekTo(0);
      $(this).find('.ds_pt_overlay_mitte > div:gt(0)').hide();
      $(this).find('.ds_pt_overlay_mitte > div:eq(0)').addClass('ds_pt_panactive');
      $(this).find('.ds_pt_kleinvideo_frame > div').each(function(index){
        $(this).click(function(){
          if( !$(this).hasClass('ds_pt_tabactive')){
            thisoverlayinstance.find('.ds_pt_overlay_mitte > div').removeClass('ds_pt_panactive');
            thisoverlayinstance.find('.ds_pt_overlay_mitte > div').hide().eq(index).fadeIn('fast').addClass('ds_pt_panactive');
            thisoverlayinstance.find('.ds_pt_kleinvideo_frame > div').removeClass('ds_pt_tabactive');
            $(this).addClass('ds_pt_tabactive');
          }       
        });
      });    
    });
     
    $(".ds_pt_videozeigen, .ds_pt_listebild",papa).overlay({
      mask: '#000000',
      top:    50,
      target: $('.ds_pt_overlay', papa),
      close:  $('.ds_pt_overlay_close', papa),
      onBeforeLoad: function(e){
        //console.log(e.currentTarget);
      }
    }).each( function(index){
       $(this).click(function(){
         $(thisoverlayinstance).find('.ds_pt_kleinvideo_frame > div').eq(index).trigger('click');
         $(thisoverlayinstance).find('.ds_pt_overlay_mitte > div.ds_pt_panactive a').flowplayer(0).load();
       });       
    });
    
    flowplayer("a.ds_pt_videolinktoplayer", "typo3conf/ext/ds_pocast/res/flowplayer/flowplayer-3.2.7.swf", {		
    	plugins:  {
    		controls:  {
    			volume: false		
    		}
    	}
    });
    $('.ds_pt_startbox .ds_pt_startbox_left_inner img').click(function(){
      $(this).parents('.ds_pt_startbox').find('.ds_pt_videozeigen').trigger('click');
    });
    
    $('.ds_pt_linkzuallenvideos a',papa).click(function(){       
       if(location.href == $(this).attr('href') || $(this).attr('href') == '') {
         $(".ds_pt_overlay_close",papa).trigger('click');
         return false;
       }  
    });
  $('.ds_pt_overlay_close', papa).click(function() {
       $(thisoverlayinstance).find('.ds_pt_overlay_mitte > div.ds_pt_panactive a').flowplayer(0).stop();
   });
    
  });
});
