// JavaScript Document

var accordViewer = Class.create();
accordViewer.prototype = {
	//
	//	Setup Variables
	//
	element : null,
	contentToFetch : null,
	windowType : null,
	overrideFileType : false,
	overrideFileExtension : false,
	showCardToggle : false,
	showCardId : '',
	inlineContent : null,
	searchResultsContent : null,
	customClose : null,
	packageId : null,
	packageTitle : null,
	//
	//	Initialize the accordViewer.
	//
	initialize : function(options) {
		courseInProgress = 1;
		this.options = Object.extend({
			dimensions : {
				media : {height : 500, width : 500},
				swf : {height : 250, width : 250},
				pdf : {height : 500, width : 700},
				inline : {height : 500, width : 500},
				external : {height : 500, width : 500}
			},
			classNames : {	
				standard : 'aVActivate'
			},
			fileTypes : {
				media : ['pdf', 'aif', 'aiff', 'asf', 'avi', 'divx', 'm1v', 'm2a', 'm2v', 'm3u', 'mid', 'midi', 'mov', 'moov', 'movie', 'mp2', 'mp3', 'mpa', 'mpa', 'mpe', 'mpeg', 'mpg', 'mpg', 'mpga', 'pps', 'qt', 'rm', 'ram', 'swf', 'viv', 'vivo', 'wav']
			},
			mimeTypes : {
				avi : 'video/avi',
				aif : 'audio/aiff',
				aiff : 'audio/aiff',
				gif : 'image/gif',
				bmp : 'image/bmp',
				jpeg : 'image/jpeg',
				m1v : 'video/mpeg',
				m2a : 'audio/mpeg',
				m2v : 'video/mpeg',
				m3u : 'audio/x-mpequrl',
				mid : 'audio/x-midi',
				midi : 'audio/x-midi',
				mjpg : 'video/x-motion-jpeg',
				moov : 'video/quicktime',
				mov : 'video/quicktime',
				movie : 'video/x-sgi-movie',
				mp2 : 'audio/mpeg',
				mp3 : 'audio/mpeg3',
				mpa : 'audio/mpeg',
				mpa : 'video/mpeg',
				mpe : 'video/mpeg',
				mpeg : 'video/mpeg',
				mpg : 'audio/mpeg',
				mpg : 'video/mpeg',
				mpga : 'audio/mpeg',
				pdf : 'application/pdf',
				png : 'image/png',
				pps : 'application/mspowerpoint',
				qt : 'video/quicktime',
				ram : 'audio/x-pn-realaudio-plugin',
				rm : 'application/vnd.rn-realmedia',
				swf	: 'application/x-shockwave-flash',
				tiff : 'image/tiff',
				viv : 'video/vivo',
				vivo : 'video/vivo',
				wav : 'audio/wav'				
			},			
			loadingDialog : {
				message : 'Loading',
				image :  'apps/resources/shared/images/ajax-loading.gif',
				options : '<a onclick="javascript: myAccordViewer.deactivate_for_real();">Cancel</a>',
				delay : 3.0
			},
			overlay : {
				color : '#000000',
				opacity : 70,
				image : 'apps/resources/shared/images/black-70.png'
			}
		}, options || {})
		this.addViewerMarkup();
		this.setupLinks('div#link-hide');
		this.setupLinks('div#link-hide-home');
		this.setupLinks('div#link-hide-custom');
		this.setupLinks('div#footer');
	},
	//
	//	Add the markup to the page.
	//
	addViewerMarkup : function() {
			var overlay = document.createElement('div');
			overlay.setAttribute('id', 'overlay');
			if(typeof(accordViewerType) != "undefined"){
				if(accordViewerType == 'v3'){
					this.options.overlay.color = '#333333';
					this.options.overlay.image = 'apps/resources/shared/images/gry-70.png';
				}
			}
			if (this.checkBrowser('firefox')) {
				overlay.style.backgroundImage = 'url('+this.options.overlay.image+')';
				overlay.style.backgroundRepeat = 'repeat';
			} else {
				overlay.style.backgroundColor = this.options.overlay.color;
				overlay.style.MozOpacity = '.'+this.options.overlay.opacity;
				overlay.style.opacity = '.'+this.options.overlay.opacity;
				overlay.style.filter = 'alpha(opacity='+this.options.overlay.opacity+')';
			}
			
			//main content
			var avBody = this.avBody = document.createElement('div');
			avBody.setAttribute('id', 'accordBody');
			
			var avHeader = document.createElement('div');
			avHeader.setAttribute('id', 'accordHeader');
			avBody.appendChild(avHeader);
			
			var avTitle = document.createElement('div');
			avTitle.setAttribute('id', 'accordTitle');
			var avClose = document.createElement('div');
			avClose.setAttribute('id', 'accordClose');
			avHeader.appendChild(avTitle);
			avHeader.appendChild(avClose);
			
			var av = document.createElement('div');
			av.setAttribute('id', 'accordViewer');
			
			//loading-dialog
			var avl = '<div id="accordViewer-loading"><img src="'+this.options.loadingDialog.image+'"/><span id="accordViewer-loading-message">'+this.options.loadingDialog.message+'</span><span id="accordViewer-loading-options">'+this.options.loadingDialog.options+'</span></div>';
			av.innerHTML = avl;
			
			var body = document.getElementsByTagName('body')[0];
			avBody.appendChild(av);
			body.appendChild(overlay);
			body.appendChild(avBody);	
			//this.deactivateObj = this.deactivate.bindAsEventListener(this);
			//Event.observe(overlay, 'click', this.deactivateObj, false);
			//overlay.onclick = function() {return false;};

			

			// This does not work because of an exception thrown from
			// Prototype.

			// // Create a dialog to confirm closing
			// this.confirm_close_dialog
			// = new Element('div', {
			// 		'class': 'confirm dialog',
			// 		'id': 'dialog-confirm-exit-without-test',
			// 		'style': 'width: 300px; height: 200px; '
			// 	})
			// .update('<p>Are your sure you want to exit? '
			// 						+ 'You have not taken your test.</p>'
			// 						+ '<button id="confirm-exit-without-test">Yes</button>'
			// 						+ '<button id="deny-exit-without-test">No</button>');

			// // Append it to the document body above the viewer
			// body.appendChild(this.confirm_close_dialog);


			// This may work better:

			var self = this; // Cheap hack

			var confirm_close_dialog           // Local scope accessor
			= this.confirm_close_dialog        // Object wide accessor
			= document.createElement('div');   // Create a dialog div
			// Change these to customise your popup
			this.confirm_close_dialog.id = 'confirm-close-dialog';
			this.confirm_close_dialog.style.color = "#3FBF68";
			//this.confirm_close_dialog.style.font = "normal big arial";
			this.confirm_close_dialog.style.padding = "13px";
			this.confirm_close_dialog.style.background = "#ffffff";
			this.confirm_close_dialog.style.border = "3px solid #99CC66";
			// Don't, however, change these
			this.confirm_close_dialog.style.left = '30%';
			this.confirm_close_dialog.style.top = '200px';
			this.confirm_close_dialog.style.position = 'absolute';
			this.confirm_close_dialog.style.width = '310px';
			this.confirm_close_dialog.style.height = '100px';
			this.confirm_close_dialog.style.zIndex = 1000;
			this.confirm_close_dialog.style.display = 'none';

			// Append it to the document body
			body.appendChild(this.confirm_close_dialog);

			$(this.confirm_close_dialog).update(
				'<p>Are you sure you want to exit this course?<br/>'
				+ 'You have not completed your test.</p>'
				+ '<center>'
				+ ' <button id="confirm-exit-without-test">Yes</button>'
				//				+ ' <button id="deny-exit-without-test">No</button>'
				+ ' <button id="take-test-instead-of-exit">Proceed to test</button>'
				+ '</center>'
			);
			// $('deny-exit-without-test').setStyle({
			// 		'marginLeft': '100px', 'marginRight': '100px'
			//   });
			$('take-test-instead-of-exit').setStyle({
					'marginLeft': '100px'
			  });
			$('confirm-exit-without-test').observe('click', function(event){
					confirm_close_dialog.style.display = 'none';
					self.avBody.style.visibility = 'visible';	
					self.deactivate_for_real();
				});
			// $('deny-exit-without-test').observe('click', function(event){
			// 		confirm_close_dialog.style.display = 'none';
			// 		self.avBody.style.visibility = 'visible';	
			// 	});
			$('take-test-instead-of-exit').observe('click', function(event){
					confirm_close_dialog.style.display = 'none';
					self.avBody.style.visibility = 'visible';	
					dynamicActivateTestSubmit();
				});
	},
	//
	//	Activate the accordViewer from a link onClick request.
	//
	activate : function(e, link){
		this.showCardToggle = false;
		if(!link){
			link = e;
		}
		link.blur();
		this.element = link;
		this.element.title = link.getAttribute('title');
		this.element.rel = link.getAttribute('rel');
		this.element.params = this.element.getAttribute('params');
		var override = link.getAttribute('override');
    	if(override){
        	var parameterPair = override.split(',');
	      this.overrideFileType = parameterPair[0];
			this.overrideFileExtension = parameterPair[1];
     	}
		this.windowType = this.fileType(this.contentToFetch = link.href);
		this.setupDimensions(true);
		this.toggleTroubleElements(true);
		this.displayAccordViewer(true);
		this.monitorKeyboard(true);	
		this.loadInfo(true);	
	},

	//
	//	Turn off the window
	//
	deactivate : function(){
		if (this.close_course_without_test_reminder == true) {
			// If a test is available and not completed, we prompt. If the
			// user does not confirm exit, we return from this function
			// without deactivating the viewer.
			if ($(this.packageTitle + ' Available')
					&& ! $(this.packageTitle + ' Completed')) {
				// if (! confirm('Are you sure you want to exit?'
				// 							+ ' You have not taken your test.')) return;

				this.avBody.style.visibility = 'hidden';	
				this.confirm_close_dialog.style.display = 'block';
				return;
			}
		}
		this.deactivate_for_real();
	},

   // Set a callback for deactivation (used below in deactivate_for_real())
   on_deactivate: function(callback) {
      this.on_deactivate_callback = callback;
   },

	//
   // Turn off the window for real, called by deactivate, and can be
   // used directly to skip the confirm dialog.
	//
	deactivate_for_real: function() {
      if (this.on_deactivate_callback) {
         this.on_deactivate_callback();
         delete this.on_deactivate_callback;
      }
      this.deactivate_without_callbacks();
	},

   //
   // Does the actual work of deactivating for the other functions,
   // can be called directly to skip both the confirm dialog and
   // callbacks
   //
   deactivate_without_callbacks: function() {
		courseInProgress = 0;
		this.displayAccordViewer(false);	
		this.monitorKeyboard(false);
		this.loadInfo(false);
		if(this.windowType == 'inline'){
			$(this.contentToFetch.substring(this.contentToFetch.indexOf('#')+1))
				.innerHTML = this.inlineContent;
		}
		this.customClose = null;
		this.showCardToggle = false;
		this.showCardId = '';
		this.packageId = null;
		this.packageTitle = null;
		this.toggleTroubleElements(false);
   },

	//
	//  Initialize specific viewer
	//
	initializeLink : function (id) {
		var link = $(id);
		if(link){
			Event.observe(link, 'click', this.activate.bindAsEventListener(this, link));
			link.onclick = function() {return false;};
		}
	},
	//
	//  Get file Extension
	//
	fileExtension : function(url) {
		if(this.showCardToggle) return 'swf';
		if (url.indexOf('?') > -1) url = url.substring(0, url.indexOf('?'));
		var extenstion = '';
		for (var x = (url.length-1); x > -1; x--) {
			if (url.charAt(x) == '.') {
				return extenstion;
			}
			extenstion = url.charAt(x)+extenstion;
		}
	},
	//
	//	Get the type of file.
	//
	fileType : function(url) {		
		if (url.indexOf('#') > -1 && (document.domain == this.getDomain(url))) return 'inline';		
		if (url.indexOf('?') > -1) url = url.substring(0, url.indexOf('?'));
		var type = 'unknown';
		if(this.overrideFileType){
			return this.overrideFileType;
		}
		var media = new RegExp("[^\.]\.("+this.options.fileTypes.media.join('|')+")\s*$", "i");
	 	if (media.test(url)) type = 'media';
		return type;
	},
	//
	//	Get the domain from a string.
	//
	getDomain : function(url) {    
		var leadSlashes = url.indexOf('//');
    	var domainStart = leadSlashes+2;
    	var withoutResource = url.substring(domainStart, url.length);
    	var nextSlash = withoutResource.indexOf('/');
    	var domain = withoutResource.substring(0, nextSlash);
		if (domain.indexOf(':') > -1){
			var portColon = domain.indexOf(':');
			domain = domain.substring(0, portColon);
  		}
		return domain;
   },
	//
	//	Hide Selects from the page because of IE.
	//
	toggleTroubleElements : function(hide){
		var hideObjs = (hide == true ? 'hidden' : 'visible');
		
		var selects = document.getElementsByTagName('select');
		for(var i = 0; i < selects.length; i++) {
			if(selects[i].id != 'skipObjectHide'){
				selects[i].style.visibility = hideObjs;
			}
		}
		
		var objects = document.getElementsByTagName('object');
		for (i = 0; i != objects.length; i++) {
			//skip the menu display - special for Asset Health
			if(objects[i].id != 'dynamicMenu' && objects[i].id != 'skipObjectHide'){
				objects[i].style.visibility = hideObjs;
			}
		}
		
		var embeds = document.getElementsByTagName('embed');
		for (i = 0; i != embeds.length; i++) {
			//skip the menu display - special for Asset Health
			if(embeds[i].id != 'dynamicMenu' && embeds[i].id != 'skipObjectHide'){
				embeds[i].style.visibility = hideObjs;
			}
		}
		
		/*var iframes = document.getElementsByTagName('iframe');
		for (i = 0; i != iframes.length; i++) {
			iframes[i].style.visibility = hideObjs;
		}*/
	},
	//
	//	Monitor the keyboard while this accordViewer is up
	//
	monitorKeyboard : function(status) {
		if (status) document.onkeydown = this.eventKeypress.bind(this); 
		else document.onkeydown = '';
	},
	//
	//  Perform keyboard actions
	//
	eventKeypress : function(e) {
		if (e == null) var keycode = event.keyCode;
		else var keycode = e.which;
		
		switch (keycode) { 
			case 27: 
				this.deactivate(); 
				break;
			case 13:
				return;
			default:
				break;
		}
	},
	//
	//	Get the contents for the window
	//
	loadInfo : function(display) {	
		var avDoc = $('accordViewer');
		var avTitle = $('accordTitle');
		var avClose = $('accordClose');
		var avContent = '<div id="accordContent"';
		//Event.stopObserving($('overlay'), 'click', this.deactivateObj, false);
		
		if(display){
			switch (this.windowType) {
				case 'media' :	
					var location = window.location;
					var swfLocation = "";
					var rnd = Math.floor(Math.random()*1000);
					if(location.protocol == 'https:' && location.hostname == 'www.assethealth.com'){
						swfLocation = 'http://' + location.hostname; //location.pathname;
					}else if(location.protocol == 'https:' && location.hostname == 'assethealth.com'){
						swfLocation = 'http://' + location.hostname; //location.pathname;
					}
					//special swf for AssetHealth
					if(this.showCardToggle){
						  avContent += '>';
						  avContent += '<object ';
				        avContent += 'classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" ';
				        avContent += 'codebase="swflash.cab#version=8,0,0,0" width="730" height="430" ';
				        avContent += 'id="cardMain" align="middle"> ';
				        avContent += '<param name="allowScriptAccess" value="always" /> ';
				        avContent += '<param name="movie" value="'+swfLocation+'/apps/cardMain.swf?rnd='+rnd+'" /> ';
				        avContent += '<param name="quality" value="high" /> ';
				        avContent += '<param name="bgcolor" value="#ffffff" /> ';
				        avContent += '<param name="wmode" value="transparent" /> ';
                	  avContent += '<param name="FlashVars" value="mainData='
	                  + swfLocation + '/apps/Lesson'
	                  + this.contentToFetch + '.xml&assetsDir='
	                  + swfLocation + '/apps/';
					        if(this.showCardId != ''){
					        	avContent += '&skipCard='+this.showCardId;
					        }
	
	                 //avContent += this.cardOptions.isolateCard;
	                 if(this.cardOptions){
	                 	if(this.cardOptions.isolateCard) avContent += this.cardOptions.isolateCard;
	                 }

				        avContent += '" /> ';
	                 avContent += '<embed FlashVars="mainData='
	                  + swfLocation + '/apps/Lesson'
	                  + this.contentToFetch + '.xml&assetsDir='
	                  + swfLocation + '/apps/';
					        if(this.showCardId != ''){
					        	avContent += '&skipCard='+this.showCardId;
					        }

						  //avContent += this.cardOptions.isolateCard;
	                 
	                 if(this.cardOptions){
	                 	if(this.cardOptions.isolateCard) avContent += this.cardOptions.isolateCard;
	                 }

				        avContent += '" src="'+swfLocation+'/apps/cardMain.swf?rnd='+rnd+'" quality="high" bgcolor="#ffffff" width="730" height="430" ';
				        avContent += 'name="cardMain" align="middle" allowScriptAccess="always" type="application/x-shockwave-flash" ';
				        avContent += 'wmode="transparent" pluginspage="http://www.macromedia.com/go/getflashplayer" /> ';
				        avContent += '</object>';
					}else{
						avContent += 'class="modalHeight">';
						mediaWidth = '100%';
						mediaHeight = '100%';
						if(this.getParameter('mediaWidth') != ''){
							mediaWidth = this.getParameter('mediaWidth');
						}
						if(this.getParameter('mediaHeight') != ''){
							mediaHeight = this.getParameter('mediaHeight');
						}
						avContent += '<embed type="'+this.options.mimeTypes[this.fileExtension(this.contentToFetch)]+'" src="'+this.contentToFetch+'" width="'+mediaWidth+'" height="'+mediaHeight+'" quality="high" wmode="transparent" align="middle" allowscriptaccess="always"/>';
					}
					break;
				case 'inline' :
					avContent += '>';
					this.inlineContent = $(this.contentToFetch.substring(this.contentToFetch.indexOf('#')+1)).innerHTML;
					avContent += this.inlineContent;
					$(this.contentToFetch.substring(this.contentToFetch.indexOf('#')+1)).innerHTML = '';
					break;
				case 'external' :	
					avContent += 'class="modalHeight">';
					avContent += '<iframe id="lightWindow-iframe" name="lightWindow-iframe" height="100%" width="100%" frameborder="0" scrolling="no" src="'+this.contentToFetch+'"></iframe>';
					break;
				default : 
					throw('Page Type could not be determined, please amend this accordViewer URL '+this.contentToFetch);
					break;
			}
			avContent += '</div>';
			avDoc.innerHTML = avContent;
			avTitle.innerHTML = this.element.title;
			if(this.getParameter('aVClose') != "false"){
				//Event.observe($('overlay'), 'click', this.deactivateObj, false);
				if(this.customClose){
					avClose.innerHTML = this.customClose;
				}else{
					avClose.innerHTML = '<a href="#" onClick="myAccordViewer.deactivate();" id="returnAV" class="accordViewerClose">Return</a>';
				}
			}
			if(this.getParameter('aVCloseTitle') != ""){
				avClose.innerHTML = '<a href="#" onClick="myAccordViewer.deactivate();" class="accordViewerClose">'+this.getParameter('aVCloseTitle')+'</a>';	
			}
			
			if(this.getParameter('aVFunct') != ''){
				this.processFunction(this.getParameter('aVFunct'));
			}
		}else{
			//clear the accordViewer
			var avl = '<div id="accordViewer-loading"><img src="'+this.options.loadingDialog.image+'"/><span id="accordViewer-loading-message">'+this.options.loadingDialog.message+'</span><span id="accordViewer-loading-options">'+this.options.loadingDialog.options+'</span></div>';
			avDoc.innerHTML = avl;
			avTitle.innerHTML = '';
			avClose.innerHTML = '';
		}
	},
	//
	//	Process a function request
	//
	processFunction : function(funct){
		switch(funct){
			case 'initSearch':
				initSearch();
				break;
			default:
				break;
		}
	},
	//
	//	Display the accordViewer.
	//
	displayAccordViewer : function(display) {
		if (display) {
			$('overlay').style.display = $('accordBody').style.display = 'block';	

                        courseHoverHideAllDescriptions();

		} else {
			$('overlay').style.display = $('accordBody').style.display = 'none'; 	
		}
	},  
	//
	//	Is this IE?
	//
	checkBrowser : function(type) {
		var detect = navigator.userAgent.toLowerCase();
		var version = parseInt(navigator.appVersion);
		var place = detect.indexOf(type)+1;
		return place;
	},
	//
	//	Get the value from the params attribute string.
	//
	getParameter : function(parameter, parameterString) {
		if (!parameterString) {
			if (this.element.params) {
				var parameterString = this.element.params;
			} else {
				return;
			}
		}
		var parameterValue;
        var parameterPair = parameterString.split(',');
        var compareString = parameter+'=';
        var compareStringLength = compareString.length;
        for (var i = 0; i < parameterPair.length; i++) {
        	if (parameterPair[i].substr(0, compareStringLength) == compareString) {
            	var tmp = parameterPair[i].split('=');
                parameterValue = tmp[1];
                break;
            }
        }
		if (!parameterValue) {
			return false;
        } else {
        	return unescape(parameterValue);
    	}
	},
	//
	//	Setup Dimensions of lightWindow.
	//
	setupDimensions : function(reset) {
		var reqHeight, reqWidth;
		var mediaType;
		
		switch (this.windowType) {
			case 'media' :
				//master media content dimensions
				reqHeight = this.options.dimensions.media.height;
				reqWidth = this.options.dimensions.media.width;
				// media type dimensions override
				mediaType = this.fileExtension(this.contentToFetch);
				if(this.options.dimensions[mediaType]){
					if(reqHeight != this.options.dimensions[mediaType].height || reqWidth != this.options.dimensions[mediaType].width){
						reqHeight = this.options.dimensions[mediaType].height;
						reqWidth = this.options.dimensions[mediaType].width;
					}	
				}
				break;
			case 'inline' :
				//master media content dimensions
				reqHeight = this.options.dimensions.inline.height;
				reqWidth = this.options.dimensions.inline.width;
				break;
			case 'external' : 
				originalHeight = this.options.dimensions.external.height;
				originalWidth = this.options.dimensions.external.width;
				break;
			default :
				break;
		}
		//parameter requested override
		var aVHeight = this.getParameter('aVHeight');
		var aVWidth = this.getParameter('aVWidth');
		if(aVHeight){
			if(reqHeight != aVHeight || reqWidth != aVWidth){
				reqHeight = aVHeight*1;
				reqWidth = aVWidth*1;
			}
		}
		
		//determine window content viewable area
		var xScroll, yScroll;

		if (window.innerHeight && window.scrollMaxY) {	
			xScroll = document.body.scrollWidth;
			yScroll = window.innerHeight + window.scrollMaxY;
		} else if (document.body.scrollHeight > document.body.offsetHeight){ 
			xScroll = document.body.scrollWidth;
			yScroll = document.body.scrollHeight;
		} else { 
			xScroll = document.body.offsetWidth;
			yScroll = document.body.offsetHeight;
		}

		var windowWidth, windowHeight;
		if (self.innerHeight) {	
			windowWidth = self.innerWidth;
			windowHeight = self.innerHeight;
		} else if (document.documentElement && document.documentElement.clientHeight) { 
			windowWidth = document.documentElement.clientWidth;
			windowHeight = document.documentElement.clientHeight;
		} else if (document.body) { 
			windowWidth = document.body.clientWidth;
			windowHeight = document.body.clientHeight;
		}	

		var pageHeight, pageWidth;
		if(yScroll < windowHeight){
			pageHeight = windowHeight;
		} else { 
			pageHeight = yScroll;
		}

		if(xScroll < windowWidth){	
			pageWidth = windowWidth;
		} else {
			pageWidth = xScroll;
		}
		
		//set top, left, height, width values
		var setTop = (pageHeight/2) - (reqHeight/2);
		var setLeft = (pageWidth/2) - (reqWidth/2);
		
		var avDoc = $('accordBody');
		avDoc.style.top = setTop - 25 + 'px';
		avDoc.style.left = setLeft + 'px';
		avDoc.style.height = reqHeight + 25 + 'px';
		avDoc.style.width = reqWidth + 'px';
		
		this.confirm_close_dialog.style.top = setTop + 192 + 'px';
		this.confirm_close_dialog.style.left = setLeft + 155 + 'px';

		var avContent = $('accordViewer');
		avContent.style.height = reqHeight + 'px';		
	},
	//
	// Set Up all Valid Links for Action
	setupLinks : function(id) {
		var links = $$(id+' .'+this.options.classNames.standard);
		links.each(function(link) {
			var url = link.getAttribute('href');
			if (link.href.indexOf('?') > -1) url = url.substring(0, url.indexOf('?'));
			container = url.substring(url.indexOf('#')+1);
			if($(container)) $(container).style.display = 'none';
			Event.observe(link, 'click', this.activate.bindAsEventListener(this, link));
			link.onclick = function() {return false;};
		}.bind(this));	
	},
	noop : function() {
		return false;
	}
}

