/**
 * Clipkit Playlist
 **/

//Set Container
var app_playlistWidth = parseInt('207');
var app_playlistHeight = parseInt('250');
var app_playlistPos = 'right';

var ck_playlistContainer = '<div id="ck_playlist-container">' +
	'<div id="player-wrapper"><div id="player"></div></div>';
if(app_playlistWidth > 0 || app_playlistHeight > 0) {
    ck_playlistContainer += //Clips container
        '<div class="playlist"><div class="clips"></div></div>';
}
ck_playlistContainer += '</div>';
document.write(ck_playlistContainer);


//Chain Funtions
lazy_load_jQuery = function() {
    lazy_load_jQuery.getScript("http://cdn.clipkit.de/lib/js/jquery.js");
    lazy_load_jQuery.tryReady(0);
}

lazy_load_jQuery.getScript = function(filename) {
    var fileref = document.createElement('script');
    fileref.setAttribute("type","text/javascript");
    fileref.setAttribute("src", filename);
    if (typeof fileref!="undefined"){
        document.getElementsByTagName("head")[0].appendChild(fileref);
    }
}

lazy_load_jQuery.tryReady = function(time_elapsed) {
    if (typeof jQuery == "undefined") {
      if (time_elapsed <= 5000) {
        setTimeout("lazy_load_jQuery.tryReady(" + (time_elapsed + 200) + ")", 200);
      }
    } else {
      jQuery.noConflict();
      lazy_load_flowplayer();
    }
}

//# Init playlist
if (typeof jQuery == "undefined") {
    lazy_load_jQuery();
}
else {
    jQuery(function(){
        lazy_load_flowplayer();
    });
}

function lazy_load_flowplayer() {
    if (typeof flowplayer == "undefined") {
      jQuery.ajax({
        url: 'http://cdn.clipkit.de/lib/js/flowplayer.js',
        type: 'GET',
        dataType: 'script',
        cache: true,
        success: function(response) {
          lazy_load_flowplayer_playlist();
        }
      })
    } else {
      lazy_load_flowplayer_playlist();
    }
  }

  function lazy_load_flowplayer_playlist(){
    jQuery.ajax({
      url: 'http://cdn.clipkit.de/lib/js/flowplayer.playlist.js',
      type: 'GET',
      dataType: 'script',
      cache: true,
      success: function(response) {
        load_player_config();
      }
    });
  }

  function lazy_load_flowplayer_nuggad(){

    var config = playlistConfig;
    var nuggrid;
    if(typeof config.user.plugins.nuggad.nuggrid == 'undefined'){
        nuggrid = encodeURIComponent(top.location.href);
      }else{
        nuggrid = config.user.plugins.nuggad.nuggrid;
      }
      
    var nuggn = config.user.plugins.nuggad.nuggn;
    var nuggsid = config.user.plugins.nuggad.nuggsid;

    jQuery.ajax({
        url: "http://clipkit.nuggad.net/rc?nuggn="+nuggn+"&nuggsid="+nuggsid+"&nuggrid=" + nuggrid,
        type: 'GET',
        dataType: 'script',
        cache: false,
        success: function(response) {
          if(typeof n_pbt1 != 'undefined'){
            playlistConfig.user.plugins.liverail.LR_TAGS  += ',' + (n_pbt1.replace(';',',').replace(';',''));
          }
          init_player();
        },
        error: function(){
          init_player();
        }
      });
}

var playlistConfig;

