/* Blue River Digital API - Video Player **********************************
 *	
 *	This API contains all the functions relating to the operation of the
 *	video player.
 *
 ***************************************************************************/
 
 // Gloabals *******************************************************************
 
 	var currentVideo = "";
	var currentVideoBtn = ""
	var currentVideoBtnHover = null;
	var currentSeries = new Array();
	var currentSeriesPosition = 0;
	
 // On Load Defaults ************************************************************
 
 	addLoadEvent(setVideoThumbEvents);
 
 /*  Video Thumb Events  * *******************************************************
 *
 *	TAKES:		NOTHING
 * 	RETURNS:	NOTHING
 *	NOTE:		Sets up the button actions for the video index.
 *	
 *************************************************************************/

	function setVideoThumbEvents()
	{
		var thumbElement = "";
		var btnArray = document.getElementsByTagName("div");
		var regExp		= /^videoBtn/; 
		var btnExists 	= false;
		
		for(var ctr=0; ctr<btnArray.length; ctr++)
		{
			if ((btnArray[ctr].id).search(regExp) != -1)
			{
				btnExists = true;
				thumbElement = btnArray[ctr];
				
				if (thumbElement)
				{
					thumbElement.onmouseover = function()
					{
						this.style.cursor = "pointer";
						this.style.backgroundColor = "#888888";
						
						if ((currentVideoBtnHover != null) & (currentVideoBtnHover != this))
						{
							currentVideoBtnHover.style.cursor = "";
							currentVideoBtnHover.style.backgroundColor = "";
						}
						
						currentVideoBtnHover = this;
					}
					
					thumbElement.onmouseout = function()
					{
						if ((this.id != currentVideoBtn) & (currentVideoBtnHover != this) & IE)
						{
							this.style.cursor = "";
							this.style.backgroundColor = "";
						}
						else if (!IE)
						{
							this.style.cursor = "";
							this.style.backgroundColor = "";
						}
						
					}
					
				}
				
			} // End btnArray contains regExp
			
		} // End For
		
		if (btnExists)
		{
			if (IE)
			{
				addMouseMoveEvent(checkVideoBtnHover);
			}
		}
		
	}

 /*  Check Video Button Hover  * *******************************************************
 *
 *	TAKES:		NOTHING
 * 	RETURNS:	NOTHING
 *	NOTE:		Needed for IE to avoid background flickering on child element hover.
 *	
 *************************************************************************/
 
	function checkVideoBtnHover()
	{
		if (currentVideoBtnHover != null)
		{
			if (!containsMousePoint(currentVideoBtnHover))
			{
				currentVideoBtnHover.style.cursor = "";
				currentVideoBtnHover.style.backgroundColor = "";
				currentVideoBtnHover = null;
			}
		}
	}
	
 /*  Load Video Player  * *******************************************************
 *
 *	TAKES:		Element Id where the video will be displayed, Video File Url, 
 *				Callback Function
 * 	RETURNS:	NOTHING
 *	NOTE:		Using SWFObject, it embeds the Flash video player in a DOM 
 *				object and sets the default video and callback function.
 *				The callback function is called once the video is finished
 *				playing.
 *	
 *************************************************************************/
 
	function loadVideoPlayer(playerId, defaultVideoUrl, videoCallbackFunction)
	{
		if (getElement(playerId))
		{
			var flashvars =
			{ 
				defaultVideo: defaultVideoUrl,
				videoCallback: videoCallbackFunction
			};
			
			var params =
			{
				menu: "false",
				wmode: "transparent"
			};
			
			var attributes =
			{
				id: "myDynamicContent",
				name: "myDynamicContent"
			};
			
			swfobject.embedSWF("/imageUploads/videos/videoTest.swf", playerId, "322", "272", "9.0.0","expressInstall.swf", flashvars, params, attributes);	
			currentSeriesPosition = 1;
		}
	}
	
 /*  Load Custom Video Player  * *******************************************************
 *
 *	TAKES:		Element Id where the video will be displayed, Video File Url, 
 *				Callback Function
 * 	RETURNS:	NOTHING
 *	NOTE:		Using SWFObject, it embeds the Flash video player in a DOM 
 *				object and sets the default video and callback function.
 *				The callback function is called once the video is finished
 *				playing.
 *	
 *************************************************************************/
 
	function loadCustomVideoPlayer(playerId, swfUrl, swfWidth, swfHeight, flashVars)
	{
		if (getElement(playerId))
		{
			var params =
			{
				menu: "false",
				wmode: "transparent",
				play: "true"
			};
			
			var attributes =
			{
				id: "myDynamicContent",
				name: "myDynamicContent"
			};
			
			swfobject.embedSWF(swfUrl, playerId, swfWidth, swfHeight, "9.0.0","expressInstall.swf", flashVars, params, attributes);	
			currentSeriesPosition = 1;
		}
	}

 /*  Load Video * *******************************************************
 *
 *	TAKES:		Video File Url 
 * 	RETURNS:	NOTHING
 *	NOTE:		Calls the Flash function "loadVideo" and sends it a video
 *				file to play. "loadVideo" is defined in the Flash object
 *				using ActionScript 3.0.
 *	
 *************************************************************************/
 
	function loadVideo(videoUrl)
	{
		getElement("myDynamicContent").loadVideo(videoUrl);	
	}

 /*  Video Button Click Actions * *****************************************
 *
 *	TAKES:		Video Button Element, Video File Url
 * 	RETURNS:	NOTHING
 *	NOTE:		Used to build a universal interface for a video player index.
 *				Assumes that videoBtnElement being passed in part of the video
 *				index. After changing the button attributes it loads video file
 *				passed to it.
 *	
 *************************************************************************/
 
	function videoBtnClickActions(videoBtnElement, videoUrl)
	{
		getElement(currentVideoBtn).style.backgroundColor = "";
		currentVideoBtn = videoBtnElement.id;
		getElement(currentVideoBtn).style.backgroundColor = "#888888";
		//alert(videoUrl);
		loadVideo(videoUrl);
	}

 /*  Set Default Video Btn * *****************************************
 *
 *	TAKES:		Video Button Element
 * 	RETURNS:	NOTHING
 *	NOTE:		Used to build a universal interface for a video player index.
 *				Assumes that videoBtnElement being passed in part of the video
 *				index, and is the button associated with the default video
 *				for the Flash player.
 *	
 *************************************************************************/
 
	function setDefaultVideoBtn(videoBtnElement)
	{
		currentVideoBtn = videoBtnElement.id;
		getElement(currentVideoBtn).style.backgroundColor = "#888888";
	}

 /*  Video Series Play Next * *****************************************
 *
 *	TAKES:		NOTHING
 * 	RETURNS:	NOTHING
 *	NOTE:		Plays the next video in the series.  Requires that the
 *				a video series exists. Also, handles the video button 
 *				view.
 *	
 *************************************************************************/
 
	function videoSeriesPlayNext()
	{
		if (currentSeries.length > 0)
		{
			if (currentSeriesPosition >= currentSeries.length)
			{
				closeVideoPopup();	
			}
			
			var nextVideoArray = currentSeries[currentSeriesPosition];
			var nextVideoFile = nextVideoArray[0];
			var nextVideoBtn = nextVideoArray[1];
			//alert(nextVideoBtn);
			currentSeriesPosition = currentSeriesPosition + 1;
			
			if (currentVideoBtn != "")
			{
				getElement(currentVideoBtn).style.backgroundColor = "";
				currentVideoBtn = "videoBtn" + nextVideoBtn;
				getElement(currentVideoBtn).style.backgroundColor = "#888888";
			}
			
			loadVideo(nextVideoFile);
		}
	}

 /*  Add To Current Series * *****************************************
 *
 *	TAKES:		Video Array: [0]: video file url, [1]: database videoId
 * 	RETURNS:	NOTHING
 *	NOTE:		Adds a video record to the currentSeries array.
 *	
 *************************************************************************/
 
	function addToCurrentSeries(videoArray)
	{
		if (videoArray.isArray)
		{
			if (currentSeries.length > 0)
			{
				currentSeries[currentSeries.length] = videoArray;
			}
			else
			{
				currentSeries[0] = videoArray; 
			}
			
		}
	}

 /*  Alert Current Series * *****************************************
 *
 *	TAKES:		NOTHING
 * 	RETURNS:	NOTHING
 *	NOTE:		Used for debugging. Creates a popup window with the contents
 *				of the currentSeries array.
 *	
 *************************************************************************/
 
	function alertCurrentSeries()
	{
		var temp = "";
		var arrayTemp = "";
		
		for(ctr=0; ctr<currentSeries.length; ctr++)
		{
			arrayTemp = currentSeries[ctr];
			temp = temp + arrayTemp[0] + ", " + arrayTemp[1] + "\n";	
		}
		alert(temp);
	}

 /*  Get Video Series * ******************************************************
 *
 *	TAKES:		Video Series Tag
 * 	RETURNS:	NOTHING
 *	NOTE:		Requires that video series accessors have already been set up
 *				on the calling page (ref. videoSeriesSetup, videoSeriesPopupSetup).
 *				Uses the accessors to build the video array records that are
 *				added to the currentSeries array.
 *	
 ****************************************************************************/
 
	function getVideoSeries(videoSeriesTag)
	{
		var seriesAccessors = getTagGroup("input", videoSeriesTag);
		var videoElement = "";
		var videoIdArray = "";
		var currentId = "";
		var prevId = "";
		var videoFile = "";
		var videoCtr = 0;
		var currentFile = "";
		
		for(ctr=0; ctr<seriesAccessors.length; ctr++)
		{
			videoElement = seriesAccessors[ctr];
			videoIdArray = videoElement.id.split("_");
			
			currentId = videoIdArray[1];
				
			switch(videoIdArray[2])
			{
				case "File":
				
					currentFile = videoElement.value;
					addToCurrentSeries(new Array(currentFile, currentId));
					
					break;
			}
			
			prevId = currentId
		}
		
		//alertCurrentSeries()
	}

 /*  Video Series Popup Setup * ******************************************************
 *
 *	TAKES:		Setup Element Id, Series Title, Series Tag (from database)
 * 	RETURNS:	NOTHING
 *	NOTE:		Sets up the video series accessors for use in the getVideoSeries
 *				and other functions that need information about hte video series.
 *				The Setup Element Id is where the accessors will be placed on the
 *				calling page.
 *	
 ************************************************************************************/
 
	function videoSeriesPopupSetup(setupElementId, seriesTitle, seriesTag)
	{
		setAjaxUrl(setupElementId, "/includes/ajaxVideoPopupAcessors.asp?seriesTag=" + seriesTag, false, "videoSeriesPopup('" + seriesTitle + "', '" + seriesTag + "');");
	}

 /*  Video Series Setup * ******************************************************
 *
 *	TAKES:		Setup Element Id, Series Tag (from database)
 * 	RETURNS:	NOTHING
 *	NOTE:		Sets up the video series accessors for use in the getVideoSeries
 *				and other functions that need information about hte video series.
 *				The Setup Element Id is where the accessors will be placed on the
 *				calling page.
 *	
 ************************************************************************************/
 
	function videoSeriesSetup(setupElementId, seriesTag)
	{
		setAjaxUrl(setupElementId, "/includes/ajaxVideoPopupAcessors.asp?seriesTag=" + seriesTag, false, "getVideoSeries('" + seriesTag + "');");
	}

 /*  Close Video Popup * ******************************************************
 *
 *	TAKES:		NOTHING
 * 	RETURNS:	NOTHING
 *	NOTE:		Calls the "stopVideo" function is defined in the Flash object
 *				using ActionScript 3.0. Closes the popup pane.
 *	
 ************************************************************************************/
 
	function closeVideoPopup()
	{
		getElement("myDynamicContent").stopVideo();
		restorePageBackground();	
	}
	
