/**
 * dalen.flipbox.js
 *
 * @package js
 * @filesource
 */

/**
 * Casovane (a "rucni") prepinani LI prvku v UL
 *
 * @author Ondrej Rais <ondrej.rais@miton.cz>
 */
var Flipbox = Class.create({
   //nastaveni
   options: {},

   /**
    * Konstuktor
    */
   initialize: function(options)
   {
      this.active = this.getMinItem();

      if (options.variableName == undefined) {
         options.variableName = 'flipbox';
      }

      if (options.area == undefined || options.switcher == undefined) {
         alert('Neni nadefinovano options.area a options.switcher');
         return false;
      }

      this.options = options;
      

      this.items = $$('#' + options.area + ' li');      
      this.setSwitch();

      //casovac
      setInterval(this.options.variableName + '.setNextItem();', 5000);
   },

   getMaxItem: function()
   {
      return this.items.length - 1;
   },

   getMinItem: function()
   {
      return 0;
   },

   setPrevItem: function()
   {
      if (this.active == this.getMinItem()) {
         var item = this.getMaxItem();
      } else {
         var item = this.active - 1;
      }

      this.setItem(item);
   },

   setNextItem: function()
   {      
      if (this.active == this.getMaxItem()) {
         var item = this.getMinItem();
      } else {
         var item = this.active + 1;
      }

      this.setItem(item);
   },

   setItem: function(item)
   {          
      for (i = this.getMinItem(); i <= this.getMaxItem(); ++i) {
         var a = $('switch-item-' + i);

         if (i != item) {
            a.removeClassName('active');
            this.items[i].hide();
         } else {
            a.addClassName('active');
            this.items[i].show();
         }
      }

      this.active = item;
   },

   setSwitch: function()
   {
      var html = '';

      //predchozi polozka
      html += '<a href="javascript:' + this.options.variableName + '.setPrevItem();void(0);" class="switch-item-prev" title="Zobrazit předchozí položku"><span><</span></a>';

      //jednotlive polozky
      for (i = this.getMinItem(); i <= this.getMaxItem(); ++i) {
         var number = i + 1;
         if (this.options.toTitlePath != undefined) {
            var title = 'a';
         } else {            
            var title = 'Zobrazit položku č. ' + number;
         }

         html += '<a href="javascript:' + this.options.variableName + '.setItem(' + i + ');void(0);" ';
         html += 'class="switch-item';
         if (this.active == i) {
            html += ' active';
            this.items[i].show();
         } else {
            this.items[i].hide();
         }
         html += '" id="switch-item-' + i + '" title="' + title + '"><span>' + number + '</span></a>';
      }

      //nasledujici polozka
      html += '<a href="javascript:' + this.options.variableName + '.setNextItem();void(0);" class="switch-item-next" title="Zobrazit následující položku"><span>></span></a>';

      $(this.options.switcher).update(html);
   }
});