/* 
-----------------------------------------------------------------
	biz2328
	common.js
	
	Common CT build functions
	
	Created 02.10.2007 by DS
	Last Updated: See SVN	
	
	Includes these updates:	
	* $class fix 17.05.2007		
	* Mac IE5 isArray() fix: 19.09.2007	
-----------------------------------------------------------------
*/

// 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');
		
		if (linkInstances)
		{					
			//	convert to array if only 1, so we can loop through all if more than one			
			if (isArray(linkInstances))
			{
				fauxlinks = linkInstances;
			}
			else
			{
				fauxlinks = new Array(linkInstances);			
			}	
	
			for (var i=0; i<fauxlinks.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(fauxlinks[i], 'mouseover', emulateLinkInteraction, false);
				addEvent(fauxlinks[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 shellClass = $id('shell').className;			
		
		if (getViewportDimension('w') >= 1000)
		{
			$id('shell').className = shellClass.replace(/narrow/, 'wide');
		}
		else
		{
			$id('shell').className = shellClass.replace(/wide/, 'narrow');		
		}
	}	
	
	function isArray(obj) 
	{
	  //	DS
	  //	check if an object is an array or not
	  // 	includes conditional compliation fix for Mac IE5.2
	 
	  /*@cc_on
		@if ((!@_win32) && (@_jscript_version < 5.6)) 
		// mac ie5
		return null;
	  @else @*/ 
		if (obj instanceof Array)
		{
		  return true;
		}
		else
		{
		  return false;
		}
	  /*@end
	  @*/ 
	}
	
	function inlineJavascriptTest()
	{	
		if ($id('inline-js-test'))
		{
			$id('inline-js-test').innerHTML = 'enabled';
		}
	}
	
	function inlineFlashTest()
	{
		if ($id('inline-flash-test'))
		{
			$id('inline-flash-test').innerHTML = '(you appear to have version <strong>' + global_flash_version + '</strong> installed)';
		}
	}