strNoticeTxt = "Those with assistive technologies, the full text for interactions on this page are further down the page.";
noRan = true;
/**
 * @name checkSetGlosseryTerms
 * @example checkSetGlosseryTerms(el);
 * @desc to check to see if there are glossary terms with in the body text
 *			and make them clickable to the glossary
 */
checkSetGlosseryTerms = function(el){
	try{
		var glossaryTerm = $('.glossaryTerm', el);

		if( glossaryTerm.size() ){
		
		if( noRan ){
			$('#bodyText').prepend('<div class="noNakedEye">'+strNoticeTxt+'</div>');
			noRan = false;
		}
		
		
			glossaryTerm.each(function(){
				var el = $(this);
				if( el.is('span') ){
					el.replaceWith('<a href="#" class="glossaryTerm" title="Link to glossary term" data="'+el.attr('data')+'">' + el.text() + "</a>");
				}
			});
		
			if(typeof docXML[1] == 'undefined'){
				if( isDynamic || isFramed ){
					loadXml(glossaryXMLFramed, 1);
				}
				else{
					loadXml(glossaryXML, 1);
				}
			}
			
			var dl = $('<dl class="noNakedEye">').appendTo(el);
			
			$('.glossaryTerm', el)
			.each(function(){
				var jqthis = $(this);
				var data = jqthis.metadata({type:'attr',name:'data'});				
				var term = $('term[id='+data.id+']', docXML[1]);
				var name = $('name', term).text();
				var definition = $('definition', term).text();
				
				dl.append('<dt>'+name+'</dt>');
				dl.append('<dd>'+definition+'</dd>');
			})
			.click(function(e){
				var jqthis = $(this);
				var data = jqthis.metadata({type:'attr',name:'data'});				
				var term = $('term[id='+data.id+']', docXML[1]);
				var name = $('name', term).text();
				var definition = $('definition', term).text();
				
				if( typeof window.jqGlossaryTerm == 'undefined' ){
					window.jqGlossaryTerm = $('<div class="infoBox" style="display:none;position:absolute;">').appendTo('body').bind('mouseenter',function(){
						clearTimeout(window.jqGlossaryTerm.to);
					}).bind('mouseleave',function(){});
					
					window.jqGlossaryTerm.append('<div class="top"><div style="width:100%;position:relative;"><div class="lcorner"></div><div class="mid"></div><div class="rcorner"></div></div></div><a href="#" class="close imagereplace" title="Close Glossary term" id="closeTerm">close</a>');
					window.jqGlossaryTerm.append('<div class="lgut"><div class="rgut"><div class="center" id="infoBoxGut"></div></div></div>');
					window.jqGlossaryTerm.append('<div class="bot"><div style="width:100%;position:relative;"><div class="lcorner"></div><div class="mid"></div><div class="rcorner"></div></div></div>');
					
					window.jqGlossaryTerm.find('a.close').click(function(){
						window.jqGlossaryTerm.fadeOut(100);
						wbt.jqGlossaryTerm = false;
						return false;
					});
				}
				else{
					$('#infoBoxGut', window.jqGlossaryTerm).empty();
				}
				
				$('#infoBoxGut', window.jqGlossaryTerm).append('<h3 class="title">'+name+'</h3>');
				$('#infoBoxGut', window.jqGlossaryTerm).append(definition);
				
				/*$(window).keypress(function(e){
					if( e.ctrlKey && e.shiftKey && e.which == 71 ){	
						window.jqGlossaryTerm.find('a.close').trigger('click');
					}
				});*/
				var w = jqthis.width();
				var c = window.jqGlossaryTerm.width() / 2;				
				var h = window.jqGlossaryTerm.height() / 2;
				
				window.jqGlossaryTerm.css({marginTop:-h,marginLeft:-c}).fadeIn(1000, function(){window.jqGlossaryTerm.focus();});
				wbt.jqGlossaryTerm = true;
				return false;
			});
		}
	}
	catch(e){
		$.iLogger.log(e.name + ': ' + e.message, 'error', 'checkSetGlosseryTerms();');
	}
};

