//VARIABLES

var baseURL 	= "functions.php?";
var cur_image	= Math.floor(Math.random()*4);
var cur_show	= -1;
var start		= 0;
var inc 		= 15;
var other_page	= 0;

var delay = 30000;

var arr = new Array();
arr[0]='images/me_bg.jpg';
arr[1]='images/me_bg2.jpg';
arr[2]='images/me_bg4.jpg';
arr[3]='images/me_bg3.jpg';
arr[4]='images/me_bg5.jpg';

var theInterval;

var topics = new Array();
topics[12] = "articles";
topics[14] = "food";
topics[3] = "links";
topics[4] = "locations";
topics[1] = "misc";
topics[7] = "music";
topics[6] = "photos";
topics[2] = "reflections";
topics[15] = "technology";
topics[8] = "movies";
topics[13] = "weather";

//FUNCTIONS

function init() {
	showWaiting();
	preLd();
	//var w = xClientWidth();
	//var h = xClientHeight();
	//if(w<1100) resizeTo(1120,h);
	create('div','about','body');
	showAjax('functions.php?about','about');
	if(location.hash) {
		hash = location.hash.substring(1,location.hash.length);
		if(hash.length<1) showTopic(-1);
		else {
			for(var i=0;i<topics.length;i++) {
				if(topics[i]==hash) {
					showTopic(i);
					break;
				}
			}
		}
	}
	else showTopic(-1);
	//showAjax('RSSReader.php?retrieve','updates');
	refreshList();
	//theInterval = window.setInterval('changeBG()',delay);
	//setTimeout('showCur()',delay);
}

function xClientHeight() {
  var v=0,d=document,w=window;
  if((!d.compatMode || d.compatMode == 'CSS1Compat') && !w.opera && d.documentElement && d.documentElement.clientHeight)
    {v=d.documentElement.clientHeight;}
  else if(d.body && d.body.clientHeight)
    {v=d.body.clientHeight;}
  else if(xDef(w.innerWidth,w.innerHeight,d.width)) {
    v=w.innerHeight;
    if(d.width>w.innerWidth) v-=16;
  }
  return v;
}

function xClientWidth() {
  var v=0,d=document,w=window;
  if((!d.compatMode || d.compatMode == 'CSS1Compat') && !w.opera && d.documentElement && d.documentElement.clientWidth)
    {v=d.documentElement.clientWidth;}
  else if(d.body && d.body.clientWidth)
    {v=d.body.clientWidth;}
  else if(xDef(w.innerWidth,w.innerHeight,d.height)) {
    v=w.innerWidth;
    if(d.height>w.innerHeight) v-=16;
  }
  return v;
}

function xDef() {
	for(var i=0; i<arguments.length; ++i) {
		if(typeof(arguments[i])=='undefined') return false;
	}
	return true;
}

function preLd() {
	var a = new Image();
	a.src = "images/me_bg.jpg";
	var b = new Image();
	b.src = "images/me_bg2.jpg";
	var c = new Image();
	c.src = "images/me_bg3.jpg";
	var d = new Image();
	d.src = "images/me_bg4.jpg";
	var e = new Image();
	e.src = "images/ajax-loader.gif";
}

function loadComments(id,link) {
	if(!document.getElementById('comments_'+id)) {
		var elm = document.getElementById('post_'+id);
		var posts = document.getElementById('posts');
		var e = document.createElement('div');
		e.setAttribute("id","comments_"+id);
		e.setAttribute("class","comments");
		elm.appendChild(e);
		AjaxIt('functions.php?comment='+id,'comments_'+id);
		link.className="active";
	}
	else {
		var elm = document.getElementById('comments_'+id);
		elm.style.display = (elm.style.display=="none")?"":"none";
		if(elm.style.display=="") link.className="active";
		else link.className="";
	}
}

function addComment(id) {
	var form = document.forms['commentForm_'+id];
	var name = form.name.value;
	var website = form.website.value;
	var comment = form.comment.value;
	if(name.split(' ').join('')=="") {
		alert("Name can't be blank!");
		return;
	}
	if(comment.split(' ').join('')=="") {
		alert("There's no comment to submit!");
		return;
	}
	AjaxIt('functions.php?addComment='+id+'&name='+name+'&website='+website+'&comment='+comment);
	form.reset();
	var com = document.createElement('div');
	com.setAttribute("class","comment");
	var p = document.createElement('p');
	p.innerHTML=comment;
	var span = document.createElement('span');
	if(website) {
		website=website.replace("http://","");
		var a = document.createElement('a');
		a.setAttribute("href","http://"+website);
		a.innerHTML=name;
		span.appendChild(a);
	}
	else span.innerHTML=name;
	com.appendChild(span);
	com.appendChild(p);
	document.getElementById('comment_list_'+id).appendChild(com);
}