/* Video Series Popup * *******************************************************
 *
 *	TAKES:		Video Series Title, Video File
 * 	RETURNS:	NOTHING
 *	NOTE:		Requires the use of the ASP function videoSeriresPopupSetup(seriesTag).
 *				Generates a page-centered video popup, with a shaded background. Not
 *				using the ActiveX filseSystemObject for security reasons. 
 *
 ***********************************************************************************/
 
	function videoSeriesPopup(videoSeriesTitle, videoSeriesTag)
	{
		currentSeries = new Array();
		shadePageBackground("#000000", 70);
		glassElement.style.display = "none";
		showLoadingGraphic();
		
		content = "<table cellpadding=\"0\" cellspacing=\"0\">";
		content = content + "<tr><td background=\"/images/shadowLeftUp.png\" height=\"9\"></td><td background=\"/images/shadowUp.png\" height=\"9\"></td><td background=\"/images/shadowRightUp.png\" height=\"9\"></td></tr>";
		content = content + "<tr><td background=\"/images/shadowLeft.png\" width=\"9\"></td><td height=\"45\" style=\"border: solid 1px #666666; background-color: #EEEEEE\">";
		content = content + "	<div style=\"padding-left: 15px; padding-right: 15px; background-image: url(http://www.blueriverdigital.com/images/videoPopupPlayerHeader.jpg); background-repeat: repeat-x; border-bottom: solid; border-bottom-width: 1px; border-bottom-color: #333333;\">";
		content = content + "	<table width=\"100%\"><tr>";
		content = content + "		<td><font color=\"#FFFFFF\"><b>" + videoSeriesTitle + "</b></td>";
		content = content + "		<td align=\"right\" valign=\"center\"><span onMouseOver=\"this.style.cursor='pointer'; getElement('closeBtn').src='/images/closeBtnHover.png';\" onMouseOut=\"getElement('closeBtn').src='/images/closeBtn.png';\" onClick=\"closeVideoPopup();\">";
        content = content + "			<img src=\"/images/closeBtn.png\" id=\"closeBtn\" border=\"0\"/>";
        content = content + "		</span></td>";
		content = content + "	</tr></table>";
		content = content + "	</div>";
		content = content + "	<table cellpadding=\"0\" cellspacing=\"0\">";
		content = content + "	<tr valign=\"top\">";
		content = content + "		<td style=\"padding: 10px; background-image: url('/images/lightboxMediaBackground.jpg'); background-repeat: repeat-x;\">";
		content = content + "			<div id=\"videoPlayer\" style=\"margin: 10px; padding: 15px; border: solid; border-width: 1px; border-color: #AAAAAA; background-color: #FFFFFF;\" align=\"center\">";
		content = content + "				Video Not Found";
		content = content + "			</div>";
		content = content + "		</td>";
		content = content + "		<td style=\"padding: 10px; background-image: url('/images/lightboxMediaIndexBackground.jpg'); background-repeat: repeat-x;\">";
		content = content + "			<div style=\"font-family: Arial; font-size: 12px; font-weight: bold; color: #FFFFFF; padding-left: 10px;\">";
		content = content + "				Video Index";
		content = content + "				<div class=\"videoSeriesBtnContainer\">";
		
		var seriesAccessors = getTagGroup("input", videoSeriesTag);
		var videoElement = "";
		var videoIdArray = "";
		var currentId = "";
		var prevId = "";
		var videoFile = "";
		var videoCtr = 0;
		var currentFile = "";
		var currentThumb = "";
		var currentTitle = "";
		var currentDescr = "";
		
		prevId = "";
		
		for(ctr=0; ctr<seriesAccessors.length; ctr++)
		{
			videoElement = seriesAccessors[ctr];
			videoIdArray = videoElement.id.split("_");
			
			currentId = videoIdArray[1];
				
			if ( (currentId != prevId) & (currentId != "") & (prevId != "") || (ctr == seriesAccessors.length-1) )
			{
				videoCtr = videoCtr + 1;
				
				content = content + "<div id=\"videoBtn" + prevId + "\" class=\"videoBtnContainer\" onClick=\"videoBtnClickActions(this, '" + currentFile + "'); currentSeriesPosition=" + videoCtr + ";\">";
                content = content + "<table cellpadding=\"0\" cellspacing=\"0\">";
				content = content + "<tr valign=\"top\">";
				content = content + "<td>";
				content = content + "<img src=\"" + currentThumb + "\" class=\"videoBtnThumb\"/>";
				content = content + "</td>";
				content = content + "<td class=\"videoBtnText\">";
				content = content + "<div class=\"featureTitle\">" + currentTitle + "</div>";
				content = content + "<div class=\"featureText\">" + currentDescr + "</div>";
				content = content + "</td>";
				content = content + "</tr>";
				content = content + "</table>";
				content = content + "</div>";
			}
			
			switch(videoIdArray[2])
			{
				case "File":
				
					currentFile = videoElement.value;
					addToCurrentSeries(new Array(currentFile, currentId));
					
					if (videoFile == "")
					{
						videoFile = videoElement.value;
						currentVideoBtn = "videoBtn" + currentId;
					}
					
					break;
				
				case "Thumbnail":
					
					currentThumb = videoElement.value;
					
					break;
					
				case "Title":
				
					currentTitle = videoElement.value;
				
					break;
					
				case "Descr":
				
					currentDescr = videoElement.value;
				
					break;
			}
			
			prevId = currentId
		}
		
		content = content + "				</div>";
		content = content + "			</div>";
		content = content + "		</td>";
		content = content + "	</tr>";
		content = content + "	</table>";
		content = content + "</td><td background=\"/images/shadowRight.png\" width=\"9\"></td></tr>";
		content = content + "<tr><td background=\"/images/shadowLeftDown.png\" height=\"9\"></td><td background=\"/images/shadowDown.png\" height=\"9\"></td><td background=\"/images/shadowRightDown.png\" height=\"9\"></td></tr>";
		content = content + "</table>";
		
		glassElement.innerHTML = content;
		hideLoadingGraphic();
		glassElement.style.display = "block";
		updateShadedPopupPosition(glassElement);
		
		setVideoThumbEvents();
		loadVideoPlayer("videoPlayer", videoFile, "videoSeriesPlayNext");
		getElement(currentVideoBtn).style.backgroundColor = "#888888";
		
		updateShadedPopupPosition(glassElement);
	}
	
	