inlineQuizCorrectInCorrect = function(el){
	try{
		el = $('.inlineQuizCorrectInCorrect', el);
		
		if( el.size() > 0 ){
			var inline = $('> li:first', el);
			var answers =  $('li', inline);
			var correctAnswers =  $('li:contains("correct:")', inline);
			
			if( correctAnswers.size() === 0 ){
				correctAnswers =  $('li:first', inline);
			}
			
			var correct = null;
			var incorrect = null;
			var correctTxt = null;
			var incorrectTxt = null;
			
			el.addClass('inlineQuizList');
			
			if( el.is('ol') ){
				
				var cleanedText = correctAnswers.text().replace('correct:','');
				cleanedText = $.trim(cleanedText);
				correctAnswers.text(cleanedText);
				
				correct = $('> li:eq(1)', el);
				incorrect = $('> li:eq(2)', el);
				
				correctTxt = correct.html();
				incorrectTxt = incorrect.html();
				
				correct.remove();
				incorrect.remove();
			
				window.correctAnswer = escape(correctAnswers.text());

				answers.each(function(){
					var li = $(this);
					var input = $('<input type="radio" name="a" value="'+escape(li.text())+'"/>').click(function(){
						var txt = $(this).val();
						var elem;
						
						if( txt == window.correctAnswer ){
							elem = $('<p class="highlight">'+correctTxt+'</p>');
							el.after(elem);
						}
						else{
							elem = $('<p class="highlight">'+incorrectTxt+'</p>');
							el.after(elem);
						}
						
						elem.addClass('nohighlight', 1000);
						answers.find('input').attr('disabled',true);
					}); 
					
					li.prepend(input).wrapInner('<label>');
				});
			}
			else{
				window.correctAnswer = Array();
				correctAnswers.each(function(i){
						$(this).text( $.trim( $(this).text().replace('correct:','') ) );
						window.correctAnswer.push( escape($(this).text()) );
				});
				
				correct = $('> li:eq(1)', el);
				incorrect = $('> li:eq(2)', el);
				
				correctTxt = correct.html();
				incorrectTxt = incorrect.html();
				
				correct.remove();
				incorrect.remove();
				
				answers.each(function(){
					var li = $(this);
					var input = $('<input type="checkbox" name="a" value="'+escape(li.text())+'"/>');
					
					li.prepend(input).wrapInner('<label>');
				});
				
				var button = $('<input type="submit" name="done" class="btn_green" value="Done"/>').click(function(){
					var a = Array();
					answers.find('input:checked').each(function(){
						a.push( $(this).val() );
					});
					
					var diff = array_diff( window.correctAnswer , a );
					
					var elem = $('<p class="highlight"></p>');

					if( diff.length || ( window.correctAnswer.length !== a.length ) ){
						elem.text(incorrectTxt);
						el.after(elem);
					}
					else{
						elem.text(correctTxt);
						el.after(elem);
					}
					
					elem.addClass('nohighlight', 1000);

					answers.find('input').attr('disabled',true);
					$(this).remove();
					return false;
				});
				
				el.after(button);
				
			}
		}
	}
	catch(e){
		$.iLogger.log(e.name + ': ' + e.message, 'error', 'inlineQuizCorrectInCorrect();');
	}
};

/**
 * @name checkSetNewWindow
 * @example checkSetNewWindow(this);
 * @desc 
 * @params
 * 		width: the width of the modal box
 *		height: the TOTAL height of the modal box
 *		position: set the position, takes center or {left:20, top: 20}
 *		restrict: set to true if the next button should be hidden
 *		titleHeight: the height of the title bar
 *		
 */
