// -----------------------------------------------------------------------------------
//
//	FlashVideoBox v1
//     by Stephen Scholtz
//	Ripped off from code by Lokesh Dhakar - http://www.lokeshdhakar.com/projects/lightbox2/
//	2007-12-11
//
//	Licensed under the Creative Commons Attribution 2.5 License - http://creativecommons.org/licenses/by/2.5/
//	
//	Credit also due to those who have helped, inspired, and made their code available to the public.
//	Including: Scott Upton(uptonic.com), Peter-Paul Koch(quirksmode.org), Thomas Fuchs(mir.aculo.us), and others.
//
//	This code is VERY specific to my usage for the NBoC website.  It is not meant for release to the public for use.
//	Don't just plug this code into your site and then expect it to work. :P
//
// -----------------------------------------------------------------------------------

// VERY IMPORTANT
// The following files *must* be included above this file in order for flashvideobox to work:
// - util.js
// - swfobject.js
// These are in addition to the prototype and scriptaculous libraries

// UPDATES
//  2008-10-16
//	- Added new "Google Analytics Event" tracking script, to help track the watching of videos
//	- Moved "extend-element.js", etc. into "/js/util.js", just need to propagate the including of those scripts
//	   in all the templates
//	- Added new function, cleanVideoURL, for GA Events, to give them a nice title
// 2008-10-21
//	- Added GA Event tracking for PDFs...already looping through all the anchors, might as well look for
//	   PDF downloads and add the GA code to them instead of looping twice

// UPDATE - 2008-10-16
/*function cleanVideoURL(myUrl) {
	var myString = myUrl.substring(myUrl.indexOf('videoURL=')+9,myUrl.indexOf('.flv')+4);
	return myString;
}*/



// Config
var fileFVNavCloseImage = "/graphics/icons/fClose.gif";
var FVOverlayDuration = 0.2;
var FVResizeDuration = 0.5;

// FVB Declaration
// init();
//

var FlashVideoBox = Class.create();

