var contentFrame = true;
var paused;

/* ** RECORD ITS STATS ** */
var itsHost;

if (navigator.appName.indexOf('Netscape') != -1) {
	browser = "ns";
} else {
	browser = "ie";
}

function setPageTitle(title){
	parent.document.title = title;
}

function setItsStats(time, complete){
	document.timetoadd = time;
	document.value = complete;
	//alert(document.timetoadd + " :: " + document.value);
}

function setItsHost(hostname){
	itsHost = hostname;
}

function endItsSession(){
	document.sessionstats.action = itsHost + "process_stats_request.do";
	document.sessionstats.submit();
}
/* ** END RECORD ITS STATS ** */


/* ** AUTO PLAY ** */
var aTimer;
var kTimer;
var regcount = 0;
var autoplay = false;
var hasBeenKilled = false;

function resetAutoPlayVars(){
	regcount = 0;
	hasBeenKilled = false;
	clearTimeout(aTimer);
	clearTimeout(kTimer);
}

function setAutoTimer(){
	aTimer = setTimeout("checkForAutoplay()", 4000);
}

function checkForAutoplay(){
	if (regcount == 0 && autoplay == true){
		killAutoPlay();
	}
}
	
function shutdownAutoFromContent(){
	kTimer = setTimeout("killAutoPlay()", 3000);
}

function killAutoPlay(){
	regcount = 0;
	hasBeenKilled = true;
	autoplay = false;
	parent.communications.location.replace("../html/autoplay-killer.html");
}

function register(){
	regcount++;
	//alert("Reg: " + regcount);
}

function unregister(){
	if (regcount > 0){
		regcount--;
	}
	if(regcount == 0 && autoplay == true && hasBeenKilled == false) {
		kickAutoPlay();
	}
}

function kickAutoPlay(){
	parent.communications.location.replace("../html/autoplay-kicker.html");
}

function toggleAutoPlay(bool){
	//alert("Toggling Auto: " + bool);
	autoplay = eval(bool);
	if(bool == true && regcount == 0){
		kickAutoPlay();
	}
}
/* **END AUTO PLAY ** */

function launchLab(url, w, h){
	parent.footer.labWindow = window.open(url, 'labWindow', 'width=' + w + ',height=' + h + ',scrollbars=0,left=' + (screen.width/2 - w/2)+',top='+(screen.height/2 - h/2));
}

function pauseContent() {
	if(paused == true){
		paused = false;
		var Video = InternetExplorer ? window.Visual : document.Visual;
		if(Video){
				Video.Play();
				Video.SetVariable("MonsoonPause", "false");
		}
		var Audio = InternetExplorer ? window.Audio : document.Audio;
		if(Audio){
				Audio.Play();
		}
	} else {
		paused = true;
		var InternetExplorer = navigator.appName.indexOf("Microsoft") != -1;
		var Video = InternetExplorer ? window.Visual : document.Visual;
		if (Video){
				Video.StopPlay();
				Video.SetVariable("MonsoonPause", "true");
		}
		var Audio = InternetExplorer ? window.Audio : document.Audio;
		if(Audio){
				Audio.StopPlay();
		}
	}
}





/* ** RENDERING ** */
//---- Assessment Stuff
function replaceAssessmentPage(pagestyle, visual, audio, heading, bodytext, pagenum){
	//Setup the new page style
	var myHTML = getPageStyle(pagestyle);
	//Replace each MEC in page
	if(visual != "null"){
		var v = createObject(visual); 
		myHTML = updateVisual(v, myHTML); 
	}
	if(audio != "null"){
		var a = createObject(audio);  
		myHTML = updateAudio(a, myHTML); 
	}
	if(heading != "null"){
		var h = createObject(heading); 
		myHTML = updateHeading(h, myHTML); 
	}
	if(bodytext != "null"){ 
		var b = createObject(bodytext); 
		myHTML = updateBodyText(b, myHTML); 
	}
	if(pagenum != "null"){ 
		var p = createObject(pagenum);
		myHTML = updatePageNum(p, myHTML); 
	}

	//alert(myHTML);
	document.getElementById("holder").innerHTML = myHTML;
	
	paused = false;
	
	parent.footer.closeLab();
	window.focus();
	
}

//Create and object from the flash strings
function createObject(props){
	var obj = new Object();
	var pairs = props.split(" ,");
	for(var i = 0; i < pairs.length; i++){
		var reg = /:/;
		var temp = pairs[i].split(reg);
		//alert(pairs[i]);
		obj[temp[0]] = temp[1];
	}
	return obj;
}