//Event.observe(window, 'load', accordViewerInit, false);
var myAccordViewer = null;
function accordViewerInit() {
	if(myAccordViewer == null){
		myAccordViewer = new accordViewer();
	}
	
	//special for Asset Health
	var hasRightVersion = DetectFlashVer(requiredMajorVersion, requiredMinorVersion, requiredRevision);
	if(hasRightVersion == false){
		myAccordViewer.activate(null, $('noflash-link'));
	}
}

//
//	Dynamically Activate cardmain. Special for Asset Health
//
function dynamicActivateCard(obj, xmlId,
                             packageId, packageTitle, displayOverride,
                             close_course_without_test_reminder,
                             options){
	obj.blur();
	myAccordViewer.element = new Object();
	myAccordViewer.element.title = obj.getAttribute('title').substring(15);
	myAccordViewer.element.rel = '';
	myAccordViewer.element.params = 'aVWidth=740,aVHeight=440';
	
	myAccordViewer.close_course_without_test_reminder
		= close_course_without_test_reminder;

  var options = options || {};
  options.isolateCard = (
    options.isolateCard
      ? '&isolateCard=' + options.isolateCard
      : ''
  );
  myAccordViewer.cardOptions = options;

	if(displayOverride == '0'){
		myAccordViewer.windowType = 'media';
		myAccordViewer.contentToFetch = xmlId;
		myAccordViewer.packageId = packageId;
		myAccordViewer.packageTitle = packageTitle;
		myAccordViewer.showCardToggle = true;
	}else{
		myAccordViewer.windowType = 'external';
		myAccordViewer.contentToFetch = displayOverride;
		myAccordViewer.packageId = packageId;
		myAccordViewer.packageTitle = packageTitle;
		myAccordViewer.showCardToggle = false;
	}

	myAccordViewer.setupDimensions(true);
	//myAccordViewer.toggleTroubleElements(true);
	myAccordViewer.displayAccordViewer(true);
	myAccordViewer.monitorKeyboard(true);	
	myAccordViewer.loadInfo(true);	
	//setCardObject();
}