checkSetNewWindow = function($this){
	$.iLogger.log('checkSetNewWindow();');
	try{
		var jqEl = $('a.optNewWindow', $this);
		var options = {
			width : 760,
			height : 500,
			position : {left: 0, top : 0},
			scroll : false
		};

		jqEl.each(function(i){
			var jqthis = $(this);
			var data = jqthis.metadata();
			
			jqthis.click(function(){
				var data = jqthis.metadata();
				data = $.extend(options,data);
				popIt(data, jqthis.attr('title'), jqthis.attr('href'));
				return false;				  
			});
			
		});
		
		function popIt(data, title, url){
			if(data.position=="center"){
				data.position = {
					left:(screen.width)?(screen.width-data.width)/2:100,
					top:(screen.height)?(screen.height-data.height)/2:100
				};
			}
			else if(typeof data.position != Array || typeof data.position != Object ){
				data.position = {
					left:0,
					top:0
				};
			}
			
			settings='width='+data.width+',height='+data.height+',top='+data.position.top+',left='+data.position.left+',scrollbars='+data.scroll+',location=no,directories=no,status=no,menubar=no,toolbar=no,resizable=yes';
			title = (title) ? title : 'NewWindow';
			win=window.open(url,title,settings);
		}
	}
	catch(e){
		$.iLogger.log(e.name + ': ' + e.message, 'error', 'checkSetModal();');
	}
};
/**
 * @name checkSetModal
 * @example checkSetModal(this);
 * @desc 
 * @params
 * 		width: the width of the modal box
 *		height: the TOTAL height of the modal box
 *		external: set to true for loading an external url
 *		restrict: set to true if the next button should be hidden
 *		titleHeight: the height of the title bar
 *		
 */
checkSetModal = function($this){
	$.iLogger.log('checkSetModal();');
	try{
		var jqEl = $('.modalBox, .optModalBox', $this);
		var options = {
			width:722,
			height:336,
			external : false,
			restrict : false,
			titleHeight : 30,
			isImg : false,
			modal : false,
			
			resizeable : true,
			draggable	: true
		};
		
		jqEl.each(function(){
			var el = $(this);
			if( el.is('span') ){
				el.replaceWith('<a href="#" class="optModalBox" data="'+el.attr('data')+'">' + el.text() + "</a>");
			}
		});
		
		var jqEl = $('.modalBox, .optModalBox', $this);
				
		// for checking if url is an img
		var reg = /(.jpg|.png|.gif)/i;
		
		jqEl.each(function(i){
			var jqthis = $(this);
			var tmpData = jqthis.metadata({type:'attr',name:'data'});
			
			if( ( tmpData.restrict ) && ($sitedata.lessonLevel[$locationMarker].sInlineQuiz != 1) ){
				$('#next').hide();
			}
			
			jqthis.click(function(){
				var el = $(this);
				this.data = el.metadata({type:'attr',name:'data'});
				this.data = $.extend(options,this.data);
				buildIt(this.data, el.attr('title'), el.attr('href'), this);
				return false;
			});
			
		});
	
		function buildIt(opts, title, url, that){
			/*
			if( typeof that.dialog != "undefined" && that.dialog != null ){
				$(that.dialog).dialog('open');
				return false;
			}*/
			
			var jqmWindow = $('<div class="dialogWindow"></div>');
			var dialogClass = null;
			
			
			if( typeof opts.id != "undefined" ){
				
				$('<div class="dtarget"/>').append( $('#'+opts.id).clone(true).removeClass('noNakedEye').removeAttr('style') )
				.appendTo(jqmWindow);
				
			}
			else if( opts.external ){
				dialogClass = 'isFramed';
				jqmWindow
				.append('<iframe src="'+url+'" width="100%" height="95%" frameborder="0" id="dtarget" allowtransparency="1" marginheight="0" marginwidth="0" scrolling="auto"/>');
			}
			else if( opts.isImg || url.match(reg ) ){
				dialogClass = 'isFramed';
				
				var image = new Image();
				
				image.onload = function(){
					image.onload = null;
					jqmWindow.append('<img src="'+url+'"/>')
					.height((image.height + opts.titleHeight))
					.width(image.width)
					.css({marginLeft:-(image.width/2)});
				
				};
				image.src = url;
			}
			else{
				dialogClass = 'isFramed';
				var href = vars.pagePath + url;

				if( opts.framed ){
					jqmWindow
					.append('<iframe src="'+href+'?time='+new Date().getTime()+'" width="100%" height="95%" frameborder="0" id="dtarget" allowtransparency="1" marginheight="0" marginwidth="0" style="margin:0;" scrolling="auto"/>')
				}
				else{
					
					$('<div id="dtarget"/>')
					.css({
						width:opts.width, 
						height:(opts.height - opts.titleHeight),
						overflow:'auto'
						})
					.appendTo(jqmWindow);
					
				}
			}
			
			if( typeof wbt.checkSetModal == "undefined" ){
				wbt.checkSetModal = {ran:true};
			}
			
			jqmWindow.dialog({
				height	: opts.height,
				width	: opts.width,
				modal	: opts.modal,
				draggable : opts.draggable,
				resizeable : opts.resizeable,
				title	: title,
				dialogClass : dialogClass, 
				minWidth	: 570,
				overlay: { 
					opacity: 0.5, 
					background: "black" 
				},
				open : function(e, ui){
					that.dialog = this;
					if( typeof wbt.checkSetModal.dialog == "undefined" ){
						wbt.checkSetModal.dialog = Array();
					}
					wbt.checkSetModal.dialog.push(this);
										
					if( $.browser.msie && $.browser.version < 7 ){
						var el = $(this);
						var p = el.parent().parent().height();
						var titleH = el.prev().height();
						el.height( p - titleH - 30 );
					}
					
				},
				close : function(){
					$(this).dialog('destroy').remove();
				},
				resizeStop : function(e, ui){
					if( $.browser.msie && $.browser.version < 7 ){
						var el = $(this);
						var titleH = el.prev().height();
						el.height( ui.size.height - titleH - 30 );
					}
				}
			});
			
/*
		var container = this.uiDialogContainer,
			titlebar = this.uiDialogTitlebar,
			content = this.element,
			tbMargin = (parseInt(content.css('margin-top'), 10) || 0)
				+ (parseInt(content.css('margin-bottom'), 10) || 0),
			lrMargin = (parseInt(content.css('margin-left'), 10) || 0)
				+ (parseInt(content.css('margin-right'), 10) || 0);
		content.height(container.height() - titlebar.outerHeight() - tbMargin);
		content.width(container.width() - lrMargin);
*/

			/*
			jqmWindow
			.jqm({
				zIndex:3000,
				overlay:50,
				target:'#dtarget',
				modal: opts.modal,
				ajax : ( !opts.framed ) ? href : false,
				onHide: function(hash, serial){
					hash.o.remove();
					hash.w.remove();
				},
				onLoad: function(){
				}
			}).jqmShow();
			*/
		}
	}
	catch(e){
		$.iLogger.log(e.name + ': ' + e.message, 'error', 'checkSetModal();');
	}
};

