/* 
 * banner
 */
 
function _banner() {
   $flba = this;
}

_banner.prototype.options = {};
_banner.prototype.options_set = {
   time:0,
   counter:0,
   loopActive:false,
   type:0,
   typeIs:'blind',
   obj:{},
   picNum:0,
   /* wenn 0 - kein Start, wenn 1 - ein Durchlauf, wenn 2 - unendlich */
   loop:0,
   timeout:{}
};

_banner.prototype.init = function(obj,index)
{
   $flba.options[index].obj = obj;
   $flba.options[index].picNum = obj.find('.banner-container').children().length;
   switch ($flba.options[index].type)
   {
      case 0:
         $flba.options[index].typeIs = 'blind';
         break;
      case 1:
         $flba.options[index].typeIs = 'bounce';
         break;
      case 2:
         $flba.options[index].typeIs = 'clip';
         break;
      case 3:
         $flba.options[index].typeIs = 'drop';
         break;
      case 4:
         $flba.options[index].typeIs = 'explode';
         break;
      case 5:
         $flba.options[index].typeIs = 'fade';
         break;
      case 6:
         $flba.options[index].typeIs = 'fold';
         break;
      case 7:
         $flba.options[index].typeIs = 'highlight';
         break;
      case 8:
         $flba.options[index].typeIs = 'puff';
         break;
      case 9:
         $flba.options[index].typeIs = 'pulsate';
         break;
      case 10:
         $flba.options[index].typeIs = 'scale';
         break;
      case 11:
         $flba.options[index].typeIs = 'shake';
         break;
      default:
         $flba.options[index].typeIs = 'slide';
         break;
         
      case 14:
         obj.find('.banner-container').children().css('display','block');
         break;
   }
   if ($flba.options[index].loop)
   {
      $flba.play(index);
   }
};

_banner.prototype.next = function(index,prev)
{
   // prev vorbelegen
   if (prev === undefined || prev!==true) {
      prev=false;
   }
   // Variablen setzen
   var container = $flba.options[index].obj.find('.banner-container');
   var current = container.find('.current');
   var nextEq;
   var showtime = current.attr('showtime');
   
   // Wenn Anzeigedauer gesetzt und mehr als ein Objekt im Banner, Animation durchführen
   if (showtime > 0 && $flba.options[index].picNum > 1)
   {
      // Index des als nächstes anzuzeigenden Objekts ermitteln
      if (!prev)
      {
         nextEq = (current.index() < $flba.options[index].picNum-1) ? (current.index()+1) : 0;
      }
      else
      {
         nextEq = (current.index() == 0) ? ($flba.options[index].picNum-1) :(current.index()-1);
      }
      
      // Für Standard-Animationen Animation durchführen
      if ($flba.options[index].type <= 12 || !$.support.style)
      {
         current.removeClass('current').hide($flba.options[index].typeIs,{
            easing:'swing'
         },$flba.options[index].time,function(){
            container.children().eq(nextEq).show($flba.options[index].typeIs,{
               easing:'swing'
            },$flba.options[index].time,function(){
               $(this).addClass('current');
               // Behandlung für Autodurchlauf
               if ($flba.options[index].loopActive && !($(this).hasClass('first') && $flba.options[index].loop < 2))
               {
                  $flba.options[index].timeout = window.setTimeout("$flba.next('"+index+"')", $(this).attr('showtime')*1000);
               }
               else
               {
                  $flba.stop(index);
               }
            })
         });
      }
      // Eigene Animationen
      else
      {
         // Variablen setzen
         var nextObj = container.children().eq(nextEq); // als nächsets anzuzeigendes Objekt ermitteln
         var w = current.width()*-1; // negative Breite des aktuellen Objekts ermitteln für margin
         var h = current.height()*-1; // negative Höhe des aktuellen Objekts ermitteln für margin
         switch ($flba.options[index].type)
         {
            // horizontales Endlosband
            case 13:
               // vorwärts
               if (!prev)
               {
                  // Wenn Ende erreicht, Elemente neu anordnen
                  if (nextEq == 0)
                  {
                     nextObj.insertAfter(container.children().eq($flba.options[index].picNum-1));
                     nextObj = container.children().eq($flba.options[index].picNum-1);
                  }
                  nextObj.show(0,function(){
                     current.removeClass('current').animate({
                        'margin-left':w
                     },$flba.options[index].time,'swing',function(){
                        current.hide().css('margin-left','');
                        nextObj.show().addClass('current');
                        // Behandlung für Autodurchlauf
                        if ($flba.options[index].loopActive && !(nextObj.hasClass('first') && $flba.options[index].loop < 2))
                        {
                           $flba.options[index].timeout = window.setTimeout("$flba.next('"+index+"')", nextObj.attr('showtime')*1000);
                        }
                        else
                        {
                           $flba.stop(index);
                        }
                     });
                  });
               }
               // rückwärts
               else
               {
                  // Wenn Anfang erreicht, Elemente neu anordnen
                  if (nextEq == ($flba.options[index].picNum-1))
                  {
                     nextObj.insertBefore(container.children().eq(0));
                     nextObj = container.children().eq(0);
                  }
                  nextObj.css({
                     'margin-left':w
                  }).show(0,function(){
                     current.removeClass('current');
                     nextObj.animate({
                        'margin-left':0
                     },$flba.options[index].time,'swing',function(){
                        current.hide();
                        nextObj.addClass('current').css({
                           'margin-left':''
                        });
                     });
                  });
               }  
               break;
               
            // vertikales Endlosband
            case 14:
               // vorwärts
               if (!prev)
               {
                  // Wenn Ende erreicht, Elemente neu anordnen
                  if (nextEq == 0)
                  {
                     nextObj.insertAfter(container.children().eq($flba.options[index].picNum-1));
                     nextObj = container.children().eq($flba.options[index].picNum-1);
                  }
                  nextObj.show(0,function(){
                     current.removeClass('current').animate({
                        'margin-top':h
                     },$flba.options[index].time,'swing',function(){
                        current.hide().css('margin-top','');
                        nextObj.show().addClass('current');
                        // Behandlung für Autodurchlauf
                        if ($flba.options[index].loopActive && !(nextObj.hasClass('first') && $flba.options[index].loop < 2))
                        {
                           $flba.options[index].timeout = window.setTimeout("$flba.next('"+index+"')", nextObj.attr('showtime')*1000);
                        }
                        else
                        {
                           $flba.stop(index);
                        }
                     });
                  });
               }
               // rückwärts
               else
               {
                  // Wenn Anfang erreicht, Elemente neu anordnen
                  if (nextEq == ($flba.options[index].picNum-1))
                  {
                     nextObj.insertBefore(container.children().eq(0));
                     nextObj = container.children().eq(0);
                  }
                  nextObj.css({
                     'margin-top':h
                  }).show(0,function(){
                     current.removeClass('current');
                     nextObj.animate({
                        'margin-top':0
                     },$flba.options[index].time,'swing',function(){
                        current.hide();
                        nextObj.addClass('current').css({
                           'margin-top':''
                        });
                     });
                  });
               }  
               break;
         }
      }
   }
};

