/* 
-----------------------------------------------------------------
	MOE Corporate
	corporate.js
	
	Unique site functions
	Created 21.12.2007 by DS
	Last Updated: See SVN	
	
	Functions:
	- init_corporate()
	- enhance_widgets()
	- add_dropshadows()
	- update_dropshadows()
	- add_nav_hovers() and primary_nav_hover()
	- add_textfield_autoclears() and textfield_autoclear()
	- add_print_button() and print_page()
	- on_font_resize() - called by textresizedetector.js
	- add_script()	
	
-----------------------------------------------------------------
*/

/* 
-----------------------------------------------------------------	
	TRIGGER FUNCTIONS ON LOAD
-----------------------------------------------------------------
*/	
	
	function init_corporate()
	{	
		if (whichbrowser.isHiFi)
		{				
			apply_browser_fixes_for_js_dependent_elements();
		
			add_print_button();
			
			add_text_resizer();
			resize_text();					
			
			activate_faux_links();

			add_nav_hovers('primary-nav');
			add_nav_hovers('tabbed-nav');			
			add_nav_hovers('tabbed-alpha-nav');

			add_textfield_autoclears();				
			
			enhance_widgets();
	
			// do_inline_javascript_test();
			// do_inline_flash_test();
			
		}
	}	
	
	add_load_event(init_corporate); // window.onload = init_corporate;	
	
/* 
-----------------------------------------------------------------	
	FUNCTIONS
-----------------------------------------------------------------
*/	

