(function ($) {
  Drupal.Trylon = Drupal.Trylon || {};
  
  Drupal.behaviors.Trylon = {
    attach: function (context) {
      
      // Start the slideshow
      
      $(".highlight-slideshow").cycle({
        timeout: 12000,
        speed: 1000,
        fx: "fade",
        sync: false,
        //pause: true,
        pager: "#slideshow-nav",
        pagerAnchorBuilder: function(idx, slide) { 
                // return selector string for existing anchor 
                return '#slideshow-nav li:eq(' + idx + ') a'; 
            } 
        });
      
      $("#screen").after('<div id="mediaplayer"></div>');
      
      
      // Build jwplayers within the slideshow
      $("#stop-bar").click(function() {
          jwplayer("mediaplayer").stop();    
          $("#mediaplayer, #stop-bar").hide();
          $("#screen").fadeIn(1000);
      });
      
      $("span.teaser-video").click(function() {
        $(".highlight-slideshow").cycle('pause');
        var fileName = $(this).attr("data"); 
        $("#screen").fadeOut(1000, function() {
            $("#mediaplayer, #stop-bar").show();
            jwplayer("mediaplayer").setup({
                autostart: true,
                height: 472,
                width: 960,
                screencolor: "FFFFFF",
                file: "videos/storyweb/LD/" + fileName,
                streamer: "rtmp://streamtrylon.starweb.tv/cfx/st",
                provider: "rtmp",
                plugins: {
                    hd: {
                        file: "videos/storyweb/HD/" + fileName,
                        state: true
                    }
                },
                'modes': [
                    {type: 'flash', src: '/sites/all/libraries/mediaplayer/player.swf'},
                    {
                      type: 'html5',
                      config: {
                       'file': 'http://streamtrylondl.starweb.tv/videos/storyweb/LD/' + fileName,
                       'provider': 'video'
                      }
                    },
                    {
                      type: 'download',
                      config: {
                       'file': 'http://streamtrylondl.starweb.tv/videos/storyweb/LD/' + fileName,
                       'provider': 'video'
                      }
                    }
                ]
            }).onComplete(function() {
              
                $("#mediaplayer, #stop-bar").hide();
                $("#screen").fadeIn(1000);
                //$(".highlight-slideshow").cycle('resume');
            });
        });
      });

      
    }
  };
} (jQuery));
;

