var browser;
var OSName;

var browserStatus;
var OSStatus;
var flashStatus;
var SFCStatus;
var popupStatus;
var connectedStatus;




/* GRAPHICS & ANIMATION */
function showHelp(helpToShowID) {
	$('#'+helpToShowID).fadeIn("0.75");
}
function hideHelp(helpToHideID) {
	$('#'+helpToHideID).fadeOut("0.75");
}

function resetListBackgrounds(clickedList) {	
	if ((clickedList != 'flash-detect') && ($('#flash-detect')[0].className == 'active-help')) {
		$('#comic-adjustments')[0].className = "hidden";
		hideHelp('flash-help');
		$('#flash-detect')[0].className = 'error-help';
	}
	if ((clickedList != 'plugin-detect') && ($('#plugin-detect')[0].className == 'active-help')) {
		$('#comic-adjustments')[0].className = "hidden";
		hideHelp('plugin-help');
		$('#plugin-detect')[0].className = 'error-help';
	}
	if ((clickedList != 'popups-detect') && ($('#popups-detect')[0].className == 'active-help')) {
		$('#comic-adjustments')[0].className = "hidden";
		hideHelp('popup-ie7-help');
		hideHelp('popup-ff-help');
		hideHelp('popup-saf-help');
		$('#popups-detect')[0].className = 'error-help';
	}
	if ((clickedList != 'connected-detect') && ($('#connected-detect')[0].className == 'cleared-toplay-active-help')) {
		$('#comic-adjustments')[0].className = "hidden";
		hideHelp('connect-help');
		hideHelp('connect-help-noplugin');
		hideHelp('connect-vista-help');
		$('#connected-detect')[0].className = 'cleared-toplay-help';
	}

}




/* BROWSER DETECTION */
function handleBrowserDetect(displayID) {
	browser = BrowserDetect.browser;
	var version = BrowserDetect.version;
	
	if (browser == "Explorer") {
		$('#'+displayID).html('Browser: '+'Internet Explorer '+version);
		$('#'+displayID)[0].className ='cleared';
		browserStatus = 'green';
	}
	else {
		$('#'+displayID).html('Browser: <span class="orange">'+browser+' '+version+' (game only)</span>');
		$('#'+displayID)[0].className ='cleared-toplay';
		browserStatus = 'orange'
	}

	
	
}

/* OS DETECTION */
function handleOSDetect(displayID) {
	OSName="Unknown OS";
	if (navigator.appVersion.indexOf("Win")!=-1) OSName="Windows";
	if (navigator.appVersion.indexOf("Mac")!=-1) OSName="MacOS";
	if (navigator.appVersion.indexOf("X11")!=-1) OSName="UNIX";
	if (navigator.appVersion.indexOf("Linux")!=-1) OSName="Linux";

	if (OSName == "Windows") {
		if (navigator.userAgent.indexOf("Windows NT 6.0") > -1)
			$('#'+displayID).html('Operating system: Windows Vista');
		else if (navigator.userAgent.indexOf("Windows NT 6.1") > -1)
			$('#'+displayID).html('Operating system: Windows 7');
		else
			$('#'+displayID).html('Operating system: '+OSName);
		
		$('#'+displayID)[0].className ='cleared';
		OSStatus = 'green';
	}
	else {
		$('#'+displayID).html('Operating system: <span class="orange">'+OSName+' (game only)</span>');
		$('#'+displayID)[0].className ='cleared-toplay';
		OSStatus = 'orange';
	}
}



