/*
  (C) by KSI media sp. z o.o. ( www.ksimedia.pl )  
  Description : SlideBox
  Author      : MP
  Create date : 2010-08-22
  Comment     :
*/

function classSlideBox_default() {

  this.slideBoxStartArray = new Array();  
  this.slideBoxUrlArray = new Array() ;
  this.slideBoxContainerId = ''; // id div do ktorego zostana wczytane divy z tablicy Url
  this.containerId = '';
  this.position = 0;
  this.position_min = 0;
  this.position_max = 0;
  this.max_width = 0;
  this.step = 1; // co ile ma skakac
  this.margin = 1; // przy pieknym zapetleniu ile pozycji ma byc marginesem, aby bylo ladnie musi miec tyle ile jest w jednym oknie wyswietlana
  this.timerSleep = 4000;
  this.timerPause = 4000;
  this.timerStop = false;
  this.animationType = 'slide'; // slide,fade
  this.timerId = null;
  this.isLoop = true; // czy jest pieknie zapetlone
  this.isBusy = false; // czy jest zajety
  
  // button
  this.buttonHtmlClassActive = null;
  this.buttonsObject = null;
  
  
  this.setContainer = function ( containerId ) {
    this.slideBoxContainerId = containerId; 
    this.containerId = '#'+containerId; 
  }
  
  // wypelnia wskazany kontener
  this.fillContainer = function () {
    if ( this.slideBoxContainerId == '' ) alert('Blad, nie podano nazwy kontenera do SlideBox');
    var containerId = this.containerId;  
    this.position_min = 0;
    this.position_max = this.slideBoxUrlArray.length-1;
    this._laduj(0);
  }  
  
  this._buttonGenerate = function () {
    var obj = this;
    $(this.containerId+' > div').each( function(index) {
      var indeks = index+1;
      $('#'+obj.buttonContainer).append('<div class="'+obj.buttonHtmlClassOff+' '+obj.buttonHtmlClassOn+'" id="'+ obj.slideBoxContainerId+'_div_'+indeks+'">'+indeks+'</div>');
    });
    $('#'+this.buttonContainer+' > div').each( function(index) {
      $(this).click( function() {
       obj.setView(index); 
      });
    });    
  }
  
  
  this._laduj = function (indeks) {
    this.isBusy = true;
    var obj = this;    
    if ( indeks < obj.slideBoxUrlArray.length ) {
      $.ajax({
        url: this.slideBoxUrlArray[indeks],
        success: function ( data ) {
          $(obj.containerId).append(data);
        },
        complete: function() {
          this.isBusy = false;      
          if ( indeks == obj.slideBoxUrlArray.length-1 ) obj._przelicz();
            else obj._laduj(indeks+1);
        }
      });
    }
  }  
  
  // przelicza szerokosci i ilosci elementow
  this._przelicz = function () {
  
    this.isBusy = true;
    this.position = 1;    
    this.position_min = 0;
    var szerokosc = 0;
    var obj = this;    
    
    if ( this.isLoop === true && this.animationType == 'slide' ) { 
    // dla zaptelonej 
      this.position = this.margin;
      var tnij = $(this.containerId+' > div').length-this.margin-1;
      var ogonek = $(this.containerId + ' > div:gt('+tnij+')').clone();
      // $(this.containerId + ' > div:lt('+this.margin+')').clone().appendTo( this.containerId );
      $(this.containerId).prepend( ogonek );
      
    }
    $(this.containerId+' > div').each( function(index) { 
      obj.slideBoxStartArray[index] = szerokosc;
      szerokosc += $(this).outerWidth(true);
    });
    this.position_max = obj.slideBoxStartArray.length -1;
    if ( this.isLoop === true ) { 
      $(this.containerId).css('left','-'+this.slideBoxStartArray[this.position]+'px');
    }  
    this.max_width = szerokosc;    
    $(this.containerId).css('width',szerokosc+'px');
    if ( this.timerStop === false ) {
      this.startNextTimer( this.timerSleep );
    }
   
    this.isBusy = false; 
  }

  // 0 - wylacza, wieksze ustawia
  this.startNextTimer = function( timerSleep ) {
    if ( timerSleep > 0 ) {
      this.timeStop = false;
      var obj = this;
      if ( this.timerId !== null ) {
        clearInterval( this.timerId );      
      }
      this.timerId = setInterval( function() { obj.nextBtn(100) }, this.timerSleep );        
    } else {
      this.timeStop = true;
      if ( this.timerId !== null ) clearInterval( this.timerId );
      this.timerId = null;
    }
  
  }  
  // setActiveButton
    this._setActiveButton = function (index) {
      if ( this.position_max < index ) index = 0;
      pozycja = 0;
      for( var i in this.buttonsObject ) {
        pozycja++;
          $( '#'+this.buttonsObject[i] ).removeClass( this.buttonHtmlClassActive );          
          if ( pozycja == index ) {
            $( '#'+this.buttonsObject[i] ).addClass( this.buttonHtmlClassActive );
          }
      }
    }
    
  // next  
  this.nextBtn = function (setPause) {
    this._przesun('N',setPause);
  }
  
  // set
  this.setView = function(numer,setPause) {
    this._przesun(numer,setPause);  
  }
  
  // prev
  this.prevBtn = function (setPause,setPause) {
    this._przesun('P');
  }  
  
  // przesuwa klatki wzgledem okna
  // int - konkretne pozycja, 'N' - next, 'P' - prev
  this._przesun = function ( move, setPause) {
    if ( this.isBusy === true ) return false;    
    if ( setPause !== 100 ) {
      this.setPause();
    }  
    this.isBusy = true;
    
    // oblicz pozycje do przesuniecia i jaka ma miec po przesunieciu    
    if ( move == 'N' ) {
      this.position += this.step;    
    } else if ( move == 'P' ) {
      this.position -= this.step;    
    } else if ( parseInt( move, 10 ) !== 'NaN' ) {
      if ( this.position == parseInt(move,10) ) {
        this.isBusy = false;
        return true;
      }      
      this.position = parseInt(move,10);
    }; 
    
    // ustaw klatke
    if ( this.position > this.position_max ) { 
      $( this.containerId ).css("left","0");
      this.position = this.margin;
    } else if ( this.position < 0 ) { 
      $( this.containerId ).css("left",'-'+this.slideBoxStartArray[this.position_max]+'px');
      this.position = this.position_max-1;      
    }
    
    switch( this.animationType ) {
      case 'slide':            
      default:
        var obj = this;
        $( this.containerId ).animate({"left": '-'+obj.slideBoxStartArray[obj.position]+'px'}, "slow", function () {
          // korekta klatki
          if ( obj.position == 0 ) {
            $( obj.containerId ).css("left",'-'+obj.slideBoxStartArray[obj.position_max]+'px');
            obj.position = obj.position_max;              
          }          
            obj._setActiveButton( obj.position );               
            obj.isBusy = false;
          });
      break;  
    }
  }
 
  this.setPause = function() {
    this.startNextTimer(0);
    var obj = this;
    setTimeout( function() { obj.startNextTimer( obj.timerSleep ) }, this.timerPause );
  }  

// end object  
}

jQuery.fn.fadeIn = function(speed, callback) { 
    return this.animate({opacity: 'show'}, speed, function() { 
        if (jQuery.browser.msie)  
            this.style.removeAttribute('filter');  
        if (jQuery.isFunction(callback)) 
            callback();  
    }); 
}; 
 
jQuery.fn.fadeOut = function(speed, callback) { 
    return this.animate({opacity: 'hide'}, speed, function() { 
        if (jQuery.browser.msie)  
            this.style.removeAttribute('filter');  
        if (jQuery.isFunction(callback)) 
            callback();  
    }); 
};
