// All Doperoms JavaScript Files
// For Doperoms.com you crazy kids!
function CrossInnerHtml(e,h) {
	if(!(e=xGetElementById(e)) || !xStr(e.innerHTML)) return null;
	var s = e.innerHTML;
	if (xStr(h)) {e.innerHTML = h;}
	return s;
}
function xStr(s) {
	for(var i=0; i<arguments.length; ++i){if(typeof(arguments[i])!='string') return false;}
	return true;
}
			function ajaxenter(e) {
				var characterCode;
				if(e && e.which) {
					e = e;
					characterCode = e.which;
				} else {
					e = event;
					characterCode = e.keyCode;
				}

				if(characterCode == 13) {
					document.forms[0].submit();
					return false;
				} else {
					ajaxsearch();
				}
			}
			function ajaxsearch() {
				stilltyping = 1;
				setTimeout('ajaxsearchv()', 3000);
				stilltyping = 0;
			}
			function ajaxsearchv() {
				if (stilltyping=="0") {
					stilltyping = 1;
					ajaxsearchx();
				} else {
					return true;
				}
				setTimeout('ajaxsearchx()', 3000);
			}
			function ajaxsearchx() {
				var gv = document.getElementById('searchbox');
				var search = gv.value;
				if (search!="") {
					cbaUpdateElement("dsearchajax","/ajaxsearch.php?data="+search+"");
				}
			}
			function ajaxsearchconsole(console) {
				var av = document.getElementById('consolesearchbox');
				var searchc = av.value;
				if (searchc!="") {
					cbaUpdateElement("csearchajax","/ajaxsearchroms.php?data="+searchc+"&consoleid="+console+"");
				}
			}
/*--------------------------------------------
 *  (c) 2007 CrossBrowserAjax, version 1.10
 *  Type: Compressed
 *--------------------------------------------*/
function cbaDOM(){this.removeElement=function(_element){_element.parentNode.removeChild(_element);};this.createElement=function(_parent,_type,_property,_text){var el=document.createElement(_type);for(var k in _property){var keys=k.split('.');if(keys.length==2){el[keys[0]][keys[1]]=_property[k];}else{el[k]=_property[k];}}if(_text!='')el.innerHTML=_text;_parent.appendChild(el);return el;};this.createScript=function(_parent,_id,_request){var span=this.createElement(_parent,'span',{'style.display':'none','id':_id},'%<s'+'cript></'+'script>');setTimeout(function(){var _script=span.getElementsByTagName('script')[0];_script.type='text/javascript';_script.src=_request;},5);}}function cbaCache(buffer_size,length_ct){this._rct=function(_request,_length){var control=0;var start=Math.floor(_request.length/_length);if(start<1)start=1;for(var i=_request.length;i>0;i-=start){control+='                                  !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~???S??Z???s??zY ???ค?ฆง?ฉ?ซฌญฎ?ฐฑ???ตถท???ป????AAAAAA?CEEEEIIII?NOOOOO?OUUUUY??aaaaaa?ceeeeiiii?nooooo?ouuuuy?y'.indexOf(_request.charAt(i));}return control;};this._search=function(_request){var _hash=this._rct(_request,this._length_ct);for(i in this._cache){if(this._cache[i][0]==_hash&&this._cache[i][1]==_request){this._search_buffer=this._data_cache[i];return true;}}return false;};this._add=function(_request,_data){if(this._cache_index>=this._buffer_size){this._cache_index=0;}var _hash=this._rct(_request,this._length_ct);this._cache[this._cache_index]=[_hash,_request];this._data_cache[this._cache_index]=_data;this._cache_index++;};this._init=function(buffer_size,length_ct){this._buffer_size=buffer_size;this._length_ct=length_ct;this._cache=new Array();this._data_cache=new Array();this._cache_index=0;this._search_buffer=null;};this._init(buffer_size,length_ct);}function cbaRequest(){this._init=function(){this._handler_error=true;if(this._handler_error){window.onerror=this.cbaJsError;}this._parent_id_name='_cba_parent_id';this._url_id='_cba_request_id';this._id_prefix='_cba_pack_';this.dom=new cbaDOM();this._parent_element=this.createParentElement();this._requests=new Array();this._caching_enabled=true;if(this._caching_enabled){this._buffer_size=25;this._length_ct=100;this.cache=new cbaCache(this._buffer_size,this._length_ct);}};this.cbaJsError=function(_message,_url,_line){var _file="File: "+_url+" :: "+_line+"\n";var _title="There was an error on this page.\n--------------------------\n";var _error=_title;_error+="Error: "+_message+"\n";_error+=_file;if(_message.indexOf('unterminated string literal')+_message.indexOf('missing ;')+_message.indexOf('Syntax error')>-3){_error+='--------------------------\nRecommendation:\n   Check response from the server.\n   He may return incorrect JavaScrip.';}};this.createParentElement=function(){if(document.getElementById(this._parent_id_name)){return document.getElementById(this._parent_id_name);}else{return this.dom.createElement(document.body,'div',{'id':this._parent_id_name},'');}};this.query=function(_url,_callback){if(arguments.length>2){var _use_cache=arguments[2];}else{var _use_cache=true;};if(_use_cache&&(this.cache&&this.cache._search(_url))){this.answer=this.cache._search_buffer;if(_callback instanceof Function)_callback();}else{var _id=this._requests.push({'callback':_callback,'request':_url})-1;if(_url.indexOf('?')==-1){_url+='?';}else{_url+='&';};_url+=this._url_id+'='+_id;this.dom.createScript(this._parent_element,this._id_prefix+_id,_url);}};this.ready=function(_id,_result){this.answer=_result;if(this.cache)this.cache._add(this._requests[_id].request,_result);if(this._requests[_id].callback instanceof Function)this._requests[_id].callback();if(document.getElementById(this._id_prefix+_id)){this.dom.removeElement(document.getElementById(this._id_prefix+_id));}delete(this._requests[_id]);};this._init(arguments);}var _cba=null;function cbaUpdateElement(_doc_id,_url){var _loading_text=(arguments[2])?arguments[2]:null;var _caching=(arguments.length>3)?arguments[3]:true;var _template=(arguments[4])?arguments[4]:'%1';var _error_template=(arguments[5])?arguments[5]:null;var _user_method=(arguments[6])?arguments[6]:null;if(_loading_text)document.getElementById(_doc_id).innerHTML=_loading_text;if(!_cba)_cba=new cbaRequest();_cba.query(_url,function(){if((_error_template)&&_cba.answer==_error_template[0]){document.getElementById(_doc_id).innerHTML=_error_template[1];}else{if(typeof(_cba.answer)=='object'){var _result=_template;for(key in _cba.answer){_result=_result.replace('%'+key+'%',_cba.answer[key]);}document.getElementById(_doc_id).innerHTML=_result;}else{document.getElementById(_doc_id).innerHTML=_template.replace('%1',_cba.answer);}};if(_user_method&&_user_method instanceof Function)_user_method();},_caching);}

