window.onload = function() {
	//window.resizeTo(1024,768);
	now=new Date();
  	
  	width=window.innerWidth;
	height=window.innerHeight;
	form= new Dynamic_Object({"id":"form_01","width":""+width+"","height":""+height+"","top":"0","left":"0","color":"rgb(0,0,0)","backgroundColor":"black","opacity":".9","zIndex":"20","visibility":"hidden"});
	form.initialize();
	
	init();
}

var nowDot;
var event= Array();
var c;
var now;

var centerX;
var centerY;
var width;
var height;
var radius=100;

function init() {
  	c = document.getElementById('canvas').getContext('2d');
	c.clearRect(0,0,width,height);

  	makeHttpRequest("./php/get93_main.php",plotEvents);
	


	//alert(document.width);
	document.getElementById('canvas').width=width;
	document.getElementById('canvas').height=height;
	centerX=width/2;
	centerY=height/2;
	

	
	makeHttpRequest("./php/entryForm.php",function(response){form.insertHTML(response)});

}

function plotEvents(response){


	var preAuthor;
	var authorRing=0;
	var result = eval('(' + response + ')');	
	for (var i=0; i<result.length;i++)
	{
		
		var author=result[i].author;
		var birth=mysqlDateToDate(result[i].birth);
		var date=mysqlDateToDate(result[i].date);
		var note=result[i].note;
		var significance=result[i].significance;
		var millisPerDay=1000*60*60*24;
		var daysPerYear=365.242199;
		var lifespan=result[i].lifespan*daysPerYear;
		var diff=(date-birth)/millisPerDay;
		var mapAlongArc=(diff/lifespan)*Math.PI*2;
		var x=centerX+Math.cos(mapAlongArc-Math.PI*.5)*radius;
		var y=centerY+Math.sin(mapAlongArc-Math.PI*.5)*radius;
	
		
		//c.clearRect(0,0,width,height);
	
		var textX;
		var textY;
		
		if(author!=preAuthor)authorRing+=20;
		preAuthor=author;
		if(mapAlongArc<Math.PI)textX=x+10+authorRing;
		else textX=x-130-authorRing;
		
		if(mapAlongArc<Math.PI*.5 || mapAlongArc>Math.PI*1.5)textY=y-35;
		else textY=y+35;		
		
		event[i]=new Dynamic_Object({"id":"event_"+i+"","className":"draggable","fontSize":"7","width":"200","top":textY,"left":textX,"color":"white","html":""+author+":<br/>"+note+"","zIndex":"1"});
		event[i].initialize();
		event[i].json={"markerX":x,"markerY":y,"significance":significance}
		addEvent(document.getElementById('event_'+i), 'mousemove', function(){ zoom(this.id); });
		addEvent(document.getElementById('event_'+i), 'mouseout', function(){ zoom(); });

		//addEvent(document.getElementById('event_'+i), 'mousedown', function(){ drag(this.id); });
		//drawLeader(x,y,event[i],'rgba(255, 155, 20, .75)');

	}
	
	drawLeaders();
        
	//plot age ticks
	/*
	for (var i=0;i<lifespan;i+=daysPerYear)
	{
		c.clearRect(0,0,width,height);
		var x=centerX+Math.cos((i/lifespan)*Math.PI*2-Math.PI*.5)*radius;
		var y=centerY+Math.sin((i/lifespan)*Math.PI*2-Math.PI*.5)*radius;
		c.moveTo(x,y);
	    c.strokeStyle = "rgba(255, 0, 20, 0.5)";
	    c.arc (x, y, 2, 0, Math.PI*2, true);
	    c.stroke();
	}
	*/
}

 
function drawLeaders(highlightEventObj)
{
	c.clearRect(0,0,width,height);

	for (var i=0; i<event.length;i++)
	{
		//console.log(event[i].json.markerX);
		leader(parseInt(event[i].json.markerX),parseInt(event[i].json.markerY),event[i],'rgba(95, 65, 20, '+event[i].json.significance/100.0+')',event[i].json.significance*.1);
	}
	
	if(highlightEventObj)leader(parseInt(highlightEventObj.json.markerX),parseInt(highlightEventObj.json.markerY),highlightEventObj,'rgba(255, 155, 20, '+highlightEventObj.json.significance/100.0+')',highlightEventObj.json.significance*.1);


		//birth
		var r=15;
		x=centerX+Math.cos(0-Math.PI*.5)*radius;
		y=centerY+Math.sin(0-Math.PI*.5)*radius+r;
        
        c.moveTo(x+r,y);
        c.strokeStyle = "rgba(255, 155, 20, 0.75)";
	   	c.lineWidth=1;
		c.arc (x, y, r, 0, Math.PI*2, true);
        c.stroke();
        
        //overall arc
        c.moveTo(centerX+radius,centerY);
        c.strokeStyle = "rgba(255, 155, 20, 0.5)";
    	c.lineWidth=3;
		c.arc (centerX, centerY, radius, 0, Math.PI*2, true);
        c.stroke();

		//break
		/*
		              //c.strokeStyle = "rgb(255, 255, 255)";

        c.moveTo(centerX+radius,centerY);
    	c.lineWidth=3;
		c.arc (centerX, centerY, radius, Math.PI, Math.PI*2, true);
        c.stroke();
 */
 
		//center
		r=5;
        c.strokeStyle = "rgba(255, 155, 20, 0.5)";
    	c.lineWidth=2;
        c.moveTo(centerX-r,centerY);	
		c.lineTo (centerX+r, centerY);
        c.moveTo(centerX,centerY-r);
		c.lineTo (centerX, centerY+r);
        c.stroke();
        
}

