/**
* OCA Homepage Boot
* "Small energy boost for the homepage with Javascript from the earliest time"
* Process a bit of JS before the page contents are drawn. jQuery isn't even available yet!
* This file should be as small as possible...
* @company Optus
* @author David HAN SZE CHUEN
* @creationDate 15/03/2010
*/
(function(){
	// apply a CSS marker to show that JS is available in CSS
	document.documentElement.className += ' js';
	
	var addCssToBody = function(css){
			document.body.className += ' ' + css;
		},
		enableOptusDinMediumFontClass = function(){
			addCssToBody('font_OptusDinMedium');
		}; 
		// Not in use in the global site at the moment.
		// ,
		// enableOptusDinBoldFontClass = function(){
			// addCssToBody('font_OptusDinBold');
		// };
	
	
	if (/*@cc_on!@*/0) { // detects IE
		enableOptusDinMediumFontClass();
	} else { // not IE, try to detect font load
		/**
		* It's assumed that the Optus font will have a smaller width footprint than regular arial font.
		* i.e. For a given font-size (10px), Optus DIN will create larger texts than Arial would.
		* Relies on a specific class .custom_font_detect
		* @param {String} fontFamily Font family
		* @param {Function} onDetectHandler Function to run when the font is detected
		* @param {Function} onTimeoutHandler Function to run when the font is not detected after the max waiting time
		* @param {Integer} maxRuntime (optional) Tries to detect the custom font for this amount of time (in seconds)
		* @return {void}
		*/
		window.detectOptusCustomFontLoad = function(fontFamily, onDetectHandler, onTimeoutHandler, maxRuntime) {
			var d = document.createElement('div'),
				defaultFont = ', arial',
				cycleTime = 100, // check every 100ms
				maxLoops = (maxRuntime !== undefined)? maxRuntime * 10 : 1200, // maxLoops x cycleTime = maxRunTime (default 120s)
				defaultHeight = 20, // default height when custom font is not loaded, so it's arial
				intervalId;
			
			// check onDetectHandler
			if (!onDetectHandler || !onDetectHandler.apply) {
				throw new Error('detectOptusCustomFontLoad(): invalid onDetectHandler parameter.');
			}
			// check font family
			if (!fontFamily) {
				throw new Error('detectOptusCustomFontLoad(): invalid fontFamily parameter.');
			}
			
			d.style.fontFamily = fontFamily + defaultFont;
			d.className = 'custom_font_detect';
			d.innerHTML = '1 2 3';
			
			document.body.appendChild(d);
			//console.log( document.getElementsByTagName('body'), document.body );
			
			//alert(d.offsetHeight + ' px');
			var checkFunction = function(){
				//console.log('test height: ', d.offsetHeight);
				if (d.offsetHeight !== defaultHeight) { // detected font
					onDetectHandler.call(this, d);
					if (intervalId !== undefined) {
						clearInterval(intervalId);
					}
					return true;
				} else {
					maxLoops--;
					if (maxLoops < 0) { // if timeout
						clearInterval(intervalId);
						
						if (onTimeoutHandler && onTimeoutHandler.call) {
							onTimeoutHandler.call(this, d);
						}
						//DEBUG
						//alert('detectOptusCustomFontLoad(): abandon waiting for font.');
					}
					return false;
				}
			};
			
			if (!checkFunction()) {
				intervalId = setInterval(checkFunction, cycleTime);
			}
		}; // end of window.detectOptusCustomFontLoad = function(fontFamily, onDetectHandler, onTimeoutHandler, maxRuntime)
		
		detectOptusCustomFontLoad('OptusDINCond-Medium', enableOptusDinMediumFontClass);
		
		// Not in use in the global site at the moment.
		//detectOptusCustomFontLoad('OptusDINCond-Bold', enableOptusDinBoldFontClass);
	}
})();
