//signal to noise js

var IE = document.all?true:false;
var reader = '';

function screenSize(){
	if (screen.width < 639)
	{document.body.style.fontSize='10pt';
	document.getElementById('reader').style.paddingTop='30px';
	document.getElementById('help').style.height='480px';
	document.getElementById('info').style.height='480px';}

	if (screen.width == 640)
	{document.body.style.fontSize='11pt';
	document.getElementById('reader').style.paddingTop='45px';
	document.getElementById('help').style.height='420px';
	document.getElementById('info').style.height='420px';}

	if (screen.width == 800)
	{document.body.style.fontSize='12pt';
	document.getElementById('reader').style.paddingTop='60px';
	document.getElementById('help').style.height='380px';
	document.getElementById('info').style.height='380px';}


	if (screen.width >= 1024)
	{document.body.style.fontSize='14pt';
	document.getElementById('reader').style.paddingTop='75px';
	document.getElementById('help').style.height='340px';
	document.getElementById('info').style.height='340px';}

}


function getReader(){
	reader = document.getElementById('reader').innerHTML;
	screenSize();
}


new PeriodicalExecuter(pollServer, 2);

function pollServer() {
	
	var pollAjax = new Ajax.Request(
		('update.php?r=' + reader + '&rand=' + parseInt(Math.random()*99999999) ),
		{
			onSuccess: function(transport) {
				if(transport.responseText){
//					document.getElementById('vision').innerHTML=dump(transport.responseText);
					populate(transport.responseText);
				}
			},
			onFailure: function(transport) {

			}
		});
}

function act(target) {
	
	var actAjax = new Ajax.Request(
		('conduct.php?r=' + reader + "&act=" + target + '&rand=' + parseInt(Math.random()*99999999) ),
		{
			onSuccess: function(transport) {
				xmlink();
				if(transport.responseText){
					populate(transport.responseText);
				}
			},
			onFailure: function(transport) {

			}
		});
}


function populate(mtxt) {
	if(mtxt) {
		mtxtFilt = pQ(mtxt, 2);
		if(mtxtFilt) {
			var mtxtA=mtxtFilt.split(" *** ");
			document.getElementById('vision').innerHTML=mtxtA[0];
			document.getElementById('mentality').innerHTML=mtxtA[1];
			document.getElementById('events').innerHTML=mtxtA[2];
			if(mtxtA[3]=='1'){
				document.getElementById('vision').innerHTML="<p id='titles' style='color: #333; text-align: center;'><span style='color: #555;'>Signal to Noise</span><br/><br/>a networked short story<br/>by Ian Hatcher</p>";
			} else if(mtxtA[3]=='2') {
				document.getElementById('vision').innerHTML="<p id='titles' style='color: #333; text-align: center;'><br/><br/><span style='color: #444; cursor:move;' onClick='window.location.reload();'></span></p>";
			}	
		}
	}
}

function pQ(pStr,type) {
	if(type==1){ qChar = "~"; }
	if(type==2){ qChar = "`"; }
	var intIndexOfMatch = pStr.indexOf(qChar);
	while (intIndexOfMatch != -1){
		pStr = pStr.replace(qChar,"'")
		intIndexOfMatch = pStr.indexOf(qChar);
	}
	return pStr;
}

function xmlink()	{
if( document.getElementById('mlink'))	{
  var d = document.getElementById('mlink');
	document.body.removeChild(d);
	}
}

//if (!IE) document.captureEvents(Event.MOUSEDOWN)
//document.onmousedown = hideIt;

function getMouseXY(e) {
	if( document.getElementById('mlink'))	{
		var d = document.getElementById('mlink');
		document.body.removeChild(d);
	}
	if (IE) {
		tempX = event.clientX + document.body.scrollLeft
		tempY = event.clientY + document.body.scrollTop
	} else {
    	tempX = e.pageX
    	tempY = e.pageY
	}  
	if (tempX < 0){tempX = 0}
	if (tempY < 0){tempY = 0}  
	
	var mlink = document.createElement('div');
	mlink.setAttribute('id','mlink');
	mlink.style.padding='10px 10px 10px 14px';
	mlink.style.position='absolute';
	mlink.style.border='solid 1px #171717';
	if(tempX > 700) {
		mlink.style.left = (tempX-260) + 'px';		
	} else { 
		mlink.style.left = (tempX+10) + 'px';
	}
	mlink.style.top=(tempY-10) + 'px';
	mlink.style.background='#0a0a0a';
	mlink.style.zIndex='4000';
//	mlink.style.color='#bbb';
	mlink.style.textAlign='left';
	mlink.style.width='240px';
	
	mlink.innerHTML = '<div style="float:right;border:solid 0px #333;color:#555;cursor:pointer" onclick="xmlink()">&nbsp;x&nbsp;</div>'
	
	document.body.appendChild(mlink);

	return true;
}



