/* 	SWFObject parameters for Focus on Film
	Was previously using UFO, but this was causing crashes in low memory settings,
	SWFObject is more stable.
*/

window.onload = function() {
	
	//set SWFObject parameters
	var topBanner = new SWFObject("/education/focuson/film/flash/focus-on-banner.swf", "topBanner", "100%", "100", "7", "#1A4967");
	topBanner.addParam("menu", "false");
	topBanner.write("focus-on-film");
	if (document.getElementById("home-film")){
		var homeIntro = new SWFObject("/education/focuson/film/flash/intro-screen.swf", "homeIntro", "290", "184", "7", "#000000");
		homeIntro.addParam("menu", "false");
		homeIntro.write("home-film");
	}
	
	//add onclick events unobtrusively
	a=document.getElementsByTagName("a");
	for(i=0;i<a.length;i++){
		
		// help links
		if(a[i].className=="help" && a[i].href=="http://"+document.domain+"/education/focuson/film/help.htm"){
			a[i].onclick=function(){
				openPopupWindow(this.href,'lcpop','620','500','scrollbars=yes,resizable=yes','yes');return false;   
			}
			a[i].title="This link opens in a new window";
		}
		
		// generic pop-up links
		// class working example: pop-up-600x400
		// this will generate a pop up window at 600x400
		popup=a[i].className;
		result=popup.search(/pop-up/);
		if(result==0){
			//get three digit value in class name for width
			var width=popup.substring(7,10);
			//get three digit value in class name for height
			var height=popup.substring(11,14);
			// add onclick function
			a[i].onclick=function(){
				openPopupWindow(this.href,'lcpop',width,height,'scrollbars=yes,resizable=yes','yes');return false; 
			}
			a[i].title="This link opens in a pop-up window";
		}
		
		// video player
		// class working example: control-videoPlayer1
		// this will create a link that will play a flash movie inside of videoPlayer1 where the swf is loaded
		videoResult=a[i].className.search(/control-video/);
		if(videoResult==0){
			
			a[i].id=a[i].className;
			a[i].onclick=function(){
				videoID=this.className.substring(8,20);
				playVideo(videoID); return false;
			}
			
			createRewind(a[i].id);

			// reset title and target as it no longer opens in _blank window which is the non-JS action
			a[i].title="";
		}
		
		
		audioResult=a[i].className.search(/control-audio/);
		if(audioResult==0){
			
			a[i].id=a[i].className;
			a[i].onclick=function(){
				audioID=this.className.substring(8,20);
				playAudio(audioID); return false;
			}
			
			createRewindAudio(a[i].id);

			// reset title and target as it no longer opens in _blank window which is the non-JS action
			a[i].title="";
		}		
		// audio player
		// class working example: control-audioPlayer1
		// this will create a link that will play a flash movie inside of audioPlayer1 where the swf is loaded
		/*audioResult=a[i].className.search(/control-audio/);
		if(audioResult==0){
			a[i].onclick=function(){
				playAudio(this.className.substring(8,20)); return false;
			}
			// reset title and target as it no longer opens in _blank window which is the non-JS action
			a[i].title="";
			a[i].target="";
			
			// create another LI containing an A that will call a rewind function for the player
			var rewindLi = document.createElement("li");
			rewindLi.className = "rewind";
			// create A and add text and events
			var rewindA = document.createElement("a");
			rewindA.href="javascript:void(0);";
			rewindA.onclick=function(){
				rewindAudio(this.className.substring(8,20)); return false;
			}
			var rewindAText = document.createTextNode("Rewind clip");
			rewindA.appendChild(rewindAText);			
			rewindLi.appendChild(rewindA);
			
			//add into DOM
			a[i].parentNode.parentNode.appendChild(rewindLi);			
			a[i].parentNode.parentNode.insertBefore(rewindLi, a[i].parentNode.nextSibling);			
			
		}
		*/
	}	
}