FlashVideoBox.prototype = {

	initialize: function() {	
		if (!document.getElementsByTagName){ return; }
		
		// Loop through all anchor tags
		var anchors = document.getElementsByTagName('a');
		for (var i=0; i<anchors.length; i++){
			var anchor = anchors[i];
			var relAttribute = String(anchor.getAttribute('rel'));
			
			// use the string.match() method to catch 'videobox' references in the rel attribute
			if (anchor.getAttribute('href') && (relAttribute.toLowerCase().match('videobox'))){
				// anchor.onclick = function () {myFlashVideoBox.launchVideo(this); return false;}
				// UPDATE - 2008-10-16
				anchor.onclick = function () {
					//console.log("Anchor: ", cleanVideoURL(this.href));
				//	pageTracker._trackEvent('Videos', 'Play', cleanVideoURL(this.href));
					myFlashVideoBox.launchVideo(this);
					return false;
				}
			// UPDATE - 2008-10-21
			} else if (anchor.getAttribute('href') && (anchor.getAttribute('href').indexOf(".pdf") > -1)) {
				anchor.onclick = function() {
				//	pageTracker._trackEvent('Downloads', 'Download PDF', this.href);
					return true;
				}
			}
		}


		//Create video box bits
		var objBody = document.getElementsByTagName("body").item(0);
		
		var objOverlay = document.createElement("div");
			objOverlay.setAttribute('id','flashVidOverlay');
			objOverlay.style.display = 'none';
			//objOverlay.onclick = function() { endVideo(); }
			objBody.appendChild(objOverlay);
		
		var objFV = document.createElement("div");
			objFV.setAttribute('id','flashVideoBox');
			objFV.style.display = 'none';
			/*objFV.onclick = function() {
				myFlashVideoBox.endVideo();
			}*/
			objBody.appendChild(objFV);
		
		var objFVBorder = document.createElement("div");
			objFVBorder.setAttribute('id','flashVideoBorder');
			objFV.appendChild(objFVBorder);
		
		
		
		var objFVNavCloseLink = document.createElement("a");
			objFVNavCloseLink.setAttribute('id','flashVideoClose');
			objFVNavCloseLink.setAttribute('href','#');

		var objFVNavCloseImage = document.createElement("img");
			objFVNavCloseImage.setAttribute('src', fileFVNavCloseImage);
			objFVNavCloseLink.appendChild(objFVNavCloseImage);/**/
			objFVBorder.appendChild(objFVNavCloseLink);

		var objFVContainer = document.createElement("div");
			objFVContainer.setAttribute('id','flashVideoContainer');
			objFVBorder.appendChild(objFVContainer);

		var objFVDataContainer = document.createElement("div");
			objFVDataContainer.setAttribute('id','flashVideoDataContainer');
			objFVDataContainer.className = 'clearfix';
			objFV.appendChild(objFVDataContainer);
		
		var objFVData = document.createElement("div");
			objFVData.setAttribute('id','flashVideoData');
			objFVDataContainer.appendChild(objFVData);

		var objFVDetails = document.createElement("div");
			objFVDetails.setAttribute('id','flashVideoDetails');
			objFVData.appendChild(objFVDetails);
		
			var objFVNav = document.createElement("div");
			objFVNav.setAttribute('id','flashVideoNav');
			objFVData.appendChild(objFVNav);

			objFVNavCloseLink.onclick = function() {
				myFlashVideoBox.endVideo();
				return false;
			}
	
	},
	
	launchVideo: function(myAnchor) {
		//hideFlash();
		
		var anchorRel = myAnchor.getAttribute('rel');
		//var attributeRegEx = /\[\d+,\d+\]/;
		var videoAttributes = anchorRel.substring(anchorRel.indexOf('[')+1, anchorRel.indexOf(']'));
		videoAttributes = videoAttributes.split(",");
		var videoWidth = parseInt(videoAttributes[0]);
		var videoHeight = parseInt(videoAttributes[1]);
		var videoURL = myAnchor.getAttribute('href');
		// stretch overlay to fill page and fade in
		var arrayPageSize = getPageSize();
		Element.setHeight('flashVidOverlay', arrayPageSize[1]);

		new Effect.Appear('flashVidOverlay', { duration: FVOverlayDuration, from: 0.0, to: 0.8 });
		
		var arrayPageScroll = getPageScroll();
//		var videoboxTop = arrayPageScroll[1] + (arrayPageSize[3] / 10);
		var videoboxTop = arrayPageScroll[1] + 50;
		
		Element.setTop('flashVideoBox', videoboxTop);
		
		Element.setWidth('flashVideoBorder',videoWidth+20);
		Element.setHeight('flashVideoBorder',videoHeight+20);
		Element.setHeight('flashVideoContainer',videoHeight);
		Element.setWidth('flashVideoContainer',videoWidth);
		Element.setWidth('flashVideoDataContainer', videoWidth+20);
		Element.hide('flashVideoDataContainer');

		//Element.setInnerHTML("flashVideoDetails",myAnchor.getAttribute('title'));
		
		// Render the main contents
		new Effect.Appear('flashVideoBox', { duration: FVResizeDuration,  from: 0.0, to: 1, afterFinish: function(){ myFlashVideoBox.loadVideo(videoURL,videoWidth,videoHeight); } }); // scriptalicious
		return false;
	},
	
	loadVideo: function(videoURL,videoWidth,videoHeight) {
		var fm = "<iframe src='"+videoURL+"' width='"+videoWidth+"' height='"+videoHeight+"' border=0 frameborder=0 name='frameBox' id='frameBox'></iframe>"
		htmlMe("flashVideoContainer",fm);
		
		
		new Effect.Parallel(
			[ new Effect.SlideDown( 'flashVideoDataContainer', { sync: true, duration: FVResizeDuration, from: 0.0, to: 1.0 }), 
			  new Effect.Appear('flashVideoDataContainer', { sync: true, duration: FVResizeDuration }) ], 
			{ duration: FVResizeDuration, afterFinish: function() {
				// update overlay size and update nav
				var arrayPageSize = getPageSize();
				Element.setHeight('overlay', arrayPageSize[1]);
				// Don't need to update the nav, 'cuz we're not going to have arrays of videos
				//updateNav();
				}
			} 
		);
	},
	
	endVideo: function() {
		document.getElementById("flashVideoContainer").innerHTML = "";
		new Effect.Fade('flashVidOverlay', { duration: FVOverlayDuration});
		new Effect.Fade('flashVideoBox', { duration: FVOverlayDuration, afterFinish: function() { showFlash() } });
	}
}

function initFlashVideoBox() { myFlashVideoBox = new FlashVideoBox(); }
Event.observe(window, 'load', initFlashVideoBox, false);
		
		
