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>");
}

/New VideoControl Object
function VideoControl(videoPlayer)
{
	this.selectControls = new Object();
	this.videoPlayer = videoPlayer;
}

VideoControl.prototype.addSelect = function(selectControl)
{
	selectControl.videoControl = this;
	this.selectControls[selectControl.id] = selectControl;
}

VideoControl.prototype.addVideoListControl = function(videoListControl)
{
	this.videoListControl = videoListControl;
	this.videoListControl.videoControl = this;
}
		
VideoControl.prototype.populateSelectControl = function(selectControl,optionArray, defaultText)
{
	optionArray = optionArray.sort();//sort select list A-Z
	if(defaultText) selectControl.addOption(null, defaultText)
	for(var i = 0; i < optionArray.length; i++)
	{
		selectControl.addOption(optionArray[i], optionArray[i]);
	}
}

VideoControl.prototype.useLibrary = function(videoLibrary)
{
	this.videoLibrary = videoLibrary;
	this.videoLibrary.videoControl = this;
}

VideoControl.prototype.playVideo = function(fileName)
{
	this.currentVideo = videoLibrary.getVideo(fileName);
	var video = [this.currentVideo.filePath + "/" + this.currentVideo.fileName];//['sexshow/SEX00113_2']
	
	var trailer = videoLibrary.getTrailer(this.currentVideo);
	if(trailer) video.push(trailer);
	/******************** BEGIN Handling Preroll *******************************/
	var currentIntervalCountdown = (videoLibrary.prerollIntervalManager && this.currentVideo.preroll) ? videoLibrary.prerollIntervalManager[this.currentVideo.preroll.bucketName] : null ;
	if(currentIntervalCountdown != null)
	{
		if(currentIntervalCountdown == this.currentVideo.preroll.interval)//play preroll
		{
			var preroll = videoLibrary.getPreroll(this.currentVideo);
			if(preroll) video.unshift(preroll);
			if(this.currentVideo.preroll.interval != 0) videoLibrary.prerollIntervalManager[this.currentVideo.preroll.bucketName]--;
		}
		else if(currentIntervalCountdown >= 0)//don't play preroll
		{
			if(currentIntervalCountdown == 0)
			{
				videoLibrary.prerollIntervalManager[this.currentVideo.preroll.bucketName] = this.currentVideo.preroll.interval;
			}
			else
			{
				videoLibrary.prerollIntervalManager[this.currentVideo.preroll.bucketName]--;
			}
		}
	}
	/******************** END Handling Preroll Preroll *******************************/
	
	//alert(this.currentVideo.showCode);
	//begin ad test
	//alert(this.currentVideo.adSlots.length);//adslots property is an array
	//end ad test
	this.videoPlayer.createPlayer(video, this.currentVideo.adSlots);
	this.displayVideoInfo(this.currentVideo);
}

VideoControl.prototype.clearVideoInfo = function(parentElement)
{
	while(parentElement.firstChild)
	{
		parentElement.removeChild(parentElement.firstChild);
	}
}