//global variables:
	var widgets = new Array(); 
	var w = 0;
	
	function enhance_widgets()
	{
		/*	Version History:
			
			0.1 (ECE)
			This gives each .panel container a css height equivalent to what it already is - for the benefit of ie6, which needs to set drop shadows to 100% of the pixel height of each .panel
	
			0.2 (ECE)
			This function now sets the height of .vert-dropshadow instead of .liner, as IE fails to correctly calculate all instances of this span as 100% of the .liner height	
	
			0.3 (ECE)
			This function is re-run when the font-size is changed
	
			0.4 (Corporate)
			Function renamed
			Width of nested widget set to allow png shadows to shrink wrap css borders	
			
			0.5 (Corporate)
			Drop shadows now generated, specifically for IE
			
			0.6 (Corporate)
			Drop shadows only added where reqd, separate update_dropshadows() function
		*/			
		
		if (whichbrowser.isIE6x)
		{			
			bodyClass = (document.body.className ? document.body.className : '');				
			if (bodyClass != '')
			{
				document.body.className += ' ie6-enhanced';
			}		
			else
			{
				document.body.className += 'ie6-enhanced';
			}	
			
			// content div.image widgets
			add_dropshadows('', '', 'content', 'div', 'image');
			
			// content span.image widgets
			add_dropshadows('', '', 'content', 'span', 'image');
			
			// content div.tabbed widget
			add_dropshadows('', '', 'content', 'div', 'tabbed');		
			
			// content div.textbox widget
			add_dropshadows('', '', 'content', 'div', 'textbox');				
			
			if (!document.getElementById('template').className.match('t-tabbed'))
			{
				// all related widgets - except those nested within #content
				add_dropshadows('', '', 'related-liner', 'div', '');	
			}			
			if (document.getElementById('template').className.match('t-tabbed'))
			{		
				// t-tabbed nests #related-liner within #content - so only process #related-liner div types that haven't been processed already (above)
			
				// related-liner div.downloads widget
				add_dropshadows('', '', 'related-liner', 'div', 'downloads');		
				
				// related-liner div.downloads widget
				add_dropshadows('', '', 'related-liner', 'div', 'links');						
			}			
							
			update_dropshadows(1);
		}
	}
	
	function add_dropshadows(template_is, template_is_not, container, el_type, widget_special_type)
	{
		
		var template_class = document.getElementById('template').className;
		
		if (template_is != '')
		{
			if (!template_class.match(template_is))
			{
				return;
			}
		}
		if (template_is_not != '')
		{
			if (template_class.match(template_is_not))
			{
				return;
			}
		}		
		
		if (!document.getElementById(container))
		{
			return;
		}
		
		var els = document.getElementById(container).getElementsByTagName(el_type);			
		
		for (var i=0; i<els.length; i++)
		{				
			var this_el = els[i];
			
			var this_el_class = (this_el.className ? this_el.className : '');	
			
			if (widget_special_type != '')
			{
				if ((this_el_class.match('widget')) && (this_el_class.match(widget_special_type)))
				{		
				// 	Save reference for later	
					widgets[w] = new Array;
					widgets[w]['type'] = 'freeform';	
					widgets[w]['element'] = el_type;	
					widgets[w]['within'] = container;						
					widgets[w]['widget'] = this_el;
					widgets[w]['wrapper'] = widgets[w]['widget'].getElementsByTagName(el_type)[0];		
			
				// 	Generate drop shadow spans																								
				//	store vert-dropshadow in array so we can resize the dropshadow later
					widgets[w]['vert_dropshadow'] = document.createElement('span');
					widgets[w]['vert_dropshadow'].className = 'vert-dropshadow';
					
					widgets[w]['widget'].appendChild(widgets[w]['vert_dropshadow']);
					
					widgets[w]['horz_dropshadow'] = document.createElement('span');
					widgets[w]['horz_dropshadow'].className = 'horz-dropshadow';
	
					widgets[w]['widget'].appendChild(widgets[w]['horz_dropshadow']);					
				
				// 	Set width of nested widgets (ie images), to allow shadows to 'shrink wrap' css borders					

					if (widget_special_type == 'image')
					{
						var nested_image = widgets[w]['widget'].getElementsByTagName('img')[0];
						widgets[w]['widget'].style.width = (nested_image.width + 11); // 11px = (6px of borders + 5px RH dropshadow)						
					}						
					
					w++; // increment array index
				}				
			}
			else
			{
				if (this_el_class.match('widget'))
				{		
				// 	Save reference for later	
					widgets[w] = new Array;
					widgets[w]['type'] = (widget_special_type ? widget_special_type : 'freeform');	
					widgets[w]['element'] = el_type;	
					widgets[w]['within'] = container;						
					widgets[w]['widget'] = this_el;
					widgets[w]['wrapper'] = widgets[w]['widget'].getElementsByTagName(el_type)[0];		
			
				// 	Generate drop shadow spans																								
				//	store vert-dropshadow in array so we can resize the dropshadow later
					widgets[w]['vert_dropshadow'] = document.createElement('span');
					widgets[w]['vert_dropshadow'].className = 'vert-dropshadow';
					
					widgets[w]['widget'].appendChild(widgets[w]['vert_dropshadow']);
					
					widgets[w]['horz_dropshadow'] = document.createElement('span');
					widgets[w]['horz_dropshadow'].className = 'horz-dropshadow';
	
					widgets[w]['widget'].appendChild(widgets[w]['horz_dropshadow']);					
					
					w++; // increment array index
				}				
			}
		}
	}	
	
	function update_dropshadows(first_run)
	{	
		for (var i=0; i<widgets.length; i++)
		{		
			if (first_run)
			{
				// Step 1: reset height, else text-size change won't trigger a proper recalculation, also 'auto' hides div contents and could be the cause of the intermittent onload crash bug	
					
				widgets[i]['wrapper'].style.height = '1px';
				
				// Step 2: update height of vert dropshadow							
							
				widgets[i]['vert_dropshadow'].style.height = (widgets[i]['wrapper'].offsetHeight + 'px'); 	
				
				// Step 3: if .widget .image, update width of horz dropshadow		
				// TBC
			}
			else
			{
				if (widgets[i]['type'] != 'image') // image heights don't change when text is resized
				{
					// Step 1: reset height, else text-size change won't trigger a proper recalculation, also 'auto' hides div contents and could be the cause of the intermittent onload crash bug					
					
					widgets[i]['wrapper'].style.height = '1px';
					
					// Step 2: update height of vert dropshadow							
					
					widgets[i]['vert_dropshadow'].style.height = (widgets[i]['wrapper'].offsetHeight + 'px'); 								
				}
			}
		}
	}
	
	function add_nav_hovers(nav_id)
	{	
		// check that the container exists
		if (document.getElementById(nav_id))
		{
			var images = document.getElementById(nav_id).getElementsByTagName('img');
		}
		
		if (images)
		{		
			for (var i=0; i<images.length; i++)
			{
				imgSrc = images[i].src;
				
				// modifed by VLP: the nav images are now saved in sitecore and sitecore automatically replace - with a space
				//if (imgSrc.match('-off.gif'))
				if (imgSrc.match('-off.gif') || imgSrc.match('%20off.gif'))
				{
					// preload overstate
					window['linkImgHover_' + i] = new Image();
					window['linkImgHover_' + i].src = imgSrc.replace('-off.gif', '-over.gif');
	
	                // added by VLP: the nav images are now saved in sitecore and sitecore automatically replace - with a space
					window['linkImgHover_' + i].src = imgSrc.replace('%20off.gif', '%20over.gif');
					
					if (nav_id == 'primary-nav')
					{
						// 	Mouse-dependent handlers
						add_event(images[i].parentNode, 'mouseover', primary_nav_hover, false);
						add_event(images[i].parentNode, 'mouseout', primary_nav_hover, false);
		
						// 	Device independent handlers
						add_event(images[i].parentNode, 'focus', primary_nav_hover, false);
						add_event(images[i].parentNode, 'blur', primary_nav_hover, false);		
					}
					else
					{
						// 	Mouse-dependent handlers
						add_event(images[i].parentNode, 'mouseover', generic_nav_hover, false);
						add_event(images[i].parentNode, 'mouseout', generic_nav_hover, false);
		
						// 	Device independent handlers
						add_event(images[i].parentNode, 'focus', generic_nav_hover, false);
						add_event(images[i].parentNode, 'blur', generic_nav_hover, false);		
					}							
				}
			}
		}
	}
	
	function primary_nav_hover(e)
	{
		// identify 'this'
		
		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.isIE5up)	
		{
			_this = targ;
		}
		else
		{
			_this = this;	
		}	
		
		// some more ie checking (while loop update)
		
		while (_this.tagName.toLowerCase() != 'a')
		{
			_this = _this.parentNode;	
		}
	
		// interaction behaviours
		
		var imgSrc = _this.getElementsByTagName('img')[0].src;		

		var primary_nav_hover = _this.parentNode.parentNode.parentNode.parentNode;
