var slideshow = function(params)
{
  this.container = params.container;
  this.data = params.data;
  
  this.bClass = params.bClass;
  this.holder = this.container.find('.js-slide-holder');
  this.selectors = this.container.find('.js-slide-selectors');
  this.slideTitle = this.container.find('.js-title');
  this.slidetext = this.container.find('.js-text');
  
  this.startStream = params.startStream;
  this.streaming = 0;
  this.currentIndex = 0;
  
  var that = this;
  
  for(var i = 0; i < this.data.length; i++)
  {
    var selector = $('<a href="#" class="js-selector '+ that.bClass +'" rel="'+ i +'">'+ that.data[i].label +'</a>');
    this.selectors.append(selector);
  }
  
  this.container.find('.js-selector').bind('click', {instance: this}, function(e){
    e.preventDefault();
    var newIndex = parseFloat($(this).attr('rel'));
    if(newIndex == this.currentIndex) return false;
    
    that.currentIndex = parseFloat($(this).attr('rel'));
    clearInterval(that.autoScroll);
    that.selectSlide();
  });
  
  this.updateTicker();
  
  this.container.find('.js-prev').bind('click', function(e){
    e.preventDefault();
    that.selectPrevious();
  });
  
  this.container.find('.js-next').bind('click', function(e){
    e.preventDefault();
    that.selectNext();
  });
  
  if(this.data.length > 1)
  {
    this.autoScroll = setInterval(function(){
      that.selectNext();
    }, 3000);
  }
  
  
  
  if(this.startStream)
    this.toggleStreaming();
  else
    this.selectSlide();
  
}

slideshow.prototype.selectPrevious = function()
{
  if(this.currentIndex == 0)
    this.currentIndex = this.data.length - 1;
  else
    this.currentIndex -= 1;
  
  this.selectSlide();
}

slideshow.prototype.selectNext = function()
{
  if(this.currentIndex + 1 == this.data.length)
    this.currentIndex = 0;
  else
    this.currentIndex += 1;
  
  this.selectSlide();
}

slideshow.prototype.selectSlide = function()
{
  var that = this;
  
  this.streaming = 0;
  that.updateTicker();
  this.selectors.find('a').removeClass('active');
  this.selectors.find('a').eq(that.currentIndex).addClass('active');
  
  this.slideTitle.html(that.data[that.currentIndex].title);
  this.slidetext.html(that.data[that.currentIndex].text);
  
  if(this.data[this.currentIndex].type && this.data[this.currentIndex].type == 'video')
    this.buildVideo();
  else
    this.buildImage();
}

// Build a slide with image
slideshow.prototype.buildImage = function()
{
  var that = this;
  this.holder.fadeOut('fast', function(){
    var content = $('<img src="'+ that.data[that.currentIndex].source +'"/>');
    
    that.holder.html($(content));
    
    if(that.data[that.currentIndex].link)
      $(content).wrap('<a href="'+ that.data[that.currentIndex].link +'" target="_blank" />');
      
    that.holder.fadeIn('fast');
  });
}

// Build a slide with video player
slideshow.prototype.buildVideo = function()
{
  clearInterval(this.autoScroll);
  
  var that = this;
  //this.holder.hide();
  this.holder.fadeOut('fast', function(){
  that.holder.html('<div id="flashPlayer"><p>Vous devez installer Flash pour profiter des contenus vidéos.</p><p><a href="http://get.adobe.com/flashplayer/" target="_blank">http://get.adobe.com/flashplayer/</a></p></div>');
  
  /*that.holder.show();
  
  setTimeout(function(){
    
  }, 100);*/
  
    var flashvars = {
      video_url: that.data[that.currentIndex].source,
      auto_play: 'true'
    };
    var params = {
      allowFullScreen: 'true',
      wmode: 'transparent'
    };
    var attributes = {
      id: 'myPlayer',
      name: 'myPlayer',
      wmode: 'opaque'
    };
    swfobject.embedSWF('/metrovision/static/swf/metrovision_player.swf?update=1', 'flashPlayer', '720', '405', '9.0.0', null, flashvars, params, attributes);
    
    that.holder.fadeIn('fast');
  });
}

slideshow.prototype.toggleStreaming = function()
{
  if(this.streaming == 1)
  {
    this.streaming = 0;
    that.updateTicker();
    this.selectSlide();
  }
  else
  {
    this.selectors.find('.active').removeClass('active');
    
    clearInterval(this.autoScroll);
    this.streaming = 1;
    var that = this;

    that.holder.hide();
    setTimeout(function(){
      
      that.holder.html('<div id="flashPlayer"><p>Vous devez installer Flash pour profiter des contenus vidéos.</p><p><a href="http://get.adobe.com/flashplayer/" target="_blank">http://get.adobe.com/flashplayer/</a></p></div>');
      
      that.updateTicker();
      
      var flashvars = { src: 'rtmp%3A//cp126259.live.edgefcs.net/live/metrovision_live@60853', autostart: 'true',themeColor: '0395d3', mode: 'overlay', scaleMode: 'stretch', frameColor: '', fontColor: 'cccccc', link:'', embed: ''};
      var params = { allowFullScreen: 'true', wmode: 'opaque' };
      var attributes = { id: 'myPlayer', name: 'myPlayer' };
      swfobject.embedSWF('/metrovision/static/swf/AkamaiFlashPlayer.swf', 'flashPlayer', '720', '405', '9.0.0', '', flashvars, params, attributes);
      
      that.holder.show();
      
    }, 200);
    
  }
}

slideshow.prototype.updateTicker = function()
{
  var that = this;
  
  if(this.streaming == 1)
    this.container.find('.stream-ticker').html('Présentement en ondes sur les écrans de Métrovision.');
  else
  {
    this.container.find('.stream-ticker').html('<a href="#stream" class="js-streaming show-streaming">Voir ce qui est en ondes sur les écrans Métrovision.</a>');
    this.container.find('.js-streaming').bind('click', function(e){
      e.preventDefault();
      that.toggleStreaming();
    });
  }
}