_banner.prototype.playStop = function(index)
{
   if ($flba.options[index].obj.find('.banner-play').attr('status') == 'on')
   {
      $flba.stop(index);
   }
   else
   {
      $flba.play(index);
      $flba.next(index);
   }
};
_banner.prototype.play = function(index)
{
   var showtime = $flba.options[index].obj.find('.banner-container .current').attr('showtime');
   if (showtime > 0 && $flba.options[index].picNum > 1)
   {
      $flba.options[index].loopActive = true;
      $flba.options[index].timeout = window.setTimeout("$flba.next('"+index+"')", showtime*1000);
      $flba.options[index].obj.find('.banner-play').attr('status','on');
   }
   else
   {
      $flba.stop(index);
   }
};
_banner.prototype.stop = function(index)
{
   if ($flba.options[index].timeout != {})
   {
      window.clearTimeout($flba.options[index].timeout);
      $flba.options[index].timeout = {};
   }
   $flba.options[index].loopActive = false;
   $flba.options[index].obj.find('.banner-play').attr('status','off');
};

_banner.prototype.goNext = function(index)
{
   $flba.stop(index);
   $flba.next(index);
};
_banner.prototype.goPrev = function(index)
{
   $flba.stop(index);
   $flba.next(index,true);
};


var banner = new _banner();
/**
 * Erwartet das jQuery-Objekt des banners
 */

$.fn.banner = function()
{
   $(this).find('.autopopup-banner').each(function(){
      thisbanner = $(this);
      var id = thisbanner.attr('id');
      if ($('.ui-dialog').find('#'+id).index()===-1)
      {
         thisbanner.dialog({			 
            width: thisbanner.attr('width'),
            height: 'auto',
            resizable: false,
            modal:false,
            position: 'center'
         }).dialog('open').closest('.ui-dialog').css('position','fixed');
         var new_top = ($(window).height()/2) - (thisbanner.height()/2);
         if (new_top < 5)
         {
            new_top = 5;
         }
         thisbanner.closest('.ui-dialog').css('top',new_top);
      }
      function bannerclose(event)
      {
         event.data.thisbanner.dialog('close');
      }
      $('#'+id).on('click','a',{'thisbanner':thisbanner},bannerclose);
   });
   
   
   var thisbanner = {};
   $(this).find('.banner').not('.banner-activated').each(function()
   {
      thisbanner = $(this);
      thisbanner.find('.controls').show();
      thisbanner.addClass('banner-activated');
      var index = thisbanner.attr('id');
      banner.options[index] = $.extend(true,{},banner.options_set,{
         time : thisbanner.attr('time')*1,
         type : thisbanner.attr('type')*1,
         loop : thisbanner.attr('animation_loop')*1
      });
      banner.init(thisbanner,index);
      $('body').delegate('.banner-prev','click',function(){
         banner.goPrev(index);
      });   
      $('body').delegate('.banner-next','click',function(){
         banner.goNext(index);
      });
      $('body').delegate('.banner-play','click',function(){
         banner.playStop(index);
      });
   });
};