//		var primary_nav_hover = document.getElementById('primary-nav').getElementsByTagName('div')[0];		

		if ((e.type == 'mouseover') || (e.type == 'focus'))
		{		
			primary_nav_hover.className = _this.parentNode.parentNode.className.substring(0,5) + '-hover';
			_this.getElementsByTagName('img')[0].src = imgSrc.replace('-off.gif', '-over.gif');
		}
		else if ((e.type == 'mouseout') || (e.type == 'blur'))
		{
			primary_nav_hover.className = '';	// default class of 'liner' is not actually used for anything
			_this.getElementsByTagName('img')[0].src = imgSrc.replace('-over.gif', '-off.gif');	
		}		
	}	
	
	function generic_nav_hover(e)
	{
		// identify 'this'
		
		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.isIE5up)	
		{
			_this = targ;
		}
		else
		{
			_this = this;	
		}	
		
		// some more ie checking (while loop update)
		
		while (_this.tagName.toLowerCase() != 'a')
		{
			_this = _this.parentNode;	
		}
	
		// interaction behaviours
		
		var imgSrc = _this.getElementsByTagName('img')[0].src;		

		if ((e.type == 'mouseover') || (e.type == 'focus'))
		{		
			_this.getElementsByTagName('img')[0].src = imgSrc.replace('-off.gif', '-over.gif');
		}
		else if ((e.type == 'mouseout') || (e.type == 'blur'))
		{
			_this.getElementsByTagName('img')[0].src = imgSrc.replace('-over.gif', '-off.gif');		
		}		
	}			
	
	function add_textfield_autoclears() 
	{
	/*	variation on AK's function */
	/*	autoclear only added to #search-nav input and #tools input */
	
		if (document.getElementById('search-nav'))
		{
			var header_inputs = document.getElementById('search-nav').getElementsByTagName('input');					
			
			for (var i=0; i<header_inputs.length; i++)
			{			
				if (header_inputs[i].type == 'text')
				{									
					add_event(header_inputs[i], 'focus', textfield_autoclear, false);
					add_event(header_inputs[i], 'blur', textfield_autoclear, false);	
				}
			}			
		}
		
		if (document.getElementById('tools'))
		{
			var tool_inputs = document.getElementById('tools').getElementsByTagName('input');													
			
			for (var i=0; i<tool_inputs.length; i++)
			{			
				if (tool_inputs[i].type == 'text')
				{									
					add_event(tool_inputs[i], 'focus', textfield_autoclear, false);
					add_event(tool_inputs[i], 'blur', textfield_autoclear, false);	
				}
			}			
		}		
	}	
	
	function textfield_autoclear(e)
	{
		// identify 'this'
		
		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.isIE5up)	
		{
			_this = targ;
		}
		else
		{
			_this = this;	
		}	
		
		// interaction behaviours
		
		if (e.type == 'focus')
		{		
			if (_this.value == _this.defaultValue) 
			{
				_this.value = '';
			}
		}
		else if (e.type == 'blur')
		{
			if (!_this.value) 
			{
				_this.value = _this.defaultValue;	
			}	
		}		
	}
	
	function add_print_button()
	{
		if (!document.getElementById || !document.getElementsByTagName || !document.createElement || !document.replaceChild)
		{
				return;
		}		
		
		// displayPrint and printLabel added by DC for CMS integration - these are set in the page header		
		//if ((document.getElementById('tools')) && (document.getElementsByTagName('div')[0].className != 'ct-3 t-home'))
		if ((document.getElementById('tools')) && (displayPrint) && (printLabel) && (printImage))
		{		
			var li = document.createElement('li');
					li.className = 'nav-1';			
				
			var button = document.createElement('span'); // changed from button to span 16.05.2008
					button.className = 'button generated';
					button.setAttribute('title', printLabel);		
				
//			if ((whichbrowser.isIE6up) || (whichbrowser.isSafari))
//			{
//				button.style.marginTop = '0'; // JS generated element - JS generated CSS fix
//			}
				
			var img = document.createElement('img');
					img.setAttribute('src', printImage);
					img.setAttribute('alt', printLabel);					
					img.setAttribute('width', '40');	
					img.setAttribute('height', '23 ');
					
					button.appendChild(img);			
					li.appendChild(button);			
					
					/*	Device independent handlers
						Generating a button instead of our standard faux-link is a bit different,	but:
						the whole page is wrapped in a .net form,
						the button action is relevant to the data/content contained within the whole page,
						the button only exists if JS is enabled,
						and it means that we can cater to non-mouse users, which is why I've used it :)
					*/			
					add_event(button, 'click', print_page, false);
				
			var next_sibling = document.getElementById('tools').getElementsByTagName('li')[0];			
					next_sibling.parentNode.insertBefore(li, next_sibling);	
		}
	}
	
	function print_page()
	{
		window.print();
		return false; // suppress submission of .net form		
	}
		
	function on_font_resize()
	{
		update_dropshadows();
	}	
	
	function add_script(script_url)
	{
		if (!document.getElementsByTagName || !document.body.appendChild || !document.body.setAttribute)
		{
			return;
		}		
		
		// Create a new script node
		var element = document.createElement('script');
		
		// set the 'src' attribute of the element
		element.setAttribute('src', script_url);
		
		// insert the new node into the current document
		document.body.appendChild(element);		
	}	
	
	function apply_browser_fixes_for_js_dependent_elements()
	{
		if (
					(whichbrowser.isNS && (whichbrowser.versionMinor == 7.1)) || 
					(whichbrowser.isNS && (whichbrowser.versionMinor == 8.1)) || 					
					(whichbrowser.isFirefox && (whichbrowser.versionMinor == 1))							
				)
		{
			//alert('phoenixed');
			document.body.className += ' phoenix'; /* for JS-enabled CSS fixes of JS-generated text-resizer element */
		}	
		if (
					(whichbrowser.isSafari)								
				)
		{
			document.body.className += ' safari'; /* for JS-enabled CSS fixes of JS-generated text-resizer element */
		}				
	}
