function __flvCreateCookie(name,value,minutes) {
	if (minutes) {
		var date = new Date();
		date.setTime(date.getTime()+(minutes * 60 * 1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	//alert(name+"="+value+expires+"; path=/; domain=.damemagazine.com");
	document.cookie = name+"="+value+expires+"; path=/";
}

function __flvReadCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function __flvEraseCookie(name) {
	__flvCreateCookie(name,"",-1);
}

function FlvMonitor(file) {
	csi = __flvReadCookie("ClientSessionId");
	return {
		playerSessionId:'',
		fileName: file,
		startTime: 0,
		clientSessionId: csi,
		playTime: 0,
		state: '',
		start: function() {
			if (this.state != '') return;
			this.state = 'playing';
			this.startTime = new Date().getTime();
			jQuery.post("/tracking/default.aspx", 
                        {
                          FileRequested:this.fileName, 
                          FunctionName:"FileRequest", 
                          ClientSessionId:this.clientSessionId
                        }, 
                        function(data){ 
                          j = eval('(' + data +')'); 
						  window.FlvMonitors[j.fileName].playerSessionId = j.playerSessionId;
						  window.FlvMonitors[j.fileName].clientSessionId = j.clientSessionId;
						  __flvCreateCookie("ClientSessionId", j.clientSessionId, 60 * 24 * 365);
                        });
			},
		pause: function() {
			if (this.state != 'playing') return; //cant pause if you arent playing
			this.state = 'paused';
			var thisTime = new Date().getTime();
			this.playTime += (thisTime - this.startTime);
			this.startTime = 0;
			jQuery.post('/tracking/default.aspx', { FileRequested: this.fileName, 
                                                    FunctionName:"UpdateStream",
                                                    ClientSessionId:this.clientSessionId,
                                                    PlayerSessionId:this.playerSessionId,
                                                    UpdateTime: this.playTime 
                                                  } );
		},
	resume: function() {
		if (this.state != 'paused') {  return; } //can only resume if pasued
		this.state = 'playing';
		this.startTime = new Date().getTime();
	},
	end: function() {
		if (this.state != 'playing' && this.state != 'paused') return; //can only end a stream if it is playing or paused
		this.pause();
		this.state = '';
		}
	}
}
window.FlvMonitors = [];
FlvStack = [];

function startFlashTracking(file) {
	window.FlvMonitors[file] = new FlvMonitor(file);
	window.FlvMonitors[file].start();
	FlvStack[FlvStack.length] = file;
}

function pauseFlashTracking(file) {
	if (!window.FlvMonitors[file]) return;
	window.FlvMonitors[file].pause();
}

function resumeFlashTracking(file) {
	if (!window.FlvMonitors[file]) return;
	window.FlvMonitors[file].resume();
}

function endFlashTracking(file) {
	if (!window.FlvMonitors[file]) return;
	window.FlvMonitors[file].end();
}
function endAllFlashTracking() {
	for (i=0;i<FlvStack.length;i++)
	{
		if (window.FlvMonitors[FlvStack[i]]) window.FlvMonitors[FlvStack[i]].end();
	}
}

window.unload = function() { endAllFlashTracking(); }