/*!
 * 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,
    boxWidth: 320,
    boxHeight: 240
  };

  /**
   * 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.dateContainer = $(o.dateContainer);
		this.jugadorplantContainer = $(o.jugadorplantContainer);
    this.puestoplantContainer = $(o.puestoplantContainer);
    this.alturaplantContainer = $(o.alturaplantContainer);
		this.cumpleplantContainer = $(o.cumpleplantContainer);
    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).find('img').attr('src', this.images[0].src);

	if (this.images[0].rel == 'shadowbox') {
	  var currentImage = this.images[0];
          var obj = this;
          $(this.image).bind('click', function() {
            obj.openBox(currentImage);
          });
	} else if (this.images[0].href) {
	  $(this.image).attr('href', this.images[0].href);
	}

        $(this.titleContainer).attr('innerHTML', this.images[0].title);
        $(this.descriptionContainer).attr('innerHTML', this.images[0].description);
        $(this.dateContainer).attr('innerHTML', this.images[0].date);
	$(this.jugadorplantContainer).attr('innerHTML', this.images[0].jugadorplant);
	$(this.puestoplantContainer).attr('innerHTML', this.images[0].puestoplant);
	$(this.alturaplantContainer).attr('innerHTML', this.images[0].alturaplant);
	$(this.cumpleplantContainer).attr('innerHTML', this.images[0].cumpleplant);
      }

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

    },

    openBox: function(data)
    {
      var width = typeof data.width != "undefined" ? data.width : this.options.boxWidth;
      var height = typeof data.height != "undefined" ? data.height : this.options.boxHeight;
      var hasParams= data.href.indexOf('?');

      if (hasParams > -1) {
        href = data.href + "&width=" + width + "&height=" + height;
      } else {
        href = data.href + "?width=" + width + "&height=" + height;
      }

      Shadowbox.open({
        content: href,
        player: "iframe",
        title: data.title,
        width: width,
        height: height
      });
    },

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

    change: function(index)
    {
      this.current = index;
      changeImage();
    },

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

    update: function()
    { var obj = this;
      $(this.image).add(this.titleContainer).add(this.descriptionContainer).add(this.dateContainer).add(this.jugadorplantContainer).add(this.puestoplantContainer).add(this.alturaplantContainer).add(this.cumpleplantContainer).fadeOut(500, function() { obj.changeImage() });
      $(this.image).add(this.descriptionContainer).add(this.titleContainer).add(this.descriptionContainer).add(this.dateContainer).add(this.jugadorplantContainer).add(this.puestoplantContainer).add(this.alturaplantContainer).add(this.cumpleplantContainer).fadeIn(500);
    },

    changeImage: function()
    {
      $(this.image).find('img').attr('src', this.images[this.current].src);
      $(this.image).find('img').attr('alt', this.images[this.current].title);

      $(this.image).unbind('click');
      if (this.images[this.current].rel == 'shadowbox') {
        $(this.image).attr('href', 'javascript:void(0)');
        var currentImage = this.images[this.current];
        var obj = this;
        $(this.image).bind('click', function() {
          obj.openBox(currentImage);
        });
      } else if (this.images[this.current].href) {
       $(this.image).attr('href', this.images[this.current].href);
      }

      $(this.titleContainer).attr('innerHTML', this.images[this.current].title);
      $(this.descriptionContainer).attr('innerHTML', this.images[this.current].description);
      $(this.dateContainer).attr('innerHTML', this.images[this.current].date);
			$(this.jugadorplantContainer).attr('innerHTML', this.images[this.current].jugadorplant);
			$(this.puestoplantContainer).attr('innerHTML', this.images[this.current].puestoplant);
			$(this.alturaplantContainer).attr('innerHTML', this.images[this.current].alturaplant);
			$(this.cumpleplantContainer).attr('innerHTML', this.images[this.current].cumpleplant);
    }

  });

  $js.extend({

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


})(jQuery);