function Graphics(canvas)
{
	this.canvas = canvas;
	this.cache = new Array;
	this.shapes = new Object;
	this.nObject = 0;

	// defaults
	this.penColor = "black";
	this.zIndex = 0;
}

Graphics.prototype.createPlotElement = function(x,y,w,h) 
{
	// detect canvas
	if ( !this.oCanvas )
	{
		switch ( typeof(this.canvas) )
		{
			case "string":
				this.oCanvas = document.getElementById(this.canvas);
				break;
			case "object":
				this.oCanvas = this.canvas;
				break;
			default:
				this.oCanvas = document.body;
				break;
		}
	}

	// retrieve DIV
	var oDiv;
	if ( this.cache.length )
		oDiv = this.cache.pop();
	else 
	{
		oDiv = document.createElement('div');
		this.oCanvas.appendChild(oDiv);

		oDiv.style.position = "absolute";
		oDiv.style.margin = "0px";
		oDiv.style.padding = "0px";
		oDiv.style.overflow = "hidden";
		oDiv.style.border = "0px";
	}

	// set attributes
	oDiv.style.zIndex = this.zIndex;
	oDiv.style.backgroundColor = this.penColor;
	
	oDiv.style.left = x;
	oDiv.style.top = y;
	oDiv.style.width = w + "px";
	oDiv.style.height = h + "px";

	oDiv.style.visibility = "visible";
	
	return oDiv;
}

Graphics.prototype.releasePlotElement = function(oDiv)
{
	oDiv.style.visibility = "hidden";
	this.cache.push(oDiv);
}

Graphics.prototype.addShape = function(shape)
{
	shape.oGraphics = this;
	shape.graphicsID = this.nObject;
	this.shapes[this.nObject] = shape;
	this.nObject++;
	shape.draw();
	return shape;
}

Graphics.prototype.removeShape = function(shape)
{
	if ( (shape instanceof Object) && 
		(shape.oGraphics == this) && 
		(this.shapes[shape.graphicsID] == shape) )
	{
		shape.undraw();
		this.shapes[shape.graphicsID] = undefined;
		shape.oGraphics = undefined;
	}
}
Graphics.prototype.clear = function()
{
	for ( var i in this.shapes )
		this.removeShape(this.shapes[i]);
}