/* FLASH DETECTION */
function handleFlashDetect(displayID) {
	var version = deconcept.SWFObjectUtil.getPlayerVersion();
	
	var major = version['major'];
	var minor = version['minor'];
	var release = version['rev'];
		
	// var major = swfobject.getFlashPlayerVersion().major;
	// var minor = swfobject.getFlashPlayerVersion().minor;
	// var release = swfobject.getFlashPlayerVersion().release;
	
	if (major == 0) {
		$('#'+displayID).html('Flash: <span class="red">Not Installed</span>');
		$('#'+displayID)[0].className ='error-help';
		$('#'+displayID).click( function() {  resetListBackgrounds(displayID); $('#'+displayID)[0].className = 'active-help'; showHelp('flash-help') } );
		flashStatus = 'red';
	}
	else if (major < 9) {
		$('#'+displayID).html('Flash: Version <span class="red">'+major+'.'+minor+'</span>');
		$('#'+displayID)[0].className ='error-help';
		$('#'+displayID).click( function() {  resetListBackgrounds(displayID); $('#'+displayID)[0].className = 'active-help'; showHelp('flash-help') } );
		flashStatus = 'red';
	}
	else {
		$('#'+displayID).html('Flash: Version '+major+'.'+minor);
		$('#'+displayID)[0].className ='cleared';
		flashStatus = 'green';
	}	
}


/* POPUP DETECTION */
function handlePopupsDetect(displayID) {
	var mine = window.open('','','width=1,height=1,left=0,top=0,scrollbars=no');
	var popUpsBlocked = true;
	if (mine) {
		popUpsBlocked = false;
		mine.close();
	}

	if(popUpsBlocked) {
		$('#'+displayID).html('Popup blockers: <span class="red">Activated</span>');
		$('#'+displayID)[0].className ='error-help';
				
		if (browser == "Firefox")
			$('#'+displayID).click(function() { resetListBackgrounds(displayID); showHelp('popup-ff-help'); $('#'+displayID)[0].className='active-help'});
		else if (browser == "Safari")
			$('#'+displayID).click(function() { resetListBackgrounds(displayID); showHelp('popup-saf-help'); $('#'+displayID)[0].className='active-help'});
		else
			$('#'+displayID).click(function() { resetListBackgrounds(displayID); showHelp('popup-ie7-help'); $('#'+displayID)[0].className='active-help' });
		
			
			//TODO replace 'popups-detect with displayID var
			
		popupStatus = 'red';
	}
	else {
		$('#'+displayID).html('Popup blockers: Deactivated');
		$('#'+displayID)[0].className ='cleared';
		popupStatus = 'green';
	}

}

/* PLUGIN DETECTION */			
function detectPluginOK(e) {
	if (e != null && (typeof e != "undefined")) {	
	    if ( e.readyState != 4 )
			return false;
		else
			return true;
	}
	
	return false;
	
} // RGNotifyActiveX()				
function handlePluginDetect(objectID, pluginID, connectedID) {
	var pluginInstalled = detectPluginOK( document.getElementById(objectID) );

	
	if (pluginInstalled)
		if (typeof getRGFCPlugin == "undefined")
			pluginInstalled = false;

	if(pluginInstalled) {	
		$('#'+pluginID).html('SFC Plugin: Installed');
		$('#'+pluginID)[0].className = 'cleared';
		pluginStatus = 'green';
		
		
		var connected = false;
	    
		if (typeof getRGFCPlugin != "undefined") {
			var plugin = getRGFCPlugin();
		
			connected = plugin.IsConnected();
		}
				
		if (connected) {
			$('#'+connectedID).html('Supersonic Fuel Cell: Connected');
			$('#'+connectedID)[0].className = 'cleared';
			connectedStatus = 'green';
		}
		else {
			$('#'+connectedID).html('Supersonic Fuel Cell: <span class="orange">Not Connected</span>');
			$('#'+connectedID)[0].className = 'cleared-toplay-help';
			if ((navigator.userAgent.indexOf("Windows NT 6.0") > -1) || (navigator.userAgent.indexOf("Windows NT 6.1") > -1))
				$('#'+connectedID).click( function() { resetListBackgrounds('connected-detect');  $('#'+connectedID)[0].className = 'cleared-toplay-active-help'; showHelp('connect-vista-help') } );	
			else
				$('#'+connectedID).click( function() { resetListBackgrounds('connected-detect');  $('#'+connectedID)[0].className = 'cleared-toplay-active-help'; showHelp('connect-help') } );	
			connectedStatus = 'orange';
		}
		
	}
	else {
		
		$('#'+pluginID).html('SFC Plugin: <span class="orange">Not Installed</span>');
		if (OSName == "Windows") {
			if (browser == "Explorer") {				
					$('#'+pluginID)[0].className ='error-help';
					$('#'+pluginID).click( function() { resetListBackgrounds('plugin-detect');  $('#'+pluginID)[0].className = 'active-help'; showHelp('plugin-help') } );
					pluginStatus = 'red';
			}
			else {
				$('#'+pluginID)[0].className ='cleared-toplay';
				pluginStatus = 'orange';
			}
		}
		else {
			$('#'+pluginID)[0].className ='cleared-toplay';
			pluginStatus = 'orange';
		}
			
		
		$('#'+connectedID).html('Supersonic Fuel Cell: <span class="orange">Not Connected</span>');
		$('#'+connectedID)[0].className ='cleared-toplay-help';
		if (browser == "Explorer") {
			if ((navigator.userAgent.indexOf("Windows NT 6.0") > -1) || (navigator.userAgent.indexOf("Windows NT 6.1") > -1))
				$('#'+connectedID).click( function() { resetListBackgrounds('connected-detect');  $('#'+connectedID)[0].className = 'cleared-toplay-active-help'; showHelp('connect-vista-help') } );	
			else
				$('#'+connectedID).click( function() { resetListBackgrounds('connected-detect');  $('#'+connectedID)[0].className = 'cleared-toplay-active-help'; showHelp('connect-help') } );	
		}
		else
			$('#'+connectedID).click( function() { resetListBackgrounds('connected-detect');  $('#'+connectedID)[0].className = 'cleared-toplay-active-help'; showHelp('connect-help-noplugin') } );
		connectedStatus = 'orange';
	}
	
}



