if(typeof(XPRIMA) == "undefined") XPRIMA = { PreRoll: {} };
if(typeof(XPRIMA.PreRoll) == "undefined") XPRIMA.PreRoll = {};
if(typeof(XPRIMA.Clips) == "undefined") XPRIMA.Clips = {};

XPRIMA.FlowPlayer = function() {
  return {
    flowplayer_key : '#$6c83dd755931b2a99b4',
    setup : function(containerid, video, lng, title, res_or_type, wmark) {
      if (typeof(XPRIMA) == "undefined" || !XPRIMA.PreRoll) return;
      if (typeof(video)  == "string") {
        video = [video];
      }
      
      if (typeof(title) == "undefined") {
        title = '';
      }
      if (typeof(res_or_type) == "undefined") {
        res_or_type = '';
      }
      title=title.replace(/<\/?h1>/gi,'');      
      
      if (!(video instanceof(Array))) return;
      var _label;
      var _replayLabel;
      var _tag_clip;
      _tag_clip = video.length>1; 
      if(lng == 'fr') {
        _label = "Visionner";
        _replayLabel = "Revisionner";
      }
      else {
        _label = "Play Video";
        _replayLabel = "Replay the Video";
      }
      _track = title ? true : false;

     trackfunc = function(clip, label) {
        var _category = (res_or_type || "???");
        var _action = 'v-'+title;
        var _label = label;
        if(typeof(pageTracker) != "undefined" && pageTracker != null) {
          pageTracker._trackEvent(_category, _action, _label);
        }
      };
      
      cuefunc = function(clip, point) {
        var _label = clip.cues[point].toString()+'%';
        trackfunc(clip,_label);
      };
      
      var playlist = [];
      if(XPRIMA.PreRoll.videoFLV)  {
        var o = { url: XPRIMA.PreRoll.videoFLV, videotype: 'pre' };
        if(XPRIMA.PreRoll.clickURL) {
          o.linkUrl = XPRIMA.PreRoll.clickURL;
          o.linkWindow = XPRIMA.PreRoll.clickTarget || "_blank";
        }
        o.showBrand = XPRIMA.PreRoll.showBrand || false;
        o.showScrubber = XPRIMA.PreRoll.showScrubber || false;
        playlist.push(o);
      }
      if(typeof(XPRIMA)!="undefined" && typeof(XPRIMA.Clips)!="undefined" && typeof(XPRIMA.Clips.showBrand)!="undefined") {
        _showBrand = XPRIMA.Clips.showBrand;
      } 
      else 
      {
        // Check if the flowplayer needs to display the watermark overlay
        if(typeof(wmark) == 'undefined' || wmark == '')
        {
          wmark = null;
        }
        if(wmark == null || wmark == true || wmark == 'true')
        {
          _showBrand = true;
        }
        else
        {
          _showBrand = false;
        }
      }
      if (_tag_clip) {
        for( var ii in video) {
          playlist.push({ url: video[ii], videotype: 'main', showBrand:_showBrand });
          }
      } else{
        playlist.push({ url: video[0], videotype: 'main', showBrand:_showBrand });
      }

      flowplayer(containerid, "/site/js/flowplayer/flowplayer.commercial-3.1.5.swf", {
        key: XPRIMA.FlowPlayer.flowplayer_key,
        clip:  {
          scaling : 'fit',
          showBrand : true,
          showScrubber : true,
          onStart: function(clip) {
            var brand = this.getPlugin("brand");
            try {
            (clip.showBrand ? brand.show : brand.hide)();
            } catch(e) {}
            this.getPlugin("controls").enable({scrubber:clip.showScrubber});
            if(_track)  {
              if(clip.videotype == "pre") trackfunc(clip, "Pre-Roll");
              else if(clip.videotype == "main") trackfunc(clip, "0%");
              if(clip.videotype == "main")  {
                if(clip.duration >= 50) {
                  cues = []; 
                  cues.push(100*parseInt((10.0*clip.duration)/4.0));
                  cues.push(100*parseInt((10.0*clip.duration)/2.0));
                  cues.push(100*parseInt((30.0*clip.duration)/4.0));
                  clip.onCuepoint(cues, cuefunc);
                  clip.cues=new Object();
                  for(var i = 0; i < cues.length; ++i) {
                    clip.cues[cues[i]]=(i+1)*25;
                  }
                }
              }
            }
          },
          onFinish: function(clip) {
            if(_track) {
              if(clip.videotype == "main") trackfunc(clip, "100%");
            }
          }
        },
        
        playlist: playlist,
        play: { label: _label, replayLabel: _replayLabel },
        plugins: {
          brand: {
            url: "/site/js/flowplayer/flowplayer.content-3.1.0.swf",
            backgroundImage: 'url(/site/img/auto123.png)',
            backgroundColor: "transparent",
            backgroundGradient: [0,0],
            border: 0,
            bottom: 30, right: 5,
            width: 100, height: 33,
            onClick: function() { window.location = 'http://www.auto123.com/'; }
          }
        }
      });
    },
    
    setupAudio : function(containerid, autoplay, height) {
      flowplayer(containerid, "/site/js/flowplayer/flowplayer.commercial-3.1.5.swf", {
        key: XPRIMA.FlowPlayer.flowplayer_key,
        clip:  { autoPlay: autoplay },
        plugins: {
          controls: {
            fullscreen: false,
            height: height
          }
        }
      });
    }
  }
}();

