var current_link;
var rotateBanners = new Object();
var class_name;
var banner_symbol;
var flash_id;
var flash_src;
var flash_width;
var flash_height;
var flash_bg;

function getBannerTime(banner){
   class_name = jQuery('#' + banner).find('.turboBannerBody:visible').attr('class');  
   class_name = class_name.match(/jakkolwiek\d+/) + '';
   class_name = class_name.slice(10, class_name.length);
   return class_name;
}

function rewindBannerIE(banner_id){
    alert(banner_id);    
    //window[flash_id].Rewind();
    //window[flash_id].Play();
}

function showNextBanner(banner){         
      current_link = jQuery('#' + banner).find('.control_panel').children('.active');
      if (current_link.next('.button').size()){
         current_link = current_link.next('.button:first');
      }
      else
         current_link = current_link.siblings('.button:first');
      class_name = current_link.attr('class');
      class_name = class_name.slice(class_name.indexOf('item_'), class_name.indexOf('item_') + 6);
      jQuery('#' + banner).find('.turboBannerBody:visible').fadeOut('fast', function() {
         jQuery(this).siblings('.turboBannerBody.' + class_name).fadeIn('fast', function() {
               clearInterval( rotateBanners[banner] );    
               rotateBanners[banner] = setInterval('showNextBanner("' + banner + '")', getBannerTime(banner) * 1000);
               if (navigator.appName == 'Microsoft Internet Explorer'){
                  if (jQuery(this).children('div.flash_banner').size()){
                     //przewijanie do początku dla IE
                     flash_id = jQuery(this).find('object').attr('id');
                     flash_src = jQuery(this).find('object').children('param[name="movie"]').attr('value');
                     flash_width = jQuery(this).find('object').attr('width');
                     flash_height = jQuery(this).find('object').attr('height');
                     flash_bg = jQuery(this).find('object').children('param[name="bgcolor"]').attr('value');
                     //alert(flash_id + '\n' + flash_src + '\n' + flash_width + '\n' + flash_height + '\n' + flash_bg);
                     jQuery(this).children('div.flash_banner').html('');
                     var so = new SWFObject(flash_src, flash_id, flash_width, flash_height, "6", flash_bg);
	                 so.addVariable("FlashVars", ""); // this line is optional, but this example uses the variable and displays this text inside the flash movie
	                 so.addParam("wmode", "transparent");
	                 so.write(jQuery(this).children('div.flash_banner').attr('id'));             
                  }
               }
            });
      });
      jQuery('#' + banner).find('.control_panel .active').removeClass('active');
      current_link.addClass('active');
   }

function createRotateBanners(){ 
   var nav_button = jQuery('<a class="button"></a>');
   var nav_bar = jQuery('<div class="control_panel"></div>');
   var count;
   jQuery('.rotateBanner').each(function(){
      count = 0;
      banner_symbol = jQuery(this).parents('.cBox_adv').attr('id'); 
      rotateBanners[banner_symbol] = setInterval('showNextBanner("' + banner_symbol + '")', getBannerTime(banner_symbol) * 1000);
      nav_bar.clone().prependTo(jQuery(this));
      jQuery(this).children('.turboBannerBody').each( function(){
         count++;
         nav_button.clone().appendTo(jQuery(this).siblings('.control_panel'));         
         jQuery(this).siblings('.control_panel').children('.button:last').addClass('item_' + count).text(count).attr('href', '#');
         if (count == 1)
            jQuery(this).siblings('.control_panel').children('.button:first').addClass('active');
      });
   });
}

jQuery(document).ready(function(){
   //var banner_shifter = setInterval('showNextBanner()', 10000);
   //alert(1);
   createRotateBanners();
   
/*   jQuery('.rotateBanner a.next_banner').click(function(){
      if (jQuery(this).parent().siblings('.turboBannerBody').size()){      
         jQuery(this).parent().fadeOut('fast', function() {
            if (jQuery(this).next('.turboBannerBody').size())
               jQuery(this).next('.turboBannerBody:first').fadeIn();
            else
               jQuery(this).siblings('.turboBannerBody:first').fadeIn();
         });
         clearInterval(banner_shifter);
         banner_shifter = setInterval('showNextBanner()', 10000);
      }
      return false;
   });
   
    jQuery('.rotateBanner a.prev_banner').click(function(){
      if (jQuery(this).parent().siblings('.turboBannerBody').size()){      
         jQuery(this).parent().fadeOut('fast', function() {
            if (jQuery(this).prev('.turboBannerBody').size())
               jQuery(this).prev('.turboBannerBody:last').fadeIn();
            else
               jQuery(this).siblings('.turboBannerBody:last').fadeIn();
         });
         clearInterval(banner_shifter);
         banner_shifter = setInterval('showNextBanner()', 10000);
      }
      return false;
   });*/
   jQuery('.rotateBanner .control_panel .button').click( function() {
      banner_symbol = jQuery(this).parents('.cBox_adv').attr('id');      
      class_name = jQuery(this).attr('class');
      class_name = class_name.slice(class_name.indexOf('item_'), class_name.indexOf('item_') + 6);
      if (jQuery(this).parents('.rotateBanner').children('.turboBannerBody.' + class_name + ':visible').size() == 0){
         jQuery(this).parents('.rotateBanner').children('.turboBannerBody:visible').fadeOut('fast', function() {
            jQuery(this).parents('.rotateBanner').children('.turboBannerBody.' + class_name).fadeIn('fast', function() {
               clearInterval( rotateBanners[banner_symbol] );     
               rotateBanners[banner_symbol] = setInterval('showNextBanner("' + banner_symbol + '")', getBannerTime(banner_symbol) * 1000);
               if (navigator.appName == 'Microsoft Internet Explorer'){
                  if (jQuery(this).children('div.flash_banner').size()){
                     //przewijanie do początku dla IE
                     flash_id = jQuery(this).find('object').attr('id');
                     flash_src = jQuery(this).find('object').children('param[name="movie"]').attr('value');
                     flash_width = jQuery(this).find('object').attr('width');
                     flash_height = jQuery(this).find('object').attr('height');
                     flash_bg = jQuery(this).find('object').children('param[name="bgcolor"]').attr('value');
                     //alert(flash_id + '\n' + flash_src + '\n' + flash_width + '\n' + flash_height + '\n' + flash_bg);
                     jQuery(this).children('div.flash_banner').html('');
                     var so = new SWFObject(flash_src, flash_id, flash_width, flash_height, "6", flash_bg);
	                 so.addVariable("FlashVars", ""); // this line is optional, but this example uses the variable and displays this text inside the flash movie
	                 so.addParam("wmode", "transparent");
	                 so.write(jQuery(this).children('div.flash_banner').attr('id'));            
                  }
               }
            });
         });
         jQuery(this).siblings('.active').removeClass('active');
         jQuery(this).addClass('active');
      }
      return false;
   });
      
});