/*	
	shared.js
	===============================================================
	NSU - Shared functions
	
	Created 24.03.2009 by DS	
	Last Updated: See SVN		
	
	@ detectbrowser
	@ add_event
	@ remove_event
	@ add_load_event
	@ get_style
	_______________________________________________________________
*/


	var whichbrowser;
	
	function detectbrowser() 
	{	
		// Browser Detect  v2.1.6
		// documentation: http://www.dithered.com/javascript/browser_detect/index.html
		// license: http://creativecommons.org/licenses/by/1.0/
		// code by Chris Nott (chris[at]dithered[dot]com)	
		
		var ua = navigator.userAgent.toLowerCase(); 
		
		// browser engine name
		this.isGecko       = (ua.indexOf('gecko') != -1 && ua.indexOf('safari') == -1);   
		
		// browser name
		this.isKonqueror   = (ua.indexOf('konqueror') != -1); 
		this.isSafari      = (ua.indexOf('safari') != - 1);
		this.isOmniweb     = (ua.indexOf('omniweb') != - 1);
		this.isOpera       = (ua.indexOf('opera') != -1);
		this.isIcab        = (ua.indexOf('icab') != -1); 
		this.isAol         = (ua.indexOf('aol') != -1); 
		this.isIE          = (ua.indexOf('msie') != -1 && !this.isOpera && (ua.indexOf('webtv') == -1) ); 
		this.isMozilla     = (this.isGecko && ua.indexOf('gecko/') + 14 == ua.length);
		this.isFirefox    = (ua.indexOf('firefox/') != -1);
		this.isNS          = ( (this.isGecko) ? (ua.indexOf('netscape') != -1) : ( (ua.indexOf('mozilla') != -1) && !this.isOpera && !this.isSafari && (ua.indexOf('spoofer') == -1) && (ua.indexOf('compatible') == -1) && (ua.indexOf('webtv') == -1) && (ua.indexOf('hotjava') == -1) ) );
		
		// spoofing and compatible browsers
		this.isIECompatible = ( (ua.indexOf('msie') != -1) && !this.isIE);
		this.isNSCompatible = ( (ua.indexOf('mozilla') != -1) && !this.isNS && !this.isMozilla);	
	
		// rendering engine versions
		this.geckoVersion = ( (this.isGecko) ? ua.substring( (ua.lastIndexOf('gecko/') + 6), (ua.lastIndexOf('gecko/') + 14) ) : -1 );
		this.equivalentMozilla = ( (this.isGecko) ? parseFloat( ua.substring( ua.indexOf('rv:') + 3 ) ) : -1 );
		this.appleWebKitVersion = ( (this.isAppleWebKit) ? parseFloat( ua.substring( ua.indexOf('applewebkit/') + 12) ) : -1 );
		
		// browser version
		this.versionMinor = parseFloat(navigator.appVersion); 	
	
	   // correct version number
		if (this.isGecko && !this.isMozilla) {
		  this.versionMinor = parseFloat( ua.substring( ua.indexOf('/', ua.indexOf('gecko/') + 6) + 1 ) );
		}
		else if (this.isMozilla) {
		  this.versionMinor = parseFloat( ua.substring( ua.indexOf('rv:') + 3 ) );
		}
		else if (this.isIE && this.versionMinor >= 4) {
		  this.versionMinor = parseFloat( ua.substring( ua.indexOf('msie ') + 5 ) );
		}
		else if (this.isKonqueror) {
		  this.versionMinor = parseFloat( ua.substring( ua.indexOf('konqueror/') + 10 ) );
		}
		else if (this.isSafari) {
		  this.versionMinor = parseFloat( ua.substring( ua.lastIndexOf('safari/') + 7 ) );
		}
		else if (this.isOmniweb) {
		  this.versionMinor = parseFloat( ua.substring( ua.lastIndexOf('omniweb/') + 8 ) );
		}
		else if (this.isOpera) {
		  this.versionMinor = parseFloat( ua.substring( ua.indexOf('opera') + 6 ) );
		}
		else if (this.isIcab) {
		  this.versionMinor = parseFloat( ua.substring( ua.indexOf('icab') + 5 ) );
		}
		
		this.versionMajor = parseInt(this.versionMinor, 10); // base 10 added for js link validation
		
		// dom support
		this.isDOM1 = (document.getElementById);
		this.isDOM2Event = (document.addEventListener && document.removeEventListener);
		
		// css compatibility mode
		this.mode = document.compatMode ? document.compatMode : 'BackCompat';
		
		// platform
		this.isWin    = (ua.indexOf('win') != -1);
		this.isWin32  = (this.isWin && ( ua.indexOf('95') != -1 || ua.indexOf('98') != -1 || ua.indexOf('nt') != -1 || ua.indexOf('win32') != -1 || ua.indexOf('32bit') != -1 || ua.indexOf('xp') != -1) );
		this.isMac    = (ua.indexOf('mac') != -1);
		this.isUnix   = (ua.indexOf('unix') != -1 || ua.indexOf('sunos') != -1 || ua.indexOf('bsd') != -1 || ua.indexOf('x11') != -1);
		this.isLinux  = (ua.indexOf('linux') != -1); 
		
		// detect ie version
		this.isIE4x = ((this.isIE) && (this.versionMajor == 4));		
		this.isIE5x = (this.isIE && this.versionMajor == 5);
		this.isIE55 = (this.isIE && this.versionMinor == 5.5);
		this.isIE5up = (this.isIE && this.versionMajor >= 5);
		this.isIE6x = (this.isIE && this.versionMajor == 6);
		this.isIE6up = (this.isIE && this.versionMajor >= 6);
		this.isIE7x = (this.isIE && this.versionMajor == 7);	
		this.isIE7up = (this.isIE && this.versionMajor >= 7);	
		
		// detect ns version
		this.isNS6x = (this.isNS && this.versionMajor == 6);
	
		// detect opera version
		this.isOpera6 = ((this.isOpera) && (this.versionMajor <= 6));	
		
		this.isSafari2 = ((this.isSafari) && (this.versionMajor <= 419.3));
		
		// Supported browsers: IE6+, FF2+, Safari 3+	
		if ((this.isIE6up) || ((this.isSafari) && (this.versionMajor > 419.3)) || ((this.isFirefox) && (this.versionMajor >=2)))
		{		
			this.isHiFi = true;					
		}	
		else
		{	
			this.isHiFi = false;						
		}
	}

	// init
	whichbrowser = new detectbrowser();	
	
	function add_event(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 remove_event(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 add_load_event(func)
	{
		// BPA P98
		
		var oldonload = window.onload;
		
		if (typeof window.onload != 'function')
		{
			window.onload = func;	
		}
		else
		{
			window.onload = function()
			{
				if (oldonload)
				{
					oldonload();	
				}
				func();
			};
		}
	}
	
	function process_stylesheet(styleSheet, ruleName, test)
	{
		var test_str = '';		
		
		var ii=0; // Initialize subCounter.                               
		var cssRule = false; // Initialize cssRule. 
		do // For each rule in stylesheet
		{
			if (styleSheet.cssRules) // Browser uses cssRules?
			{
				// FF
				 cssRule = styleSheet.cssRules[ii];
			} 
			else 
			{ 
				// IE
				cssRule = styleSheet.rules[ii]; 
			} 
			if (cssRule) // If we find a rule  
			{							
				if (cssRule.selectorText) // DS start check, else error
				{	
					if (test == 'test')
					{							
						test_str += (cssRule.selectorText.toLowerCase() + "\n\r");
					}
				
					if (cssRule.selectorText.toLowerCase() == ruleName) // does the rule name match  
					{
						return cssRule; // return the style object.
					}								 
				} // DS end check, else error								 
			} // end found cssRule
			ii++; // Increment sub-counter
		} 
		while (cssRule); // end While loop		
		
		if (test === 'test')
		{							
			alert(test_str); // outputs an alert listing all available selectors
		}				
		
		return false;
	}	
	
	function getCSSRule(ruleName, test)
	{
	// Return requested style object	
	// http://www.hunlock.com/blogs/Totally_Pwn_CSS_with_Javascript
	// Cut down by DS to remove unneeded add/edit functionality, 
	//  then extended to allow for @imports (as structuralNSU.css is imported rather than linked)
		
		// Convert test string to lower case.
		ruleName = ruleName.toLowerCase();     

		// if there are linked stylesheets, check them first
		if (document.styleSheets) 
		{ 
	 		for (var i=0; i<document.styleSheets.length; i++) 
			{
				var linkedSheet = document.styleSheets[i]; 
				
				var result = process_stylesheet(linkedSheet, ruleName, test);		
				if (result !== false)
				{
					return result;
				}
			} // end For loop
			
			// if the script got this far, it means that no match has been found yet, so check any imported stylesheets as well
			
	 		for (var j=0; j<document.styleSheets.length; j++) 
			{							
				// check for imported stylesheets, either an @import within a stylesheet, or one within a style block (only the latter has been tested)				
				var linkedSheet2 = document.styleSheets[j]; 								
				var importedSheet = false;	
				
				if (linkedSheet2.imports) // IE
				{
					if (linkedSheet2.imports.length > 0) // this prevents IE from throwing a nasty 'invalid procedure call or argument' error
					{
						importedSheet = linkedSheet2.imports[0];
					}
				} 
				else // FF
				{									
					importedSheet = linkedSheet2.cssRules[0].styleSheet;
				}	
				
				// if this stylesheet/style block uses an @import, check the imported stylesheet for the ruleName
				
				if (importedSheet)
				{
					var result2 = process_stylesheet(importedSheet, ruleName, test);		
					if (result2 !== false)
					{
						return result2;
					}					
				}			
				
			} // end For loop			

		} // end styleSheet ability check		
		
		return false; // nothing found
	} // end getCSSRule 
	
	function to_hex(N) 
	{
		// http://www.linuxtopia.org/online_books/javascript_guides/javascript_faq/rgbtoHex.htm		
	 if (N === null) 
	 {
		 return "00";
	 }
	 N = parseInt(N, 10); 
	 if (N === 0 || isNaN(N)) 
	 {
		 return "00";
	 }
	 N = Math.max(0,N); 
	 N = Math.min(N,255); 
	 N = Math.round(N);
	 return ("0123456789ABCDEF".charAt((N-N%16)/16) + "0123456789ABCDEF".charAt(N%16));
	}		
	
	function rgb_to_hex(color)
	{	
		// FF formats the color as RGB, so we need to convert it to hex for sIFR
		if (color.indexOf('rgb') != -1)		
		{
			// rgb(193, 19, 30)
			var colors = color.replace('rgb(', '');
					colors = colors.replace(')', '');
					colors = colors.replace(' ', '');		
			
			// 193,19,30
			var colors_array = colors.split(',');
			var r = colors_array[0];
			var g = colors_array[1];
			var b = colors_array[2];		
	
			// http://www.linuxtopia.org/online_books/javascript_guides/javascript_faq/rgbtoHex.htm
			var hex_string = ('#' + to_hex(r) + to_hex(g) + to_hex(b));
			
			return (hex_string.toLowerCase());
		}
		else
		{
			// IE already formats the color as hex
			return (color.toLowerCase());
		}
	}
	
	function get_theme_colour()
	{
		var theme_color;
		
		if (whichbrowser.isHiFi)
		{
			var contentHeaderBlock_h1 = getCSSRule('.contentHeaderBlock h1', 'notest');
		}
		
		if (contentHeaderBlock_h1)
		{		
			theme_color = rgb_to_hex(contentHeaderBlock_h1.style.color);	
		}		
		else
		{
			// a hardcoded hex, just in case.
			theme_color = '#008882';
		}		
		
		return theme_color;
	}
	
	function get_flash_theme_colour()	
	{
		// var theme_colour = '0x' + get_theme_colour().substr(1);	
		var theme_colour = '0x00bbb2'; // from design - note that this is not actually the theme colour
		
		return theme_colour;
	}
	