function leader(x,y,_obj,color,radius){

		c.lineCap='round';
		c.lineWidth=1;
		c.moveTo(x+radius,y);
		c.strokeStyle = color;
		c.arc (x, y, radius, 0, Math.PI*2, true);
		c.stroke();
		//c.fill();
		
		c.beginPath();
		c.moveTo(x,y);
		//c.lineWidth=1;
		//c.lineTo(x+10,y-10);
		//c.lineTo(x+25,y-10);
		var el=document.getElementById(_obj.id);
		var xpos=el.offsetLeft;
		var ypos=parseInt(el.offsetTop)-3;
		if(xpos<x)
		{		
			c.lineTo(x-Math.abs(y-ypos)*.5,ypos);
		}
		else
		{
			c.lineTo(x+Math.abs(y-ypos)*.5,ypos);
		}
		c.lineTo(xpos,ypos);
		//c.lineTo(xpos,ypos);

		c.arc (xpos,ypos, 1, 0, Math.PI*2, false);
		c.stroke();
	

}



/////////////////drag
if  (document.getElementById){ (function(){
var n = 500;
var dragok = false;
var y,x,d,dy,dx;

function move(e){
if (!e) e = window.event;
 if (dragok){
  d.style.left = dx + e.clientX - x + "px";
  d.style.top  = dy + e.clientY - y + "px";
  return false;
 }
}

function down(e){
if (!e) e = window.event;
var temp = (typeof e.target != "undefined")?e.target:e.srcElement;
if (temp.tagName != "HTML"|"BODY" && temp.className != "draggable"){
 temp = (typeof temp.parentNode != "undefined")?temp.parentNode:temp.parentElement;
 }
if (temp.className == "draggable"){
 if (window.opera){
  document.getElementById("Q").focus();
 }
 dragok = true;
 temp.style.zIndex = n++;
 d = temp;
 dx = parseInt(temp.style.left+0);
 dy = parseInt(temp.style.top+0);
 x = e.clientX;
 y = e.clientY;
 
 
 //console.log(x);
 document.onmousemove = move;
 return false;
 }
}

function up(){
	dragok = false;
	document.onmousemove = null;


	
/*
	var highlightObj;
	console.log(ev);

	for(var i=0;i<event.length;i++)
	{
		if(event[i].id==ev.id)highlightObj=event[i];
	}
	drawLeaders(highlightObj);
*/
}

document.onmousedown = down;
document.onmouseup = up;

})();}
/////////////////////End drag




