
var tpgNotifier; // global var that we are storing the notifier object in. 

function updateCountdown(objComment) {
	var objCountdown = $('countdown');
	var objReplacement = document.createElement('strong');
	var iLength = 80 - parseInt(objComment.value.length, 10);
	var notificationPref = TPG.Cookie.getCookieParam('tpgNotifierPrefs', 'notificationMethod',  "flash", ["flash", "alerts", "liveregion"]);
	
	//when appropriate, notify the user with a previously defined notification 
	switch(iLength) {
		case 30:
			tpgNotifier.notify('alert1');
			break;
		case 15:	
			tpgNotifier.notify('alert2');
			break;
		case 5:
			tpgNotifier.notify('alert3');
			break;
	}
	
	if (iLength <= 0) {
		objComment.value = objComment.value.substring(0, 79);
	}	
	objReplacement.setAttribute('id', 'countdown');
	objReplacement.appendChild(document.createTextNode(iLength));
	objCountdown.parentNode.replaceChild(objReplacement, objCountdown);
}

//This function is included for the purpose of demomnstrating multiple sounds and sound schemes. Normally you would not need this much code to use 
// the TPG notifier. In other words, ignore this function as it is not a required part of the notifier.
function setSounds() {
	var doNotUpdateSchemes = false;
	var ignorebasePath = false;
	var fileIndexArray = [-1, -1, -1];
	var mp3FileNames = [
		"SFX_GL_Menu_Selection_MouseOver.mp3", //0
		"typebell.mp3", //1
		"smash.mp3", //2
		"setlink.mp3", //3
		"pathblocked.mp3", //4
		"mysttest.mp3",	 //5
		"speech1.mp3",// 6
		"speech2.mp3", // 7
		"speech3.mp3" // 8					
		];	
	
	var profileIndex = TPG.Cookie.getCookieParam('tpgNotifierPrefs', 'profileIndex', 0);
	if (!profileIndex || isNaN(profileIndex)) {
		profileIndex = 0;
	}
	var schemeIndex = TPG.Cookie.getCookieParam('tpgNotifierPrefs', 'schemeIndex', 0);
	if (!schemeIndex || isNaN(schemeIndex)) {
		schemeIndex = 0;
	}
	switch (parseInt(profileIndex)) {
		case 5://recorded speech
			fileIndexArray[0] = 6;
			fileIndexArray[1] = 7;
			fileIndexArray[2] = 8;
			doNotUpdateSchemes = true;
			break;
		case 6: // custom sound	
			var soundFilePath = TPG.Cookie.getCookieParam('tpgNotifierPrefs', 'customSoundPath', "");
			mp3FileNames.push(soundFilePath);
			fileIndexArray[0] = mp3FileNames.length - 1;
			ignorebasePath = true;
			break;		
		default: // any other sound
			fileIndexArray[0] = profileIndex;
			break;
	}
	
	if (parseInt(schemeIndex) == 3) {
		// random sounds	
		var indexMax = 4;
		fileIndexArray[0] = TPG.getRnd(0, indexMax);
		fileIndexArray[1] = TPG.getRnd(0, indexMax);
		fileIndexArray[2] = TPG.getRnd(0, indexMax);	
		doNotUpdateSchemes = true;									  
	}
	
	var schemeValues = [
		//loops values
		1,
		2,
		3,
		//volume values
		5,
		50,
		100,
		//pan values
		-100,
		0,
		100
	];
	
	//set defaults for sound properties
	var sndId = "", loops = 1, volume = 50, pan = 0, offset = 0;
	//create sound objects and add them to the notificaitons we created earlier using tpgNotifier.addNotification()
	for (var i = 0; i < fileIndexArray.length; i++) {
		if (!doNotUpdateSchemes) {
			// set the sound parameters based on the chosen scheme index. Otherwise, keep defaults	
			loops = schemeValues[TPG.checkNumberVar(i + schemeIndex * 3, 0, 0, 2)];
			volume = schemeValues[TPG.checkNumberVar(i + schemeIndex * 3, 4, 3, 5)];
			pan = schemeValues[TPG.checkNumberVar(i + schemeIndex * 3, 7, 6, 8)];
		}
		// if the file index is set to the default (-1), don't create a new sound, but use the id of an earlier created sound object
		sndId = fileIndexArray[i] != -1 || sndId === "" ? tpgNotifier.createSound(mp3FileNames[fileIndexArray[i]], ignorebasePath) : sndId;
		//use the sound ID we received when using tpgNotifier.createSound, and add it to the notifier we created using tpgNotifier.createNotification 
		tpgNotifier.setNotificationSound('alert' + (i + 1), sndId, loops, volume, pan, offset);
	}
	//load sound test
	tpgNotifier.setNotificationSound('testAlert', tpgNotifier.createSound(mp3FileNames[5]));	
}


function init() {
	var updateMsg = $('updateMsg');
	var objComment = $('comment');
	var objCountdown = document.createElement('strong');
	var iLength;
	var i;
	var method;
	var objSpan = document.createElement('span'); 
	
	//get cookie parameters 
	TPG.enableLogger(TPG.Cookie.getCookieParam('tpgNotifierPrefs', 'enableLogger', false));		
	method = TPG.Cookie.getCookieParam('tpgNotifierPrefs', 'notificationMethod', "flash", ["flash", "alerts", "liveregion"]);
	//create dynamically updating label for text area
	iLength = 80 - parseInt(objComment.value.length, 10);	
	objCountdown.setAttribute('id', 'countdown');
	objCountdown.appendChild(document.createTextNode(iLength));
	
	updateMsg.appendChild(document.createTextNode('('));	
	objSpan.appendChild(objCountdown);
	objSpan.appendChild(document.createTextNode(' characters left'));
	objSpan.id = "liveRegion1";
	updateMsg.appendChild(objSpan);
	updateMsg.appendChild(document.createTextNode(')'));
	
	objComment.onkeydown = function() {return updateCountdown(this);};
	
	//object used to send any appropriate parameters to the TPG.Notifier constructor
	var params = {};
	params.basePath = "flash/sounds/";
	params.id = "liveRegion1";
	params.parentId = "textAreaContainer";
	params.tag = "em";
	params.hideLiveRegion = false;
	params.ignoreAlerts = true;
	params.className = "salient";
	params.makeParentLive = "false";
	params.triggerSuffix = "";
	params.callFunction = function() {
		TPG.Focus.drawAttention(params.id, 0);
	};
	// create the notifier object
	tpgNotifier = new TPG.Notifier(method, params);
	// We ALWAYS set text notifications, even when using Flash. This way the notifier can fall back if Flash is not supported
	tpgNotifier.addNotification("30 characters left", "alert1");
	tpgNotifier.addNotification("15 characters left", "alert2");
	tpgNotifier.addNotification("5 characters left, wrap it up!", "alert3");
	tpgNotifier.addNotification("This is a test notification", "testAlert"); 
	
	if (method == "flash") {
		setSounds();
	} 
	//enable the 'test notification method' button
	$("soundCheck").onclick = function() {tpgNotifier.notify('testAlert')};
}

swfobject.addDomLoadEvent(init);