function load_player_config() {
    jQuery.ajax({
      type: 'GET',
      url: 'http://signin.clipkit.de/259/player/311/playlist/745/config.json',
      dataType: 'jsonp',
      success: function(response) {
        playlistConfig = response;
        if(typeof response.user.plugins.nuggad != 'undefined' && response.user.plugins.liverail != 'undefined'){
            lazy_load_flowplayer_nuggad();
        }else{
             init_player();  
        }

        init_css();
       }
    });
  }

  function init_css(){
    jQuery("head").append('<link rel="stylesheet" href="http://cdn.clipkit.de/lib/css/playlist_scroll.v2.css" type="text/css" />');
  }


  function init_player() {
    var config = playlistConfig;

    jQuery('#player').css({
        'width': config.player.css.width + 'px',
        'height': config.player.css.height + 'px'
    });
    jQuery('#player-wrapper').css({
        'width': config.player.css.width + 'px',
        'height': config.player.css.height + 'px'
    });
    jQuery('#ck_container-player').css({
        'width': config.player.css.width + 'px',
        'height': config.player.css.height + 'px'
    });

    var actualTime = new Date();

    var playerParams = {
        width: config.player.css.width + 'px',
        height: config.player.css.height + 'px',
        logo: config.player.logo,
        clip: config.clip,
        canvas: config.player.canvas,
        playlist: config.playlist+'?'+actualTime.getTime(),
        plugins: {
            controls: config.player.plugins.controls,
            rtmp: {
                "url":"http://cdn.clipkit.de/lib/swf/clipkitplayer.rtmp-3.2.3.swf"
            }
          },
        onLoad: function() {
            /*
            var playlistObj = this.getPlaylist();
            var splashScreen = "http://cdn.clipkit.de/227/video/93635/thumbnails/previous.png?phash=57d67065409d337bc448f59cd1e4a323";
            
            if(splashScreen != playlistObj[0].url)
            {
                var newClip = {
                    scaling: 'fit',
                    url: splashScreen,
                    title: 'Preview'
                };
                this.addClip(newClip,0);
                this.play(0);
            }
            */
            if(this.getCommonClip().autoMute == true){
              this.mute();
            }else{
              this.unmute();
            }

            clipkitPlayerLoaded(this);
        },
        onBegin: function(clip) {
            if(typeof clip['clipkit:newsfeed_title'] != 'undefined') {
                var newsfeed, newsfeedCont, coLogo;
                newsfeedCont = '<div id="ck_video-infos" style="display:none"></div>';
                newsfeed = '<h1>' + clip['clipkit:newsfeed_title'] + '</h1>' +
                    '<h2>' + clip['clipkit:newsfeed_subtitle'] + '</h2>' +
                    '<p>' + clip['clipkit:newsfeed_description'] + '</p>';
                coLogo = '<a target="_blank" href="http://www.myvideo.de/Musik" id="ck_co-logo">' +
                    '<img src="http://cdn.clipkit.de/lib/images/myvideo-logo-klein.png" alt="MyVideo Logo" /></a>';

                if(jQuery("#ck_video-infos").length <= 0)
                {
                    jQuery("#player-wrapper").append(coLogo);
                    jQuery("#player-wrapper").append(newsfeedCont);
                }
                jQuery("#ck_video-infos").html(newsfeed).slideDown('slow');
                jQuery("#ck_co-logo").fadeIn('slow');
            }
            else {
                if(jQuery("#ck_video-infos").length > 0) {
                    jQuery("#ck_video-infos").slideUp('slow');
                    jQuery("#ck_co-logo").fadeOut('slow');
                }
            }
        }
    };
    if(config.player.autoMute != 'undefined'){
        if(playerParams.clip.autoMute == 'undefined'){
            playerParams.clip.autoMute = config.player.autoMute;
        }
        playerParams.clip.autoMute = playerParams.clip.autoMute || config.player.autoMute;
    }
    if(typeof config.user.plugins.liverail != "undefined" && typeof config.user.plugins.liverail.LR_PUBLISHER_ID != "undefined"){
      playerParams.plugins.liverail = config.user.plugins.liverail;
      if(typeof config.clip.clipkitPlaylistId != 'undefined'){
        playerParams.plugins.liverail.LR_VIDEO_AMID = config.clip.clipkitPlaylistId;
      }
      if(config.user.plugins.liverail.LR_URL == 'undefined' || config.user.plugins.liverail.LR_URL == ''){
        playerParams.plugins.liverail.LR_URL = top.location.href;
      }
    }


    if(typeof config.player.autoPlay != 'undefined')
    {
      var autoplayValue = config.player.autoPlay;
      
      playerParams.clip.autoPlay = autoplayValue;
      
      for (var key in playerParams.playlist)
      {
        playerParams.playlist[key].autoPlay = autoplayValue;
      }
      
    }

    if(typeof config.user.plugins.secure_stream != 'undefined'){
        playerParams.plugins.secure_stream = config.user.plugins.secure_stream;
    }

    if(typeof config.user.plugins.openAdFallback != "undefined"){
      playerParams.plugins.openAdFallback = config.user.plugins.openAdFallback;
    }

    if(typeof clipkitPlayerLoaded == 'undefined'){
        clipkitPlayerLoaded = function(player){}
    }

    playerParams.plugins.tracker = {
      'url': "http://cdn.clipkit.de/lib/swf/clipkit.tracking.swf"
    }


    flowplayer('player', {src: 'http://cdn.clipkit.de/lib/swf/v-3.2.5/clipkitplayer.swf', wmode: 'opaque' }, playerParams);
    

    //If Playlist links to be shown
    if(app_playlistWidth > 0 || app_playlistHeight > 0)
    {
        var playerWidth = parseInt(config.player.css.width );
        var playerHeight = parseInt(config.player.css.height);
        var playlistContainerHeight = (playerHeight > app_playlistHeight) ? playerHeight : app_playlistHeight; //Playlist container

        //Set playlist position
        switch(app_playlistPos) {
            case 'left':
            jQuery('#ck_playlist-container').css({
                'width': (playerWidth  + app_playlistWidth) + 'px',
		'height': playlistContainerHeight + 'px'
		});
            jQuery('.playlist').css({
                'width': app_playlistWidth + 'px',
                'height':playlistContainerHeight + 'px',
                'left': 0,
                'top': 0
                });
            jQuery('#player-wrapper').css({
                'position':'absolute',
                'width': playerWidth + 'px',
                'height': playerHeight + 'px',
                'left': app_playlistWidth + 'px',
                'top': 0 + 'px'
                });
            break;
				
            case 'right':
            jQuery('#ck_playlist-container').css({
                'width': (playerWidth  + app_playlistWidth) + 'px',
		'height': playlistContainerHeight + 'px'
		});
            jQuery('.playlist').css({
                'width': app_playlistWidth + 'px',
                'height':playlistContainerHeight + 'px',
                'left': playerWidth + 'px',
                'top': 0 + 'px'
                });
            jQuery('#player-wrapper').css({
                'position':'absolute',
                'width': playerWidth + 'px',
                'height': playerHeight + 'px',
                'left': 0,
                'top': 0
                });
            break;
				
            case 'top':
            jQuery('#ck_playlist-container').css({
		'width': playerWidth  + 'px',
		'height': (playerHeight + app_playlistHeight) + 'px'
		});
            jQuery('.playlist').css({
                'width': app_playlistWidth + 'px',
                'height':app_playlistHeight + 'px',
                'left': 0,
                'top': 0
                });
            jQuery('#player-wrapper').css({
                'position':'absolute',
                'width': playerWidth + 'px',
                'height': playerHeight + 'px',
                'left': 0,
                'top': app_playlistHeight + 'px'
                });
            break;
				
            default:
            jQuery('#ck_playlist-container').css({
		'width': playerWidth  + 'px',
		'height': (playerHeight + app_playlistHeight) + 'px'
		});
            jQuery('.playlist').css({
                'width': app_playlistWidth + 'px',
                'height':app_playlistHeight + 'px',
		'left': 0,
                'top': playerHeight + 'px'
                });
            jQuery('#player-wrapper').css({
                'position':'absolute',
                'width': playerWidth + 'px',
                'height': playerHeight + 'px',
                'left': 0,
                'top': 0
                });
            break;	
        } //--End switch playlist position


        var playlistTmpl = '<a href="${url}" class="first ${clipkit:className}" title="Clip abspielen / anhalten">' +
                '<img src="${clipkit:imagePath}" />' +
                '<span><strong>${title}</strong><br/>${clipkit:vdescription}</span>' +
            '</a>';

        flowplayer('player').playlist("div.clips", {
            preview: 0,
            loop: true,
            template: playlistTmpl
            });
    }
    //if(typeof config.user.clickThruTracker != "undefined")
    //{
        var trackerCode =
            '<object width="1" height="1">' +
                '<param name="movie" value="http://cdn.clipkit.de/lib/swf/clipkit.third-party-ad-tracker.swf"></param>' +
                '<param name="allowFullScreen" value="true"></param>' +
                '<param name="allowscriptaccess" value="always"></param>' +
                '<param name="flashvars" value="pc=' + config.user.clickThruTracker + '&publisher=' + config.user.website + '&lr_publisher=' + config.user.plugins.liverail.LR_PUBLISHER_ID + '"></param>' +
                '<embed src="http://cdn.clipkit.de/lib/swf/clipkit.third-party-ad-tracker.swf" flashvars="pc=' + config.user.clickThruTracker + '&publisher=' + config.user.website + '&lr_publisher=' + config.user.plugins.liverail.LR_PUBLISHER_ID + '" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="1" height="1"></embed>' +
            '</object>';

        jQuery('#ck_container-player').append( trackerCode );
     //}
  }

  
