/* 
-----------------------------------------------------------------
	MOE Corporate
	educate.js
	
	Unique site functions
	Created 26.11.2008 by DS
	Last Updated: See SVN	
	
	Functions:
	- init_educate()
	- match_tab_heights()
	
-----------------------------------------------------------------
*/

/* 
-----------------------------------------------------------------	
	TRIGGER FUNCTIONS ON LOAD
-----------------------------------------------------------------
*/	
	
	function init_educate()
	{	
		if (whichbrowser.isHiFi)
		{		
			enhance_picture_view();		
			
			// only affect tabbed-section-page template
			if (document.getElementById('template').className.match('t-tabbed'))
			{
				add_nav_hovers('tab-nav');							
				match_tab_heights('tabbed-nav');
			}		
			
			// only affect pages which use the A/V Player			
			if (typeof(window['mediaURL']) != "undefined") // if the var exists
			{		
				if (mediaURL != '') // if the var has a value
				{			
					// SWFOBJECT Flash [warning, no expressInstall]												
					var flash_src = '/generic/flash/ui/av-player_368x342.swf'; 						
					var flash_width = '368';				
					var flash_height = '342';					
					var flash_reqdVersion = '9.0.124';
					var flash_containerId = 'flash-warning'; 			
					var flash_expressInstaller = false;											
					var flash_attributes = {};	
					// 	flash_attributes.name = 'value';					
							flash_attributes.id = 'flash_video_player';
							flash_attributes.name = 'flash_video_player';
			
					var flash_params = {}; 				
					// 	flash_params.name = 'value';				
					var flash_vars = {}; 				
							flash_vars.mediaURL = mediaURL;
							flash_vars.mediaImageURL = mediaImageURL;	
							flash_vars.closedCaptionURL = closedCaptionURL;			
					var flash_custom_args = {};
							flash_custom_args.flash_warningContainerId = 'flash-warning';			
							
							flash_advancedEmbed(flash_src, flash_containerId, flash_width, flash_height, flash_reqdVersion, flash_expressInstaller, flash_vars, flash_params, flash_attributes, flash_custom_args);		
							
							enhance_av_transcript();									
							activate_faux_links(); /* process transcript links */
							
							preload_images(
								'/generic/images/ui/widgets/av-player/download-over.gif'
								,'/generic/images/ui/widgets/av-player/help-over.gif'
								,'/generic/images/ui/widgets/av-player/chapter-previous-over.gif'
								,'/generic/images/ui/widgets/av-player/chapter-next-over.gif'
							);
					}
				}														
		}
	}	
	
	add_load_event(init_educate); // window.onload = init_educate;	
	