//
//	Dynamically Activate cardmain from a search. Special for Asset Health
//
function dynamicActivateCardSearch(obj, xmlId, cardId, packageId, packageTitle){
	//close search results first
	myAccordViewer.searchResultsContent = $('accordViewer').innerHTML;
	myAccordViewer.deactivate();
	
	//obj.blur();
	myAccordViewer.element = new Object();
	myAccordViewer.element.title = obj.getAttribute('title');
	myAccordViewer.element.rel = '';
	myAccordViewer.element.params = 'aVWidth=740,aVHeight=440';
	myAccordViewer.windowType = 'media';
	myAccordViewer.customClose = '<span class="linkimg"><a href="#" onClick="backToSearch();"><img src="apps/resources/self_service/images/arrow_left.gif" align="absmiddle"/></a></span> <a href="#" onClick="backToSearch();">Back to Search Results</a> &nbsp;<a href="#" onClick="myAccordViewer.deactivate();">Close</a>';
	myAccordViewer.contentToFetch = xmlId;
	myAccordViewer.packageId = packageId;
	myAccordViewer.packageTitle = packageTitle;
	myAccordViewer.showCardToggle = true;
	myAccordViewer.showCardId = cardId;

	myAccordViewer.setupDimensions(true);
	//myAccordViewer.toggleTroubleElements(true);
	myAccordViewer.displayAccordViewer(true);
	myAccordViewer.monitorKeyboard(true);	
	myAccordViewer.loadInfo(true);	
}