//Find the page style to load
function getPageStyle(ps){
	var pagestyle = parent.footer.document.getElementById(ps).innerHTML;
	return pagestyle;
}

//Replace $ELEMENTNAME
function updateVisual(v, html){
	//Select the mime type
	//var visualTag = parent.footer.document.getElementById(v["mt"]).innerHTML;
	var visualTag = parent.footer.getMediaElement(v["mt"]);
	var reg;
	
	if(v["mt"] == "application/x-shockwave-flash"){
		reg = /\x24(id)/gi;
		visualTag = visualTag.replace(reg, "Video");
		visualTag = replaceProtocol(visualTag);
	}
	
	if(v["mt"] == "text/plain"){
		reg = /\x24(t)/gi;
	} else {
		reg = /\x24(u)/gi;
	}
	visualTag = visualTag.replace(reg, v["u"]);
	
	reg = /\x24(w)/gi;
	visualTag = visualTag.replace(reg, v["w"]);
	reg = /\x24(h)/gi;
	visualTag = visualTag.replace(reg, v["h"]);
	
	reg = /\x24(visual)/gi;
	html = html.replace(reg, visualTag);
	return html;
}

function updateAudio(a, html){
	//var audioTag = parent.footer.document.getElementById(a["mt"]).innerHTML;
	var audioTag = parent.footer.getMediaElement(a["mt"]);
	var reg;
	
	if(a["mt"] == "application/x-shockwave-flash"){
		reg = /\x24(id)/gi;
		audioTag = audioTag.replace(reg, "Audio");
		audioTag = replaceProtocol(audioTag);
	}
	
	if(a["mt"] == "text/plain"){
		reg = /\x24(t)/gi;
	} else {
		reg = /\x24(u)/gi;
	}
	
	audioTag = audioTag.replace(reg, a["u"]);
	reg = /\x24(w)/gi;
	audioTag = audioTag.replace(reg, a["w"]);
	reg = /\x24(h)/gi;
	audioTag = audioTag.replace(reg, a["h"]);
	
	reg = /\x24(audio)/gi;
	html = html.replace(reg, audioTag);
	return html;
}

function updateHeading(h, html){
	if(h["u"].indexOf("$") != -1){
		var reg = /\x24/gi;
		h["u"] = h["u"].replace(reg, "&#36;");
	}
	reg = /\x24(heading)/gi;
	html = html.replace(reg, h["u"]);
	return html;


	if(h["u"].indexOf("$1") != -1){
		var reg = /\x24/gi;
		h["u"] = h["u"].replace(reg, "&#36;1");
	}
	reg = /\x24(heading)/gi;
	html = html.replace(reg, h["u"]);
	return html;

}

function updateBodyText(bt, html){
	if(bt["u"].indexOf("$") != -1){
		var reg = /\x24/gi;
		bt["u"] = bt["u"].replace(reg, "&#36;");
	}
	
	reg = /\x24(bodytext)/gi;
	html = html.replace(reg, bt["u"]);
	return html;


	if(bt["u"].indexOf("$1") != -1){
		var reg = /\x24/gi;
		bt["u"] = bt["u"].replace(reg, "&#36;1");
	}
	
	reg = /\x24(bodytext)/gi;
	html = html.replace(reg, bt["u"]);
	return html;

}



function updatePageNum(pn, html){
	var reg = /\x24(pagenumber)/gi;
	html = html.replace(reg, pn["u"]);
	return html;
}

function submitAssessmentQuestion(action, direction){
	//alert("question submit");
	document.assessmentquestion.direction.value = direction;
	document.assessmentquestion.target = "communications";
	document.assessmentquestion.action = action;
	document.assessmentquestion.submit();
}

//Set the question num for recording answers
function setQuestionNumberValue(num){
	if(document.assessmentquestion){
	  document.assessmentquestion.num.value = num;
	} else {
		setTimeout(setQuestionNumberValue, 500, num);
	}
}


