$(function(){
  
  var autoScroll = setInterval(function(){
    slideshow.find('a.next').trigger('selectNext');
  },4000);

  $('a[href^="#"]').bind('click', function(){ return false; });
  
  var slideshow = $('#slideshow');
  var holder = $('#slideshow #slideshow_holder');
  var direction = 'rtl';
  var currentIteration = 1;
  var nbSlide = 0;
  var videoPlayer = new videoplayer();
  slideshow.find('.button_select').each(function(){
    nbSlide++;
  });
     	
  // select image
  slideshow.find('.button_select').bind('click', function(e){
    e.preventDefault();
    clearInterval(autoScroll);
    $(this).trigger('selectImage');
  });
  
  slideshow.find('.button_select').bind('selectImage', function(e){
    holder.unbind('click').bind('click', function(ev){
      ev.preventDefault();
      clearInterval(autoScroll);
      slideshow.find('a.next').trigger('selectNext');
      return false;
    });
    
    if(!$(this).hasClass('active'))
    {
      var nextIteration = $(this).text();
      
      if(currentIteration == 1 && nextIteration == nbSlide || currentIteration == nbSlide && nextIteration == nbSlide-1)
      {
        direction = 'ltr';
      }
      else if(currentIteration == nbSlide && nextIteration == 1 || currentIteration == nbSlide-1 && nextIteration == nbSlide)
      {
        direction = 'rtl';
      }
      else
      {
        if(nextIteration > currentIteration)
        {
          direction = 'rtl';
        }
        else
        {
          direction = 'ltr';
        }
      }
     		
      currentIteration = nextIteration;
      
      var prevImg = holder.find('img').addClass('prevImg');
      var nextImg = $('<img class="nextImg" />');
      
      if(direction == 'ltr'){
        $(nextImg).css({left: -820}).attr('src', $(this).attr('href')).appendTo(holder);
      }else{
        $(nextImg).css({left: 820}).attr('src', $(this).attr('href')).appendTo(holder);
      }
      
      slideshow.find('.button_select').removeClass('active');
      $(this).addClass('active');
      
      if(direction == 'ltr'){
        $(prevImg).animate({ left: (0 + $(prevImg).innerWidth()) }, {duration: 800, complete: function(){
          $(this).remove();
        }});
      }else{
        $(prevImg).animate({ left: (0 - $(prevImg).innerWidth()) }, {duration: 800, complete: function(){
          $(this).remove();
        }});
      }
      $(nextImg).animate({ left: 0 }, {duration: 800, complete: function(){
        $(this).removeClass('nextImg');
      }});
      
      // image caption
      if($(this).attr('title'))
      {
        slideshow.find('.layer_button').html( $(this).attr('title') ).fadeIn();
      }
      else
      {
        slideshow.find('.layer_button').html( '' ).fadeOut();
      }
      
      $('.play_button').hide();
          
      if($(this).hasClass('slideVideo'))
      {
        $('.play_button').show();
        var url = $(this).attr('rel');
        holder.unbind('click').bind('click', function(e){
          videoPlayer.open(url);
        });
      }
      
    }
    return false;
  });
  
  // next image
  slideshow.find('a.next').bind('click', function(e){
    e.preventDefault();
    clearInterval(autoScroll);
    $(this).trigger('selectNext');
  });
  
  slideshow.find('a.next').bind('selectNext', function(){
    var _next = slideshow.find('.select a.active').next();
    //direction = 'rtl';
    
    if($(_next).length < 1)
      _next = slideshow.find('.select a:eq(0)');
    
    $(_next).trigger('selectImage');
    
    return false;
  });
  
  // previous image
  slideshow.find('a.prev').bind('click', function(e){
    e.preventDefault();
    clearInterval(autoScroll);
    $(this).trigger('selectPrev');
  });
  
  slideshow.find('a.prev').bind('selectPrev', function(){
    var _prev = slideshow.find('.select a.active').prev();
    var count = slideshow.find('.select a').length;
    //direction = 'ltr';
    
    if(_prev.length < 1)
      _prev = slideshow.find('.select a').eq(count - 1);
    
    _prev.trigger('selectImage');
    return false;
  });
  
  slideshow.find('.select a:eq(0)').trigger('selectImage');
  
  $('#loginForm').bind('submit', function(e){
    e.preventDefault();
    
    var params = {
      'username' : jQuery('#username').val(), 
      'password' : jQuery('#password').val()
    };
    heap.api.users.login(function(response)
    {
      if (response.type != heap.SUCCESS)
      {
        //TODO: Print errors
        $('#errorMsg').html(heap.translate(response.code));
        return false;
      }
      window.location.href = heap.urlmap.extranet;
    }, params);
  });
  
});


var videoplayer = function(json)
{ 
  $('.videoStandalone').bind('click', {instance:this}, function(e){
    e.data.instance.open($(this).attr('href'));
    e.preventDefault();
  });
  $('#mmplus_overlay #overlay').bind('click', this.close);
  $('#mmplus_overlay .bt-close').live('click', this.close);
}

videoplayer.prototype.open = function(videoUrl)
{
  var playerWidth = $('#videoplayer').innerWidth();
  var playerHeight = $('#videoplayer').innerHeight();
  var windowWidth = $(window).width();
  var windowHeight = $(window).height();
  
  $('#videoplayer').css({top: (windowHeight - playerHeight) / 2, left: (windowWidth - playerWidth) / 2});
  
  this.embed(videoUrl);
  $('#mmplus_overlay').show();
}

videoplayer.prototype.close = function()
{
  $('#mmplus_overlay #videoplayer').html('<div id="videoplayer_embed"></div><a href="#close" class="bt-close"></a>');
  $('#mmplus_overlay').hide();
}

videoplayer.prototype.embed = function(videoUrl)
{
  $('#mmplus_overlay #videoplayer').html('<div id="videoplayer_embed"></div><a href="#close" class="bt-close"></a>');
  var flashvars = {};
  flashvars.width = 960;
	flashvars.height = 540;
	flashvars.src = videoUrl;
	flashvars.autoplay = "true";

  var params = {
    wmode: 'opaque'
  };
  var attributes = {
    id: "videoplayer",
    name: "videoplayer"
  };

  swfobject.embedSWF("/mmplus/static/swf/videoplayer.swf", "videoplayer_embed", "960", String(flashvars.height + 30), "9.0.0","expressInstall.swf", flashvars, params, attributes);
}
