/** *****************************
 * As4u roller - based on jquery
 * 
 * 1.2.2011 - Jan Navratil
 */
 

function As4uRoller(options) {
  this.options = options;
  this.automatic = true;
  this.animeTime = options.animeTime ? options.animeTime : 1500;
  this.stayTime = options.stayTime ? options.stayTime : 10000;
}


As4uRoller.prototype.show = function () {
  var toto = this;
  this.rollerMain = $('#'+this.options.rollid);
  this.rollerMain.css('position','relative');
  
  
  //nalezeni vnitrniho contu
  this.itemsCont = this.rollerMain.find('.rollbox-cont').eq(0);
  
  this.itemsCont.css('position','absolute');
  this.itemsCont.css('top','0');
  this.itemsCont.css('left','0');
  
  //projdeme si v cyklu existujici itemy
  this.itemsCount = 0;
  this.items = [];
  this.itemWidth = 0;
  this.itemHeight = 0;
  
  this.itemsCont.find('.rollitem').each(function(index, element) {
  
    //mouse eventy
    /*$(element).mouseenter(function(){
      toto.automaticBefore = toto.automatic;
      toto.automatic = false;       
    });
    $(element).mouseleave(function(){
      toto.automatic = toto.automaticBefore;  
      if (toto.automatic)
        toto.animate();       
    });   */
  
    toto.items.push($(element)); //create array of elements
    var width = $(element).outerWidth();
    if (width > toto.itemWidth) {
      toto.itemWidth = width;  
    }
    if ($(element).outerHeight() > toto.itemHeight) {
      toto.itemHeight = $(element).outerHeight();  
    }   
    toto.itemsCount++;  
  });
  
  //naklonujeme posledni element pred prvni  
  this.itemsCont.prepend(this.items[this.items.length-1].clone());
  if (this.itemsCount*this.itemWidth > this.rollerMain.width() + this.itemWidth) {
    this.items[this.items.length-1].detach();
    this.items.pop();
    this.itemsCount--;  
  }
  
  this.itemsCont.css('width', this.itemsCount*this.itemWidth+this.itemWidth);
  this.itemsCont.css('height', this.itemHeight);
  
  //posuneme absolutni pozici
  this.itemsCont.css('left', -parseInt(this.itemWidth));
  
  this.smer = this.options.smer ? this.options.smer : "left";
  
  //nastaveni akci na tlacitka
  if ($('#'+this.options.btnleft)) {
    $('#'+this.options.btnleft).click(function(){
      toto.btnLeft();
    });  
  }
  if ($('#'+this.options.btnright)) {
    $('#'+this.options.btnright).click(function(){
      toto.btnRight();
    });  
  }  
  if (this.itemsCount*this.itemWidth > this.rollerMain.width() && this.options.automatic == true) //animovat jen kdyz je kam
    this.animate();
 
};

As4uRoller.prototype.btnLeft = function () {  
  if (!this.inProgress) { 
    this.smer = "left";
    if (this.options.automatic == true)
      this.afterBtnClick();
      
    this.automatic = false;
    if (this.itemsCount*this.itemWidth > this.rollerMain.width())
      this.animate();  
  }
}

As4uRoller.prototype.btnRight = function () { 
  if (!this.inProgress) { 
    if (this.options.automatic == true)
      this.afterBtnClick();
      
    this.smer = "right";
    this.automatic = false;
    if (this.itemsCount*this.itemWidth > this.rollerMain.width())
      this.animate();  
    
  }
}

As4uRoller.prototype.afterBtnClick = function () { 
  var toto = this;
  clearTimeout(this.clickTimeout);
  var fce = function () {
    toto.smer = toto.options.smer ? toto.options.smer : "left"; //vychozi hodnota
    toto.automatic = toto.options.automatic;
    if (toto.itemsCount*toto.itemWidth > toto.rollerMain.width() && toto.options.automatic == true)
      toto.animate();
  };
  this.clickTimeout = setTimeout(fce, 15000); 
}


As4uRoller.prototype.animate = function () {  
  var toto = this;
  if (!this.inProgress) {
    this.direction = this.smer == 'right' ? '+=' : '-=';  
    this.inProgress = true;
    this.itemsCont.animate({   
      left: this.direction+this.itemWidth    
    }, this.animeTime, function (){toto.afterAnimate();});
  }                             
}

As4uRoller.prototype.afterAnimate = function () {  
  this.inProgress = false; //animace ukoncena  
  //prosunuti elementu
  if (this.smer == "left") { //jede to doleva
    //vezmeme element zleva a nacpem doprava
    this.itemsCont.append(this.itemsCont.find('.rollitem').eq(0));            
  } else {  //obracene    
    this.itemsCont.prepend(this.itemsCont.find('.rollitem').eq(-1));  
  }
  this.itemsCont.css('left', -parseInt(this.itemWidth));  
  if (this.automatic) {
    var toto = this;
    setTimeout(function() { if (toto.automatic) toto.animate();}, this.stayTime);    
  }
}





jQuery(document).ready(function(){
  // pr clanky
  if(jQuery('#prRoller').length > 0){
    var as4uRoller = new As4uRoller({rollid:'prRoller',smer:'left',automatic:true,btnleft:'rollLeft',btnright:'rollRight'});
    as4uRoller.show();
  }
  
  //novinky roller
  if(jQuery('#aktualityRollerCont').length > 0){
    var aktualityRoller = new As4uRoller({rollid:'aktualityRollerCont',smer:'left',automatic:true,btnleft:'aktualityRollLeft',btnright:'aktualityRight',stayTime:3000})
    aktualityRoller.show();
  }

  // poledni menu
  if(jQuery('#poledniMenuRollerCont').length > 0){
    var poledniMenuRoller = new As4uRoller({rollid:'poledniMenuRollerCont',smer:'left',automatic:true,btnleft:'poledniMenuLeft',btnright:'poledniMenuRight'});
    poledniMenuRoller.show();
  }

  // reklama
  if(jQuery('#reklamaRectangleRollerCont').length > 0){
    var reklamaRectangleRoller = new As4uRoller({rollid:'reklamaRectangleRollerCont',smer:'left',automatic:true,btnleft:'reklamaRectangleRollerLeft',btnright:'reklamaRectangleRollerRight',stayTime:10000});
    reklamaRectangleRoller.show();
  }

});


 
  
 
 
 
 
