// Flash video interaction
// Author: Florian Wagner, work@flowagner.org
// Version: July 31, 2009
// 
// 

// prefix for text sections
var sectionPrefix = "fvi_text_";

// section elements
var sectionElements = "*";

// get our flash movie object
var flashMovie = "fvi_player";

 var timeCodes = new Array();
 var currentCode = 0;
 var nextCode = 0;
 var timer = 0;
 var currentSection = "";
 var videoHide = false;
 var waitForElement = "";


function switchHighlight(prevElement, nextElement) {
    $("#" + nextElement).addClass("currentSection");
    $.scrollTo($("#" + nextElement).offset().top - 410, {'axis': 'y', 'duration': 2000});
    if(prevElement != "") {
      $("#" + prevElement).removeClass("currentSection");       
    }
}

function handleTimeoutEvent() {
    var player =  $f(flashMovie);
    currentTime = player.getTime();
    $(timeCodes).each(function(i) {
            if(timeCodes[i] < currentTime && ((i+1 < timeCodes.length && timeCodes[i+1] - 1 > currentTime) || i+1 >= timeCodes.length)) {
                var id = sectionPrefix + timeCodes[i];
                if(waitForElement != "" && waitForElement != id) return;
                if(id != currentSection) {
                    var previousSection = currentSection;
                    currentSection = id;
                    switchHighlight(previousSection, currentSection);
                } else if(waitForElement == id) {
                    waitForElement = "";
                }
            }
    });
}



// movie param drops in as global var from HTML snippet
// generated by t3 backend (Smarty->main.html)
function loadPlayer(movie) {
        $f(
            flashMovie,  
            "/fileadmin/gib/player/flowplayer-3.1.1.swf",  
            {
          // configure clip to use "streaming" plugin for providing video data 
          clip: {
              //url:'/uploads/flash_videointeraction/video/beratungsgespraech.f4v',  
          // dude, this took its time...
              url: escape(movie),
              provider: 'streaming',
              autoPlay: true,
              autoBuffering: true,
              bufferLength: '0',
              scaling: 'orig'
          },
          // streaming plugins are configured normally under plugins node 
          plugins: { 
              streaming: {  
                  url: '/fileadmin/gib/player/flowplayer.pseudostreaming-3.1.2.swf'  
              },
              controls: { 
                  url: '/fileadmin/gib/player/flowplayer.controls-3.1.1.swf', 
                  left: 0,
                  bottom: 0,
                  opacity: 1,
                  volume: false,
                  mute: false
              }
          }
    });
        
     $f(flashMovie).onStart(function() {
            timer = setInterval('handleTimeoutEvent()', 500);
     });
     $f(flashMovie).onStop(function() {
             clearInterval(timer);
     });
     $f(flashMovie).onPause(function() {
             clearInterval(timer);
     });
     $f(flashMovie).onResume(function() {
             timer = setInterval('handleTimeoutEvent()', 500);
     });        
        
}


