/*!
 * 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
  };

  $.fn.cambiar = function(index)
  {
    $(this).data('jslider').change(index);
  }

  /**
   * 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', "<a href=\""+this.images[0].href+"\" class=\"blanco\">"+this.images[0].title+"</a>");
        $(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.images[0].video!=null)
        {
          for (i=0;i<this.images.length;i++)
          {
            if (i!=0)
            {
              cambiacss = {"opacity":"0.7", "border-color":"#000"}
              $('#miniatura'+this.images[i].noticia+' img').css(cambiacss);
            }
            else
            {
              cambiacss = {"opacity":"1", "border-color":"#A5D22D"}
              $('#miniatura'+this.images[0].noticia+' img').css(cambiacss);
            }
          }
          if (this.images[0].video==0)
          {
            $('#playplayer').hide();
          }
          else if (this.images[0].video!=null)
          {
            if (this.images[0].video.substring(0,4)=="api:")
            {
              imagen = this.images[0];
              $('#playplayer a').click(function(){
                $('#zona_imagen').jslider('pause');
                $('#video').attr('href',imagen.video);
                $('#zona_imagen').css('visibility','hidden');
                $('#video').css('visibility','visible');
                $f('video','http://www.unicajabaloncesto.com/flash/flowplayer.commercial-3.2.7.swf',conf2);
                $f().play($('#video').attr('href'));
              });
            }
            else
            {
              $('#playplayer a').attr('onClick','$(\'#zona_imagen\').jslider("pause");$f("video","http://www.unicajabaloncesto.com/flash/flowplayer.commercial-3.2.7.swf",conf);$(\'#zona_imagen\').css(\'visibility\',\'hidden\');$(\'#video\').css(\'visibility\',\'visible\');$(\'#video\').attr(\'href\',\'http://www.unicajabaloncesto.com/videos/'+this.images[0].video+'\');$f().play(document.getElementById(\'video\').href);');
            }
            $('#playplayer').show();
          }
        }
/*        if (this.images[0].video2!=null)
        {
          for (i=0;i<this.images.length;i++)
          {
            if (i!=0)
            {
              cambiacss = {"opacity":"0.7", "border-color":"#000"}
              $('#miniatura'+this.images[i].noticia+' img').css(cambiacss);
            }
            else
            {
              cambiacss = {"opacity":"1", "border-color":"#A5D22D"}
              $('#miniatura'+this.images[0].noticia+' img').css(cambiacss);
            }
          }
          if (this.images[0].video2==0)
          {
            $('#playplayer').hide();
          }
          else if (this.images[0].video2!=null)
          {
            $('#playplayer a').attr('onClick','$(\'#zona_imagen\').css(\'visibility\',\'hidden\');$(\'#video2\').css(\'visibility\',\'visible\');');
            $('#playplayer').show();
          }
        }*/
      }


      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(this.images[this.current].video);
/*      this.update(this.images[this.current].video2);      */
    },

    prev: function()
    {
      this.current--;
      if (this.current < 0) {
        this.current = this.images.length - 1;
      }
      this.update(this.images[this.current].video);
/*      this.update(this.images[this.current].video2);      */
    },

    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;
      this.update(this.images[this.current].video);
/*      this.update(this.images[this.current].video2);      */
      this.pause();
    },

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

    update: function(v)
    {var obj = this;
      if (v!=null)
      {
        valorfade = 0;
      }
      else
      {
        valorfade = 500;
      }
      $(this.image).add(this.titleContainer).add(this.descriptionContainer).add(this.dateContainer).add(this.jugadorplantContainer).add(this.puestoplantContainer).add(this.alturaplantContainer).add(this.cumpleplantContainer).fadeOut(valorfade, 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(valorfade);
    },

    changeImage: function()
    {
      if (this.images[this.current].video!=null)
      {
        for (i=0;i<this.images.length;i++)
        {
          if (i!=this.current)
          {
            cambiacss = {"opacity":"0.7", "border-color":"#000"}
            $('#miniatura'+this.images[i].noticia+' img').css(cambiacss);
          }
          else
          {
            cambiacss = {"opacity":"1", "border-color":"#A5D22D"}
            $('#miniatura'+this.images[i].noticia+' img').css(cambiacss);
          }
        }
        if (this.images[this.current].video==0)
        {
          $('#playplayer').hide();
        }
        else if (this.images[this.current].video!=null)
        {
          if (this.images[this.current].video.substring(0,4)=="api:")
          {
            $('#playplayer a').attr('onClick','$(\'#zona_imagen\').jslider("pause");$f("video","http://www.unicajabaloncesto.com/flash/flowplayer.commercial-3.2.7.swf",conf2);$(\'#zona_imagen\').css(\'visibility\',\'hidden\');$(\'#video\').css(\'visibility\',\'visible\');$(\'#video\').attr(\'href\',\''+this.images[this.current].video+'\');$f().play(document.getElementById(\'video\').href);');
          }
          else
          {
            $('#playplayer a').attr('onClick','$(\'#zona_imagen\').jslider("pause");$f("video","http://www.unicajabaloncesto.com/flash/flowplayer.commercial-3.2.7.swf",conf);$(\'#zona_imagen\').css(\'visibility\',\'hidden\');$(\'#video\').css(\'visibility\',\'visible\');$(\'#video\').attr(\'href\',\'http://www.unicajabaloncesto.com/videos/'+this.images[this.current].video+'\');$f().play(document.getElementById(\'video\').href);');
          }
          $('#playplayer').show();
        }
      }
/*      if (this.images[this.current].video2!=null)
      {
        for (i=0;i<this.images.length;i++)
        {
          if (i!=this.current)
          {
            cambiacss = {"opacity":"0.7", "border-color":"#000"}
            $('#miniatura'+this.images[i].noticia+' img').css(cambiacss);
          }
          else
          {
            cambiacss = {"opacity":"1", "border-color":"#A5D22D"}
            $('#miniatura'+this.images[i].noticia+' img').css(cambiacss);
          }
        }
        if (this.images[this.current].video2==0)
        {
          $('#playplayer').hide();
        }
        else if (this.images[this.current].video2!=null)
        {
          $('#playplayer a').attr('onClick','$(\'#zona_imagen\').css(\'visibility\',\'hidden\');$(\'#video2\').css(\'visibility\',\'visible\');');
          $('#playplayer').show();
        }
      }*/
      $(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', "<a href=\""+this.images[this.current].href+"\" class=\"blanco\">"+this.images[this.current].title+"</a>");
      $(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);