//
//	Dynamically Activate TeckChek test taking screen. Special for Asset Health
//
function dynamicActivateTest(obj){
	obj.blur();
	myAccordViewer.element = new Object();
	myAccordViewer.element.title = obj.getAttribute('title').substring(0, obj.getAttribute('title').indexOf('Available'));
	myAccordViewer.element.rel = '';
	myAccordViewer.contentToFetch = '#testconsole';
	myAccordViewer.element.params = 'aVWidth=800,aVHeight=478';
	myAccordViewer.windowType = 'inline';

	myAccordViewer.setupDimensions(true);
	myAccordViewer.displayAccordViewer(true);
	myAccordViewer.monitorKeyboard(true);	
	myAccordViewer.loadInfo(true);	
}

//
// Dynamically Activate the complete test taking process. Special for Asset Health
//
function dynamicActivateTestSubmit(){
	//determine which package id was just open
	var localPackageId = myAccordViewer.packageId;
	var localPackageTitle = myAccordViewer.packageTitle;
	//close card content first
	myAccordViewer.deactivate_without_callbacks();
	
	myAccordViewer.element = new Object();
	myAccordViewer.element.title = localPackageTitle;
	myAccordViewer.element.rel = '';
	myAccordViewer.contentToFetch = '#testconsole';
	myAccordViewer.element.params = 'aVWidth=800,aVHeight=478';
	myAccordViewer.windowType = 'inline';

	myAccordViewer.setupDimensions(true);
	myAccordViewer.displayAccordViewer(true);
	myAccordViewer.monitorKeyboard(true);	

	myAccordViewer.loadInfo(true);
   //Delay processing from here to circumvent a race condition.  If
   //finishCourse is not done processing on the server when this
   //request starts, a new session will be created for this request.
   //This is really a server side bug (the server should block until
   //the session is available rather than giving out a new session).
   //If mutexing is built in to the AJAX requests at some point, this
   //delay could be removed.  Note that the loadInfo funtion is inside
   //this delay, since it clears the "please wait" display.
   setTimeout(function(){
	   //call backend to process test registration
	   processAjax('AHJViewCourses', '', 'initiateTest,'+localPackageId);
   }, 3000);
}

