function Logger() {
	this.logConsole = null;
	this.logTextArea = null;
	this.debugTab = null;
	this.logs = "";
	this.isExpanded = 0;
	this.enabled = 0;
}

Logger.prototype = {
	init: function() {
		this._createLogWindow();
	}
	,
	
	info: function(logText) {
		if (this.enabled) {
			this._isLogWindowExists();
			this._append(logText);
		}
	}
	,
	_isLogWindowExists: function() {
		if (this.logConsole == null) {
			this._createLogWindow();
		}
	}
	,
	
	_createLogWindow: function() {
    this._constructLogConsole();
    this._constructLogTextArea();
    this._constructDebugTab();
		this.logConsole.appendChild(this.logTextArea);
		this.logConsole.appendChild(this.debugTab);
		if (this.enabled) {
		  document.body.appendChild(this.logConsole);
    }
		this._repositionLogWindow();
	}
	,
	
	_constructLogConsole: function() {
    this.logConsole = document.createElement('div');
		this.logConsole.className='logBackground';
		this.logConsole.style.left = '0px';
	  this.logConsole.style.top = '0px';
		this.logConsole.style.zIndex = 94000;
		this.logConsole.style.visibility = 'visible';
		this.logConsole.style.display = 'block';
  }
  ,
  
  _constructLogTextArea: function() {
		this.logTextArea = document.createElement('textarea');
		this.logTextArea.className='logTextarea';
		this.logTextArea.readOnly = 'true';
		this.logTextArea.style.border = 'solid 1px';
		this.logTextArea.style.visibility = 'hidden';
		this.logTextArea.style.display = 'none';
  }
  ,
  
  _constructDebugTab: function() {
		this.debugTab = document.createElement('div');
		this.debugTab.className = 'debugText';
		this.debugTab.style.cursor = 'pointer';
		this.debugTab.style.backgroundColor = '#AAA';
		this.debugTab.style.marginTop = '-2px';
		this.debugTab.style.border = 'solid 1px';
  	this.debugTab.style.width = '60px';
		this.debugTab.style.height = '16px';
		this.debugTab.innerHTML = '<b>DEBUG</b>';
		logInstance = this;
		this.debugTab.attachEvent('onclick', function(e){logInstance._expand()});
		this.debugTab.attachEvent('onselectstart', function(e){return false});
  }
  ,
	
	_expand:function() {
    if (this.isExpanded) {
      this.logTextArea.style.visibility = 'hidden';
		  this.logTextArea.style.display = 'none';
		  this.isExpanded = 0;
    }
    else {
      this.logTextArea.style.visibility = 'visible';
		  this.logTextArea.style.display = 'block';
		  this.isExpanded = 1;
    }
  }
  ,
  
	_repositionLogWindow: function() {
		var topOffset = Math.max(document.body.scrollTop, document.documentElement.scrollTop);
  	var leftOffset = Math.max(document.body.scrollLeft, document.documentElement.scrollLeft + document.getElementById('shadowLeft').offsetLeft);

		browserSize = this._getBrowserSize();
		var bodyWidth = browserSize[0];
		var bodyHeight = browserSize[1];
		
		this.logConsole.style.left = leftOffset + 'px';;
	}
	,
	
	_append: function(logText) {
		this.logs = '-' + this._formatTimestamp() + ": " + this._formatLog(logText) + '\n';
		this.logTextArea.value = this.logTextArea.value + this.logs;
	}
	,
	
	_formatTimestamp: function() {
		var date = new Date();
		return date.toLocaleString();
	}
	,
	
	_formatLog: function(logText) {
		return logText;
	}
	,
	
	_getBrowserSize : function() {
		var bodyWidth = document.documentElement.clientWidth;
		var bodyHeight = document.documentElement.clientHeight;

		if (self.innerHeight){
		   bodyWidth = self.innerWidth; 
		   bodyHeight = self.innerHeight; 
		} 
		else if (document.documentElement && document.documentElement.clientHeight) {
		   bodyWidth = document.documentElement.clientWidth; 
		   bodyHeight = document.documentElement.clientHeight; 
		}
		else if (document.body) {
		   bodyWidth = document.body.clientWidth; 
		   bodyHeight = document.body.clientHeight; 
		}
		
		return [bodyWidth, bodyHeight];
	}	
	
}

