var timer = new Timer();
var ulpYoutube = {
    playerId        : 'ulp_youtube_player',
    lessonId        : 0,
    autoplay        : 0,
    autocomplete    : 0,
    target          : '',
    width           : 450,
    height          : 390,

    init: function( args ){
        
        args = JSON.parse( args );
        var obj = this;
        obj.setAttributes(obj, args);

        obj.initPlayer( obj );
    },

    initPlayer: function( obj ){
        console.log('YT ready');
        window.onYouTubeIframeAPIReady = function () {
            player = new YT.Player( obj.playerId, {
                height      : obj.height,
                width       : obj.width,
                videoId     : obj.target,
                playerVars  : {
                              'autoplay'                : obj.autoplay,
                              'controls'                : 0,
                              'cc_load_policy'          : 0,
                              'rel'                     : 0,
                },
                events      : {
                    'onReady'         : obj.onPlayerReady,
                    'onStateChange'   : obj.onPlayerStateChange
                }
            });
        }
    },

    onPlayerReady: function(obj){
        //obj.target.playVideo();
        var totalSeconds = player.getDuration();
        
        var hour = Math.floor(totalSeconds /3600);
        var minute = Math.floor((totalSeconds - hour*3600)/60);
        var seconds = totalSeconds - (hour*3600 + minute*60);
        if(hour < 10)
          hour = "0"+hour;
        if(minute < 10)
          minute = "0"+minute;
        if(seconds < 10)
          seconds = "0"+seconds;
        
        jQuery("#timer").html(hour + ":" + minute + ":" + seconds);
        timer.start({countdown: true, startValues: {seconds: totalSeconds}});
        timer.addEventListener('secondsUpdated', function (e) {
	        jQuery('#timer').html(timer.getTimeValues().toString());
	    });	
        console.log(timer);
    },

    onPlayerStateChange: function( evt ){
        if ( evt.data == -1 ){
            timer.pause();
        }
        if ( evt.data == 1 ){
            console.log('video play');
            timer.start();
        }
        if ( evt.data == 2 ){
            console.log('video paushed');
            timer.pause();
        }
        if ( evt.data == 0 ){
          if ( ulpYoutube.autocomplete == 0 ){
              return;
          }
          jQuery.ajax({
              type: 'post',
              url: decodeURI(window.ulp_site_url)+'/wp-admin/admin-ajax.php',
              data: {
                      action       : "ulp_complete_lesson",
                      lesson_id    : ulpYoutube.lessonId,
              },
              success: function( response ){
                console.log('reloadthis');
                location.reload(); /// refresh
              }
          });

        }
    },

    setAttributes: function(obj, args){
        for (var key in args) {
          obj[key] = args[key];
        }
    },

}

ulpYoutube.init( ulp_youtube_settings );
