/*
 * Changebox - jQuery plugin
 *
 * vytvori box s prepinanim polozek
 *
 * autor: Daniel Repa
 *
 * verze: 1.0 (23/08/2010)
 * vyzaduje: jQuery v1.4+
 */

(function($) {

	function Changebox(node,options) {
    this.defaults = {
      'arrows'      : true, // zobrazit prepinaci sipky
      'numbers'     : true, // zobrazit prepinaci cisilka
      'play_pause'  : true, // zobrazit tlacitko play/pause
      'play'        : true, // zacit automaticky prepinat
      'time'        : 6000, // cas mezi prepinanim v milisekundach
      'delay'       : 0, // spozdeni zacatku prepinani v milisekundach
      'pause_on_hover': true, // pri prejeti mysi nad prolinackou pozastavit prepinani
      'previous_text': '<', // text tlacitka prechozi
      'next_text'   : '>', // text tlacitka dalsi
      'play_text'   : 'play', // text tlacitka play/pause - spustit
      'pause_text'  : 'pause', // text tlacitka play/pause - zastavit
      'isPlaying'   : false
    };

    this.options = $.extend({},this.defaults,options);
    this.node = node;
    this.timer = null;
  };

  $.extend(Changebox.prototype, {
    init: function() {
      var thisObj = this;
      if ($(".item", this.node).length < 2) {
				return;
			}
// zobrazit ovladaci prvky?
      if (this.options['arrows'] || this.options['numbers'] || this.options['play_pause']) {
// listicka
        var control_cont = $("<ul>", {'class' : 'controls'});
// polozky seznamu
        var li = null;
        var href = null;
        var text = null;
// zobrazit sipku zpet
        if (this.options['arrows']) {
          li = $("<li>", {'class' : 'previous'});
          text = this.options['previous_text'];
          href = $("<a>", {
            'href' : '#prev',
            'text' : text,
            click : function(){
              thisObj.change('prev');
              return false;
            }
          });
          li.append(href);
          control_cont.append(li);
        }
// zobrazit cisilka
        if (this.options['numbers']) {
          $(".item", this.node).each(function(j, item){
            li = $("<li>", {'class' : 'number' + (!j ? ' act' : '')});
            href = $("<a>", {
              'href' : '#' + (j + 1),
              'text' : (j + 1),
              click : function(){
// pokud je jiz cislo aktivni, nemusime na nej prepinat 
                if (!$(this).parent("li").hasClass('act')) {
                  thisObj.change(j);
                  thisObj.stop();
                }
                return false;
              }
            });
            li.append(href);
            control_cont.append(li);
          });
        }

// zobrazit sipku next
        if (this.options['arrows']) {
          li = $("<li>", {'class' : 'next'});
          text = this.options['next_text'];
          href = $("<a>", {
            'href' : '#next',
            'text' : text,
            click : function(){
              thisObj.change('next');
              return false;
            }
          });
          li.append(href);
          control_cont.append(li);
        }

//zobrazit tlacitko play/pause 
        if (this.options['play_pause']) {
          li = $("<li>", {'class' : 'play_pause play'});
          text = ((this.options.play == false)? this.options.pause_text : thisObj.options.play_text);
          href = $("<a>", {
            'href' : "#",
            'text' : text,
            click : function(){
              if (thisObj.options.isPlaying)
                thisObj.stop();
              else
                thisObj.play();
              return false;
            }
          });
          li.append(href);
          control_cont.append(li);
        }
        this.node.append(control_cont);
      }
// skryjeme vsechny elementy pro animaci
      $(".item", this.node).hide();
// zobrazime prvni
      $(".item:first", this.node).show();
// pridame prvnimu itemu tridu act  
      $(".item:first", this.node).addClass("act");

// pokud se ma automaticky zacit prepinat 
      if (this.options['play']) {
// prepinani spustit se spozdenim delay (pokud je nastaveno) */
        if (this.options['delay'])
          setTimeout(function() {
            thisObj.play();}, 
            thisObj.options['delay']
          );
        else
          thisObj.play();
      }

// pri najeti mysi pozastavit prepinani
      if (this.options['pause_on_hover']) {
        $(".item",this.node).hover(
          function () {
            thisObj.stop();
          },
          function () {
            thisObj.play();
          }
        );
      }
      return true;
    },
    play: function() {
      var thisObj = this;
// priznak isPlaying a objekt timeru ulozime do daneho nodu */
      if (!this.options.isPlaying) {
        function intervalCallback() {thisObj.change('next');};
        this.timer = window.setInterval(intervalCallback, this.options['time']);
        this.options.isPlaying = true;
        $("li.play_pause", this.node).removeClass('play').addClass('stop');
        $("li.play_pause a", this.node).text(this.options['pause_text'])
      }
      return true;
    },
    stop: function() {
      if (this.timer) {
//smazeme timer 
        window.clearInterval(this.timer);
        $("li.play_pause", this.node).removeClass('stop').addClass('play');
        $("li.play_pause a", this.node).text(this.options['play_text'])
      }
//a prepneme priznak 
      this.options.isPlaying = false;
      return true;
    },
    change: function(to) {
//ziskame aktualni item a aktualni cisilko 
      var act_item = $(".item.act", this.node);
      var act_ctl = $(".number.act", this.node);
      var to_element = null;
      var ctl = null;
//skryt vsechny viditelne itemy */
      var visible = $(".item:visible", this.node);
      visible.fadeOut('slow');
      visible.removeClass('act');

// odaktivnit aktivni cisilko */
      $(".number", this.node).removeClass('act');

// prepnout na dalsi item */
      if (to == 'next'){
        to_element = act_item.next(".item");
        ctl = act_ctl.next(".number");
        if (!to_element.length){
          to_element = $(".item:first", this.node);
          ctl = $(".number:first", this.node);
        }
        ctl.addClass('act');
      }
// prepnout na predchozi item */
      else if (to == 'prev') {
        to_element = act_item.prev(".item");
        ctl = act_ctl.prev(".number");
        if (! to_element.length){
          to_element = $(".item:last", this.node);
          ctl = $(".number:last", this.node);
        }
        ctl.addClass('act');
      }
// prepnout na n-ty item (pocitano od nuly) */
      else {
        to_element = $(".item:eq(" + to + ")", this.node);
        $(".controls .number:eq(" + to + ")", this.node).addClass('act');
      }
// zobrazit pozadovany item a zaktivnit ho */
      to_element.fadeIn('slow');
      to_element.addClass('act');
      return true;
    }
  });
  
  $.fn.changebox = function(options) {
    var changebox = null;
    return this.each(function(){
      changebox = new Changebox($(this),options);
      changebox.init();
    });
  }
})(jQuery);

