/** *****************************
 * As4u roller - based on jquery
 * 
 * 1.2.2011 - Jan Navratil
 * v 1.1 - # oprava 7.11.2011 - spatne pocitana sirka elementu s marginem 
 */
 

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

function log(d) {
  try {
    console.log(d);
  } catch(e) {}
}


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(true);
    if (width > toto.itemWidth) {
      toto.itemWidth = width;  
    }
    if ($(element).outerHeight(true) > toto.itemHeight) {
      toto.itemHeight = $(element).outerHeight(true);  
    }   
    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--; 
  } else { //mame pouze o jeden element vice -> pri posunu budou dva vedle sebe, to neni dobre -> naklonujeme druhy zleva (drive prvni) na konec
      //#BUG - 5.1.2012   
    if (this.rollerMain.width() < this.itemsCount*this.itemWidth) {   
      this.itemsCont.append(this.items[0].clone());
      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(){
      if (!$(this).hasClass('noScroll'))
        toto.btnLeft();
    });  
  }
  if ($('#'+this.options.btnright)) {
    $('#'+this.options.btnright).click(function(){
      if (!$(this).hasClass('noScroll'))
        toto.btnRight();
    });  
  }  
  /*
  try {
    console.log(' --- ' + this.options.rollid + ' ---- ');
    console.log(this.itemWidth);
    console.log(this.itemsCount*this.itemWidth);
    console.log(this.rollerMain.width());
  } catch(e) {} */ 
  
  if (this.itemsCount*this.itemWidth > this.rollerMain.width() && this.options.automatic == true) {//animovat jen kdyz je kam
    var fce = function() {
        toto.animate();
    };
    setTimeout(fce, this.stayTime);
    //this.animate();
  } else { //neni kam animovat, nastavime tlacitkum specialni tridy
    if ( !(this.itemsCount*this.itemWidth > this.rollerMain.width()) ) {
      $('#'+this.options.btnleft).addClass('noScroll');
      $('#'+this.options.btnright).addClass('noScroll');
    }
  }
 
};

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);    
  }
}








 
  
 
 
 
 
