/*!
 * jSlider - Image gallery slider with jQuery
 *
 * Copyright (c) 2010 T�cnicos en Tiendas Virtuales Euromediterr�neas (http://www.t2v.com)
 *
 * Built on top of the jQuery library
 *   http://jquery.com
 *
 * Requires the following libraries:
 *   - Jquery Timers (http://plugins.jquery.com/project/timers)
 */

(function($) {

  /**
   * Crea un slider para todos los elementos coincidentes
   * @example $('#mySlider').jslider();
   * @method jslider
   * @return jQuery
   * @param o {Hash|Stromg} Conjunto de pares clave/valor para definir un conjunto de propiedades de configuraci�n o el nombre de un m�todo para llamar de una instancia ya creada.
   *
   */
  $.fn.jslider = function(o, value)
  {
    if (typeof o == 'string') {
      var instance = $(this).data('jslider'), args = Array.prototype.slice.call(arguments, 1);
      if (value == null) {
        return instance[o].apply(instance, args);
      } else {
        return instance[o].images = value;
      }
    } else {
      return this.each(function() {
        $(this).data('jslider', new $js(this, o));
      });
    }
  };

  var defaults = {
    id: 'jslider',
    autoStart: true,
    effect: 'fade',
    period: '4 s',
    src: null,
    script: null
  };

  /**
   * El objeto jSlider
   * @class jslider
   * @param e {HTMLElement} El elemento para el que crear el slider
   * @param o {Object} Conjunto de pares clave/valor para definir propiedades de configuraci�n
   * @cat Plugins/jSlider
   */
  $.jslider = function(e, o)
  {
    
    this.options = $.extend({}, defaults, o || {});

    this.buttonNext = $(o.buttonNext);
    this.buttonPrev = $(o.buttonPrev);
    this.buttonStart = $(o.buttonStart);
    this.buttonPause = $(o.buttonPause);
    this.image = $(o.image);
    this.titleContainer = $(o.titleContainer);
    this.descriptionContainer = $(o.descriptionContainer);
    this.compartirContainer = $(o.compartirContainer);
    this.current = null;
    this.images = null;
    this.container = e;
    
    if (o.src) {
      this.images = o.src;
    } else if (o.script) {
      $.ajax({
        url: o.script,        
        success: function(data) {
          $(e).jslider('images', eval(data));
        }
      });
    }
  
    if (this.buttonNext.length > 0) {
      this.buttonNext.bind('click', {slider: this}, function(event) {
        var resume = false;
        if (event.data.slider.playing) {          
          event.data.slider.pause();
          resume = true;
        }
        event.data.slider.next();
        if (resume) {
          event.data.slider.start();
        }
      });
    }

    if (this.buttonPrev.length > 0) {
      this.buttonPrev.bind('click', {slider: this}, function(event) {
        var resume = false;
        if (event.data.slider.playing) {
          event.data.slider.pause();
          resume = true;
        }
        event.data.slider.prev();
        if (resume) {
          event.data.slider.start();
        }
      });
    }

    if (this.buttonStart.length > 0) {
      this.buttonStart.bind('click', {slider: this}, function(event) {
        event.data.slider.start();
      });
    }

    if (this.buttonPause.length > 0) {
      this.buttonPause.bind('click', {slider: this}, function(event) {
        event.data.slider.pause();
      });
    }

    this.setup();

  }

  // Internal use shortcut
  var $js = $.jslider;

  $js.fn = $js.prototype = {
    jslider: '0.1'
  };

  $js.fn.extend = $js.extend = $.extend;

  $js.fn.extend({
    /**
     * Configura el slider
     */
    setup: function()
    {
      this.current = 0;
      this.playing = false;

      if (this.images.length > 0) {
        $(this.image).attr('src', this.images[0].src);
        $(this.titleContainer).attr('innerHTML', this.images[0].title);
        $(this.descriptionContainer).attr('innerHTML', this.images[0].description);
        $(this.compartirContainer).attr('innerHTML', this.images[0].compartir);
      }

      if (this.options.autoStart) {
        this.start();
      }

    },

    next: function()
    {
      this.current++;
      if (this.current >= this.images.length) {
        this.current = 0;
      }
      this.update();      
    },

    prev: function()
    {
      this.current--;
      if (this.current < 0) {
        this.current = this.images.length - 1;
      }
      this.update();
    },

    pause: function()
    {
      $(this.container).stopTime('jslider-' + this.id);
      this.playing = false;
    },

    start: function()
    {
      $(this.container).everyTime(this.options.period, 'jslider-' + this.id, function() { $(this).jslider('next'); });
      this.playing = true;
    },

    restartTimer: function()
    {
      this.pause();
      this.start();
    },

    update: function()
    {
      $(this.image).add(this.titleContainer).add(this.descriptionContainer).add(this.compartirContainer).fadeOut(500, this.changeImage());
      $(this.image).add(this.descriptionContainer).add(this.titleContainer).add(this.compartirContainer).fadeIn(500);
    },

    changeImage: function()
    {
      $(this.image).attr('src', this.images[this.current].src);
      $(this.titleContainer).attr('innerHTML', this.images[this.current].title);
      $(this.descriptionContainer).attr('innerHTML', this.images[this.current].description);
      $(this.compartirContainer).attr('innerHTML', this.images[this.current].compartir);
    }
    
  });

  $js.extend({

    defaults: function(d)
    {
      return $.extend(defaults, d || {});
    }
  });


})(jQuery);
