var tmpStr = '';
var navOffSet = 0;
var __mzOverride = false;
Event.observe(document, 'dom:loaded', function() {

	navOffSet = $('features-nav').cumulativeOffset()[1];
	
	if ( !__mzOverride ) {
		
		/* Target Test */
		if ( document.location.hash == '' ){
			document.body.id = 'target-selector';
			document.location.hash = 'target-selector';
		} else {
			h = document.location.hash.substr(1);
			document.location.hash = '';
			document.body.id = h;
			document.location.hash = h;
		}
		
		/* http://jsbin.com/afosa */
		__addHTML5Tests();
		
		/* Add Modernizr's InputType tests to HTML */
		__addInputTests();
		
		/* Add Modernizr's InputAttr tests to HTML */
		__addInputAttributes();
		
		/* Add Modernizr's Audio/Video tests to HTML */
		__addAV();
	
		// Highligh correct tab in Primary navigation
		$$('#home-nav ul a').invoke('observe','click', function() {
			$(this).up('ul').select('li').each(function(e) {
				e.removeClassName('active');
			});
			$(this).up('li').addClassName('active');
		});
		
		/* Modernizer
		------------------------------------------------*/
		if ($$('ul.modernizer-support')[0]) {
			var mzrList = $$('ul.modernizer-support')[0];
		}
		
		// Get the Modernizr Object and convert to a Hash
		var mzr = $H(window.Modernizr);
		
	} else {
		
		document.documentElement.className = '';
		$$('.selector-tests').invoke('removeClassName','selector-tests');
		
		$H(__mzOverride).each(function(pair){
			if ( pair.value == '1' ) {
				// console.log([pair.key,$(pair.key),pair.value]);
				$(pair.key).setStyle({ 
					backgroundImage :'url(/r/i/icons/silk_set_ie6/tick.png)' 
				});
			} else {
				$(pair.key).setStyle({ 
					backgroundImage :'url(/r/i/icons/silk_set_ie6/cross.png)' 
				});
			}
		})
	}
	
	
	
	
	/* Other bits & bobs.. 
	------------------------------------------------*/
	/** Hook up the navigation widget.. */
	Event.observe(window,'scroll',__navScroller);
	 
});

function checkOverwrite(mzr){
	if ( typeof __mzOverride != 'undefined' ) {
		$H(__mzOverride).each(function(pair){
			if ( mzr.get(pair.key) != 'undefined' ) {
				alert( pair.key + " = " + pair.value + " == " + !!(pair.value=='1'));
				mzr.set(pair.key, !!(pair.value=='1'));
			}
		})
	}
}	



function __addHTML5Tests(){
	/**
	 *	Now using Modernizr 1.1 supported classes where practical..
	 */
	if ( mXtra = document.body ) {
		// localstorage -> localstorage (ok)
		Modernizr.localstorage			&& mXtra.addClassName('localstorage');
		
		// sessionstorage -> sessionstorage (ok)
		Modernizr.sessionstorage		&& mXtra.addClassName('sessionstorage');
		
		
		// offlineapplications -> offlineapplications
		// !!window.applicationCache		&& $('modernizer').addClassName('offlineapplications');
		Modernizr.applicationcache 		&& mXtra.addClassName('offlineapplications');
		
		// workers -> webworkers
		// !!window.Worker					&& $('modernizer').addClassName('workers');
		Modernizr.webworkers			&& mXtra.addClassName('workers');
		
		
		// No apparent modernizr support?
		!!window.postMessage			&& mXtra.addClassName('postmessage');
		!!document.querySelector		&& mXtra.addClassName('queryselector');
		!!window.openDatabase			&& mXtra.addClassName('webdatabase');
		!!navigator.geolocation			&& mXtra.addClassName('geolocation');
	}
}


function __addInputTests(){
	inputClasses = [];
	$H( window.Modernizr.inputtypes ).each(function(a,b){
		// God knows what's happened but a prototype/modernizr clash... 
		if (Object.isString(a[0]) && !a[0].startsWith('function')){
			if ( a[1] ) { // true
				inputClasses.push('input-'+a[0]);
			}
		}
	});
	if ( document.documentElement ) {
		document.documentElement.className += ' ' + inputClasses.join( ' ' );
	}
}
function __addInputAttributes(){
	inputClasses = [];
	$H( window.Modernizr.input ).each(function(a,b){
		// God knows what's happened but a prototype/modernizr clash... 
		if (Object.isString(a[0]) && !a[0].startsWith('function')){
			if ( a[1] ) { // true
				inputClasses.push('input-'+a[0]);
			}
		}
	});
	if ( document.documentElement ) {
		document.documentElement.className += ' ' + inputClasses.join( ' ' );
	}
}
function __addAV(){
	classes = [];
	'h264 ogg webm'.split(' ').each(function(a){
		if ( Modernizr.video[a] && ( Modernizr.video[a] != '' ) ){
			//alert(a + " = " + Modernizr.video[a]);
			classes.push('video-'+a);
		}
	});
	'm4a mp3 ogg wav'.split(' ').each(function(a){
		//alert(a + " = " + Modernizr.audio[a]);
		if ( Modernizr.audio[a] && ( Modernizr.audio[a] != '' ) ){
			classes.push('audio-'+a);
		}
	});
	if ( document.documentElement ) {
		document.documentElement.className += ' ' + classes.join( ' ' );
	}
}






 













function __navScroller(){
	if ( $('features-nav') ) {
		if( document.body && document.body.scrollTop ) {
			y = document.body.scrollTop;
		} else if( document.documentElement && ( document.documentElement.scrollTop ) ) {
			y = document.documentElement.scrollTop;
		} else {
			y = 0;
		}
		
		
	
		if ( y > navOffSet ) {
			if ( !$('features-nav').hasClassName('floating-nav-container') ){
				$('features-nav').addClassName('floating-nav-container');
			}
		} else {
			
			if ( $('features-nav').hasClassName('floating-nav-container') ){
				$('features-nav').removeClassName('floating-nav-container');
			}
		}
	}
}
