/*
* File:    player4.2.js
* Version: 1.2
*
* Wrapper for player.swf v4.2 with WebTrends On-Demand play tracking.
*
* Player References:
*   Overview:  http://developer.longtailvideo.com/trac/wiki/Player4Overview
*   Flashvars: http://developer.longtailvideo.com/trac/wiki/Player4FlashVars
*   API:       http://developer.longtailvideo.com/trac/wiki/Player4Api
*   Events:    http://developer.longtailvideo.com/trac/wiki/Player4Events
*/

/*
Usage:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/swfobject/2.2/swfobject.js"></script> 
<script type="text/javascript" src="player4.2.js"></script> 
<script type="text/javascript">
// Create the player objects
var player1 = createPlayer('flvPlayer1', width, height, 'base_name_without_extension', 'title_for_webtrends_tracking');
var player2 = createPlayer('flvPlayer2', width, height, 'base_name2_without_extension', 'title_for_webtrends_tracking');

// Helper function to create the players
function createPlayer(playerId, width, height, file, title) {
	var player = new calix.player42(playerId, width, height, "9.0.115", file, title);
	
	player.enableWebTrends = true;
	
	player.flashvars = { width:width, height:height, wmode:"opaque", autostart:"false", usefullscreen:"false",
		file:file + '.flv', 
		image:file + '.png' }
	
	player.params = { javascriptid:playerId, enablejs:"true", allowfullscreen:"true", allowscriptaccess:"always", showdigits:"true", shuffle:"false", repeat:"false", wmode:"opaque" }
	
	player.attributes = { id:playerId, name:playerId }
	
	return player;
}

// Called by each player.swf when it is loaded.
function playerReady(obj) {
	player1.playerReader(obj);
	player2.playerReader(obj);
}
</script>

<div id="player1Content"><!-- Default Content --></div>
<script type="text/javascript"> 
player1.embedSWF("player4.2.swf", "player1Content");
</script>
*/

