/********************************************
 * 
 * 
 * 
 *
 * 	Detect Prototype...load prototype as necessary
 * 
 * HanfordVideo Object
 * 	displayVideo()
 * 	createOverlay()
 * 	HideOverlay()
 * 	ShowOverlay()
 * 
 * 
 * Dependency:
 * 	ProtoType
 * 	swfobject
 */
if ( ! window.Prototype)	
	document.write("<script src='c.cfm/video/scripts/prototype.js'></script>");
if ( ! window.swfobject )
	document.write("<script src='c.cfm/video/scripts/swfobject.js'></script>");
//document.write('<link href="c.cfm/video/style/body.css")" type="text/css" rel="stylesheet">');	
	
if (! window.HanfordVideo) {
	HanfordVideo = {
		/****************************************
		* Class Level Variables
		*
		*
		*/
		videoOpen : false,
		videoConst: 'hanfordVideo',
		overlayObjectID: this.videoConst + '_overlay_' + new Date().getTime(), 
		videoElementID: this.videoConst + '_vidContainer_' + new Date().getTime(), 
		videoData: 'c.cfm/video/cfc/dataAccess.cfc?method=getVideoPage',
		relatedVideoData: 'c.cfm/video/cfc/dataAccess.cfc?method=getDataPage',
		loadingContent: '<img src="c.cfm/Video/images/spinner.gif">',
		videoTemplate: '<table class="imageBorder" cellpadding="0" cellspacing="0"><tbody><tr><td class="topLeft"/><td class="top"/><td class="topRight"/></tr><tr><td class="left"/><td class="content"><div class="thumbNail"><a class="galleryLink ImageLink" href="#{VideoLink}" onclick="HanfordVideo.ShowVideo(\'#{VideoID}\');return false;"><img src="#{Thumbnail}" height="123" width="123" border=0></a></div><button class="plusSign"/></td><td class="right"/></tr><tr><td class="bottomLeft"/><td class="bottom"/><td class="bottomRight"/></tr></tbody></table><div class="popupText" title="#{FullTitle}">#{Title}</div>',
		videoTemplate: '<div class="RelatedThumbNail"><a class="galleryLink ImageLink" href="#{VideoLink}" onclick="HanfordVideo.ShowVideo(\'#{VideoID}\');return false;"><img src="#{Thumbnail}" height="123" width="123" border=0></a></div><button class="plusSign" onclick="HanfordVideo.clickLink(this.previousSibling.firstChild)"></button><div class="popupText" title="#{FullTitle}">#{Title}</div>',
		basePath: undefined,
		clickLink: function(linkobj)
		{
			
			if ( linkobj.click )
			{
				
				linkobj.click();
			} else	if (linkobj.getAttribute('onclick') == null) {
     			if (linkobj.getAttribute('href')) document.location = linkobj.getAttribute('href');
  			}
  				else linkobj.onclick();
		},
		Demo : function()
		{
			this.ShowVideo('drChuVid5');
		},
		CreateVideoElement: function()
		{
			if ( ! this.basePath )
			{
				var tmpBases = document.getElementsByTagName("BASE");
				if ( tmpBases.length > 0)
					this.basePath = tmpBases[0].href;				
			}
			var elm = document.createElement("div");
			elm.id = this.videoElementID;
			document.body.appendChild(elm);
			
			
			elm.style.zIndex = 1010;
			elm.style.position = "absolute";
			elm.style.top = 20;
			elm.style.left = (Element.getWidth(document.body) / 2 ) - 400;
			//elm.style.backgroundColor = 'silver';
			elm.className = "popupBackground"
			elm.style.width = '815px';
			
			//elm.style.backgroundImage = "appimages/hanfordGov/gradated_background.jpg";
			//var style = "background-image:url("+ this.basePath + "/appimages/hanfordGov/gradated_background.jpg)";
			var style="background-color:#353535;border:thin solid white;padding-left:10px;";
			this.CreateStyle('#' + this.videoElementID, style);
			this.CreateStyle('.ImagePopupMetaLabel', 'color:white')
			this.CreateStyle('.ImagePopupMetaLabel a', 'color:white')
			this.CreateStyle('.popupTitle', "color:white;font-size:0.75em;")
			this.CreateStyle('.popupText', "color:white;font-size:0.75em;")
			this.CreateStyle('#ImagePopupItem_Summary', "display:block;height:100px;overflow:auto;padding-right:5px;");
			this.CreateStyle('.ImagePopupClose', "background-image:url('"+ this.basePath + "/c.cfm/Video/images/Popup_Close.png');background-repeat:no-repeat;display:block;margin-left:15px;	float:right;");
			this.CreateStyle('.ImagePopupClose img', "width:30px;height:24px;");			
			this.CreateStyle(".imageSurround", "width:790px;");
			this.CreateStyle('.plusSign', "background-image:url(" + this.basePath + "/c.cfm/video/images/PlusTransparent.png);display:block;margin-left:3px;margin-top:-20px;padding:1px;height:16px;opacity:0.99;width:16px;background-repeat:no-repeat;background-color:transparent;border:none;margin-bottom:7px;");
			this.CreateStyle('.RelatedThumbNail', "border:thin solid white;width:123px;height:123px;");
			this.CreateStyle('.RelatedVideos', "padding-left:25px;padding-right:25px;padding-bottom:10px;display:block;");
			
		},
		
		ShowVideo: function(inVideoID)
		{
			if ( ! this.videoOpen )
				this.currentY = window.pageYOffset || document.body.scrollTop || document.documentElement.scrollTop;
			
			this.videoOpen = true;
			if ( $(this.videoElementID))
			{
				$(this.videoElementID).show();
			} else {
				this.CreateVideoElement();
			}
			this.ShowOverlay();
			$(this.videoElementID).update(this.loadingContent);
			var elm = $(this.videoElementID);
			//alert(elm);
			elm.style.left = (Element.getWidth(document.body) / 2 ) - 400;
		
			new Ajax.Request(this.videoData, 	
			{ 
				parameters: 'VideoID=' + inVideoID, 
				onSuccess: this.ShowVideo_Ajax_Complete.bindAsEventListener(this) 
			}
			);
			/* Show Loading Page */
			
		},
		
		CloseVideo: function()
		{
			this.videoOpen = false;
			$(this.videoElementID).hide();
			$(this.videoElementID).update("");
			this.HideOverlay();
			scroll(0, this.currentY)
		},
		getMaxSize: function()
		{
			var maxH = window.height ?window.height:  window.innerHeight ? window.innerHeight :document.body.clientHeight;
			var maxW = window.width ?window.width:  window.innerWidth ? window.innerWidth:document.body.clientWidth;
			var heightResized = false;
			maxH = maxH - 390;
			maxW = 800;
			
			maxW = 300 * maxH / 242
			
			if ( maxH < 242 || maxW < 300)
			{
				maxH = 242;
				maxW = 300;				
			}
				
			return  { _w: maxW, _h: maxH};
		},
		
		ShowVideo_Ajax_Complete: function(transport)
		{
			var responseData = transport.responseText.evalJSON();
			/*
			 * .content
			 * .swf
			 * .flashvars
			 * .swfID
			 */
			
			var sz = this.getMaxSize();
			
			$(this.videoElementID).update(responseData.content);
			var FO = {
				"movie" : responseData.SWF,
				"width" : sz._w,
				"height": sz._h
				};
				
				
				params = {
		
				allowfullscreen: true,
				flashvars: responseData.flashvars
				}
				
				
				//swfUrlStr, replaceElemIdStr, widthStr, heightStr, swfVersionStr, xiSwfUrlStr, flashvarsObj, parObj, attObj, callbackFn
		    swfobject.embedSWF(FO.movie, responseData.swfID,FO.width,FO.height, "9.0.0", undefined, undefined, params);
			$$("#" + this.videoElementID + " .ImagePopupClose").each( function(s) {
				Event.observe(s, "click", HanfordVideo.CloseVideo.bindAsEventListener(HanfordVideo))
			});
		
			new Ajax.Request(this.relatedVideoData, {
				onSuccess : this.GetRelatedVideo_Complete.bindAsEventListener(this),
				parameters:{ maxVideos: 6, pageNum: 1, videoID: responseData.VideoID} 
			} );
			scroll(0,0)
			
		},
		GetRelatedVideo_Complete: function(transport)
		{
			var resp = transport.responseText.evalJSON();
			this.curPageNum = resp.PageNum;
			this.numPages = resp.Pages;
			var outStr = "";
			var t = new Template(this.videoTemplate);
			 if ( resp.Videos.length > 0 )
			 	outStr += "<span class='popupText'><strong>Related Videos</strong></span>";
				outStr += "<table>"		
				var i = 0;
				for (i = 0 ; i < resp.Videos.length; i++)
				{
					if ( i % this.vidsPerRow == 0 && i > 0)
						outStr += "</tr>"
					if ( i % this.vidsPerRow == 0)
						outStr += "<tr>"
						resp.Videos[i].FullTitle = resp.Videos[i].Title;
					if ( resp.Videos[i].Title.length > 50)
					{
						resp.Videos[i].Title = resp.Videos[i].Title.substr(0,30) + "...";
					}
					outStr += "<td valign='top' class='vidThumb'>" + t.evaluate(resp.Videos[i]) + "</td>";
					 					
				}
				for (var j=(i%this.vidsPerRow); j < this.vidsPerRow; j++)
				{
					outStr += "<td></td>"
				}
				outStr += "</tr></table>"
				$$("#" + this.videoElementID + " .RelatedVideos").each ( function(s) {
						s.update(outStr)
					});
				
			
			
			
			
			if ( this.curPageNum >= this.numPages)
			{
				$A(document.getElementsByClassName(this.videoNextClass)).each(Element.hide);
				
			} else {
				$A(document.getElementsByClassName(this.videoNextClass)).each(Element.show);
				
			}
			
			if ( this.curPageNum <= 1)
			{
				$A(document.getElementsByClassName(this.videoPreviousClass)).each(Element.hide);
				
			} else {
				$A(document.getElementsByClassName(this.videoPreviousClass)).each(Element.show);
				
			}
			if ( this.numPages > 1)
				$A(document.getElementsByClassName(this.pagesClass)).each(this.setPageNum.bindAsEventListener(this));
			
		},
		/****************************************
		 * Public Methods
		 * 
		 * 	Overlay Functions
		 * 		HideOverlay
		 * 		ShowOverlay
		 */
		HideOverlay: function()
		{
			
	
			if ( $(this.overlayObjectID))
				$(this.overlayObjectID).hide();
				
			if ( Prototype.Browser.IE )
			{
				var bod = document.getElementsByTagName('body')[0];
				
					bod.style.position = "relative";
					bod.style.width = "100%"
					bod.style.height= "100%"
					bod.style.overflow = "auto";
			}
			
		},
		
		ShowOverlay: function()
		{
			if ( $(this.overlayObjectID))
				$(this.overlayObjectID).show();
			else
				this.CreateOverlay();
				
				
			if (!Prototype.Browser.IE) {
				$(this.overlayObjectID).style.position = "fixed";
				
			}
			else {
				var bod = document.getElementsByTagName('body')[0];
				
				bod.style.position = "relative";
				bod.style.width = "100%"
				bod.style.height = "100%"
				bod.style.overflow = "hidden";
				
				var htmlElms = document.getElementsByTagName("html");
				if (htmlElms.length > 0) 
					htmlElms[0].style.height = "100%";
				htmlElms[0].style.overflow = "hidden";
			}
		},
		
		CreateOverlay: function()
		{		
			var elm = document.createElement("div");
			elm.id = this.overlayObjectID;
			document.body.appendChild(elm);
			var style = "position:absolute;	top:0;left:0;margin : 0 0 0 0;padding: 0 0 0 0;width:100%;height:100%;z-index:1000;background-color:#333;-moz-opacity: 0.8;	opacity:.80;filter: alpha(opacity=80);";
			this.CreateStyle('#' + this.overlayObjectID, style);	
			Event.observe(elm, "click", this.CloseVideo.bindAsEventListener(this))	
		},
		
		CreateStyle: function(selector, style){
			if ( !document.styleSheets) return;
			var thecss = new Array();
			var lastStyleSheet = document.styleSheets.length - 1;
			if ( document.styleSheets[lastStyleSheet].cssRules)
			{
				thecss = document.styleSheets[lastStyleSheet].cssRules;				
			} else {
				thecss = document.styleSheets[lastStyleSheet].rules;
			}
			if ( document.styleSheets[lastStyleSheet].insertRule)
				document.styleSheets[lastStyleSheet].insertRule(selector + '{' + style + '}', thecss.length);
			else 
				document.styleSheets[lastStyleSheet].addRule(selector, style, thecss.length);
			
		},
		
		prototype : {
			
		},
		
		CheckKeyPress: function(e){
			var kC = (window.event) ? event.keyCode : e.keyCode;
			var Esc = ( window.event) ? 27 : e.DOM_VK_ESCAPE;
			if ( kC == Esc && this.videoOpen)
				this.CloseVideo();
		}
		
	}
	
}
Event.observe(document, "keypress", HanfordVideo.CheckKeyPress.bindAsEventListener(HanfordVideo));

