//var xmlReq = null;
function loadData(URL) {
// Create the XML request  
	var xmlReq = null;
	if(window.XMLHttpRequest)
		xmlReq = new XMLHttpRequest();
	else if(window.ActiveXObject)
		xmlReq = new ActiveXObject("Microsoft.XMLHTTP");
	if(xmlReq==null) return; // Failed to create the request

	xmlReq.onreadystatechange = function() {
		switch(xmlReq.readyState) {
		case 0:	// Uninitialized
			break;
		case 1: // Loading
			break;
		case 2: // Loaded
			break;
		case 3: // Interactive
			break;
		case 4:	// Done!
			if (xmlReq.getResponseHeader("Content-Type") == 'text/xml')
				ajaxReturn(xmlReq.responseXML);
			else
				ajaxReturn(xmlReq.responseText);
			break;
		default:
			break;
		}
	}
	xmlReq.open ('GET', URL, true);
	xmlReq.send (null);
}

function ajaxReturn(txt) {
	var obj = null;
	try {
		obj = JSON.parse(txt);
	} catch(e) { }
	if(!obj)
		return;
	if(obj.type=="refreshGallery") {
		updateGalleryImagees(obj);
	} else if(obj.type=="postShout") {
		postShoutResult(obj);
	} else if(obj.type=="getShoutbox") {
		var shoutarea = getObj("shoutarea");
		shoutarea.innerHTML = obj.content;
		if(obj.loggedIn) {
			var shoutName = getObj("shout_name");
			shoutName.setAttribute('value',obj.userName);
			shoutName.setAttribute('disabled','disabled');
		}
	}
}
function refreshImages() {
	//how many do we want?
	var imgs = Math.floor(screen.width / 150) - 2;
	if(imgs < 1)
		imgs = 5;
	var url = "index.php?action=refreshGallery&type=refreshGallery&num="+imgs.toString();
	loadData(url);
}
function updateGalleryImagees(obj) {
	var images = obj.images;
	var ul = getObj("galleryWrapper");
	if(!ul)
		return;
	//We made the new one file! delete the old one
	while(ul.hasChildNodes()) {
		ul.removeChild(ul.lastChild);
	}
	for(var i=0;i<images.length;i++) {
		var li = document.createElement('li');
		var div = document.createElement('div');
		div.setAttribute('class','caption');
		div.appendChild(document.createTextNode(images[i].cap));
		li.appendChild(div);
		var a = document.createElement('a');
		a.setAttribute('href',images[i].lnk);
		var img = document.createElement('img');
		img.setAttribute('src',images[i].thm);
		img.setAttribute('alt',images[i].cap);
		a.appendChild(img);
		li.appendChild(a);
		ul.appendChild(li);
	}
}

addLoadEvent(refreshImages);

function postShout() {
	var shout = {};
	shout.name = getObj('shout_name').value;
	shout.text = getObj('shout_text').value;
	shout.type = "postShout";
	var str = JSON.stringify(shout);
	var url = "shoutbox/index.php?action=postShout&text="+str;
	loadData(url);
	return false;
}
function postShoutResult(obj) {
	if(obj.pass) {
		var shoutarea = getObj("shoutarea");
	
		//Remove last comment
		if(shoutarea.childNodes.length)
			shoutarea.removeChild(shoutarea.childNodes[shoutarea.childNodes.length - 1]);
	
		//Build and add a new one!
		var container = document.createElement("div");
		var timeSpan = document.createElement("span");
		timeSpan.setAttribute("class","time");
		timeSpan.appendChild(document.createTextNode(obj.date));
		container.appendChild(timeSpan);
		container.appendChild(document.createTextNode(obj.name+": "+obj.text));
		shoutarea.insertBefore(container, shoutarea.childNodes[0]);
		
		getObj('shout_text').value = "";
		container.style.outline = '#f00 solid 2px';
		setTimeout(function() {
			container.style.outline = '0px';
		},milliseconds);
		
	} else {
		//Something went wrong!
		alert("Something went wrong!");
	}
}
function getShouts() {
	var url = "shoutbox/index.php?action=json";
	loadData(url);
}

addLoadEvent(getShouts);
