if (typeof(Sys) == "undefined")
{
//	alert(window.location.pathname + " says: Registering BrowserUtil.js because my developer forgot to.");
	document.writeln("<scr" + "ipt src=\"/js/BrowserUtil.js\" type=\"text/javascript\"></script>");
}

function VideoPlayer()
{
	this.player = null;
	this.playlist = null;
	this.url = "";
	this.setId(); 
	this.setHeight();
	this.setWidth();
	this.setQuickTimeHeightDifference();
	this.setParentBlockId();
	this.setPlayerType();				
	this.setPlaylistFileNames();
	this.setHost();
	this.setPlaylistDirectoryPath();
	this.isSafari = navigator.userAgent.indexOf("Safari")> -1;
}

VideoPlayer.prototype.createPlayer = function(playlistArray, adSlotsArray)//["filepath/filename", "filepath/filename"]
{
	if(this.player) this.destroyPlayer();
	this.playlist = (playlistArray) ? playlistArray : this.playlist;
	var adSlotsArray = (adSlotsArray) ? adSlotsArray : null;
	this.playlistQueryString = VideoPlayer.buildPlaylistQueryString(this.playlist, adSlotsArray);
					
	if(this.playlistQueryString)
	{
		this.url = this.host + this.playlistDirectoryPath + this.playlistFileNames[this.playerType] + "?" + this.playlistQueryString;
	//	alert(this.url);
	}
	else 
	{
		alert("there are no clips to play");
	}
					
	switch(this.playerType)
	{
		case "windowsMedia":
			this.player = VideoPlayer.buildWindowsMediaPlayer(this);
		break;
		case "quickTime":
			this.player = VideoPlayer.buildQuickTimePlayer(this);
		break;
		default:
			alert("The platform you are using is not supported by our video player")
		break;
	}
	
	this.setParentBlock(this.parentBlockId);
	if(typeof this.player == "string")
	{
		this.parentBlock.innerHTML = this.player;
	}
	else 
	{
		this.parentBlock.appendChild(this.player);
	}	
	
	if (Sys.Browser.agent == Sys.Browser.InternetExplorer)
		event.returnValue = false;
	
	return false;
}

VideoPlayer.prototype.destroyPlayer = function()
{	
	if(this.player)
	{
		if(typeof this.player.stop == "unknown") this.player.stop();//windows IE
		if(typeof this.player == "string")//Safari
		{
			if(document.getElementById(this.id).Stop) document.getElementById(this.id).Stop();//Qt7
			this.parentBlock.innerHTML = "";//all QT versions
		}	
		else this.parentBlock.removeChild(this.player);
		delete this.player;
	}
	
	if (Sys.Browser.agent == Sys.Browser.InternetExplorer)
		event.returnValue = false;
		
	return false;
}

//player id
VideoPlayer.prototype.setId = function(id)
{				
	this.id = (id) ? id :"defaultVideoPlayer";
}

VideoPlayer.prototype.getId = function()
{				
	return this.id;
}
			
//parentBlockId
VideoPlayer.prototype.setParentBlockId = function(parentBlockId)
{				
	this.parentBlockId = (parentBlockId) ? parentBlockId : null;
}

VideoPlayer.prototype.getParentBlockId = function()
{				
	return this.parentBlockId;
}

//player parentBlock
VideoPlayer.prototype.setParentBlock = function(parentBlockId)
{				
	this.parentBlockId = (parentBlockId) ? parentBlockId : "defaultParentBlockId";
	this.parentBlock = (parentBlockId) ? document.getElementById(parentBlockId) : document.body;
}

VideoPlayer.prototype.getParentBlock = function()
{				
	return this.parentBlock;
}
		
//player height
VideoPlayer.prototype.setHeight = function(height)
{				
	this.height = (height) ? height : 309;
}

VideoPlayer.prototype.getHeight = function()
{				
	return this.height;
}

//player width
VideoPlayer.prototype.setWidth = function(width)
{				
	this.width = (width) ? width : 320;
}

VideoPlayer.prototype.getWidth = function()
{				
	return this.width;
}

//player quicktimeHeightDifference
VideoPlayer.prototype.setQuickTimeHeightDifference = function(heightDifference)
{				
	this.quickTimeHeightDifference = (heightDifference) ? heightDifference : -53;
}

VideoPlayer.prototype.getQuickTimeHeightDifference = function()
{				
	return this.quickTimeHeightDifference;
}

//player type
VideoPlayer.prototype.setPlayerType = function()
{				
	this.playerType = (navigator.platform.indexOf("Win") > -1) ? "windowsMedia" : (navigator.platform.indexOf("Mac")> -1) ? "quickTime" : "unknown";
}

VideoPlayer.prototype.getPlayerType = function()
{				
	return this.playerType;
}