function zoom(_id)
{
	//c.clearRect(0,0,width,height);

	var highlightObj;
	//console.log("zoom "+_id);
	for(var i=0;i<event.length;i++)
	{
		document.getElementById(event[i].id).style.color='rgb(150,150,150)';
		document.getElementById(event[i].id).style.zIndex=0;
		document.getElementById(event[i].id).style.fontSize='7px';
		document.getElementById(event[i].id).style.width='200px';
		
		if(_id && event[i].id==_id)highlightObj=event[i];
	}	
	if(_id)
{
		//drawLeader(this.offsetTop,this.offsetLeft,'rgba(255, 155, 20, 1)');
		document.getElementById(_id).style.color='rgb(250,250,250)';
		document.getElementById(_id).style.zIndex=10;
		document.getElementById(_id).style.fontSize='12px';
		document.getElementById(_id).style.width='350px';

		//leader(parseInt(event[i].json.markerX),parseInt(event[i].json.markerY),event[i],'rgba(255, 155, 20, '+event[i].json.significance/100.0+')');
		drawLeaders(highlightObj);
		}
		
}




function submitInputs(form){
	//if(form.date.value)	
	makeHttpRequest("./php/enter93_main.php?author="+form.author.value+"&birth="+form.birth.value+"&lifespan="+form.lifespan.value+"&note="+form.note.value+"&significance="+form.significance.value+"&date="+form.date.value+"",function()
	{
	form.note.value="note entered into database";

	enterdata();	

	//form.date.value="";
	//init();
	});
	//alert(form.author.value);
}


function checkAuthor(author, form)
{
	makeHttpRequest("./php/getAuthor.php?author="+author+"",function(response)
	{
	if(response){ 
		var result = eval('(' + response + ')');	
		if(result)
		{
			form.lifespan.value=result[0].lifespan;
			form.birth.value=result[0].birth;
		}
	}
	});		
}


function drawNowDot(form)
{
		var author=form.author.value;
		var birth=mysqlDateToDate(form.birth.value);
		var date=mysqlDateToDate(form.date.value);
		//console.log(birth);

		var millisPerDay=1000*60*60*24;
		var daysPerYear=365.242199;
		
		var lifespan=form.lifespan.value*daysPerYear;	
		var diff=(date-birth)/millisPerDay;
		
		var mapAlongArc=(diff/lifespan)*Math.PI*2;		
		
		//console.log(mapAlongArc);
		var x=centerX+Math.cos(mapAlongArc-Math.PI*.5)*radius;
		var y=centerY+Math.sin(mapAlongArc-Math.PI*.5)*radius;
		
		c.strokeStyle = 'rgb(255,0,0)';		
		c.lineWidth=5;
		c.moveTo(x+5,y);
		c.arc (x, y, 5, 0, Math.PI*2, true);
		c.stroke();


}




function $style(ElementId, CssProperty)
{
	
	function $(stringId)
	{
	    return document.getElementById(stringId);
	}   
	
	if($(ElementId).currentStyle)
	{

	    var convertToCamelCase = CssProperty.replace(/\-(.)/g, function(m, l){return l.toUpperCase()});
	    return $(ElementId).currentStyle[convertToCamelCase];
	}
	else if (window.getComputedStyle)
	{
	    var elementStyle = window.getComputedStyle($(ElementId), "");
	    return elementStyle.getPropertyValue(CssProperty);
	}

}






var dateFuture = new Date(2009,0,20,00,00,00);

function GetCount(){
	dateNow = new Date();									//grab current date
	amount = dateFuture.getTime() - dateNow.getTime();		//calc milliseconds between dates
	delete dateNow;
	// time is already past
	if(amount < 0){
		document.getElementById('time').innerHTML="";
	}
	// date is still good
	else{
		days=0;hours=0;mins=0;secs=0;
		
		out="";
		
		amount = Math.floor(amount/1000);//kill the "milliseconds" so just secs
		days=Math.floor(amount/86400);//days
		amount=amount%86400;
		hours=Math.floor(amount/3600);//hours
		amount=amount%3600;
		mins=Math.floor(amount/60);//minutes
		amount=amount%60;
		secs=Math.floor(amount);//seconds

		//if(days != 0){out += days +" day"+((days!=1)?"s":"")+" and ";}
		//if(days != 0 || hours != 0){out += hours +" hour"+((hours!=1)?"s":"")+" ";}
		//if(days != 0 || hours != 0 || mins != 0){out += mins +" minute"+((mins!=1)?"s":"")+", ";}
		
		//if(days != 0){out += ((days<10)?"0":"")+days+":";}
		if(days != 0 || hours != 0){out += ((hours<10)?"0":"")+hours+":";}
		if(days != 0 || hours != 0 || mins != 0){out += ((mins<10)?"0":"")+mins+":";}
		if(days != 0 || hours != 0 || mins != 0 || secs != 0){out += ((secs<10)?"0":"")+secs;}

		document.getElementById('Menu_11').innerHTML=out;
		setTimeout(GetCount, 1000);
	}
}



