(function() {
  
  $(document).ready(function() {
     
       if (Modernizr.video) {
          $('#video_overlay_nav, #video_overlay_people').show();
          
        // The browser supports HTML5 Video
        $('#video').data('video', 1);
        
        
        $('video').click(function(e) {
          e.preventDefault();
          
          var video = document.getElementById('video');
          if (video.paused) {
            playVideo();  
          } else {
            pauseVideo();
          }
          
        });
        
        $('#switch_video').click(function(e){
          e.preventDefault();
          var video = document.getElementById('video');
          
          var src = video.currentSrc;
          var filePath = src.substr(0, src.lastIndexOf('/') + 1);
          var suffix = src.substr(src.lastIndexOf('.'), src.length);
          
          if ($(video).data('video') == 1) {
            var newSrc = filePath + 'CarpeDiem2' + suffix;
            $(video).data('video', 2);
            $(this).text('Se vår första film');
          }
          else {
            var newSrc = filePath + 'CarpeDiem1' + suffix;
            $(video).data('video', 1);
            $(this).text('Se vår andra film');
          }
          
          video.src = newSrc;
          
          $('#video_overlay_people a.selected').removeClass('selected');
          hideInfoBox();
        });
        
        $('#video_overlay_people a').click(function(e){
          e.preventDefault();
          var that = $(this);
          var url = this.href;
          var person = url.substr(url.lastIndexOf('/', url.length - 2), url.length).replace(/\//g, '');
          
          $('#video_overlay_people a.selected').removeClass('selected');
          that.addClass('selected');
          
          var video = document.getElementById('video');
          if (video.currentTime) {
            pauseVideo();
            var time = timeStamp($('#video').data('video'), person) || video.currentTime;
            video.currentTime = time;
       
          }
          
          
                    
          displayInfo(that.text(), that.attr('title'));
          
        });
      }
     
    
   }); // $(document)ready()
   
   
   
    function pauseVideo() {
      var video = document.getElementById('video');
      if (video.pause) {
        video.pause();
      }
    } 
    function playVideo() {
      var video = document.getElementById('video');
      if (video.play) {
        video.play();
      }
    } 
   
   function displayInfo(name, description) {
     var firstName = name.substr(0, name.indexOf(' '));
     var url = '/teamet/' + firstName.toLowerCase() + '/';
     
     if ($('#video_overlay_info').length) {
       
       var div = $('#video_overlay_info');
       div.fadeOut('fast', function() {
         div.find('.name').text(name);
         div.find('.summary').text(description);
         div.find('a.read_more')
          .text('Läs mer om ' + firstName)
          .attr('href', url)
          .unbind()
          .click(function(e) {
             e.preventDefault();
             openInfoBox(firstName);
          });  
       });
       
       
     } else {
       var div = $('<div id="video_overlay_info" />')
        var close = $('<a href="#" class="close" title="Stäng rutan">Stäng</a>').click(function(e) {
          e.preventDefault();
          $('#video_overlay_people a.selected').removeClass('selected');
          hideInfoBox();
          playVideo();
        });
      
      div.append(close);  
      div.append('<h4 class="name">' + name + '</h4>')
      
      
      var link = $('<a class="read_more" href="' + url + '">Läs mer om ' + firstName + '</a>').click(function(e) {
        e.preventDefault();
        pauseVideo();
        openInfoBox(url);
      });
      
      
      var p = $('<p class="summary">' + description + '</p>');
      
      var p2 = $('<p class="more_info"/>').append(link);
      div.append(p);
      div.append(p2);
      div.css('display', 'none');
     }
     
     
     
      
     
     $('#team_video').append(div);
     div.fadeIn();
   }
   
    function hideInfoBox() {
      $('#video_overlay_info').fadeOut();
      $('#video_overlay_people a.selected').removeClass('selected');
    }
   
   
   function openInfoBox(url) {
     
      $.fancybox({
        href: url,
        autoDimensions: false,
        width: 640,
        height: 480,
        padding: 20,
        type: 'iframe',
        onClosed: function() {
          hideInfoBox();
          playVideo();
        }
      }); 
     }
    
    function timeStamp(videoNo, name) {
     var time;
     name = name.toLowerCase();
     
     if (videoNo == 1) {
       switch (name) {
         case 'alexandra':
           time = 34.5;
           break;
         case 'mari':
           time = 4.3;
           break;
         case 'caroline':
           time = 14.6;
           break;
         case 'mia':
           time = 12.0;
           break;
         case 'ann':
           time = 24.3;
           break;
         case 'sandra':
           time = 23;
           break;
         case 'amy':
           time = 26.2;
           break;
         case 'helena':
           time = 7.2;
           break;
         case 'elin':
           time = 20.8;
           break;
         case 'sanna':
           time = 11.0;
           break;
       }
     } else {
        switch (name) {
         case 'alexandra':
           time = 25.0;
           break;
         case 'mari':
           time = 4.0;
           break;
         case 'caroline':
           time = 10.5;
           break;
         case 'mia':
           time = 14.4;
           break;
         case 'ann':
           time = 27.5;
           break;
         case 'sandra':
           time = 21.8;
           break;
         case 'amy':
           time = 19.7;
           break;
         case 'helena':
           time = 15.3;
           break;
         case 'elin':
           time = 13.0;
           break;
         case 'sanna':
           time = 7.0;
           break;
       }
     }
     return time;
   }
   function timeStampForFlash(videoNo, name) {
     var time;
     name = name.toLowerCase();
     
     if (videoNo == 1) {
       switch (name) {
         case 'alexandra':
           time = 34.1;
           break;
         case 'mari':
           time = 4.3;
           break;
         case 'caroline':
           time = 14.5;
           break;
         case 'mia':
           time = 12.0;
           break;
         case 'ann':
           time = 23.9;
           break;
         case 'sandra':
           time = 22.5;
           break;
         case 'amy':
           time = 26.0;
           break;
         case 'helena':
           time = 6.8;
           break;
         case 'elin':
           time = 20.0;
           break;
         case 'sanna':
           time = 11.0;
           break;
       }
     } else {
        switch (name) {
         case 'alexandra':
           time = 25.0;
           break;
         case 'mari':
           time = 4.0;
           break;
         case 'caroline':
           time = 10.0;
           break;
         case 'mia':
           time = 14.0;
           break;
         case 'ann':
           time = 27.2;
           break;
         case 'sandra':
           time = 21.8;
           break;
         case 'amy':
           time = 19.1;
           break;
         case 'helena':
           time = 14.4;
           break;
         case 'elin':
           time = 13.0;
           break;
         case 'sanna':
           time = 6.5;
           break;
       }
     }
     
     return time;
   }
   window.timeStampForFlash = timeStampForFlash;
   
   function closeInfoBox() {
     for (var j = 0; j < peopleLength; j++) {
       $f().getPlugin('info_' + j).hide();
     }
   }
   window.closeInfoBox = closeInfoBox;
   
   function getVideoNumber() {
     if ($f().getClip().completeUrl == 'http://www.carpediemvaxjo.se/wp-content/themes/carpe_diem/media/CarpeDiem1.flv') {
       return 1;
     } else {
      return 2;
     }
   }
   window.getVideoNumber = getVideoNumber;
   
 })();