VideoControl.prototype.displayVideoInfo = function(videoObj)
{
	//info gets written to --- this.showInfoParentBlock
	//determine if showpage is null or showName is null and don't overwrite current promo
	this.clearVideoInfo(this.showInfoParentBlock);
	if(videoObj.showName)
	{
		var showNameElement = document.createElement("DIV");
		showNameElement.setAttribute("id", "nowPlayingBlock");
		var showNameLabel = document.createElement("SPAN");
		showNameLabel.setAttribute("id", "nowPlayingLabel");
		var showNameLabelText = document.createTextNode("Now Playing: ");
		showNameLabel.appendChild(showNameLabelText);
		showNameElement.appendChild(showNameLabel);
		var showNameText = document.createTextNode(videoObj.showName);
		showNameElement.appendChild(showNameText);
		
		var airingElement = document.createElement("DIV");
		airingElement.setAttribute("id", "airingBlock");
		var airingLabel = document.createElement("SPAN");
		airingLabel.setAttribute("id", "airingLabel");
		var airingLabelText = document.createTextNode("Next Airing: ");
		airingLabel.appendChild(airingLabelText);
		airingElement.appendChild(airingLabel);
		var airingText = (videoObj.airing) ? document.createTextNode(videoObj.airing) : document.createTextNode("No current schedule information");
		airingElement.appendChild(airingText);
		
		var showPageElement = document.createElement("DIV");
		showPageElement.setAttribute("id", "showPageBlock");
		var showPageLink = document.createElement("A");
		showPageLink.setAttribute("href", videoObj.showPage);			
		showPageLink.onclick = function()
		{
			var url = videoObj.showPage;
			if(window.opener) window.opener.location = url;
			else
			{
				launchPopup({url:url, name:"main", features:"top=10,left=10,height=800,width=800,location=yes,menubar=yes,resizable=no,scrollbars=no,status=yse,toolbar=yes"});
				PopupManager.mainWindow.focus();
			}
			
			if (Sys.Browser.agent == Sys.Browser.InternetExplorer)
				event.returnValue = false;
			
			return false;
		}
		var showPageLinkText = document.createTextNode("Get More " + videoObj.showName);
		showPageLink.appendChild(showPageLinkText);
		showPageElement.appendChild(showPageLink);
		
		this.showInfoParentBlock.appendChild(showNameElement);
		this.showInfoParentBlock.appendChild(airingElement);
		this.showInfoParentBlock.appendChild(showPageElement);
	}
	else
	{
		var schedulePromoElement = document.createElement("DIV");
		schedulePromoElement.setAttribute("id", "schedulePromoBlock");
		var schedulePromoLabel = document.createElement("SPAN");
		schedulePromoLabel.setAttribute("id", "schedulePromoLabel");
		var schedulePromoLabelText = document.createTextNode("Oh! TV: ");
		schedulePromoLabel.appendChild(schedulePromoLabelText);
		schedulePromoElement.appendChild(schedulePromoLabel);
		var schedulePromoTextBegin = document.createTextNode("Visit the ");
		schedulePromoElement.appendChild(schedulePromoTextBegin);
		var schedulePromoLink = document.createElement("A");
		schedulePromoLink.setAttribute("href", "");
		schedulePromoLink.onclick = function()
		{
			var url = "/schedule/";
			if(window.opener) window.opener.location = url;
			else
			{
				launchPopup({url:url, name:"main", features:"top=10,left=10,height=800,width=800,location=yes,menubar=yes,resizable=no,scrollbars=no,status=yse,toolbar=yes"});
				PopupManager.mainWindow.focus();
			}
			if (Sys.Browser.agent == Sys.Browser.InternetExplorer)
				event.returnValue = false;
				
			return false;
		}
		schedulePromoElement.appendChild(schedulePromoLink);
		
		var schedulePromoLinkText = document.createTextNode("Oxygen schedule");
		schedulePromoLink.appendChild(schedulePromoLinkText);
		
		
		var schedulePromoTextEnd = document.createTextNode(" to see what\'s on this week");
		schedulePromoElement.appendChild(schedulePromoTextEnd);
		this.showInfoParentBlock.appendChild(schedulePromoElement);
	}
}