function createRewind(id){
	playButton=document.getElementById(id);

	var rewindLi = document.createElement("li");
	rewindLi.className = "rewind";
	
	// create A and add text and events
	var rewindA = document.createElement("a");
	rewindA.href="javascript:void(0);";
	var rewindAText = document.createTextNode("Rewind clip");
	rewindA.appendChild(rewindAText);			
	rewindLi.appendChild(rewindA);
			
	//add into DOM
	playButton.parentNode.parentNode.appendChild(rewindLi);			
	playButton.parentNode.parentNode.insertBefore(rewindLi, playButton.parentNode.nextSibling);
	
	rewindA.onclick=function(){
		//note: whitespace has to be removed for this to work correctly
		//we have to traverse the DOM a bit here, if we try to pass the value dynamically
		//it gets overwritten every time by the next button
		if(this.parentNode.parentNode.firstChild.firstChild){
			//insurance in case there is whitespace -- better to have nothing happen than an error
			videoID=this.parentNode.parentNode.firstChild.firstChild.className.substring(8,20);
			rewindVideo(videoID); return false;
		}
	}	
}

function createRewindAudio(id){
	playButton=document.getElementById(id);

	var rewindLi = document.createElement("li");
	rewindLi.className = "rewind";
	
	// create A and add text and events
	var rewindA = document.createElement("a");
	rewindA.href="javascript:void(0);";
	var rewindAText = document.createTextNode("Rewind clip");
	rewindA.appendChild(rewindAText);			
	rewindLi.appendChild(rewindA);
			
	//add into DOM
	playButton.parentNode.parentNode.appendChild(rewindLi);			
	playButton.parentNode.parentNode.insertBefore(rewindLi, playButton.parentNode.nextSibling);
	
	
	
	rewindA.onclick=function(){
		//note: whitespace has to be removed for this to work correctly
		//we have to traverse the DOM a bit here, if we try to pass the value dynamically
		//it gets overwritten every time by the next button
		//alert(this.parentNode.parentNode.childNodes[1].firstChild.className);
		if(this.parentNode.previousSibling.childNodes[0].className){
			//insurance in case there is whitespace -- better to have nothing happen than an error
			audioID=this.parentNode.previousSibling.childNodes[0].className.substring(8,20);
			rewindAudio(audioID); return false;
		}
	}	
	//alert('apparently done');*/
}

/* Flash player resizing */
function fullscreen(id,seek){
	speed=1;
	// speed for transitions

	if(clipContainer=document.getElementById("clip-container")){
	// for films in the archive
		if (clipContainer.offsetWidth!=337){
		// fade in content, reduce player
			fadeInClipInfo(speed);
			fadeInClipDetails(speed);	
		} else {
		// fade out content, enlarge player
			fadeOutClipInfo(speed);
			fadeOutClipDetails(speed);
		}
	} else {
		clipContainer=document.getElementById(id).parentNode;
		// inline film clip which has no clip-container id
		if (clipContainer.offsetWidth!=337){
		// fade in content, reduce player
			reduceClipInline(clipContainer,speed);			
		} else {
		// fade out content, enlarge player
			enlargeClipInline(clipContainer,speed);
		}

		//document.getElementById(id).parentNode.style.right=-24+"px";
		//document.getElementById(id).parentNode.style.top=-21+"px";
	
	}
	document.getElementById(id).SetVariable("listenerClip.playAt",seek);	
}

function enlargeClipInline(clipContainer,speed) {
	setEnlargeClip=setInterval("enlarge(clipContainer,setEnlargeClip)",speed);
}

function reduceClipInline(clipContainer,speed) {
	setReduceClip=setInterval("reduce(clipContainer,setReduceClip)",speed);
}

function enlarge(clipContainer,setEnlargeClip) {
	clipContainerParent=clipContainer.parentNode;
	// generic fade out function -- note this runs for each object faded out [2 at the moment]
	clipContainerParent.style.height=(clipContainerParent.offsetHeight+25)+"px";
	if(clipContainerParent.offsetHeight>=310){
		clipContainer.style.width=465+"px";
		clipContainer.style.height=379+"px";
		clipContainer.style.right=-13+"px";
		clipContainer.style.top=-11+"px";
		clipContainer.parentNode.style.height=353+"px";			
		// clear interval when fading is complete
		clearInterval(setEnlargeClip);
	}	
}

