/* 
-----------------------------------------------------------------
	nfa2291
	common.js
	
	Common CT build functions
	
	This version: 17.08.2007 by DS	
	Created 24.09.2007 by DS
	Last Updated: See SVN	
-----------------------------------------------------------------
*/

// DOM shorthand wrappers - these are MR's but are probably ok
	
	/**
	* getElementById
	* @return DomElement
	*/
	function $id(id) {
		return document.getElementById(id);
	}
	
	/**
	* getElementsByClassName
	* @return DomElement if single result, else array of DomElements 
	  updated by MR 17.05.2007 */ 
	function $class(name) {
		var result = new Array();
		var target = new RegExp("(^|\\s)" + name + "(\\s|$)");
		var elements = document.getElementsByTagName("*");
		var length = elements.length;
		var current;
		for (var i=0; i<length; i++) {
			current = elements[i];
			if (target.test(current.className)) {
				result.push(current);
			}
		}
		return (result.length > 1) ? result : result[0]; }
	
	/**
	* getElementsByTagName
	* @return DomElement if single result, else collection of DomElements
	*/
	function $tag(name) {
		result = document.getElementsByTagName(name);
		return (result.length > 1) ? result : result[0]; 
	}
	
//	Reusable functions	

	function createImgRollovers()
	{	
		var images = $tag('img');	
		
		for (var i=0; i<images.length; i++)
		{
			imgSrc = images[i].src;
			
			if (imgSrc.match('-off.gif'))
			{
				// preload overstate
				window['linkImgHover_' + i] = new Image();
				window['linkImgHover_' + i].src = imgSrc.replace('-off.gif', '-over.gif')				
				
				// apply event listeners
				addEvent(images[i].parentNode, 'mouseover', swapImg, false);
				addEvent(images[i].parentNode, 'mouseout', swapImg, false);	
			}
		}
	}
	
	function swapImg(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
		
		if (_this.tagName.toLowerCase() == 'img')
		{		
			_this = _this.parentNode;
		}
	
		// rollover behaviour
		
		var imgSrc = _this.getElementsByTagName('img')[0].src;
		


		if (e.type == 'mouseover')
		{			
			_this.getElementsByTagName('img')[0].src = imgSrc.replace('-off.gif', '-over.gif');
		}
		else if (e.type == 'mouseout')
		{
			_this.getElementsByTagName('img')[0].src = imgSrc.replace('-over.gif', '-off.gif');
		}		
	}

	function makeFauxLinks()
	{
	//	DS
	//	fakin' it
		var linkInstances = $class('a');
					
		//	convert to array if only 1, so we can loop through all if more than one			
		if (isArray(linkInstances))
		{
			links = linkInstances;
		}
		else
		{
			links = new Array(linkInstances);			
		}	

		for (var i=0; i<links.length; i++)
		{
			// apply ids now for easy fontsize control without messing about with finding out parent node on hover

		/* 	Because links are inside list items (for fontsize control - 2 buttons inside one),
			we need a way to change the list item (button) background
			(link or faux link background is an icon)
			This is a high end site, so we'll assume that most people have JS enabled.
			If not, only the icon will change colour.
		*/
			addEvent(links[i], 'mouseover', emulateLinkInteraction, false);
			addEvent(links[i], 'mouseout', emulateLinkInteraction, false);	
		}			
	}
	function addEvent(obj, evType, fn, useCapture)
	{
		// http://www.scottandrew.com/weblog/articles/cbs-events
		
		if (obj.addEventListener)
		{
			obj.addEventListener(evType, fn, useCapture);
			return true;
		} 
		else if (obj.attachEvent)
		{
			var r = obj.attachEvent('on' + evType, fn);
			return r;
		} 
		else 
		{
			alert("Handler could not be attached");
		}
	}	
	
	function removeEvent(obj, evType, fn, useCapture)
	{
		// http://www.scottandrew.com/weblog/articles/cbs-events		
		
		if (obj.removeEventListener)
		{
			obj.removeEventListener(evType, fn, useCapture);
			return true;
		} 
		else if (obj.detachEvent)
		{
			var r = obj.detachEvent('on' + evType, fn);
			return r;
		} 
		else 
		{
			alert("Handler could not be removed");
		}
	}	
	function emulateLinkInteraction(e)
	{
		// DS - hope this will be reusable
		
		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;	
		}
	
		if (e.type == 'mouseover')
		{			
			if (_this.className)
			{		
			// is _this.className				
			
				if (_this.parentNode.id)
				{		
					// is _this.parentNode.id				
				
					var parentId = _this.parentNode.id;		
				
					if ((parentId == 'decrease') || (parentId == 'increase'))
					{						
						if (current_size == 2)
						{
							// the default set-up is textsize-X-hover, cos you have to be hovered to click
							// to this we need to add the id of the calling button, because at textsize-2 there are 2 hover possibilities		
								
							// easier to rebuild class from group up rather than try the logic path..	
							// this also prevents us adding extra strings to the classname
							window['ssButton'].className = ('textsize-' + current_size + '-' + parentId + '-hover');	
						}	
						else
						{
							parentClass = window['ssButton'].className;
							window['ssButton'].className = parentClass.replace(/blur/, 'hover');	
						}							
					}	
					else
					{
						// not increase	or decrease, but print, and any new ones
						// this is a duplicate of the code block below
						
						thisClass = _this.className;					
									
						if ((thisClass.indexOf('current') == -1) && (thisClass.indexOf('blur') != -1)) // space removed so we can reuse for dimmed-[state]
						{				
							//	if className already contains 'blur' (ie if it has been hovered over already), just change this part of the className
							//	this check also covers 'dimmed-blur'
								_this.className = thisClass.replace(/blur/, 'hover');
						}			
						else if (thisClass.indexOf('current') == -1)
						{
								// else link hasn't been hovered over yet, so we need to add 'hover' to the existing className
								_this.className += ' hover';
						}						
					}
				}
				else
				{
					// not _this.parentNode.id
					
					thisClass = _this.className;					
								
					if ((thisClass.indexOf('current') == -1) && (thisClass.indexOf('blur') != -1)) // space removed so we can reuse for dimmed-[state]
					{				
						//	if className already contains 'blur' (ie if it has been hovered over already), just change this part of the className
						//	this check also covers 'dimmed-blur'
							_this.className = thisClass.replace(/blur/, 'hover');
					}			
					else if (thisClass.indexOf('current') == -1)
					{
							// else link hasn't been hovered over yet, so we need to add 'hover' to the existing className
							_this.className += ' hover';
					}			
				}
			}
			else
			{
			// not _this.className					
				
				//	else there's no className at all yet, so it's ok to set the className to 'hover'
				_this.className = 'hover';
			}					
			
		}
		else if (e.type == 'mouseout')
		{			

			// THE THIS
	
			thisClass = _this.className;			
		
			if (thisClass.indexOf('current') == -1)
			{			
				_this.className = thisClass.replace(/hover/, 'blur');
			}		
			
			// AND THE THAT

			if (_this.parentNode.id)
			{
				var parentId = _this.parentNode.id;
				
				if ((parentId == 'decrease') || (parentId == 'increase'))
				{	
					// with windows vars no script is an island :)
					var parentClass = window['ssButton'].className;
					
					// on mouse out we want to blur
					// this normal
					// but if the textsize is 2 there are 2 controllers on one button
					// we don't want to show  the button as off if there's another controller we could click!
					// so we need to do something special if (current_size == 2)
					
					// no ur wrong
					// we can mouse off in safety because if we mouse over another the mouseover state will display anyway
					// what u were trying to say was that when (current_size == 2) the button starts off in an overstate
					// whereas the others start off in an downstate
					
					window['ssButton'].className = parentClass.replace(/hover/, 'blur');	

										
					// we only want to blur if we didn't just move the mouse over this controller's nextdoor neighbour 						
				}			
			}

		}	
	}
	function getViewportDimension(dimension)
	{
		frameWidth = 0;
		frameHeight = 0;
	
		if (self.innerWidth)
		{
			frameWidth = self.innerWidth;
			frameHeight = self.innerHeight;
			
			if (dimension == 'w')
			{			
				return frameWidth;
			}
			else if (dimension == 'h')
			{			
				return frameHeight;
			}			
		}
		else if (document.documentElement && document.documentElement.clientWidth)
		{
			frameWidth = document.documentElement.clientWidth;
			frameHeight = document.documentElement.clientHeight;
			
			if (dimension == 'w')
			{			
				return frameWidth;
			}
			else if (dimension == 'h')
			{			
				return frameHeight;
			}		
		}
		else if (document.body)
		{
			frameWidth = document.body.clientWidth;
			frameHeight = document.body.clientHeight;
			
			if (dimension == 'w')
			{			
				return frameWidth;
			}
			else if (dimension == 'h')
			{			
				return frameHeight;
			}			
		}
		else return;	
	}
		
	function widenInterface()
	{	
		var bodyClass = document.body.className;			
		
		if (getViewportDimension('w') >= 1000)
		{		
			document.body.className = bodyClass.replace(/narrow/, 'wide');		
			
			if (gWidened == false)
			{			
				addVaultString(); // add first run only, thereafter hidden with CSS
			}
			
			shuffleModules('widen');		
			
			if ((document.body.id == 't-calendar-day') || (document.body.id == 't-calendar-week') || (document.body.id == 't-calendar-month') || (document.body.id == 't-calendar-search') || (document.body.id == 't-calendar-detail'))
			{			
				add_padding_to_match_calendar_content_height_to_featured_height(); // DS, 18.07.2008	[NFA2415]
			}
		}
		else
		{				
			document.body.className = bodyClass.replace(/wide/, 'narrow');				
			shuffleModules('narrow');
			
			if ((document.body.id == 't-calendar-day') || (document.body.id == 't-calendar-week') || (document.body.id == 't-calendar-month') || (document.body.id == 't-calendar-search') || (document.body.id == 't-calendar-detail'))
			{						
				remove_padding_to_match_calendar_content_height_to_featured_height();	// DS, 18.07.2008	[NFA2415]	
			}
		}			
	}	
	
	function enhanceTextFields() 
	{
	/*	variation on AK's function */
	/*	you may also be able to populate the defaultValue with the associated label's value onload,
		(which might go someway to making up for the fact that said labels have been hidden to match the design),
		but the ssc might prefer that the inputs have a default value in noscript browsers */	
	
		// formTextFields = $class('text'); ~ $class() not supported by ie5
		
		if ($tag('form'))
		{
			formInputs = $tag('input');
			
			for (var i=0; i<formInputs.length; i++)
			{
				if (formInputs[i].type == 'text')
				{
					formInputs[i].onfocus = function () 
					{
						if (this.value == this.defaultValue) 
						{
							this.value = '';
						}
					}
					formInputs[i].onblur = function () 
					{
						if (!this.value) 
						{
							this.value = this.defaultValue;	
						}
					}
				}
			}
		}
	}	
	
	function isArray(obj) 
	{
	//	DS
	//	check if an object is an array or not
	
		if (obj instanceof Array)
		{
			return true;
		}
		else
		{
			return false;
		}
	}	
	
	function inlineJavascriptTest()
	{	
		if ((document.getElementById('js-test')) && (document.body.id == 't-calendar'))
		{
			// quick coding
			document.getElementById('js-test').innerHTML = '<p>You appear to have Javascript <strong class="js-status">enabled</strong>. You should be able to use the enhanced version of this calendar, on <a href="/">the homepage</a>. </p>';
		}
	}
	
	function show_html()
	{
		var div_body = document.getElementById('body');
		div_body.className = 'show_html';
	}	
