var wwfEarthHourLightsOff_staticPath = 'http://static.wwf.org.uk/';
var wwfEarthHourLightsOff_pagePath = 'http://earthhourmap.wwf.org.uk/lightswitch/';
var wwfEarthHourLightsOff_partnerCode = 'ECR001006';

function wwfEarthHourLightsOff_DoFSCommand(command, args) {
	function getPageHeight(){
		
		var yScroll;
		if (window.innerHeight && window.scrollMaxY) {	
			yScroll = window.innerHeight + window.scrollMaxY;
		} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
			yScroll = document.body.scrollHeight;
		} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
			yScroll = document.body.offsetHeight;
		}
		
		var windowHeight;
		if (self.innerHeight) {	// all except Explorer
			windowHeight = self.innerHeight;
		} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
			windowHeight = document.documentElement.clientHeight;
		} else if (document.body) { // other Explorers
			windowHeight = document.body.clientHeight;
		}	
		
		return Math.max(windowHeight, yScroll);
	}
	function setStyle(elem, style) {
		for (prop in style) elem.style[prop] = style[prop];
	}

	if (command == 'lightbox') {
		var overlay = document.createElement("div");
		overlay.id = "wwf_earth_hour_light_switch_overlay";
		document.body.appendChild(overlay);
		setStyle(overlay, {
			position: 'absolute', top: 0, left: 0, zIndex: 900, width: '100%',
			height: getPageHeight() + 'px',
			backgroundColor: '#000000',	opacity: '0.8', filter: ' alpha(opacity=80)'
		});
		
		var lightboxPosition = document.createElement("div");
		overlay.id = "wwf_earth_hour_light_switch_lightbox_position";
		document.body.appendChild(lightboxPosition);
		setStyle(lightboxPosition, {
			position: 'absolute', top: '40px', left: '0px', zIndex: 910, width: '100%'
		});
		lightboxPosition.align = 'center';
		
		var lightbox = document.createElement("div");
		lightboxPosition.appendChild(lightbox);
		setStyle(lightbox, {
			width: '640px', height: '480px', margin: 'auto',
			padding: '16px',
			backgroundImage: 'url(' + wwfEarthHourLightsOff_staticPath + 'border.png)', position: 'relative'
		})
		lightbox.onclick = function(e) {
			if (!e) var e = window.event;
			e.cancelBubble = true;
			if (e.stopPropagation) e.stopPropagation();
		}
		
		var closeButton = document.createElement('img');
		closeButton.src = wwfEarthHourLightsOff_staticPath + 'close.png';
		closeButton.width = 40;
		closeButton.height = 40;
		setStyle(closeButton, {
			position: 'absolute', top: '-18px', right: '-18px', cursor: 'pointer'
		});
		lightbox.appendChild(closeButton);
		closeButton.onclick = function() {
			document.body.removeChild(overlay);
			document.body.removeChild(lightboxPosition);
		}

		var iframe = document.createElement('iframe');
		iframe.src = wwfEarthHourLightsOff_pagePath + '?pc=' + encodeURIComponent(wwfEarthHourLightsOff_partnerCode);
		iframe.width = 640;
		iframe.height = 480;
		iframe.style.border = '0px';
		iframe.border = 0;
		iframe.frameBorder = 0;

		lightbox.appendChild(iframe);
		
		window.wwfEarthHourLightsOff_close = function() {
			document.body.removeChild(overlay);
			document.body.removeChild(lightboxPosition);
		}
		overlay.onclick = function() {
			window.wwfEarthHourLightsOff_close();
		}
		
		lightboxPosition.onclick = function() {
			document.body.removeChild(overlay);
			document.body.removeChild(lightboxPosition);
		}
	} else if (command == 'close') {
		var lightSwitch = document.getElementById('wwf_earth_hour_light_switch');
		lightSwitch.parentNode.removeChild(lightSwitch);
	}
};

(function() {
	var FlashDetect = new function(){
		var self = this;
		self.installed = false;
		self.raw = "";
		self.major = -1;
		self.minor = -1;
		self.revision = -1;
		self.revisionStr = "";
		var activeXDetectRules = [
			{
				"name":"ShockwaveFlash.ShockwaveFlash.7",
				"version":function(obj){
					return getActiveXVersion(obj);
				}
			},
			{
				"name":"ShockwaveFlash.ShockwaveFlash.6",
				"version":function(obj){
					var version = "6,0,21";
					try{
						obj.AllowScriptAccess = "always";
						version = getActiveXVersion(obj);
					}catch(err){}
					return version;
				}
			},
			{
				"name":"ShockwaveFlash.ShockwaveFlash",
				"version":function(obj){
					return getActiveXVersion(obj);
				}
			}
		];
		var getActiveXVersion = function(activeXObj){
			var version = -1;
			try{
				version = activeXObj.GetVariable("$version");
			}catch(err){}
			return version;
		};
		var getActiveXObject = function(name){
			var obj = -1;
			try{
				obj = new ActiveXObject(name);
			}catch(err){
				obj = {activeXError:true};
			}
			return obj;
		};
		self.FlashDetect = function(){
			if(navigator.plugins && navigator.plugins.length>0){
				var type = 'application/x-shockwave-flash';
				var mimeTypes = navigator.mimeTypes;
				if(mimeTypes && mimeTypes[type] && mimeTypes[type].enabledPlugin && mimeTypes[type].enabledPlugin.description){
					self.installed = true;
				}
			}else if(navigator.appVersion.indexOf("Mac")==-1 && window.execScript){
				var version = -1;
				for(var i=0; i<activeXDetectRules.length && version==-1; i++){
					var obj = getActiveXObject(activeXDetectRules[i].name);
					if(!obj.activeXError){
						self.installed = true;
					}
				}
			}
		}();
	};
	FlashDetect.JS_RELEASE = "1.0.4";
	
	if (FlashDetect.installed) {
		document.write('<div id="wwf_earth_hour_light_switch" style="position: absolute; top: 0px; right: 0px; z-index: 999;"><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=10,0,0,0" width="120" height="100" id="wwfEarthHourLightsOff" align="middle"><param name="allowScriptAccess" value="always" /><param name="wmode" value="transparent"><param name="allowFullScreen" value="false" /><param name="movie" value="' + wwfEarthHourLightsOff_staticPath + 'lightswitch.swf" /><param name="quality" value="high" /><param name="bgcolor" value="#ffffff" /><embed src="' + wwfEarthHourLightsOff_staticPath + 'lightswitch.swf" quality="high" bgcolor="#ffffff" width="120" height="100" name="wwfEarthHourLightsOff" swLiveConnect="true" wmode="transparent" align="middle" allowScriptAccess="always" allowFullScreen="false" type="application/x-shockwave-flash" pluginspage="http://www.adobe.com/go/getflashplayer" /></object></div>');
	}
})()