/* 
-----------------------------------------------------------------	
	FUNCTIONS
	
	@ match_tab_heights
	@ add_text_nav_hovers
	@ getStyle
	@ getPadding
	@ section_tabbed_nav_hover
	@ enhance_picture_view	
	@ link_to
-----------------------------------------------------------------
*/	

	function match_tab_heights(nav_id)
	{
		if (nav_id == 'tabbed-nav')
		{
			var nav_a = document.getElementById(nav_id).getElementsByTagName('a');
			var nav_strong = document.getElementById(nav_id).getElementsByTagName('strong');			
			
			var nav_maxheight = 0;
			
			// get maxHeight from A
			for (var n=0; n<nav_a.length; n++)
			{
				if (nav_a[n].offsetHeight > nav_maxheight)
				{
					nav_maxheight = nav_a[n].offsetHeight;
				}
			}
			
			// get maxHeight from STRONG
			for (var n=0; n<nav_strong.length; n++)
			{
				if (nav_strong[n].offsetHeight > nav_maxheight)
				{
					nav_maxheight = nav_strong[n].offsetHeight;
				}
			}								
			
			// apply maxHeight to A
			for (var n=0; n<nav_a.length; n++)
			{
				if (nav_a[n].offsetHeight < nav_maxheight)
				{
					nav_a_vert_pad = getPadding(nav_a[n], 'v');					
					
					if (whichbrowser.isIE)
					{
						nav_a[n].style.height = ((nav_maxheight - nav_a_vert_pad) + 'px');						
					}
					else
					{
						nav_a[n].style.minHeight = ((nav_maxheight - nav_a_vert_pad) + 'px');									
					}
				}
			}
			
			// apply maxHeight to STRONG
			for (var n=0; n<nav_strong.length; n++)
			{
				if (nav_strong[n].offsetHeight < nav_maxheight)
				{
						nav_strong_vert_pad = getPadding(nav_strong[n], 'v');							
					
					if (whichbrowser.isIE)
					{
						nav_strong[n].style.height = ((nav_maxheight - nav_strong_vert_pad) + 'px');					
					}
					else
					{
						nav_strong[n].style.minHeight = ((nav_maxheight - nav_strong_vert_pad) + 'px');						
					}
				}
			}			
			
		}
	}
	
	function getStyle(el,styleProp)
	{	
		var x = el;
		
		if (x.currentStyle)
		{
			var y = x.currentStyle[styleProp];
		}
		else if (window.getComputedStyle)
		{
			var y = document.defaultView.getComputedStyle(x,null).getPropertyValue(styleProp);
		}
		return parseInt(y); // eg 13px -> 13
	}	
	
	function getPadding(el, type)
	{
		if ((whichbrowser.isIE) || (whichbrowser.isOpera))
		{
			if (type == 'h')
			{			
				padding = (getStyle(el, 'paddingLeft') + getStyle(el, 'paddingRight'));
			}
			else if (type == 'v')
			{	
				padding = (getStyle(el, 'paddingTop') + getStyle(el, 'paddingBottom'));
			}
		}
		else
		{
			if (type == 'h')
			{			
				padding = (getStyle(el, 'padding-left') + getStyle(el, 'padding-right'));
			}
			else if (type == 'v')
			{	
				padding = (getStyle(el, 'padding-top') + getStyle(el, 'padding-bottom'));
			}
		}
		return padding;		
	}	
	
	function enhance_picture_view()
	{
		/*
			To work around integration issues, the img link is removed and reapplied using JS. 			
			The hardcoded caption link remains usable for keyboard users.
		*/
		
		// search for div.picture-view	
		var lvi = 0; // lightview index				
		var divs = document.getElementById('content-liner').getElementsByTagName('div');
		for (var d=0; d<divs.length; d++)
		{		
			if (divs[d].className.match('picture-view'))
			{			
				// search for div.widget.image					
				var child_divs = divs[d].getElementsByTagName('div');
				{
					for (var e=0; e<child_divs.length; e++)
					{
						if (child_divs[e].className.match('image'))
						{				
							child_divs[e].className += ' a blur'; // apply faux link class
							var enlargement_link = child_divs[e].getElementsByTagName('a')[0];
							
							if (enlargement_link) // VL, as not all images will have enlargements
							{			
								// apply a unique ID for use by lightview
								lvi ++;
								enlargement_link.setAttribute('id', ('enlargement-link-' + lvi));							
								// make the parent div clickable, to trigger this link							
								var image_liner = enlargement_link.parentNode.parentNode;
								add_event(image_liner, 'mouseover', picture_view_hover, false);		
								add_event(image_liner, 'mouseout', picture_view_hover, false);								
								add_event(image_liner, 'click', picture_view_hover, false);	
							}
						}
					}
				}
			}
		}
	}
	
	function picture_view_hover(e)
	{
		// updated ~ compressed
		
		if (!e) 
		{
			var e = window.event; // http://www.quirksmode.org/js/events_properties.html
		}
		
		// 
		
		if (e.target) 
		{
			targ = e.target;
		}
		else if (e.srcElement) 
		{
			targ = e.srcElement;
		}
		if (targ.nodeType == 3) 
		{	
			// defeat Safari bug
			targ = targ.parentNode;
		}
		
		// IE
		
		if (whichbrowser.isIE6up)	
		{
			_this = targ;
		}
		else
		{
			_this = this;	
		}
		
		// some more ie checking
		while (_this.tagName.toLowerCase() != 'div')
		{			
			_this = _this.parentNode;	// div.liner
		}		
		_this_link = _this.getElementsByTagName('a')[0];
		_this = _this.parentNode.parentNode; // div.widget.image		
		
		// interaction behaviours		
		faux_link_classname = _this.className;	
		
		switch (e.type)
		{	
			case 'mouseover' :		
				_this.className = _this.className.replace(/blur/, 'hover');	
				_this.setAttribute('title', _this_link.title);
				window.status = _this_link.href;
				break;
			case 'mouseout' :				
				_this.className = faux_link_classname.replace(/hover/, 'blur');
				_this.removeAttribute('title');						
				window.status = '';	
				break;
			case 'click' :			
				window.status = '';		
				Lightview.show(_this_link.id); // VL, for lightbox integration				
		}		
	}	
	
	function enhance_av_transcript()
	{
		if (!document.getElementById('av-transcript'))
		{
			return;
		}
		
		window['transcript'] = document.getElementById('av-transcript');
		
		window['expander_text'] = document.createTextNode('Expand full transcript');
		window['collapser_text'] = document.createTextNode('Contract transcript');	
		window['collapser_text_2'] = window['collapser_text'].cloneNode(true);
		
		window['toggler_top'] = document.createElement('button');
		window['toggler_top'].setAttribute('type', 'button');
		window['toggler_top'].className = 'a blur toggler-top';		
		window['toggler_top'].appendChild(window['expander_text']);		
		add_event(window['toggler_top'], 'click', toggle_av_transcript);
			
		// create toggler_btm, but don't insert (yet)
		window['toggler_btm'] = document.createElement('button');
		window['toggler_btm'].setAttribute('type', 'button');		
		window['toggler_btm'].className = 'a blur toggler-btm';		
		window['toggler_btm'].appendChild(window['collapser_text_2']);				
		add_event(window['toggler_btm'], 'click', toggle_av_transcript);	
		// activate faux link here, as this isn't inserted until long after the page load scripts have run
		add_event(window['toggler_btm'], 'mouseover', emulate_link_hover, false);
		add_event(window['toggler_btm'], 'mouseout', emulate_link_hover, false);			
		
		window['transcript'].className = 'av-transcript-collapsed';		
		// add toggler_top before the .content wrap (.liner), but not toggler_btm (yet) 
		window['transcript'].insertBefore(window['toggler_top'], window['transcript'].getElementsByTagName('div')[0]);						
		
		if (document.getElementById('template').className.match('t-tabbed'))
		{		
			update_dropshadows(0); // resize vert tabbed shadow so that it doesn't include the hidden transcript
		}
	}
	function toggle_av_transcript()
	{		
		switch (window['transcript'].className)
		{		
			case 'av-transcript-collapsed' :
				// show transcript, and collapse links
				// change text on top link
				window['transcript'].className = 'av-transcript-expanded';				
				window['toggler_top'].removeChild(window['expander_text']);
				window['toggler_top'].appendChild(window['collapser_text']);
				// add btm link
				window['transcript'].appendChild(window['toggler_btm']);
				
				if (document.getElementById('template').className.match('t-tabbed'))
				{					
					update_dropshadows(0); // resize vert tabbed shadow so that it does include the hidden transcript				
				}
				
				break;
				
			case 'av-transcript-expanded' :
				// hide transcript, and show expand link (top only)
				window['transcript'].className = 'av-transcript-collapsed';
				// change text on top link				
				window['toggler_top'].removeChild(window['collapser_text']);
				window['toggler_top'].appendChild(window['expander_text']);	
				// remove btm link
				window['toggler_btm'].parentNode.removeChild(window['toggler_btm']);	
				
				if (document.getElementById('template').className.match('t-tabbed'))
				{					
					update_dropshadows(0); // resize vert tabbed shadow so that it doesn't include the hidden transcript				
				}
				
				break;		
		}				
	}
	
/*	
   @ preload_images()
   ---------------------------------------------------------------
	 Preload hover overstates, incl those used by the CSS
   _______________________________________________________________
*/

	function preload_images()
	{
		if (arguments.length > 0)
		{					
			for (var a=0; a<arguments.length; a++)
			{				
				var new_img = document.createElement('img');
				var new_src = arguments[a];	
						new_img.setAttribute('src', new_src);
			}	
		}	
	}	