/**
 * @param oInst
 */

var iStatusUpdateInMseconds = 10 * 1000;

jxFlowPlayerGetFpos = function(oInst) {
  if (! oInst || ! oInst.metaData) return;

  this.oKeys = oInst.metaData.keyframes;
	this.rFpos = this.oKeys.filepositions;
	this.rTms  = this.oKeys.times;
	this.rMap	= [];

	for (var key in this.rTms) {
		var iTime = this.rTms[key].toFixed(0);
		if (! isset(this.rMap[iTime])) this.rMap[iTime] = this.rFpos[key];
	}
};

/**
 *
 */
jxFlowPlayerGetFpos.instance 		= null;

/**
 * @param oInst
 * @return
 */
jxFlowPlayerGetFpos.GetInstance = function(oInst) {
	if (jxFlowPlayerGetFpos.instance === null) {
		jxFlowPlayerGetFpos.instance = new jxFlowPlayerGetFpos(oInst);
	}
	return jxFlowPlayerGetFpos.instance;
};

/**
 * @param oInst
 * @param iPos
 * @return
 */
jxFlowPlayerGetFpos.GetPos = function(oInst, iPos) {
  var
		rData = jxFlowPlayerGetFpos.GetInstance(oInst).rMap,
		iPos  = (iPos && is_number(iPos) ? iPos.toFixed(0) : 0);

	return (isset(rData) && isset(rData[iPos]) ? rData[iPos] : 0);
};

/**
 *
 */
var bIsVideoInits    = false,
    bIsVideoStarted  = false,
    hdPlayerListener = null,
		t6FlowPlayerConf = {
      loop:                 false,
      usePlayOverlay:       false,
      play:                 { opacity:0 },

      onError:              function(iErr, sErr) {
        this.unload();
        switch (iErr) {
          case 200:
          case 201:
            if (bIsVideoInits) ShowMessageDialogAsError(t6lessons.lang.MSG_ERR_VIDEO_STREAM + " (" + iErr + ")");
            break;
          default:
            ShowMessageDialogAsError("code: " + iErr);
        }
      },

      clip:	{
          seekableOnBegin:  true,
          autoPlay:         true,
          provider:         'apache',

          onBeforeBegin:		function(oInst) {
            if (isset(t6FlowPlayerConf.bPreventService) && t6FlowPlayerConf.bPreventService) {
              return true;
            }

            $.ajax({
              async:    false,
              dataType: "json",
              url:      "norw/service.php",
              timeout:  1000,
              success:  function(o) {
                if (o && o.flag) bIsVideoInits = ! ! o.flag;
              }
            });

            if (! bIsVideoInits) {
              ShowMessageDialogAsError(t6lessons.lang.MSG_ERR_CURRENT_LOGOUT, function() { top.location.reload(); } );
              this.unload();
            }

            return bIsVideoInits;
          },

          onStart:          function(oInst) {
      			if ( !jxFlowPlayerIsEvents() || bIsVideoStarted) return;

      			bIsVideoStarted = true;

      			if (t6FlowPlayerConf.t6lessons && t6FlowPlayerConf.t6lessons.start) {
      			  this.seek(t6FlowPlayerConf.t6lessons.start);
      			}

      			var oClos = this;
      			hdPlayerListener = window.setInterval(
      					function(oInst) {
      						jxFlowPlayer(oClos, oInst);
      					}, iStatusUpdateInMseconds);

      			jxFlowPlayer(this, oInst, "start");
      		},

          onStop:           function() {
      		},

          onFinish:         function(oInst) {
            var oClos = this;

            bIsVideoStarted = false;

            $(".videoMain, .videoBox").fadeOut(0, function() {
              oClos.unload();
              $(".videoMain, .videoBox").fadeIn(1000);
            });

      		  if ( !jxFlowPlayerIsEvents()) return;

      			window.clearInterval(hdPlayerListener);
      			jxFlowPlayer(this, oInst, "finish");
      		},

          onPause:          function() {
      			window.clearInterval(hdPlayerListener);
          },

          onResume:         function(oInst) {
            if ( !jxFlowPlayerIsEvents()) return;

      			var oClos = this;
          	hdPlayerListener = window.setInterval(
      					function(oInst) {
      						jxFlowPlayer(oClos, oInst);
      					}, iStatusUpdateInMseconds);
          }
      },
      plugins: {
        apache:   {
          url:          IqPlayerVer.Stream,
          queryString:  ''
        },
        controls: {
          hideDelay:  2500,
          autoHide:   "always",
          all:        false,
          play:       true,
          scrubber:   true,
          volume:     true,
          mute:       true,
          time:       false,
          fullScreen: false
        }
      }
		},

  	jxFlowPlayer = function(oPlayer, oInst, sEvent) {
			if (! jxFlowPlayerIsEvents()) return;

			var
  			iPos 		= oPlayer.getTime(),
  			oParams	= {
  				pos: 	iPos,
  				fpos: jxFlowPlayerGetFpos.GetPos(oInst, iPos),
  				evt:	sEvent || "play",
  				vid:	t6FlowPlayerConf.t6lessons.vid,
  				lid:	t6FlowPlayerConf.t6lessons.lid};

      if (sEvent == "start")  oParams.url = oInst.url;

      var hdCall = function(o) {
      	if (o && o.error) {
      	  ShowMessageDialogAsWarn(o.error, function() { top.location.reload(); });
          window.clearInterval(hdPlayerListener);
      	  oPlayer.stop();
      	  oPlayer.unload();
      	  return;
      	}

        if (o && o.flag && o.flag == 1 && $("#LessonNextStepActive").css("display") == "none") {
      		$("#LessonNextStepActive,#LessonNextStepInactive").toggle();
      	}
      };

      $.ajax({
        url:      "norw/jx/video_player_stat.php",
        timeout:  10 * 1000,
        type:     "POST",
        data:     oParams,
        dataType: "json",
        success:  hdCall,
        error:    function(o, e) {
          if (e == "timeout") {
            o.onreadystatechange = function () {};
            o.abort();
          }
        }});
  	},

  	jxFlowPlayerIsEvents = function() {
  	  return (isset(t6FlowPlayerConf.t6lessons.events) && t6FlowPlayerConf.t6lessons.events == true);
  	};