/////////////////////////////////////////////////////////////////////////

function interfaceToggle(_dynamicObjectArray, _switchObject, _intervalString, _intervalTime)
{
	var intervalTime = 10000;
	if (_intervalTime) intervalTime = _intervalTime;
	
	
	for(i = 0; i<_dynamicObjectArray.length; i++)
	{
		_dynamicObjectArray[i].switchVisibility();
	}
	if(_dynamicObjectArray[0].visibility == 'hidden')
	{
	//console.log(_switchObject.id);
		document.getElementById(_switchObject.id).style.color = _switchObject.color;
		if(_intervalString)
		{
		clearInterval(_switchObject.myIntervals[0]);
		}
	}
	else 
	{
		document.getElementById(_switchObject.id).style.color = 'rgb(190,190,190)';
		if(_intervalString) _switchObject.myIntervals[0] = setInterval(_intervalString, intervalTime);
	}	
}

function interfaceToggle2(_dynamicObjectArray, _switchObject,_intervalString, _intervalString1)
{

	for(i = 0; i<_dynamicObjectArray.length; i++)
	{
		_dynamicObjectArray[i].switchVisibility();
	}
	if(_dynamicObjectArray[0].visibility == 'hidden')
	{
		document.getElementById(_switchObject.id).style.color = _switchObject.color;
		
		if(_intervalString)
		{
		clearInterval(_switchObject.myIntervals[0]);
		clearInterval(_switchObject.myIntervals[1]);
		}
	}
	else 
	{
		document.getElementById(_switchObject.id).style.color = 'rgb(190,190,190)';
		if(_intervalString) _switchObject.myIntervals[0] = setInterval(_intervalString,60000);
		if(_intervalString1) _switchObject.myIntervals[1] = setInterval(_intervalString1,33);
		//interfaceArray[switchId].myIntervals[0];
	}
}



window.onkeyup=function(e)
{
	//alert(e.keyCode);
	if(e.keyCode==192) // ~ (tilda)
	{

	}
	
	if(e.keyCode==27)
	{
		enterdata();
	}
}


function enterdata()
{

	if(form.visibility=='hidden')
	{
		fadeIn(form,.9);
	}
	else
	{
		fadeOut(form,.9);
	}

}


function addEvent( _el, _trigger, _fn ) {
	if (_el.addEventListener)
		_el.addEventListener( _trigger, _fn, false );
	else if (_el.attachEvent) {
		_el["e"+_trigger+_fn] = _fn;
		_el[_trigger+_fn] = function() { _el["e"+_trigger+_fn]( window.event ); }
		_el.attachEvent( "on"+_trigger, _el[_trigger+_fn] );
	}
}

function removeEvent( _el, _trigger, _fn ) {
	if (_el.removeEventListener)
		_el.removeEventListener( _trigger, _fn, false );
	else if (_el.detachEvent) {
		_el.detachEvent( "on"+_trigger, _el[_trigger+_fn] );
		_el[_trigger+_fn] = null;
		_el["e"+_trigger+_fn] = null;
	}
}

 function mysqlDateToDate(date) {
    //function parses mysql datetime string and returns javascript Date object
    //input has to be in this format: 2007-06-05 15:26:02
    var regex=/^([0-9]{2,4})-([0-1][0-9])-([0-3][0-9]) ?$/;
    var parts=date.replace(regex,"$1 $2 $3 $4 $5 $6").split(' ');
    return new Date(parts[0],parts[1]-1,parts[2]);  }


function nada(){}