/**
 * @package T6 Lessons
 * @copyright IQ-Labs, 2009-05-18
 * @author YURGEN.GOA <yurgen.goa@gmail.com>
 * @version $Id: player.js 54133 2009-11-20 11:20:51Z jurgen $
 */

// create rotatable playlist
var
  bIsExtraVideoStarted    = false,
  bIsExtraVideoFinished   = false,

  sSourceStr = t6strictPlayerSource(),

  IqExtraPlayer = {
      bIsSplashLoaded:    false,
      rInitPlayListItem:  [],
      rRowRand:           [],
      oVideoContainer:    null,
      iCurrentItemIdx:    null,
      bIsRandomPlaylist:  true,
      bIsRotate:          true,
      rPlaylist:          [
                            {img:      "intro/pitch_video/Talent6_Pitch_Video_NEW.jpg",
                             video:    "intro/pitch_video/Talent6_Pitch_Video_NEW.flv",
                             vol:      1},

                            {img:      "intro/adbc_s4/first.jpg",
                             img_last: "intro/adbc_s4/last.jpg",
                             video:    "/video/t6_video/idx_ABDC_S4_Trailer.flv",
                             vol:      1,
                             hdFinish: function(oPlayer) {
                               oPlayer.getPlugin("overlay").css({display:"none"});

                               oPlayer.unload();

                               $("#IqExtraBackPlayerLayer").html("<a href='castingcalls.php?view=414726,414727,414728,414730,414731,415309'><img src='intro/adbc_s4/last.jpg' width='272' height='206' /></a>");
                               $("#IqExtraBackPlayerLayer").css("display", "block");
                             },
                             overlay: {
                               css: { display:"block", backgroundGradient:[0, 0], backgroundColor:"#000000", height:25},
                               html: "<a href='castingcalls.php?view=414726,414727,414728,414730,414731,415309'><img src='intro/submittocc.png' hspace='0' vspace='1' /></a>"
                             }
                            },
                            {img:      "intro/cldg_workout/first.jpg",
                             img_last: "intro/cldg_workout/last.jpg",
                             video:    "/video/t6_video/idx_CLDG_Workout_CR.flv",
                             vol:      1,
                             hdFinish: function(oPlayer) {
                               oPlayer.getPlugin("overlay").css({display:"none"});

                               oPlayer.unload();

                               $("#IqExtraBackPlayerLayer").html("<a href='castingcalls.php?view=403474'><img src='intro/cldg_workout/last.jpg' width='272' height='206' /></a>");
                               $("#IqExtraBackPlayerLayer").css("display", "block");
                             },
                             overlay: {
                               css: { display:"block", backgroundGradient:[0, 0], backgroundColor:"#000000", height:24},
                               html: "<a href='castingcalls.php?view=403474'><img src='intro/submittocc.png' hspace='0' vspace='1' /></a>"
                             }
                            },
                            { img:      "intro/shegotlooks/first.jpg",
                              img_last: "intro/shegotlooks/last.jpg",
                              video:    "/video/t6_video/idx_Gotthelook.flv",
                              vol:      1,
                              hdFinish: function(oPlayer) {
                                oPlayer.getPlugin("overlay").css({display:"none"});

                                oPlayer.unload();

                                $("#IqExtraBackPlayerLayer").html("<a href='castingcalls.php?view=420513,420512,420511'><img src='intro/shegotlooks/last.jpg' width='272' height='206' /></a>");
                                $("#IqExtraBackPlayerLayer").css("display", "block");
                                },
                              overlay: {
                                css: { display:"block", backgroundGradient:[0, 0], backgroundColor:"#000000", height:24},
                                html: "<a href='castingcalls.php?view=420513,420512,420511'><img src='intro/submittocc.png' hspace='0' vspace='1' /></a>"
                              }
                            }
                          ],

      GetPlaylistLen:     function() { return this.rPlaylist.length; },

      InitPlayItem:       function() {
        if (! this.bIsRotate) return false;

        if (this.bIsRandomPlaylist) {
          for (var key in this.rPlaylist) {
            var iVol = this.rPlaylist[key]["vol"] || 1;
            for (var i = 1; i <= iVol; i++) {
              this.rRowRand.push(key);
            }
          }

          this.iCurrentItemIdx = this.rRowRand[Math.floor(Math.random() * this.rRowRand.length)];
        } else {
          if (this.iCurrentItemIdx == null) this.iCurrentItemIdx = 0;
          else this.iCurrentItemIdx++;

          if (this.iCurrentItemIdx >= this.GetPlaylistLen()) this.iCurrentItemIdx = 0;
        }
      },

      GetCurrentPlayItem: function() {
        var oRetVal = null;
        if (typeof this.rPlaylist[this.iCurrentItemIdx] != "undefined") {
          oRetVal = this.rPlaylist[this.iCurrentItemIdx];
        } else oRetVal = this.rPlaylist[0];

        return oRetVal;
      },

      GetCurrentPlayItemImg: function() {
        return sSourceStr + this.GetCurrentPlayItem()["img"];
      },

      GetCurrentPlayItemImgLast: function() {
        var sImgLast = this.GetCurrentPlayItem()["img_last"];

        if (sImgLast === false) return false;

        var sImg = sImgLast || this.GetCurrentPlayItem()["img"];

        return sSourceStr + sImg;
      },

      GetCurrentPlayItemVideo: function() {
        return sSourceStr + this.GetCurrentPlayItem()["video"];
      },

      GetVideoContainer: function() {
        if (this.oVideoContainer == null) this.oVideoContainer = $("#" + sExtraVideoContainerId + " IMG");
        return this.oVideoContainer;
      },

      Init: function() {
        this.InitPlayItem();

        var
          oClos     = this,
          sVideo    = this.GetCurrentPlayItemVideo(),
          sImgLast  = this.GetCurrentPlayItemImgLast();

        if (sVideo)   this.rInitPlayListItem.push(sVideo);
        if (sImgLast) this.rInitPlayListItem.push(sImgLast);

        if (! this.bIsSplashLoaded) {
          $(document).ready(function() {
            oClos.ShowSplash();
          });
          this.bIsSplashLoaded = true;
        } else {
          this.ShowSplash();
        }
      },

      ShowSplash: function() {
        $(this.GetVideoContainer()).attr("src", this.GetCurrentPlayItemImg());
        $(this.GetVideoContainer()).css("display", "block");
      }
  };


