﻿/* Requires SWFObject, ActiveContent, Omniture, and jQuery */

YB_VideoPlayer.constructor = YB_VideoPlayer;

function YB_VideoPlayer(config) {
	var me = this;

	this.elemId = _getConfigValue(config, "elemId", "video_player_window");
	this.listingId = _getConfigValue(config, "listingId");
	this.videoUrl = _getConfigValue(config, "videoUrl");
	this.hostSource = _getConfigValue(config, "hostSource", "YBcom");
	this.trackingEnabled = _getConfigValue(config, "trackingEnabled", true);
	this.trackingFrequency = _getConfigValue(config, "trackingFrequency", 10);
	this.width = _getConfigValue(config, "width", 430);
	this.height = _getConfigValue(config, "height", 240);
	this.accountName = _getConfigValue(config, "accountName", "ybdev");
	this.swfSource = _getConfigValue(config, "swfSource", "/flash/yb_player2.swf");
	this.bgcolor = _getConfigValue(config, "bgcolor", "#000000");

	var lastOffsetSeen = 0;
	var videoDuration = 0;
	var lastStopTime = 0;
	var videoLoggingDisabled = false;

	if (this.trackingEnabled == true)
		init_tracking();

	function _getConfigValue(config, name, defaultValue) {
		if ((config) && (config[name] || typeof (config[name]) == "boolean" || typeof (config[name]) == "string"))
			return config[name];
		if (defaultValue || typeof (defaultValue) == "boolean" || typeof (defaultValue) == "string")
			return defaultValue;
		return null;
	}

	function init_tracking() {
		if (typeof (s) != "undefined") {
			s.loadModule("Media");
			s.Media.trackWhilePlaying = true;
			s.Media.trackSeconds = me.trackingFrequency;
			s.Media.trackVars = "events";
			s.Media.trackEvents = "event8";
			s.Media.monitor = function(s_videoMonitor, media) {
				if (media.event == "MONITOR") {
					lastOffsetSeen = media.offset;
				} if (media.event == "CLOSE") {
					s_videoMonitor.events = "event8";
					s_videoMonitor.Media.track(media.name);
				}
			}
		}
	}

	/* The updateStatus() function is called from the SWF from the onStatus() method of the NetStream object. */
	this.UpdateStatus = function(message, param) {
		if (this.trackingEnabled == true) {
			if (typeof (s) != "undefined") {
				if (!videoLoggingDisabled) {
					s_account = me.accountName;
					var s_video = s_gi(s_account);
					var id = new String(me.listingId);
					if (message == "VideoStart") {
						me.lastOffsetSeen = 0;
						me.videoDuration = param;
						me.lastStopTime = param;
						s_video.Media.open(id, param, 'yb_player');
						s_video.Media.play(id, 0);
					} else if (message == "VideoPlay") {
						if (lastStopTime < param) { // user fast-forwarded
							me.videoLoggingDisabled = true;
						} else {
							s_video.Media.play(id, param);
						}
					} else if (message == "VideoPause") {
						s_video.Media.stop(id, param);
						me.lastStopTime = param;
					} else if (message == "VideoEnd") {
						s_video.Media.stop(id, param);
						s_video.Media.close(id);
					}
				}
			}
		}
	}

	this.StartMovie = function() {
		if (me.videoUrl == null) {
			alert("Sorry, it seems there has been an internal error.");
			return 0;
		}

		var src = me.swfSource;
		var version = "9.0.0";

		var flashvars = {};
		flashvars.ybVideo = me.videoUrl;
		if (me.hostSource)
			flashvars.hostSource = me.hostSource;
		//flashvars.autoStart = "true";
		//flashvars.skinPath = "/flash/";

		var params = {};
		params.bgcolor = me.bgcolor;
		params.wmode = "opaque";
		params.loop = "false";

		swfobject.embedSWF(src, me.elemId, me.width, me.height, version, "/flash/expressInstall.swf", flashvars, params);


	}

	this.StopMovie = function() {

		if (videoDuration > 0) { // has movie played once?
			// notify omniture movie stopped as of the last known offset.
			updateStatus("VideoEnd", lastOffsetSeen);
		}

		// Due to a limitation in the video player itself, the video cannot be stopped without
		// destroying its container, rather than passing a stop message to it.
		$("#" + me.elemId).parent().html("<div id=\"" + me.elemId + "\" />");
	}

	return this;
}