checkSetInlineTestQuestion = function($this){
	$.iLogger.log('checkSetInlineTestQuestion();');
	try{
		var inlineTextQuestion = $('.optInlineTextQuestion', $this);
		
		if( inlineTextQuestion.size() ){
			inlineTextQuestion
			.each(function(i){
				var jqthis = $(this);
				var p = jqthis.parent();
				var data = jqthis.metadata({type:'attr',name:'data'});
				var e = this;
				e.input = jqthis;
				e.data = data;
				e.index = i;
			});
			
			var sbmt = $('<input type="submit" name="doAnswer" value="Done" class="btn_green"/>');
			
			if( $('#optInlineTextQuestionSubmit').size() ){
				$('#optInlineTextQuestionSubmit').replaceWith(sbmt);
			}
			else{
			
				var lastOne = inlineTextQuestion.eq( inlineTextQuestion.size()-1 );
				var lastOneParents = lastOne.parents();
				var lastOneParent = null;
				
				lastOneParents
				.each(function(i){
					if( lastOneParent === null ){
						var el = $(this);
						
						if( el.parent().is('#bodyText') ){
							lastOneParent = el;
						}
					}
				});
	
				lastOneParent.after(sbmt);
			
			}
			
			sbmt
			.bind('click', function(){
				var el = $(this);
				var goodToGo = false;
				
				inlineTextQuestion
				.each(function(i){
					var it = this;
					
					if( it.input.val() === '' ){
						alert('Please enter an answer(s)');
						it.focus();
						ccnt.push(false);
						return false;
					}

					var txt = "";
					var val = it.input.val();
					var id = "inlineTextQuestion"+it.index;
					var data = $(it).metadata({type:'attr',name:'data'});
					var p = $(it).parent();

					val = val.toString().toLowerCase();
					var ex = data.a.toString().toLowerCase().replace(/\$/, "\\$").replace(/\./, "\\.");
					var regex = new RegExp('^('+ex+')$');
					
					if( regex.exec( val ) ){
						txt = data.c;
					}
					else{
						txt = data.i;
					}
					
					$("#"+id).remove();

					var elem = $("<p id='"+id+"' class='highlight'>"+txt+"</p>");
					
					var optinlineTextQuestionDiv = $('#optinlineTextQuestionDiv');
					
					if( optinlineTextQuestionDiv.size() ){
						optinlineTextQuestionDiv.append(elem);
					}
					else if( p.is("p") ){
						p.after(elem);
					}
					else{
						$(it).after(elem);
					}
					
					elem.addClass('nohighlight', 1000);
					
					goodToGo = true;
				});
				
				if( goodToGo ){
					sbmt.remove();
				}
				
				return false;
			});
		}	
	}
	catch(e){
		$.iLogger.log(e.name + ': ' + e.message, 'error', 'checkSetInlineTestQuestion();');
	}
};