function checkRequirements() {
	handleBrowserDetect('browser-detect');
	handleOSDetect('os-detect');
	handleFlashDetect('flash-detect');
	handlePopupsDetect('popups-detect');
	handlePluginDetect('RGFCPlugin', 'plugin-detect', 'connected-detect');
	
	if (browserStatus == 'red'	||
		OSStatus == 'red'		||
		flashStatus == 'red'	||
		SFCStatus == 'red'		||
		popupStatus == 'red'	||
		connectedStatus == 'red'
		) {
		$('#comic-blastoff')[0].className = 'hidden';
		$('#comic-adjustments')[0].className = 'shown';
		$('#comic-cannotconnect')[0].className = 'hidden';
		
		$('#launch-button')[0].className = 'denied';
		$('#launch-button').html('<a href="" onclick="document.location.href = \'/play/index.html?\'+Math.random();return false;"></a>');
		
		$('#preflight-report p').html('<span class="red">WARNING: </span>Your system is not properly configured to connect your robot or play the online game. Please click on the <img src="/images/play/transition/help-small.gif" style="display: inline; margin-bottom: -3px" /> buttons next to the problems above to be cleared for launch.');		
	}
	else if (browserStatus == 'orange'	||
		OSStatus == 'orange'			||
		flashStatus == 'orange'			||
		SFCStatus == 'orange'			||
		popupStatus == 'orange'			||
		connectedStatus == 'orange'
		) {
		$('#comic-blastoff')[0].className = 'hidden';
		$('#comic-adjustments')[0].className = 'hidden';
		$('#comic-cannotconnect')[0].className = 'shown';
		
		$('#launch-button')[0].className = 'cleared-toplay';
		$('#launch-button').html('<a href="" onclick="goEnvironment();return false;"></a>');
	
		$('#preflight-report p').html('<span class="orange">CAUTION: </span>Your system is not able to connect your robot. Please click the <img src="/images/play/transition/help-small.gif" style="display: inline; margin-bottom: -3px" /> buttons to get help resolving the problems. You can play the game by clicking on the launch button to the right.');			
	}
	else {
		$('#comic-blastoff')[0].className = 'shown';
		$('#comic-adjustments')[0].className = 'hidden';
		$('#comic-cannotconnect')[0].className = 'hidden';
		
		$('#launch-button')[0].className = 'cleared-tolaunch';
		$('#launch-button').html('<a href="" onclick="goEnvironment();return false;"></a>');
	
		$('#preflight-report p').html('<span class="green">CONGRATULATIONS: </span>Your system is properly configured to connect your robot and play the online game. Please click on the launch button to the right!');			
	}
}