var calix = (function() {
	
	function player42(playerId, width, height, version, file, title) {
	
		// The Javascript Id of this player object
		this.id = playerId;
		
		// The width of the player
		this.width = width;
		
		// The height of the player (including the 20 pixels for the controlbar)
		this.height = height;
		
		// The minimum required flash version
		this.version = version;
		if (!this.version) this.version = '8.0';
		
		// Base filename
		this.file = file;
		if (!this.file) this.file = '';
		
		// Title
		this.title = title;
		if (!this.title) this.title = '';
		
		// Flash Variables
		this.flashvars = {};
		
		// Player Parameters
		this.params = {};
		
		// Player Attributes
		this.attributes = {};
		
		// Current player state.  Valid values: 'NONE', 'IDLE', 'BUFFERING', 'PLAYING', 'PAUSED', 'COMPLETED'
		this.currentState = 'NONE';
	 
		// Player object
		this.player = null;
		
		// Object for events
		this.obj = playerId + 'Object';
		eval(this.obj + '=this');
		
		// WebTrends
		this.enableWebTrends = false;
		this.dcsid = "dcse9z3xl00000oi29rg1lpis_9f9i";
		this.ti = title;
		
		/* 
		*  Function: embedSWF()
		*    Dynamically add the player to the webpage replacing the content of the
		*    HTML element with the specified id.
		*  Parameters:
		*    swfUrl (String, required) - specifies the URL of your SWF
		*    id (String, required) - specifies the id of the HTML element (containing your alternative content) you would like to have replaced by your Flash content
		*/
		this.embedSWF = function(swfUrlStr, replaceElemIdStr) {
			if (swfobject) {
				swfobject.embedSWF(swfUrlStr, replaceElemIdStr, ''+this.width, ''+this.height, this.version, false, this.flashvars, this.params, this.attributes);
			}
		};
		
		/* 
		*  Function: embedSWF2()
		*    Dynamically add the player to the webpage replacing the content of the
		*    HTML element with the specified id.
		*  Parameters:
		*    swfUrl (String, required) - specifies the URL of your SWF
		*    id (String, required) - specifies the id of the HTML element (containing your alternative content) you would like to have replaced by your Flash content
		*    width (String, required) - specifies the width of your SWF
		*    height (String, required) - specifies the height of your SWF
		*    version (String, required) - specifies the Flash player version your SWF is published for (format is: "major.minor.release" or "major")
		*    expressInstallSwfurl (String, optional) - specifies the URL of your express install SWF and activates Adobe express install. Please note that express install will only fire once (the first time that it is invoked), that it is only supported by Flash Player 6.0.65 or higher on Win or Mac platforms, and that it requires a minimal SWF size of 310x137px.
		*    flashvars (Object, optional) - specifies your flashvars with name:value pairs
		*    params (Object, optional) - specifies your nested object element params with name:value pairs
		*    attributes (Object, optional) - specifies your object's attributes with name:value pairs
		*    callbackFn (JavaScript function, optional) - can be used to define a callback function that is called on both success or failure of embedding a SWF file (SWFObject 2.2+)
		*/
		this.embedSWF2 = function(swfUrlStr, replaceElemIdStr, widthStr, heightStr, swfVersionStr, 
								xiSwfUrlStr, flashvarsObj, parObj, attObj, callbackFn) {
			if (swfobject) {
				swfobject.embedSWF(swfUrlStr, replaceElemIdStr, widthStr, heightStr, swfVersionStr, xiSwfUrlStr, flashvarsObj, parObj, attObj, callbackFn);
			}
		};
		
		/*
		*  Function: playerReader()
		*    Called by a player object after it is loaded and instantiated.
		*  Parameters:
		*    obj - (Object, required) - The 'obj' object will contain the id, version and client of the player, so you immediately know what player it is an where to subscribe to. Use the id variable to get a reference to the player itself
		*/
		this.playerReader = function(obj) {
			if (obj && obj.id) {
				if (this.id) {
					if (obj.id == this.id) {
						//alert('Adding player: ' + obj.id + ' to id ' + this.id);
						this.player = document.getElementById(obj.id);
						this.addListeners();
					}
				}
			}
		};
		
		/*
		*/
		this.addListeners = function() {
			if (this.player) { 
				this.player.addModelListener("STATE", this.obj + ".stateListener");
			}
			else {
				setTimeout(this.obj + ".addListeners()", 100);
			}
		}
		
		/*
		*/
		this.stateListener = function(obj) {
			this.currentState = obj.newstate; 
			//alert('player ' + this.id + ' state: ' + this.currentState);
			this.playTracker(obj);
		};
		
		/*
		*/
		this.playTracker = function() {
			var currentTime = new Date().getTime();
			var url = '';
			var uri = '';
			var title = '';
		 
			if (this.enableWebTrends && this.player && this.currentState == 'PLAYING') {
				uri = this.player.getConfig().file;
				
				// Replace https with http since videos can be accessed with either
				if (uri.length > 5) {
					if (uri.substr(0, 5) == 'https') {
						uri = 'http' + uri.substr(5);
					}
				}
				
				// Make www.calix.com links relative
				if (uri.length > 20) {
					if (uri.substr(0, 21) == 'http://www.calix.com/') {
						uri = uri.substr(21);
					}
				}
				
				// Replace spaces in the title with '%20' for url encoding
				title = this.title.replace(/ /g, "%20");
				
				// Build the tracking url as a link to an image
				url = 'http://statse.webtrendslive.com/' + this.dcsid + '/dcs.gif'
					+ '?dcsuri=' + uri
					+ '&dcsdat=' + currentTime
					+ '&WT.cg_n=Calix&WT.cg_s=Video'
					+ '&WT.ti=' + title;
				// + '&WT.sv=.calix.com'
				
				//alert(uri + ",\n'" + title + "',\n" + this.currentState + "\n" + url);  return;
				
				// Send to Webtrends by creating a new Image with the url
				if (url != '') {
					if (document.images && _tag) {
						// Add to the webtrends image array
						_tag.images[_tag.index]=new Image();
						_tag.images[_tag.index].src=url;
						_tag.index++;
						//alert(_tag.images[_tag.index-1].src);
					}
				}
			}
		};
	}
	
	return { 
		player42:player42
	};
})();