VideoControl.prototype.initialize = function(showInfoBlockId)
{
	this.showInfoParentBlock = document.getElementById(showInfoBlockId);
	var queryString = document.location.search.substr(1);
	
	if(queryString != "")//create criteriaObj from query string
	{
		criteriaObj = new Object();
		queryString = (queryString.charAt(0) != "&") ? queryString : queryString.substr(1);
		var pairsArray = queryString.split("&");
		for(var i=0; i < pairsArray.length; i++)
		{
			var tempArray = pairsArray[i].split("=");
			var key = tempArray[0];
			var value = tempArray[1];
			criteriaObj[key] = value;
		}
	}
	else
	{
		//populate list with show or topic
		criteriaObj = {category:"showName", value:"Girls Behaving Badly"};
	}
	
	var videoArray = this.videoLibrary.getVideoArray(criteriaObj);
	if(videoArray != null)
	{
		var videoElements = this.videoListControl.loadVideoList(videoArray, criteriaObj.category);
		//highlite first video on list
		this.videoListControl.highliteSelection(videoElements[0]); 
	}
	
	if(criteriaObj.fileName)
	{
		//setSelectControls
		this.selectControls[criteriaObj.category].setAsSelected(this.videoLibrary.getVideo(criteriaObj.fileName)[criteriaObj.category]);
		//playVideo
		this.playVideo(criteriaObj.fileName);
	}
}

//New VideoControl Object

//New SelectControl Object
function SelectControl(controlId, parentBlockId, onchangeFunction)
{
	this.id = controlId;
	this.parentBlockId = parentBlockId;
	this.selectElement = document.createElement("SELECT");
	this.selectElement.setAttribute("name", this.id);
	this.selectElement.setAttribute("id", this.id);
	document.getElementById(this.parentBlockId).appendChild(this.selectElement);
	this.options = new Array();
	var self = this;
	this.selectElement.onchange = function()
	{
		self.currentSelection = self.getCurrentSelection();
		onchangeFunction(self);
	}
}

SelectControl.prototype.getCurrentSelection = function()
{
	currentSelection = this.selectElement.options[this.selectElement.selectedIndex].value;
	return currentSelection;
}

SelectControl.prototype.reset = function()
{
	this.selectElement.selectedIndex = 0;
}
				
SelectControl.prototype.addOption = function(value, text)
{
	optionElement = document.createElement("OPTION");
	optionElement.setAttribute("value", value);
	optionElement.innerHTML = text;
	document.getElementById(this.id).appendChild(optionElement);
	this.options.push(optionElement);
}

SelectControl.prototype.setAsSelected = function(arg)//selectedIndex or valueString
{
	if(typeof arg == "string")
	{
		var valueString = arg;
		for(var i = 0; i < this.options.length; i++)
		{
			if(this.options[i].value == valueString) this.selectElement.selectedIndex = i;
		}
	}
	else if(typeof arg == "number")
	{
		var selectedIndex = arg;
		if(selectedIndex < this.options.length) this.selectElement.selectedIndex = selectedIndex;
	}
}
//New SelectControl Object

//New VideoLibrary Object
function VideoLibrary(xmlRequest)
{
	this.videoInfoObj = eval(xmlRequest.responseText);
	this.videos = this.videoInfoObj.videos;
	this.trailerInfo = this.videoInfoObj.trailerInfo;
	this.addTrailerInfo();
	/********************** BEGIN Preroll *******************************/
	this.prerollInfo = this.videoInfoObj.prerollInfo;
	if(this.prerollInfo)
	{
		this.createPrerollIntervalManager();
		this.addPrerollInfo();
	}
	/********************** BEGIN Preroll *******************************/
	this.showInfo = this.videoInfoObj.showInfo;
	this.addShowInfo();
}

VideoLibrary.prototype.createPrerollIntervalManager = function()
{
	this.prerollIntervalManager = new Object();
	for(bucket in this.prerollInfo)
	{
		this.prerollIntervalManager[bucket] = this.prerollInfo[bucket].interval;
	}
}

VideoLibrary.prototype.addShowInfo = function()
{
	for(var i = 0; i < this.videos.length; i++)
	{
		if(this.videos[i].showCode)//check for showCode
		{
			var showInfoObj = this.getShowInfo(this.videos[i].showCode);
			for(prop in showInfoObj)
			{
				this.videos[i][prop] = showInfoObj[prop];
			}
		}	
	}	
}