//=============================================================================
// Point
Graphics.prototype.drawPoint = function(x,y)
{
	return this.addShape(new Point(x,y))
}

function Point(x,y)
{
	this.x = x;
	this.y = y;
}
Point.prototype.draw = function()
{
	this.oDiv = this.oGraphics.createPlotElement(this.x,this.y,1,1);
}
Point.prototype.undraw = function()
{
	this.oGraphics.releasePlotElement(this.oDiv);
	this.oDiv = undefined;
}

//=============================================================================
// Line
Graphics.prototype.drawLine = function(x1,y1,x2,y2)
{
	return this.addShape(new Line(x1,y1,x2,y2))
}

function Line(x1,y1,x2,y2)
{
	this.x1 = x1;
	this.y1 = y1;
	this.x2 = x2;
	this.y2 = y2;
}

Line.prototype.draw = function()
{
	this.plots = new Array;

	var dx = this.x2 - this.x1;
	var dy = this.y2 - this.y1;
	var x = this.x1;
	var y = this.y1;

	var n = Math.max(Math.abs(dx),Math.abs(dy));
	dx = dx / n;
	dy = dy / n;
	for ( i = 0; i <= n; i++ )
	{
		this.plots.push(this.oGraphics.createPlotElement(Math.round(x),Math.round(y),1,1));

		x += dx;
		y += dy;
	}
}
Line.prototype.undraw = function()
{
	while ( this.plots.length )
		this.oGraphics.releasePlotElement(this.plots.pop());
	this.plots = undefined;
}

//=============================================================================
// Circle
Graphics.prototype.drawCircle = function(x,y,r)
{
	return this.addShape(new Circle(x,y,r))
}

function Circle(x,y,r)
{
	this.x = x;
	this.y = y;
	this.radius = r;
}

Circle.prototype.draw = function()
{
	this.plots = new Array;

	var r2 = this.radius * this.radius;
	var x = 0;
	var y = this.radius;

	while ( x <= y )
	{
		this.plots.push(this.oGraphics.createPlotElement(Math.round(this.x + x), Math.round(this.y + y), 1, 1));
		this.plots.push(this.oGraphics.createPlotElement(Math.round(this.x - x), Math.round(this.y + y), 1, 1));
		this.plots.push(this.oGraphics.createPlotElement(Math.round(this.x + x), Math.round(this.y - y), 1, 1));
		this.plots.push(this.oGraphics.createPlotElement(Math.round(this.x - x), Math.round(this.y - y), 1, 1));
		this.plots.push(this.oGraphics.createPlotElement(Math.round(this.x + y), Math.round(this.y + x), 1, 1));
		this.plots.push(this.oGraphics.createPlotElement(Math.round(this.x + y), Math.round(this.y - x), 1, 1));
		this.plots.push(this.oGraphics.createPlotElement(Math.round(this.x - y), Math.round(this.y + x), 1, 1));
		this.plots.push(this.oGraphics.createPlotElement(Math.round(this.x - y), Math.round(this.y - x), 1, 1));

		x++;
		y = Math.round(Math.sqrt(r2 - x*x));
	}
}
Circle.prototype.undraw = Line.prototype.undraw;

//=============================================================================
// FillRectangle
Graphics.prototype.fillRectangle = function(x,y,w,h)
{
	return this.addShape(new FillRectangle(x,y,w,h))
}

function FillRectangle(x,y,w,h)
{
	this.x = x;
	this.y = y;
	this.w = w;
	this.h = h;
}

FillRectangle.prototype.draw = function()
{
	this.oDiv = this.oGraphics.createPlotElement(this.x,this.y,this.w,this.h);
}
FillRectangle.prototype.undraw = Point.prototype.undraw;

//=============================================================================

function drawProp()
{
	if ( p ) gr.removeShape(p);
	
	var x = Math.round(Math.sin(a) * 45);
	var y = Math.round(Math.cos(a) * 45);
	a -= Math.PI / 25;
	gr.penColor = "black";
	p = gr.drawLine(100 + x, 100 + y, 100 - x, 100 - y);
	window.setTimeout("drawProp();", 10);
}

function drawShapes()
{
	gr.penColor = "red";
	gr.drawLine(10,10,190,190);

	gr.penColor = "green";
	gr.drawLine(190,10,10,190);

	gr.penColor = "blue";
	c = gr.drawCircle(100,100,45);

	gr.zIndex = 1;	
	gr.penColor = "lime";
	gr.fillRectangle(50,70,100,20);
	gr.zIndex = 0;
	
	drawProp();
}
Point.prototype.move = function(x,y)
{
	this.oDiv.style.left = x + "px";
	this.oDiv.style.top = y + "px";
}