//player host
VideoPlayer.prototype.setHost = function(host)
{				
	this.host = (host) ? host : "http://" + window.location.host;
}

VideoPlayer.prototype.getHost = function()
{				
	return this.host;
}

//player playlistDirectoryPath
VideoPlayer.prototype.setPlaylistDirectoryPath = function(playlistDirectoryPath)
{				
	this.playlistDirectoryPath = (playlistDirectoryPath) ? playlistDirectoryPath : "/video/playlists/" ;
}

VideoPlayer.prototype.getPlaylistDirectoryPath = function()
{				
	return this.playlistDirectoryPath;
}

//player playlistDirectoryPath			
VideoPlayer.prototype.setPlaylistDirectoryPath = function(playlistDirectoryPath)
{				
	this.playlistDirectoryPath = (playlistDirectoryPath)? playlistDirectoryPath : "/video/playlists/";
}

VideoPlayer.prototype.getPlaylistDirectoryPath = function()
{				
	return this.playlistDirectoryPath;
}

//player playlistFileNames	
VideoPlayer.prototype.setPlaylistFileNames = function(playlistFileNameObj)//{windowsMedia:"", quickTime:""}
{				
	this.playlistFileNames = (playlistFileNameObj) ? {windowsMedia:playlistFileNameObj.windowsMedia, quickTime:playlistFileNameObj.quickTime} : {windowsMedia:"Asx.aspx", quickTime:"Smil.smi"};
}

VideoPlayer.prototype.getPlaylistFileNames = function()//getPlaylistFileNames().windowsMedia getPlaylistFileNames().quickTime
{				
	return this.playlistFileNames;
}

//TODO
VideoPlayer.prototype.setParam = function(paramsObj)//{name:value, name:value}
{
	//if name exists change value	
}

VideoPlayer.prototype.getParams = function()
{
	return this.playerParams;
}
			
//Static Methods 
VideoPlayer.buildWindowsMediaPlayer = function(playerObject)
{
	playerObject.playerParams =
		{
			AutoStart:"1",
			ShowStatusBar:"1",
			AutoRewind:"1",
			TransparentAtStart:"1",
			//begin remove
			enabled:"1",
			//end remove
			fileName:playerObject.url
		}				
	playerElement = document.createElement("object");
	playerElement.setAttribute("id", playerObject.id);
	playerElement.setAttribute("width", playerObject.width);
	playerElement.setAttribute("height", playerObject.height);
	playerElement.setAttribute("style", "display:block");
	for(param in playerObject.playerParams)
	{
		var tempParam = document.createElement("param");
		tempParam.setAttribute("name", param);
		tempParam.setAttribute("value", playerObject.playerParams[param]);
		playerElement.appendChild(tempParam);
	}
	playerElement.setAttribute("type", "video/x-ms-asf");// has to be last in IE
	
	return playerElement;
}

VideoPlayer.buildQuickTimePlayer = function(playerObject)
{
	var quickTimeHeight = playerObject.height + playerObject.quickTimeHeightDifference;
	playerObject.playerParams =
		{
			controller:"true",
			autostart:"true",
			data:playerObject.url
		}	
					
	switch(playerObject.isSafari)
	{
		case true://remove when Safari is fixed ha.. ha..
			playerElement = "<object id='" + playerObject.id + "' " +
				"height='" + quickTimeHeight + "' " +
				"width='" + playerObject.width + "' " +
				"controller='" + playerObject.playerParams.controller + "' " +
				"autoplay='" + playerObject.playerParams.autostart + "' " +
				"type='video/quicktime' " +
				"style='display:block' " +
				"EnableJavaScript='true' " +
				"data='" + playerObject.url + "' " +
				"qtsrc='" + playerObject.url + "'/>";
		break;
		
		default://handle Mozilla browsers
		playerElement = document.createElement("object");
		playerElement.setAttribute("id", playerObject.id);
		playerElement.setAttribute("width", playerObject.width);
		playerElement.setAttribute("height", quickTimeHeight);
		playerElement.setAttribute("style", "display:block");
		for(param in playerObject.playerParams)
		{
			playerElement.setAttribute(param, playerObject.playerParams[param]);
		}
		playerElement.setAttribute("type", "video/quicktime");//must be set last
	}
	
	return playerElement;
}

VideoPlayer.buildPlaylistQueryString = function(playlist, adSlotsArray)//added adSlotsArray 6-20-06
{
	if(playlist[0] != "")
	{
		var queryString = "";
		for(var i = 0 ; i < playlist.length; i++)
		{
			if(i != 0) queryString += "&";
			queryString += "clip=" + playlist[i];
		}
		
		if(adSlotsArray != null)
		{
			for(var i = 0 ; i < adSlotsArray.length; i++)
			{
				queryString += "&";
				queryString += "ad=" + adSlotsArray[i];
			}
		}
		
		return queryString;
	}
	return null;
}