VideoLibrary.prototype.getShowInfo = function(showCode)
{
	for(code in this.showInfo)
	{
		if(code == showCode) return this.showInfo[code];
	}
}

VideoLibrary.prototype.addTrailerInfo = function()
{
	for(var i = 0; i < this.videos.length; i++)
	{
		if(this.videos[i].topic)//check for topic
		{
			var trailerInfoObj = this.getTrailerInfo(this.videos[i].topic);
			if(trailerInfoObj) this.videos[i].trailer = new Object();
			for(prop in trailerInfoObj)
			{
				this.videos[i].trailer[prop] = trailerInfoObj[prop];
			}
		}	
	}	
}

VideoLibrary.prototype.getTrailerInfo = function(topicName)
{
	for(topic in this.trailerInfo)
	{
		if(topic == topicName) return this.trailerInfo[topic];
	}
	return null;
}

VideoLibrary.prototype.addPrerollInfo = function()
{
	for(var i = 0; i < this.videos.length; i++)
	{
		if(this.videos[i].topic)//check for topic
		{
			//look in bucket.topic array for video topic then attach bucket props to video
			var bucketInfoObj = this.getBucketInfo(this.videos[i].topic);
			if(bucketInfoObj) this.videos[i].preroll = new Object();
			for(prop in bucketInfoObj)
			{
				this.videos[i].preroll[prop] = bucketInfoObj[prop];
			}
		}
	}	
}

VideoLibrary.prototype.getBucketInfo = function(topicName)//movies, sex, etc
{
	for(bucket in this.prerollInfo)//buckets
	{
		var topicArray = this.prerollInfo[bucket].topic;
		for(topic in topicArray)
		{
			if(topicName == topicArray[topic])
			{
				this.prerollInfo[bucket].bucketName = bucket;
				return this.prerollInfo[bucket];
			}	
		}
	}
	return null;
}
	
VideoLibrary.prototype.handleSelectChange = function(selectControl)
{
	var selectControls = selectControl.videoControl.selectControls;
	for(control in selectControls)
	{
		if(selectControls[control].id != selectControl.id) selectControls[control].reset();
		else
		{
			var videoArray = videoLibrary.getVideoArray({category:selectControls[control].id, value:selectControl.getCurrentSelection()});
			var selectedCategory = selectControls[control].id
		}	
	}
	if(videoArray != null) this.videoListControl.loadVideoList(videoArray, selectedCategory);
}

VideoLibrary.prototype.getPropertyArray = function(property)
{
	propertyArray = new Array();
	for(var i = 0; i < this.videos.length; i++)
	{
		var videoProperty = this.videos[i][property];
		if(videoProperty && (propertyArray.indexOf(videoProperty) == -1)) propertyArray.push(videoProperty);
	}
	return propertyArray;
}

VideoLibrary.prototype.getVideo = function(fileName)
{
	for(var i = 0; i < this.videos.length; i++)
	{
		if(this.videos[i].fileName == fileName) return this.videos[i];
	}
	alert("video: " + fileName + " was not found in Video Library");
}

VideoLibrary.prototype.getTrailer = function(videoObj)
{
	trailerObj = videoObj.trailer;
	trailer = (trailerObj) ? trailerObj.filePath  + "/" + trailerObj.fileName : null;
	return trailer;
}

VideoLibrary.prototype.getPreroll = function(videoObj)
{
	prerollObj = videoObj.preroll;
	preroll = (prerollObj) ? prerollObj.filePath  + "/" + prerollObj.fileName : null;
	return preroll;
}