checkSetImageMap = function($this){
	specialCases($this);
	$.iLogger.log('checkSetImageMap();');
	try{
		var img = $('img[usemap]', $this);
		
		if( noRan ){
			$('#bodyText').prepend('<div class="noNakedEye">'+strNoticeTxt+'</div>');
			noRan = false;
		}
		
		if( img.size() ){
			
			var id = img.attr('usemap');
			var map = $(id);
			var areas = $('area', map);
			
			if( typeof wbt.toolTip == 'undefined' ){
			
				window.toolTip = $('<div class="infoBox" style="display:none;position:absolute;">').appendTo('.wrap');
				window.toolTip.append('<div class="top"><div style="width:100%;position:relative;"><div class="lcorner"></div><div class="mid"></div><div class="rcorner"></div></div></div><a href="#" class="close imagereplace">close</a>');
				window.toolTip.append('<div class="lgut"><div class="rgut"><div class="center" id="infoBoxGut"></div></div></div>');
				window.toolTip.append('<div class="bot"><div style="width:100%;position:relative;"><div class="lcorner"></div><div class="mid"></div><div class="rcorner"></div></div></div>');

				window.toolTip.find('a.close')
				.click(function(){
					window.toolTip.fadeOut(100);
					wbt.toolTip = false;
					return false;
				});
			}
			else{
				$('#infoBoxGut', window.toolTip).empty();
			}
			
			areas
			.each(function(){
				var el = $(this);
				var div = el.attr('rel');
				el
				.attr({'rel':"#"+div,'href':"#"+div})
				.click(function(event){
					var el = $(this);
					var title = el.attr('alt');
					var id = el.attr('rel');
					$('#infoBoxGut', window.toolTip).empty();
					
					$('#infoBoxGut', window.toolTip).append('<h3 class="title">'+title+'</h3>');
					$('#infoBoxGut', window.toolTip).append( $(id).html() );
					
					var c = window.toolTip.width() / 2;				
					var h = window.toolTip.height() / 2;
					
					window.toolTip.css({marginTop:-h,marginLeft:-c}).fadeIn(1000,function(){window.toolTip.focus();});
					wbt.toolTip = true;
				
					return false;
				});
			});
		}
	}
	catch(e){
		$.iLogger.log(e.name + ': ' + e.message, 'error', 'checkSetInlineTestQuestion();');
	}
};

