var currx=0;
var currspeed=1;
var interval;
var current="";
var animating=false;
var contentdiv=null;
var templat=0;
var templong=0;
var tempnote="";
request=null;
function setup()
{
	var el=document.getElementById('menu');
	el.style.overflowX='hidden';
	el=document.getElementById('advancetl');
	el.onmousedown=function(){interval=setInterval('advancetimeline()',50)};
	el.onmouseup=function(){clearInterval(interval); currspeed=1;};
	el.onmouseout=function(){clearInterval(interval); currspeed=1;};
	el.onclick=function(){return false};
	el=document.getElementById('backtl');
	el.onmousedown=function(){interval=setInterval('backtimeline()',50)};
	el.onmouseup=function(){clearInterval(interval); currspeed=1;};
	el.onmouseout=function(){clearInterval(interval); currspeed=1;};
	el.onclick=function(){return false};
}
function setup2()
{
	contentdiv=document.getElementById('content');
}
function backtimeline()
{
	var el=document.getElementById('menu');
	var tl=document.getElementById('timeline');
	if((currx+currspeed)<(tl.offsetWidth-el.offsetWidth))
	{
		currx+=currspeed;
		if(currspeed<20);
			currspeed+=2;
		tl.style.right='-'+currx+'px';
	}
	else
	{
		tl.style.right='-'+(tl.offsetWidth-el.offsetWidth)+'px';
		currspeed=1;
	}
}
function advancetimeline()
{
	var el=document.getElementById('menu');
	var tl=document.getElementById('timeline');
	if((currx-currspeed)>0)
	{
		currx-=currspeed;
		if(currspeed<20);
			currspeed+=2;
		tl.style.right='-'+currx+'px';
	}
	else
	{
		tl.style.right='0px';
		currspeed=1;
	}
}
function initializemap()
{
	var lat=templat;
	var long=templong;
	var note=tempnote;
	if(GBrowserIsCompatible())
	{
		var map = new GMap2(document.getElementById("map"));
		map.setCenter(new GLatLng(lat,long), 10);
		map.setMapType(G_HYBRID_MAP);map.setUIToDefault();
		var marker=new GMarker(new GLatLng(lat,long));
		GEvent.addListener(marker, "click", function() {
    						marker.openInfoWindowHtml(note);
							});
		map.addOverlay(marker);
	}
}
function newAjaxObject()
{
	request=null;
	try 
	{
	   request=new XMLHttpRequest();
	}
	catch(trymicrosoft) 
	{
	   try 
	   {
		  request=new ActiveXObject("Msxml2.XMLHTTP");
	   }
	   catch (othermicrosoft) 
	   {
		  try
		  {
			 request=new ActiveXObject("Microsoft.XMLHTTP");
		  }
		  catch (failed) 
		  {
			 request=false;
		  }
	   }
	}
}
function ajaxGetDay(day)
{
	if(contentdiv==null)
	{
		contentdiv=document.createElement('div');
		contentdiv.id="content";
		var el=document.getElementById('container');
		el.appendChild(contentdiv);
	}
	else
	{
		contentdiv.style.height='10px';
	}
	newAjaxObject();
	var url="getday.php";
	var args="day="+day;
	request.open("POST",url,false);
	request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	request.send(args);
		testdiv=document.createElement('div');
		testdiv.id="test";
		var el=document.getElementById('container');
		el.appendChild(testdiv);
	testdiv.style.marginLeft=-9000+'px';
	testdiv.innerHTML=request.responseText;
	h=testdiv.offsetHeight;
	el.removeChild(testdiv);
	list=request.responseText.split('*');
	contentdiv.innerHTML=list[4];
	templat=list[1];
	templong=list[2];
	tempnote=list[3];
	if(list[0]=="map")
		slideVert('content',15,h,4,2,true);
	else
		slideVert('content',15,h,4,2,false);
	return false;
}
function ajaxGetVideos()
{
	if(contentdiv==null)
	{
		contentdiv=document.createElement('div');
		contentdiv.id="content";
		var el=document.getElementById('container');
		el.appendChild(contentdiv);
	}
	else
	{
		contentdiv.style.height='10px';
	}
	newAjaxObject();
	var url="getvideo.php";
	request.open("POST",url,false);
	request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	request.send(null);
		testdiv=document.createElement('div');
		testdiv.id="test";
		var el=document.getElementById('container');
		el.appendChild(testdiv);
	testdiv.style.marginLeft=-9000+'px';
	testdiv.innerHTML=request.responseText;
	h=testdiv.offsetHeight;
	el.removeChild(testdiv);
	contentdiv.innerHTML=request.responseText;
		slideVert('content',15,h,4,2,false);
	return false;
}
function ajaxGetPhotos()
{
	if(contentdiv==null)
	{
		contentdiv=document.createElement('div');
		contentdiv.id="content";
		var el=document.getElementById('container');
		el.appendChild(contentdiv);
	}
	else
	{
		contentdiv.style.height='10px';
	}
	newAjaxObject();
	var url="getphoto.php";
	request.open("POST",url,false);
	request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	request.send(null);
		testdiv=document.createElement('div');
		testdiv.id="test";
		var el=document.getElementById('container');
		el.appendChild(testdiv);
	testdiv.style.marginLeft=-9000+'px';
	testdiv.innerHTML=request.responseText;
	h=testdiv.offsetHeight;
	el.removeChild(testdiv);
	contentdiv.innerHTML=request.responseText;
		slideVert('content',15,h,4,2,false);
	return false;
}
// Modal windows
function modalWindow(innerhtml, width, height)
{	
	//create a semi-transparent overlay
	var h=document.getElementsByTagName('html');
	var b=document.getElementsByTagName('body');
	b=b[0];
	var h=h[0].offsetHeight;
	var overlay=document.createElement('div');
	overlay.id='overlay';
	overlay.style.backgroundColor='#000';
	overlay.style.opacity='0';
	overlay.style.filter='alpha(opacity=0)';
	overlay.style.height=h+'px';
	overlay.style.width='100%';
	overlay.style.top='0px';
	overlay.style.left='0px';
	overlay.style.zIndex='1000';
	b.appendChild(overlay);
	setTimeout("fadeIn('overlay',0,50,5)",20);
	//create the modal window
	var win=document.createElement('div');
	win.id='modalwindow';
	win.style.backgroundColor='#fff';
	win.style.opacity='0';
	win.style.filter='alpha(opacity=0)';
	/*win.style.width=width+'px';
	win.style.height=height+'px';*/
	win.style.top='80px';
	win.style.left='50%';
	win.style.padding='10px';
	win.style.marginLeft='-'+(width/2)+'px';
	win.style.zIndex='1001';
	win.innerHTML=innerhtml;
	b.appendChild(win);
	setTimeout("fadeIn('modalwindow',0,100,10)",20);
}
function closemodalwindow()
{
	var obj=document.getElementById('modalwindow');
	var parent=obj.parentNode;
	parent.removeChild(obj);
	obj=document.getElementById('overlay');
	parent=obj.parentNode;
	parent.removeChild(obj);
	return false;
}
function commentbox()
{
	modalWindow("<form action=\"comments.php\" method=\"post\"><h3>Please enter your comment:</h3><textarea name=\"comment\"></textarea><h4>Name:</h4><input type=\"text\" name=\"author\" /></form>", 300, 200);
}
function fadeIn(id,op,end,inc)
{
	ref=document.getElementById(id);
	op=op+inc;
	if(op>=end)
	{
		op=end;
		ref.style.opacity=op/100;
		ref.style.filter='alpha(opacity='+op+')';
	}
	else
	{
		ref.style.opacity=op/100;
		ref.style.filter='alpha(opacity='+op+')';
		setTimeout("fadeIn('"+id+"',"+op+","+end+","+inc+")",20);
	}
}
function showpicture(src,title,width,height)
{
	modalWindow('<h2 class="modaltitle">'+title+'</h2><img src="'+src+'" width="'+width+'" height="'+height+'" alt="'+title+'" title="Click to close" onclick="closemodalwindow()" />', width, height);
}
function showvideo(src,title,width,height)
{
	modalWindow('<h2 class="modaltitle">'+title+'</h2><object width="'+width+'" height="'+height+'"><param name="movie" value="'+src+'"></param><param name="allowFullScreen" value="true"></param><embed src="'+src+'" type="application/x-shockwave-flash" width="'+width+'" height="'+height+'" allowfullscreen="true"></embed></object><br /><a href="#" onclick="return closemodalwindow();">Close this video</a>', width, height);
}
function show(section,url)
{
	var content=document.getElementById('animbox');
	if(content.style.display!='block')
	{
		//ajax stuff to fetch blog
		newAjaxObject();
		request.open("POST",url,false);
		request.send(null);
		current=section;
		showdelay(request.responseText);
	}
	else
	{
		hidedelay();
		if(current!=section)
		{
			//ajax stuff to fetch blog
			newAjaxObject();
			request.open("POST",url,false);
			request.send(null);
			current=section;
			showdelay();
		}
	}
	return false;
}
function showdelay(HTML)
{
	var content=document.getElementById('animbox');
	if(!animating)
	{
		content.innerHTML=request.responseText;
		showContent();
		animating=true;
	}
	else
		setTimeout("showdelay()",50);
}
function hidedelay()
{
	if(!animating)
	{
		hideContent();
		animating=true;
	}
	else
		setTimeout("hidedelay()",50);
}
function showContent()
{
	var content=document.getElementById('animbox').style;
	content.display='block';
	content.width='1px';
	content.height='5px';
	slideHorizandVert('animbox',2,700,4,2,400,false);
}
function hideContent()
{
	var content=document.getElementById('animbox').style;
	slideHorizandVert('animbox',700,1,-4,2,400,true);
}
function slideHorizandVert(id,start,end,speed,accel,height,hide)
{
	var el=document.getElementById(id);
	if((speed>1 && start>end)||(speed<1 && start<end))
	{
		el.style.width=end+'px';
		if(hide)
			slideVert(id,height,6,-4,2,hide);
		else
			slideVert(id,6,height,4,2,hide);
	}
	else
	{
		el.style.width=start+'px';
		start+=speed;
		speed*=accel;
		setTimeout("slideHorizandVert('"+id+"',"+start+","+end+","+speed+","+accel+","+height+","+hide+")",50);
	}
}
function slideVert(id,start,end,speed,accel,map)
{
	var el=document.getElementById(id);
	if((speed>1 && start>=end)||(speed<1 && start<=end))
	{
		el.style.height='';
		if(map)
			setTimeout("initializemap()",50);
		animating=false;
	}
	else
	{
		el.style.height=start+'px';
		start+=speed;
		speed*=accel;
		if(speed>50)
			speed=50;
		setTimeout("slideVert('"+id+"',"+start+","+end+","+speed+","+accel+","+map+")",50);
	}
}
function showcomments()
{
	var el=document.getElementById('commentbox');
	var el2=document.getElementById('show');
	if(el.style.height!='')
	{
		el.style.height='';
		el2.innerHTML='Hide';
	}
	else
	{
		el.style.height='3.17em';
		el2.innerHTML='Show';
	}
	return false;
}
function addcomment(day)
{
	modalWindow("<div syle=\"width: 260px;\"><h4>Leave a comment:</h4><textarea name=\"comment\" id=\"commententry\"></textarea><h4>Your name:</h4><input type=\"text\" name=\"author\" id=\"author\" /><br /><br /><input type=\"button\" name=\"close\" id=\"close\" value=\"Cancel\" style=\"margin-right: 100px;\" onclick=\"closemodalwindow();\" /><input type=\"button\" name=\"submit\" id=\"submit\" value=\"Submit &raquo;\" onclick=\"submitcomment("+day+");\" /></div>",260,300);
}
function submitcomment(day)
{
	var el=document.getElementById('commententry');
	var el2=document.getElementById('author');
	if(el.value=='' || el2.value=='')
	{
		alert('Please fill both boxes before submitting');
		return false;
	}
	var s=escape(el.value);
	s=s.replace(/\+/g,"+");
	s=s.replace(/@/g,"@");
	s=s.replace(/\//g,"/");
	s=s.replace(/\*/g,"*");
	c=s;
	s=escape(el2.value);
	s=s.replace(/\+/g,"+");
	s=s.replace(/@/g,"@");
	s=s.replace(/\//g,"/");
	s=s.replace(/\*/g,"*");
	a=s;
	newAjaxObject();
	var url="submit.php";
	var args="c="+c+"&a="+a+"&d="+day;
	request.open("POST",url,false);
	request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	request.send(args);
	closemodalwindow();
	ajaxGetDay(day);
	return false;
}