function reduce(clipContainer,setReduceClip) {
	clipContainerParent=clipContainer.parentNode;
	clipContainer.firstChild.style.visibility='hidden';
	// generic fade out function -- note this runs for each object faded out [2 at the moment]
	clipContainerParent.style.height=(clipContainerParent.offsetHeight-35)+"px";
	if(clipContainerParent.offsetHeight<=300){
		clipContainer.parentNode.style.height=275+"px";	
		clipContainer.style.width=337+"px";
		clipContainer.style.height=275+"px";
		clipContainer.style.right=51+"px";
		clipContainer.style.top=0+"px";	
		clipContainer.firstChild.style.visibility='visible';
		clipContainer.firstChild.style.display='block';
		// clear interval when fading is complete
		clearInterval(setReduceClip);
	}	
}

function fadeOutClipInfo(speed) {
	// fade out clip info -- because of setInterval being set for each object, no way to modularise?
	clipInfo=document.getElementById("clip-information");
	if(clipInfo){
		clipInfo.style.opacity=1;
		clipInfo.filter="alpha(opacity=100)";
		setFadeOutClipInfo=setInterval("fadeOut(clipInfo,setFadeOutClipInfo)",speed);
	}
}

function fadeOutClipDetails(speed) {
	// fade out clip details
	clipDetails=document.getElementById("clip-details");
	clipDetails.style.opacity=1;
	clipDetails.filter="alpha(opacity=100)";
	setFadeOutClipDetails=setInterval("fadeOut(clipDetails,setFadeOutClipDetails)",speed);
}

function fadeOut(fadeObj,setFadeOut) {
	// generic fade out function -- note this runs for each object faded out [2 at the moment]
	if(fadeObj.style.opacity<.4){
		// resize player once fading is almost complete
		clipContainer=document.getElementById("clip-container");
		clipContainer.style.width=707+"px";
		clipContainer.style.height=577+"px";
	}
	fadeObj.style.opacity=fadeObj.style.opacity-.2;
	fadeObj.style.filter="alpha(opacity="+(fadeObj.style.opacity*100)+")";
	if(fadeObj.style.opacity<0){
		// clear interval when fading is complete
		clearInterval(setFadeOut);
	}
}

function fadeInClipInfo(speed) {
	clipInfo=document.getElementById("clip-information");
	clipInfo.style.opacity=0;
	setFadeInClipInfo=setInterval("fadeIn(clipInfo,setFadeInClipInfo)",speed);
}

function fadeInClipDetails(speed) {
	clipDetails=document.getElementById("clip-details");
	clipDetails.style.opacity=0;
	setFadeInClipDetails=setInterval("fadeIn(clipDetails,setFadeInClipDetails)",speed);
}

function fadeIn(fadeObj,setFadeIn) {
	clipContainer=document.getElementById("clip-container");
	if(clipContainer.offsetWidth<337){
		// hide flash player when resizing -- reduces flickering in IE
		// as we're using UFO, there is no ID on the <object>, so use firstChild
		clipContainer.firstChild.style.display='none';
	}else{
		// restore when resized
		clipContainer.firstChild.style.display='block';
	}
	clipContainer.style.width=337+"px";
	clipContainer.style.height=275+"px";
	fadeObj.style.opacity=(parseFloat(fadeObj.style.opacity)+0.2);
	fadeObj.style.filter="alpha(opacity="+(fadeObj.style.opacity*100)+")";	
	if(fadeObj.style.opacity>1){
		// clear interval when fading is complete
		clearInterval(setFadeIn);
		fadeObj.style.display='block';
		fadeObj.style.position='relative';
		fadeObj.style.backgroundColor='#ffffff'; // attempt to sort out IE anti-aliasing bugs
	}
}

function playAudio(id){
	//JS to Actionscript for embedded audio 
	document.getElementById(id).SetVariable("externalPlay","toggle");
}

function playVideo(id){
	//JS to Actionscript for embedded video 
	document.getElementById(id).SetVariable("listenerClip.externalPlay","toggle");
}

function rewindVideo(id){
	//JS to Actionscript for embedded video 
	document.getElementById(id).SetVariable("listenerClip.externalPlay","rewind");
}

function rewindAudio(id){
	//JS to Actionscript for embedded audio 
	document.getElementById(id).SetVariable("externalPlay","rewind");
}

function editorInit(){
	editingBegin=true;
}

function promptSave(){
	if(editingBegin==true) {
	//see editors-room/default for notes on this variable
     return "You have started a project. Are you sure you want to leave this page? Any unsaved work will be lost.";
	}
}