Point.prototype.setSize = function(nSize)
{
	this.oDiv.style.width = nSize + "px";
	this.oDiv.style.height = nSize + "px";
}

function StarFieldSaver(settings)
{	
	// default values
	this.speed = 50;
	this.nStars = 50;
	this.zIndex = 1000;
		
	// override with settings values
	if ( settings.speed ) this.speed = settings.speed;
	if ( settings.nStars ) this.nStars = settings.nStars;
	if ( settings.zIndex ) this.zIndex = settings.zIndex;
}
StarFieldSaver.prototype.init = function()
{	
	this.oCanvas = document.createElement("div");
	document.body.appendChild(this.oCanvas);
	this.oCanvas.style.zIndex = this.zIndex;
	this.oCanvas.style.position = "absolute";
	this.oCanvas.style.background = "#000000";
	this.oCanvas.style.backgroundImage = "url(http://www.doperoms.com:81/cosmos.jpg)";

	this.g = new Graphics(this.oCanvas);
	this.g.penColor = "white";
	
	this.aPoints = new Array();
}
StarFieldSaver.prototype.start = function()
{
	if ( this.bActive ) return;
	this.bActive = true;
	
	if ( !this.oCanvas ) this.init();
	this.oCanvas.style.visibility = "visible";
	this.tick();
}
StarFieldSaver.prototype.stop = function()
{
	if ( !this.bActive ) return;
	
	window.clearTimeout(this.timerID);
	this.oCanvas.style.visibility = "hidden";

	for ( var i in this.aPoints )
	{
		this.aPoints[i].undraw();
	}
	this.aPoints = new Array();

	this.bActive = false;
}
StarFieldSaver.prototype.tick = function()
{
	this.oCanvas.style.left = document.body.scrollLeft + "px";
	this.oCanvas.style.top = document.body.scrollTop + "px";
	this.oCanvas.style.width = document.body.clientWidth + "px";
	this.oCanvas.style.height = document.body.clientHeight + "px";
	
	this.width = document.body.clientWidth;
	this.height = document.body.clientHeight;
	
	var cx = this.width / 2;
	var cy = this.height / 2;
	for ( var i in this.aPoints )
	{
		var p = this.aPoints[i];
		var dx = p.x - cx;
		var dy = p.y - cy;
		
		// move point outwards
		p.x += dx / 50;
		p.y += dy / 50;
		if ( (p.x < 0) || (p.x > this.width - Math.ceil(p.n/50)) || (p.y < 0) || (p.y > this.height - Math.ceil(p.n/50)) )
		{
			p.x = Math.random() * this.width;
			p.y = Math.random() * this.height;
			p.n = 0;
		}
		p.move(Math.floor(p.x),Math.floor(p.y));

		// resize as they get closer
		p.setSize(Math.ceil(p.n++ / 40)) 
	}
	
	while ( this.aPoints.length < this.nStars )
	{
		var x = Math.random() * this.width;
		var y = Math.random() * this.height;
		var p = this.g.drawPoint(Math.floor(x), Math.floor(y));
		p.setSize(3);
		p.x = x;
		p.y = y;
		p.n = 0;
		this.aPoints.push(p);
	}

	var pThis = this;
	var f = function(){pThis.tick();}
	this.timerID = window.setTimeout(f,this.speed);
}

function ScreenSaver(settings)
{
	this.settings = settings;

	this.nTimeout = this.settings.timeout;
			
	document.body.screenSaver = this;

	// link in to body events
	document.body.onmousemove = ScreenSaver.prototype.onevent;
	document.body.onmousedown = ScreenSaver.prototype.onevent;
	document.body.onkeydown = ScreenSaver.prototype.onevent;
	document.body.onkeypress = ScreenSaver.prototype.onevent;
	
	var pThis = this;
	var f = function(){pThis.timeout();}
	this.timerID = window.setTimeout(f, this.nTimeout);
}
ScreenSaver.prototype.timeout = function()
{
	if ( !this.saver )
	{
		this.saver = this.settings.create();
	}
	this.saver.start();
}
ScreenSaver.prototype.signal = function()
{
	if ( this.saver )
	{
		this.saver.stop();
	}
	
	window.clearTimeout(this.timerID);
	
	var pThis = this;
	var f = function(){pThis.timeout();}
	this.timerID = window.setTimeout(f, this.nTimeout);
}

ScreenSaver.prototype.onevent = function(e)
{
	this.screenSaver.signal();
}


var saver;
function initScreenSaver()
{
	saver = new ScreenSaver({timeout:180000,speed:15,nStars:111,zIndex:100,create:function(){return new StarFieldSaver(this);}});
}
