// JavaScript Document
function new_post() {
	
	var post = document.getElementById('post').value;
	var topic = document.getElementById('topic').value; 
	
	var callback = function () {
		document.getElementById('post').value = '';
		window.location.reload();		
	}	

	var XObj;
	try { XObj = new XMLHttpRequest(); }
	catch(e) { XObj = new ActiveXObject(Microsoft.XMLHTTP); }
	
	XObj.onreadystatechange = function () {
		if(XObj.readyState == 4) {
			if(callback) {
				callback();
			}
		}		
	}

	XObj.open('POST','php/input_post.php?topic='+topic+'&post='+escape(post),true);
	XObj.send(null);
}

function new_topic() {
	var name = document.getElementById('name').value;
	var info = document.getElementById('info').value;
	
	var callback = function () {
		window.location.reload();		
	}
	
	var XObj;
	try { XObj = new XMLHttpRequest(); }
	catch(e) { XObj = new ActiveXObject(Microsoft.XMLHTTP); }
	
	XObj.onreadystatechange = function () {
		if(XObj.readyState == 4) {
			if(callback) {
				callback();
			}
		}		
	}		
	XObj.open('POST','php/input_post.php?name='+name+'&info='+info,true);
	XObj.send(null);
}

function guest_topic() {
	var name = document.getElementById('new_topic_name').value;
	var info = document.getElementById('new_topic_info').value;
	
	var callback = function () {
		window.location.reload();		
	}
	
	var XObj;
	try { XObj = new XMLHttpRequest(); }
	catch(e) { XObj = new ActiveXObject(Microsoft.XMLHTTP); }
	
	XObj.onreadystatechange = function () {
		if(XObj.readyState == 4) {
			if(callback) {
				callback();
			}
		}		
	}		
	XObj.open('POST','php/input_post.php?uid=1&name='+name+'&info='+escape(info),true);
	XObj.send(null);
}

function remove_topic(topic_serial) {

	var callback = function (x) {
		window.location.reload();
	}

	var XObj;
	try { XObj = new XMLHttpRequest(); }
	catch(e) { XObj = new ActiveXObject(Microsoft.XMLHTTP); }
	
	XObj.onreadystatechange = function () {
		if(XObj.readyState == 4) {
			if(callback) {
				callback(XObj);
			}
		}		
	}		
		
	XObj.open('POST','php/posts.php?del=1&topic=' + topic_serial, true);
	XObj.send(null);
}

function load_topic(start, qty, length) {
	var forum = new Forum_Widget(topics, start, qty);
	forum.display();
}

function Forum_Widget(topic, start, qty) {
	this.topic = topic;
	this.start = start;
	this.qty = qty;
	this.root_div = 'topic_entry';	
	this.scanner_div = 'reply_scanner';	
}

Forum_Widget.prototype.display = function () {
	document.getElementById(this.root_div).innerHTML = this.entry_format();
	document.getElementById(this.scanner_div).innerHTML = this.scanner();
}

Forum_Widget.prototype.entry_format = function () {
	var topic = this.topic['topic_entry'];
	var length = (this.start+this.qty)-1;
	var table = "";
	var i = 0;
	for(var i = this.start; i<= length;i++) {
		if(topic[i]) {
			var user_name = topic[i]['user_name'];
			var user = user_name.split('@');

			table += "<div style='float: left' class='topic_user'>";
			table += "<div class='user_box' style='font-size: 11px; text-align: center'>";
			table += "<img src='" + topic[i]['img'] + "' width='120px' ";
			table += "/><br />";
			table +=  user[0];
			table += "<div class='small' style='font-weight: 100'>" 
			table += "" + make_date(topic[i]['date']);
			table += "</div><br />";
			table += "</div>";
			table += "</div>";
			table += "<div style='float: left' class='topic_entry'>";
			table += this.make_links(unescape(topic[i]['text']));
			table += "</div>";
			table += "<div style='clear: both; width: 500'></div>";		
		}
	}
	return  table;
}

Forum_Widget.prototype.scanner = function () {

	var length = this.topic['topic_entry'].length;
	var start = this.start;
	var qty = this.qty;
	var count = start + qty;
	
	var previous = "<span style='cursor: pointer' onclick='load_topic(" + (start-qty) + " , 10, " + length + ")'><u>new</u></span> | "; 
	var next = " | <span style='cursor: pointer' onclick='load_topic(" + (start + qty) + ", 10, " + length + ")'><u>older</u></span>";
		
	if(start == 0) {
		previous = "";
	}
 
	if(length < count) {
		next = "";
	}
	
	if(length < count) {
		count = start + (qty - (count - length));
	}
	return previous + (start+1) + "-" + count + " of " + length + next + "<br />";
}

Forum_Widget.prototype.make_links = function(raw) {
		
		var paragraph = raw.replace("  ", " ");
		var words = paragraph.split(" ");
		var text = "";
		for(var x=0; x<words.length;x++) {
			var word = words[x];
			if(word.match('.jpg') || word.match('.JPG') || word.match('.gif') || word.match('.png')) {
				word = "<img  style='cursor: pointer' src='" + word + "' width='100%' onclick=\"javascript:(window.open('" + word + "'))\"/>";
			} else if (word.match('href=')) {
				word = word;
			} else if (word.match('"http:') || word.match("'http:")) {
				word = word;
			} else if (word.match('http:')) {
				word = "<a href='" + word + "'>" + word + "</a>";
			} else if (word.match('www.')) {
				word = "<a href='http://" + word + "'>" + word + "</a>";
			}
			text += word + " ";
		}
		return text;
}