$(document).ready(function() {
        if($("#fvi_player").length == 0) return;
        if(/MSIE 6/i.test(navigator.userAgent)) return;
        $("#fvi_player").empty();
        $.getScript("/fileadmin/gib/js/jquery.scrollTo-min.js");

        loadPlayer(movie);

        // onclick-Handler für alle Textbereiche installieren
     $(sectionElements + "[id^='" + sectionPrefix + "']").click(function (event) {
             if(videoHide) {
                 $("#fvi_show_video").click();
             }
             var timeCode = $(this).attr("id").substring(sectionPrefix.length);
             if(flashMovie) {
                if(!$f(flashMovie).isPlaying()) {
                  $f(flashMovie).play();
                }
                clearInterval(timer);
                if($f(flashMovie).getStatus() != 5 || timeCode != 0) {
                    $f(flashMovie).seek(timeCode);
                }
                if($(this).attr("id") != currentSection) {
                    switchHighlight(currentSection, $(this).attr("id"));
                    waitForElement = $(this).attr("id");
                    currentSection = $(this).attr("id");
                }
                timer = setInterval('handleTimeoutEvent()', 500);
             }
     });

     $(sectionElements + "[id^='" + sectionPrefix + "'] a").click(function (event) {
         $(event).stopPropagation();
     });

     // onhover-Handler für alle Textbereiche installieren
     $(sectionElements + "[id^='" + sectionPrefix + "']").hover(
         function() {
             $(this).addClass("sectionHighlight");
         },
         function() {
             $(this).removeClass("sectionHighlight");
         })

     // setup video-to-text interaction

     $($(sectionElements + "[id^='" + sectionPrefix + "']")).each(function(i) {
             timeCodes.push($(this).attr("id").substring(sectionPrefix.length));
     });
     timeCodes.sort(function(a,b) {return a-b;});

     nextCode = 0;                



     
     // prepare fixed layout
     $("#header").css({'position': 'fixed', 'top': '0', 'left': '0', 'width': '100%', 'z-index':'100'});
     $("#schnellzugriff").css({'position': 'fixed', 'top': '75px', 'left': '0', 'width': '100%', 'z-index':'100'});
     //$("#fvi_video").css({'position': 'fixed', 'top': '103px', 'width': '700px', 'z-index':'100', 'margin-top': '0'});
     $("#fvi_video").addClass("videoFixed");
     $("#content-wrap").css({'padding-top': '103px'});
     $("#fvi_content").css({'padding-top': '270px'});
     
     
     

     
     // install resize event handler...
     $(window).resize(function (event) {
        if(($("#left").height() < $(window).height() - 103) && !$("#left").hasClass("menuFixed")) {
            $("#left").addClass("menuFixed");
            $("#content").addClass("menuFixed");
            if($("#left").height() > $("#outer").height()) {
                $("#outer").height($("#left").height() + 20);
            }
        } else if($("#left").hasClass("menuFixed")) {
            $("#left").removeClass("menuFixed");
            $("#content").removeClass("menuFixed");
        }
     });
     
     // ...and kick it off once at launch
     $(window).resize();
     
     if(jQuery.support.leadingWhitespace) {
         $("#content-wrap").append($('<div id="shadow-top"></div>'));
         $("#content-wrap").append($('<div id="shadow-video"></div>'));
         $("#content-wrap").append($('<div id="shadow-video-side"></div>'));
     }
     
     // insert "Hide Video" link
     $("#fvi_video").append($('<a id="fvi_hide_video" href="#">Video ausblenden</a>').click(function(e) {
         $f(flashMovie).pause();
         $f(flashMovie).hide();
         $("#shadow-video").hide();
         $("#shadow-video-side").hide();
         $("#fvi_video").addClass("hideMovie");
         $("#fvi_content").css('padding-top', '0');
         $("#fvi_content div").removeClass("currentSection");
         $("#fvi_show_video").show();
         e.preventDefault();
         videoHide = true;
     }));
     
     // insert "Show Video" link
     $("#fvi_wrap").append($('<div id="fvi_show_video_wrap"></div>'));
     $("#fvi_show_video_wrap").append($('<a id="fvi_show_video" href="#">Video einblenden</a>').click(function(e) {
         currentSection = "";
         previousSection = "";             
         $("#fvi_show_video").hide();
         $("#fvi_content").css('padding-top', '270px');
         //$("#fvi_video").show();
         //loadPlayer();
         $("#fvi_video").removeClass("hideMovie");
         $f(flashMovie).show();
         $("#shadow-video").show();
         $("#shadow-video-side").show();             
         e.preventDefault();
         videoHide = false;             
     }));
     // .. and hide it by default
     $("#fvi_show_video").hide();
     
     // click handler for Content Navigation
     $("#fvi_navigation ol li a").click(function(e) {
             var section = $(this).attr('href').match('#([A-Za-z0-9-_]+)').pop();
             $("#" + section + " + div[id^='fvi_text_']").click();
             e.preventDefault();
     });
     
     // tooltip handler
     $("#fvi_content div[id^='fvi_text_']").css('cursor', 'pointer');
     

});