function create(type,id,parent) {
	var e = document.createElement(type);
	e.id=id;
	document.getElementById(parent).appendChild(e);
	return e;
}

function changeBG() {
	document.getElementById('body').style.backgroundImage="url('"+arr[cur_image]+"')";
	cur_image++;
	if(cur_image>3) cur_image=0;
}

function AjaxIt(url,id,postfunc,append) {
	var myAjax = new AjaxMe();
	myAjax.open("GET",url,true);
	myAjax.onreadystatechange=function() {
		if(myAjax.readyState==4 || myAjax.readyState=="complete") {
			if(id) {
				if(append==1) document.getElementById(id).innerHTML+=myAjax.responseText;
				else document.getElementById(id).innerHTML=myAjax.responseText;
			}
			if(postfunc) eval(postfunc+"(myAjax)");
		}
	}
	myAjax.send(null);
}

function AjaxMe() {
	var xmlHttp=null;
	try {
		xmlHttp=new XMLHttpRequest();
	}
	catch (e) {
	 try {
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e) {
			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	return xmlHttp;
}

function showWaiting() {
	document.getElementById('posts').innerHTML='<center><img src="images/ajax-loader.gif" /><br/><br/><br/>loading. please wait.</center>';
}

function showMore(type) {
	showWaiting();
	start = start+inc;
	var url=baseURL+"show="+type+"&start="+start+"&next="+(start+inc);
	showAjax(url,'posts','setupImgs');
}

function showLess(type) {
	showWaiting();
	start = start-inc;
	var url=baseURL+"show="+type+"&start="+start+"&next="+(start+inc);
	showAjax(url,'posts','setupImgs');
}

function showTopic(type) {
	showWaiting();
	start=0;
	cur_show=type;
	var url=baseURL+"show="+type;
	showAjax(url,'posts','setupImgs');
}

function showCur() {
	if(start==0) {
		var url=baseURL+"show="+cur_show;
		showAjax(url,'posts','setupImgs');
		refreshList();
	}
	setTimeout('showCur()',delay);
}

function refreshList() {
	var url=baseURL+"refreshList";
	showAjax(url,'legend');
}

function showAbout() {
	el = document.getElementById('about');
	el.style.visibility = (el.style.visibility == "visible") ? "hidden" : "visible";
	if(el.style.visibility=='visible') {
		//clearInterval(theInterval);
		//document.getElementById('body').style.backgroundImage='url('+arr[4]+')';
	}
	else {
		//changeBG();
		//theInterval = window.setInterval('changeBG()',delay);
	}	
}

function setupImgs() {
	var imgs = document.getElementById('posts').getElementsByTagName('img');
	for(var i=0;i<imgs.length;i++) {
		if(imgs[i].hasAttribute("class")) {
			if(imgs[i].hasAttribute("onclick")) continue;
			if(imgs[i].parentNode.nodeName=='A') {
				if(imgs[i].parentNode.setAttribute("target","_blank"));
				continue;
			}
			var url = imgs[i].getAttribute("src");
			if(imgs[i].hasAttribute("width")) {
				var width = imgs[i].getAttribute("width");
				var link = "showPic('"+url+"',"+width+")";
			}
			else link = "showPic('"+url+"')";
			imgs[i].setAttribute("onclick",link);
		}
	}
}

function showPic(url,width) {
	var p = document.createElement('div');
	p.setAttribute("id","overlay");
	var div = document.createElement('div');
	p.appendChild(div);
	var im = document.createElement('img');
	im.setAttribute("src",url);
	im.setAttribute("onclick","closePic()");
	div.appendChild(im);
	var cl = document.createElement('a');
	cl.setAttribute("id","close_link");
	cl.setAttribute("onclick","closePic()");
	div.appendChild(cl);
	if(width && width==370) {
		im.setAttribute("width","690");
		div.style.width="690px";
	}
	else im.setAttribute("width","370");
	document.getElementById('body').appendChild(p);
}

function closePic() {
	document.getElementById('body').removeChild(document.getElementById('overlay'));
}

function showAjax(url,id,postfunc) {
	var myAjax = new AjaxMe();
	myAjax.open("GET",url,true);
	myAjax.onreadystatechange=function() {
		if(myAjax.readyState==4 || myAjax.readyState=="complete") {
			document.getElementById(id).innerHTML=myAjax.responseText;
			if(postfunc) eval(postfunc+"(myAjax)");
		}
	}
	myAjax.send(null);
}