//
//	Dynamically Activate TeckChek test results screen. Special for Asset Health
//
function dynamicActivateTestResults(obj){
	obj.blur();
	myAccordViewer.element = new Object();
	myAccordViewer.element.title = obj.getAttribute('title');
	myAccordViewer.element.rel = '';
	myAccordViewer.contentToFetch = '#testconsoleResults';
	myAccordViewer.element.params = 'aVWidth=800,aVHeight=478';
	myAccordViewer.windowType = 'inline';

	myAccordViewer.setupDimensions(true);
	myAccordViewer.displayAccordViewer(true);
	myAccordViewer.monitorKeyboard(true);	
	myAccordViewer.loadInfo(true);	
}

//
//	Dynamically Activate Manits help screen. Special for Asset Health
//
function dynamicActivateHelp(obj){
	//close current help screen first
	myAccordViewer.deactivate_for_real();
	
	//obj.blur();
	myAccordViewer.element = new Object();
	myAccordViewer.element.title = obj.getAttribute('title');
	myAccordViewer.element.rel = '';
	myAccordViewer.contentToFetch = '#status_backup';
	myAccordViewer.element.params = 'aVWidth=450,aVHeight=450';
	myAccordViewer.windowType = 'inline';

	myAccordViewer.setupDimensions(true);
	myAccordViewer.displayAccordViewer(true);
	myAccordViewer.monitorKeyboard(true);	
	myAccordViewer.loadInfo(true);	
}