inlineSpecific = function($this){
	var optInlineSpecific = $('input.optInlineSpecific', $this);
	
	if( optInlineSpecific.size() ){
		optInlineSpecific
		.each(function(){
			var it = this;
			var el = $(this);
			var data = el.metadata({type:'attr',name:'data'});
			
			var inputs = data.el.split(',');
			var inputSize = inputs.length;
			var operator = data.op.split(',');
			
			if( operator.length === 1 ){
				var curO = operator[0];
				var operator = Array();
				for( var i = 0; i < inputSize; i++ ){
					operator[i] = curO;
				}
			}
			
			var prev = null;
			var total = 0;
			
			for( i = 0; i < inputSize; i++ ){
				input = $('input[name="'+inputSize[i]+'"]');
				
				var e = input.get(0);
				
				e.el = el;
				e.i = i;
				
				input
				.keyup(function(){
					var val = this.el.val();
					
					if( typeof this.jqthis == "undefined" ){
						this.jqthis = $(this);
					}
					
					if( val == '' ){
						val = 0;
					}
					
					inputVal = this.jqthis.val().replace(/\$+/g, "");
					
					if( isNaN( parseFloat( inputVal ) ) ){
						inputVal = 0;
					}
					else{
						inputVal = parseFloat( inputVal );
					}
					
					
									 
				});

			}
		});
	}
};

specialCases = function( html ){
	$('#doStep1')
	.click(function(){
		var options = {
			width:701,
			height:500,
			external : false,
			restrict : false,
			titleHeight : 30,
			isImg : false,
			modal : true,
			id : 'isStep1'
		};
		goBuildIt(options, "This is a title", 'step1');
		return false;
	});// end $('doStep1')
};

tmplog = function(v){
	try{
		console.log(v);
	}catch(e){}
};

function goBuildIt(opts, title, step){
	// for checking if url is an img
	var reg = /(.jpg|.png|.gif)/i;

	var jqmWindow = $('<div class="jqmWindow" id="theDialog" style="width:'+opts.width+'px;height:'+opts.height+'px;margin-left:-'+(opts.width/2)+'px;top:20px;">').appendTo('body').append('<div class="head"><div class="title">'+title+'</div><div class="jqmCloseDiv"><a class="jqmClose" href="#">close</a></div></div>');
			
		$('<div id="dtarget2"/>').append( $('#'+opts.id).clone(true).removeClass('noNakedEye').removeAttr('style') )
		.css({
			width:opts.width, 
			height:(opts.height - opts.titleHeight),
			overflow:'auto'
			})
		.appendTo(jqmWindow);

	
	
	jqmWindow
	.jqm({
		zIndex:3000,
		overlay:50,
		target:'#dtarget2',
		modal: opts.modal,
		ajax : false,
		onHide: function(hash, serial){
			hash.o.remove();
			hash.w.remove();
		},
		onShow: function(hash){
			hash.w.show();
			var c = $('#dtarget2',hash.w);
			var input = $('input[type=text]', hash.w).removeAttr('disabled');
			var sbmt = $('<input type="submit" name="doAnswer" value="Done" class="btn_green"/>');
			
			c.append(sbmt);
			
			if( step == 'step1'){
				doStep1(c);
			}
		}
	}).jqmShow();
}

doStep1 = function( v ){
	var inputs = $('input[type=text]', v);
	var smt = $('input[type=submit]', v);
	
	smt
	.click(function(){
		var errors = Array();
		
		inputs.each(function(){
			var el = $(this);
			var data = el.metadata({type:'attr',name:'data'});
			var val = el.val().replace(/\$|,/g, "");
			var title = data.title;
			var name = $(this).attr('name');

			if( data.empty === true && val != '' ){
				el.css('border-color','red');
				this.focus();
				alert(title+' should be empty');
				errors.push(true);
				return false;
			}
			else if( data.empty === false && val == '' ){
				el.css('border-color','red');
				this.focus();
				alert(title+' should be filled in');
				errors.push(true);
				return false;
			}
			else if( data.empty === false && data.value != val ){
				el.css('border-color','red');
				this.focus();
				alert(title +' is incorrect');
				errors.push(true);
				return false;				
			}
			else{
				el.css('border-color','black');
			}
			
			$('input[name='+name+']:eq(0)').val( val.toString() );
			
			
		});
		
			
		if( errors.length == 0 ){
			$('.jqmClose').click();
		}
		
		return false;
	});
};