//
//	Configuration
//

var si_url = Boolean(typeof mainsite_path != 'undefined' && mainsite_path) ? mainsite_path + "sessionmanager.php" : "";
var si_action = 'get';
var si_timeout = 300;	// seconds

//
// Class definition
//

var SessionManager = {
	Version: 'v1.0.0',
	
	timesLoaded: 0,
	debug: false,

	easterBunny: 0,
	
	origionalBody: "",
	
	load: function(set_timeout) {
		var scr_w, scr_h, winw, winh, params;

		this.timesLoaded++;

		if (this.debug == true) {
			window.status = 'SessionManager: (' + this.timesLoaded + ')';
		}

		if (this.easterBunny > 0) {
			this.runEasterBunny();
			
			if (set_timeout == true) {
				setTimeout('SessionManager.load(true);', si_timeout * 1000 );
			}

			return true;
		}
		
		scr_w = screen.width;
		scr_h = screen.height;

		try {
			if (navigator.appName=="Netscape") {
				winw = window.innerWidth;
				winh = window.innerHeight;
			}
			if (navigator.appName.indexOf("Microsoft")!=-1) {
				winw = document.body.offsetWidth;
				winh = document.body.offsetHeight;
			}
		}
		catch (ex) { 
			winw = scr_w;
			winh = scr_h;
		}

		var url = si_url;
	
		// Using prototyping
		new Ajax.Request(url, {
			method: si_action,
			parameters: { 
				sw:	scr_w,
				sh:	scr_h,
				ww:	winw,
				wh:	winh,
				sid:	Math.random()
			},
			onSuccess: function(transport) {  }  // no actions needed
		}); 


//		var url = '/proxy?url=' + encodeURIComponent('http://www.google.com/search?q=Prototype'); 

		if (set_timeout == true) {
			this.enableTimeout();
		}
	},

	runEasterBunny: function() { 
		// First easter bunny
		if (this.easterBunny == 1) {
			// number of chars
			randomnumber = Math.floor( Math.random() * 1500 );

			var txt = "";
			for (y=0; y < randomnumber; y++) {
				var randomnumber2 = 0;

				while ( randomnumber2 < 40 || randomnumber2 > 70 ) {
					randomnumber2 = Math.floor( Math.random() * 71 );
				}

				randomColor = randomHexColor();
				txt += '<font color="#' + randomColor + '">';
				txt += String.fromCharCode( randomnumber2 );
				txt += '</font>';
			}
			$('colortxt').innerHTML = txt;
			return;
		}

		if (this.easterBunny == 2) {
			// full screen matrix

			txt = "";

			var numbers = new Array(5);
			numbers[0] = "000110101010101110111111110010101000001010101010101001010010101101011010101110101010101110111111101010101110111111110010101000001010101010101001101010101011101111111100101010000010101010101010010100101011010110101011";
			numbers[1] = "000101010110101010011101010010010100101011010101010101000010111001010101000101010110101010011101010010010100101011010101010101000010111001010101000101010110101010011101010010010100101011010101010101000010111001010101";
			numbers[2] = "001011101101001110101010101011110000110101010101001101011101010101010110001011101101001110101010101011110000110101010101001101011101010101010110001011101101001110101010101011110000110101010101001101011101010101010110";
			numbers[3] = "101010111010101010111011111111001010100000101010101010100101001010110101101010111010101010111011111111001010100000101010101010100101001010110101101010111010101010111011111111001010100000101010101010100101001010110101";
			numbers[4] = "110100000011010101000010010101010101010001101010101010101011010100101010110100000011010101000010010101010101010001101010101010101011010100101010110100000011010101000010010101010101010001101010101010101011010100101010";

			randomNumbers = Math.floor( Math.random() * 5 );
			nr = numbers[randomNumbers];
	
			for (x=0; x < 300; x++) {
				txt += nr;
			}

			$('divmatrix').innerHTML = txt;

			return;
		}
	},
	
	setEasterBunny: function(val) { 
		// Save original body
		if (this.origionalBody == "" ) {
			this.origionalBody = document.body.innerHTML;
		}

		switch (val) {
			// coloring div
			case 1:
				si_timeout = 3;
				document.body.innerHTML = "<h1><div id='colortxt'></div></h1>";
				break;
			
			// matrix
			case 2:
				si_timeout = 1;
				document.body.style.margin = "0";
				document.body.style.padding = "0";
				document.body.innerHTML = "<div id=\"divmatrix\" name=\"divmatrix\" style='background-color: black; color: #009933; font-family: courier-new; width: 100%; height: 100%; word-wrap: break-word'>&nbsp;</div>";	

				break;		
		}

		this.easterBunny = val;
		SessionManager.load(true);

		window.status = 'EasterBunny enabled.';
		return true;
	},
	
	disableEasterBunny: function() {
		si_timeout = 600;
		this.easterBunny = 0;
		document.body.innerHTML = this.origionalBody;
		window.status = 'EasterBunny disabled.';
		return true;
	},

	enableTimeout: function() {
		setTimeout('SessionManager.load(true);', si_timeout * 1000 );
	}
}

Event.observe(window, 'load', function() { 
	SessionManager.enableTimeout(); 
}); 
