// -----------------------------------------------------------------------------------
//
//	FlashVideoBox v1.1
//     by Stephen Scholtz
//	2007-12-11 - Ripped off from code by Lokesh Dhakar - http://www.lokeshdhakar.com/projects/lightbox2/
//	2008-07-18 - Updated to make use of swfobject v2; had to create a temp div because swfobject now
//		replaces the target element, instead of writing the flash code inside of it.
//
//	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 Soulpepper 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:
// - extend-element.js
// - page-size-functions.js
// - show-hide-flash.js
// - show-hide-select.js
// These are in addition to the prototype, scriptaculous and swfobject libraries

// Config
var fileFVNavCloseImage = "/images/lightbox/closelabel.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 that have a rel attribute with 'videobox' in the name
		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;}
			}
		}


		//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 objFVContainer = document.createElement("div");
			objFVContainer.setAttribute('id','flashVideoContainer');
			objFVBorder.appendChild(objFVContainer);
		
		// 2008-07-18 - new temp holding div; gets destroyed when flash video put in place.
		//	- div recreated again later if it doesn't exist
		var objFVTemp = document.createElement("div");
			objFVTemp.setAttribute('id','flashVideoTemp');
			objFVContainer.appendChild(objFVTemp);
		
		
		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);
	
		var objFVNavCloseLink = document.createElement("a");
			objFVNavCloseLink.setAttribute('id','flashVideoClose');
			objFVNavCloseLink.setAttribute('href','#');
			objFVNavCloseLink.onclick = function() {
				myFlashVideoBox.endVideo();
				return false;
			}
			objFVNav.appendChild(objFVNavCloseLink);
	
		var objFVNavCloseImage = document.createElement("img");
			objFVNavCloseImage.setAttribute('src', fileFVNavCloseImage);
			objFVNavCloseLink.appendChild(objFVNavCloseImage);
	},
	
	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);
		
		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) {
		// swfobject 2 completely replaces the target element, instead of inserting itself into the target
		// element, so we need to double check and, if necessary, recreate the Temp element for swfobject
		// to replace with flash
		if (!document.getElementById('flashVideoTemp')) {
			var objFVTemp = document.createElement("div");
			objFVTemp.setAttribute('id','flashVideoTemp');
			document.getElementById('flashVideoContainer').appendChild(objFVTemp);
		}
		swfobject.embedSWF(videoURL, "flashVideoTemp", videoWidth, videoHeight, "9.0.0");
		
		
		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() {
		// Destroy the flash embed
		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);
		
		