function TheEye() {
	var flashEye;
	var vulnerability;
	var hasFlashPlayer;
	var hasActivated;
	var requiredFlashVersion;

	this.flashEye = null;
	this.vulnerability = null;
	this.hasFlashPlayer = false;
	this.hasActivated = false;
	this.requiredFlashVersion = 9;
}

TheEye.prototype = {
	init: function() {
    this._isFlashPlayerAvailable();
	}
	,
	
	_isFlashPlayerAvailable: function() {
	  try {
      //detect whether installed version is 9
      var axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.9");
      var majorVersion = axo.GetVariable("$version").split(" ")[1].split(",")[0];
      if (majorVersion == null || majorVersion < this.requiredFlashVersion) {
        return
      }
		}
		catch (e) {
		  return;
    }
		
		this.flashEye = document.getElementById("flashEye");
		if (this.flashEye != null && this.flashEye.readyState == 4) {
			this.hasFlashPlayer = true;
		}
		return;
  }
  ,
	
	setVulnerability: function(vuln) {
		this.vulnerability = vuln;
	}
	,
	
	activate: function() {
		if (this.hasActivated)
			return

		document.getElementById('eyebg').style.visibility = "visible";
		//flash player is available
		if (this.hasFlashPlayer) {
			document.getElementById('eyeanim').style.visibility = "visible";
			if (this.flashEye.restartLoop)
				this.flashEye.restartLoop();
		}
		else {//no flash player
			document.getElementById('eyegray').style.visibility = "visible";
		}
		
		this.hasActivated = true;
	}
	,
	
	/**
	 * 1=solved red
	 * 2=solved orange
	 */	
	solve: function(color) {
		if (1 == color)
			this.vulnerability.red--;
		else {
			this.vulnerability.orange--;
			this.vulnerability.gray++;
		}
			
		this.updateColor();
	}
	,
	
	updateColor: function() {
		if (this.vulnerability.red == 0 && this.vulnerability.orange == 0 && this.vulnerability.gray == 0){
			this.playAndStopGreen();
		}
		else if (this.vulnerability.red > 0) {
			this.playAndStopRed();
		}
		else if (this.vulnerability.orange > 0) {
			this.playAndStopYellow();
		}
		else {
			this.playAndStopGray();
		}
	}
	,

	playAndStopGreen: function() {
		if (this.hasFlashPlayer) {
			this.stopAnimate();
			this.flashEye.stopGreen();
		}
		else {
			this.hideImageEyes();
			document.getElementById('eyegreen').style.visibility = "visible";
		}
	}
	,
	
	playAndStopYellow: function() {
		if (this.hasFlashPlayer) {
			this.stopAnimate();
			this.flashEye.stopYellow();
		}
		else {
			this.hideImageEyes();
			document.getElementById('eyeorange').style.visibility = "visible";
		}
	}
	,
	
	playAndStopRed: function() {
		if (this.hasFlashPlayer) {
			this.stopAnimate();
			this.flashEye.stopRed();
		}
		else {
			this.hideImageEyes();
			document.getElementById('eyered').style.visibility = "visible";
		}
	}
	,	

	playAndStopGray: function() {
		if (this.hasFlashPlayer) {
			this.stopAnimate();
			this.flashEye.stopGray();
		}
		else {
			this.hideImageEyes();
			document.getElementById('eyegray').style.visibility = "visible";
		}
	}
	,
	
	hideImageEyes: function() {
		document.getElementById('eyegreen').style.visibility = "hidden";
		document.getElementById('eyeorange').style.visibility = "hidden";
		document.getElementById('eyered').style.visibility = "hidden";
		document.getElementById('eyegray').style.visibility = "hidden";
	}
	,
	
	deactivate: function() {
		document.getElementById('eyegreen').style.visibility = "hidden";
		document.getElementById('eyeorange').style.visibility = "hidden";
		document.getElementById('eyered').style.visibility = "hidden";
		document.getElementById('eyegray').style.visibility = "hidden";
		document.getElementById('eyeanim').style.visibility = "hidden";
		document.getElementById('eyebg').style.visibility = "hidden";
		this.hasActivated = false;
	}
	,

	stopAnimate: function() {
		if (this.hasFlashPlayer) {
			this.flashEye.stopLoop();
			this.flashEye.stopRing();
		}
	}
	,
	
	animate: function() {
		if (hasFlashPlayer)
			this.flashEye.animate();
	}
}

logger = new Logger();
theEye = new TheEye();