//---- Regular Content
function replacePageStyle(co){
	var p = eval(co);
	
	//Premiere Page Styles
	if(p.pageStyle == "ptr" || p.pageStyle == "ptl" || p.pageStyle == "ptc"){ 
		var numPairs = 0;
		while(p.mediaElements["PremiereVisual" + numPairs] || p.mediaElements["PremiereText" + numPairs]){
			numPairs++;
		}
		
		var template = parent.footer.document.getElementById("premierebase").innerHTML;
		
		var tag = handleMediaElement(p.mediaElements.Heading, "Heading");
		var reg = new RegExp("\\x24(Heading)", "gi");
		template = template.replace(reg, tag);
		
		tag = handleMediaElement(p.mediaElements.PageNumber, "PageNumber");
		reg = new RegExp("\\x24(PageNumber)", "gi");
		template = template.replace(reg, tag);
		
		for(var i = 0; i < numPairs; i++){
			if(i == 0){
				var temp = parent.footer.document.getElementById(p.pageStyle).innerHTML;
			} else {
				var temp = parent.footer.document.getElementById("premierepair").innerHTML;
			}
			if(p.mediaElements["PremiereVisual" + i]){
				tag = handleMediaElement(p.mediaElements["PremiereVisual" + i], "PremiereVisual");
				reg = new RegExp("\\x24(PremiereVisual)", "gi");
				temp = temp.replace(reg, tag);
			} else {
				tag = "";
				reg = new RegExp("\\x24(PremiereVisual)", "gi");
				temp = temp.replace(reg, tag);
			}
			
			if(p.mediaElements["PremiereText" + i]){
				tag = handleMediaElement(p.mediaElements["PremiereText" + i], "PremiereText");
				reg = new RegExp("\\x24(PremiereText)", "gi");
				temp = temp.replace(reg, tag);
			} else {
				tag = "";
				reg = new RegExp("\\x24(PremiereText)", "gi");
				temp = temp.replace(reg, tag);
			}
			
			template += temp;
		}
		
	} else { //Normal Page Styles
		var template = parent.footer.document.getElementById(p.pageStyle).innerHTML;
		
		for(var i in p.mediaElements){
			if(p.mediaElements[i] != null && i != "SplashUrl"){ //Handle non null elements
				var tag = handleMediaElement(p.mediaElements[i], i);
			} else if(p.mediaElements[i] == null && i != "SplashUrl"){ //Handle NULL elements
				var tag = "";
			}
			
			var reg = new RegExp("\\x24("+i+")", "gi");
			
			if(i == "PageNumber" && p.mediaElements[i]!= null){ //For adjusting the PageNumber mec
				if(p.mediaElements[i].fullText == "" || p.mediaElements[i].fullText == null){
					template = template.replace(reg, tag);
				} else {
					template = template.replace(reg, "<br><br>" + tag);
				}
			} else if(i == "SplashUrl"){
				template = template.replace(reg, p.mediaElements[i].uri);
			} else { //All other mecs
				template = template.replace(reg, tag);
			}
		}
	}
	
	document.getElementById("holder").innerHTML = template;
	
	//reset autoplay and checks for each new slide
	resetAutoPlayVars();
	setAutoTimer()
	
	paused = false;
	
	parent.footer.closeLab();
	window.focus();
	window.scrollTo(0,0);
}

function replaceDollarSign(text){
	if(text.indexOf("$") != -1){
		var reg = /\x24/gi;
		text = text.replace(reg, "&#36;");
	}
	return text;
}


function handleMediaElement(me, typeOfME){
	//Get the mime type embed tag
	//var tag = parent.footer.document.getElementById(me.mimeType).innerHTML;
	var tag = parent.footer.getMediaElement(me.mimeType);
	
	if(me.mimeType == "application/x-shockwave-flash"){
		reg = /\x24(id)/gi;
		tag = tag.replace(reg, typeOfME);
		tag = replaceProtocol(tag);
	}	
	
	var reg = /\x24(t)/gi;
	
	if(me.fullText != undefined && me.fullText != null){ //Handling null text
		me.fullText = replaceDollarSign(me.fullText);
		tag = tag.replace(reg, me.fullText);
	} else {
		tag = tag.replace(reg, " ");
	}
	
	var reg = /\x24(u)/gi;
	tag = tag.replace(reg, me.uri);
	reg = /\x24(w)/gi;
	tag = tag.replace(reg, me.width);
	reg = /\x24(h)/gi;
	tag = tag.replace(reg, me.height);
	
	return tag;	
}
/* ** END RENDERING ** */

function checkFillInEnter(evt){
	characterCode = evt.keyCode;
	if(characterCode == 13){
		return false;
	} else {
		return true;
	}
}

//Bogus Calls from Netscape
function Video_DoFSCommand(){}
function Visual_DoFSCommand(){}
function visual_DoFSCommand(){}
function Audio_DoFSCommand(){}
function SplashGraphic_DoFSCommand(){}