function backToSearch(){
	//close card content first
	myAccordViewer.deactivate_for_real();
	
	myAccordViewer.element = new Object();
	myAccordViewer.element.title = 'Course Content Search';
	myAccordViewer.element.rel = '';
	myAccordViewer.contentToFetch = '#coursesearch';
	myAccordViewer.element.params = 'aVWidth=740,aVHeight=440';
	myAccordViewer.windowType = 'inline';
	
	myAccordViewer.setupDimensions(true);
	myAccordViewer.displayAccordViewer(true);
	myAccordViewer.loadInfo(true);
	//relace content with saved search results
	$('accordViewer').innerHTML = myAccordViewer.searchResultsContent;
	myAccordViewer.monitorKeyboard(true);			
}

//
//	Dynamically Activate Employee Resource
//
function dynamicActivateResource(obj, title, resource){
	obj.blur();
	myAccordViewer.element = new Object();
	myAccordViewer.element.title = title;
	myAccordViewer.element.rel = '';
	myAccordViewer.element.params = 'aVWidth=740,aVHeight=540';
	myAccordViewer.windowType = 'external';
	myAccordViewer.contentToFetch = '/shared/displayResource.php?wait=true';
	if(resource != undefined){
		myAccordViewer.contentToFetch = '/shared/displayResource.php?'+resource;
	}
	myAccordViewer.showCardToggle = false;
	myAccordViewer.setupDimensions(true);
	myAccordViewer.displayAccordViewer(true);
	myAccordViewer.monitorKeyboard(true);	
	myAccordViewer.loadInfo(true);	
}