VideoLibrary.prototype.getVideoArray = function(criteriaObj)//{category:topic, value:featured} or {fileName:BLS00301_1, category:showName}
{
	var fileName = (criteriaObj.fileName) ? criteriaObj.fileName : null;
	var category = (criteriaObj.category) ? criteriaObj.category : null;
	var selectedVideo = null;
	var videoArray = new Array();
	if(fileName)
	{
		var video = videoLibrary.getVideo(fileName);
		var value = video[category];	
	}
	else var value = (criteriaObj.value) ? criteriaObj.value : null;
	if(value)//check for null
	{
		for(var i = 0; i < this.videos.length; i++)
		{
			if(this.videos[i][category] == value)
			{
				if(this.videos[i].fileName == fileName)
				{
					selectedVideo = this.videos[i];//pullout selectedVideo
				}
				else
				{
					videoArray.push(this.videos[i]);//add all but selectedVideo
				}
			}
		}
		//now sort and make slectedVideo appear 1st on list
		if(selectedVideo) videoArray.unshift(selectedVideo);
	}
		
	return (videoArray.length == 0) ? null : videoArray;
}
//End VideoLibrary Object


function VideoListControl(controlId, parentBlockId)
{
	this.videoLibrary = videoLibrary;
	this.id = controlId;
	this.parentBlockId = parentBlockId;
	this.divElement = document.createElement("DIV");
	this.divElement.setAttribute("id", this.id);
	this.divElement.style.overflow = "auto";
	document.getElementById(this.parentBlockId).appendChild(this.divElement);
	this.videoElements = new Array();
}

VideoListControl.prototype.clearVideoList = function()
{
	this.videoElements.splice(0, this.videoElements.length);//delete array elements
	var parentElement = this.divElement;
	while(parentElement.firstChild)
	{
		parentElement.removeChild(parentElement.firstChild);
	}
}

VideoListControl.prototype.loadVideoList = function(videoArray, categoryString)
{
	this.clearVideoList();
	//add Topic Heading or Show Heading to list
	var headerElement  = document.createElement("DIV");
	headerElement.setAttribute("id","videoListHeader");
	if(categoryString == "showName")
	{
		var showNameString = videoArray[0].showName
		var headerElementText = document.createTextNode("Watch these video clips from " + showNameString);
	}
	else if(categoryString == "topic")
	{
		var topicString = videoArray[0].topic
		var headerElementText = document.createTextNode("Watch these video clips about " + topicString);
	}
	headerElement.appendChild(headerElementText);
	this.divElement.appendChild(headerElement);		
	// add all videos that meet criteria to video list
	for(var i = 0; i < videoArray.length; i++)
	{
		var videoElement = this.addVideoToList(videoArray[i]);
		this.videoElements.push(videoElement);
		this.divElement.appendChild(videoElement);
	}
	return this.videoElements;
}

VideoListControl.prototype.addVideoToList = function(video)
{
	var self = this;
	var videoElementBlock = document.createElement("DIV");
	videoElementBlock.className = "videoElementBlock";
	videoElementBlock.id = "videoElementBlock_" + this.videoElements.length;
	var videoLink = document.createElement("A");
	videoLink.href = "";
	videoLink.onclick = function()
	{
		//highlite selection and unhighlite previous selection
		self.highliteSelection(this.parentNode);
		self.videoControl.playVideo(video.fileName);
		
		if (Sys.Browser.agent == Sys.Browser.InternetExplorer)
			event.returnValue = false;
		
		return false;
	}
	var videoTitle = document.createTextNode(video.title + ":");
	var videoDescription = document.createTextNode(" " + video.description);
	videoLink.appendChild(videoTitle);
	videoElementBlock.appendChild(videoLink);
	videoElementBlock.appendChild(videoDescription);
	return videoElementBlock;
}

VideoListControl.prototype.highliteSelection = function(videoElementBlock)
{
	for(var i = 0; i < this.videoElements.length; i++)
	{
		var spaceIndex = this.videoElements[i].className.indexOf(" ");
		if(spaceIndex > -1) this.videoElements[i].className = this.videoElements[i].className.substring(0,spaceIndex);
	}
	videoElementBlock.className += " selectedVideo";
}