// init playlist
IqExtraPlayer.Init();


// configuration of player
var
  sExtraVideoContainerId  = "IqExtraBackPlayer",
  iLastVideo              = 0,
  hdStrictPlayerListener  = null,

  t6FlowPlayerConf        = {
    wmode:                "transparent",
    loop:                 false,
    usePlayOverlay:       false,
    play:                 { opacity:0.0 },
    screen:   {
        width:  272,
        height: 206
    },

    clip:     {
        seekableOnBegin:  true,

        onStart:          function() {
          if (bIsExtraVideoStarted) return;

          bIsExtraVideoFinished = false;
          bIsExtraVideoStarted  = true;

          hdStrictPlayerListener = window.setInterval(function() { jxPlayerEvent(); }, 5000);
          jxPlayerEvent("start");

          var
            oOver     = IqExtraPlayer.GetCurrentPlayItem()["overlay"],
            oOverPlug = this.getPlugin("overlay");

          if (oOver && oOver.css) oOverPlug.css(oOver.css);
          if (oOver && oOver.html) oOverPlug.setHtml(oOver.html);
        },

        onStop:           function() {
          window.clearInterval(hdStrictPlayerListener);
        },

        onFinish:         function() {
          if (this.getTime() != 0 && ! bIsExtraVideoFinished) {
            window.clearInterval(hdStrictPlayerListener);
            jxPlayerEvent("finish");

            var
              oItem   = IqExtraPlayer.GetCurrentPlayItem(),
              hdCall  = (oItem && typeof oItem["hdFinish"] == "function" ? oItem["hdFinish"] : null);

            if (hdCall) hdCall.call(this, this);

            bIsExtraVideoFinished = true;
          }
        },

        onResume:         function() {
          hdStrictPlayerListener = window.setInterval(function() { jxPlayerEvent(); }, 5000);
        },

        onPause:          function() {
          jxPlayerEvent("pause");
          window.clearInterval(hdStrictPlayerListener);
        }
    },

    playlist: IqExtraPlayer.rInitPlayListItem,

    plugins:  {
      controls: {
        hideDelay:  2500,
        autoHide:   "always",
        all:        false,
        play:       true,
        scrubber:   true,
        volume:     true,
        mute:       true,
        time:       false,
        fullScreen: false
      },
      overlay:  {
        url: "js/flowplayer/flowplayer.content-3.1.0.swf",
        top:0, left:0, width:271,
        borderRadius:0, border:0, display:"none",
        padding:0,
        "margin-left":0,
        "margin-top":0
      }
    }
  },

  oExtraPlayer      = flowplayer(sExtraVideoContainerId, "js/flowplayer/flowplayer-3.1.3.swf", t6FlowPlayerConf),

  jxPlayerEvent     = function(sEvent) {
    var oParams = {};

    oParams.tm = oExtraPlayer.getTime();

    if (sEvent)             oParams.evt = sEvent;
    if (sEvent == "start")  oParams.url = encodeURIComponent(oExtraPlayer.getClip().originalUrl);

    $.get("norw/jx_extra_player.php", oParams, function(data) { iLastVideo = data; });
  };