// 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/add_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/add_post.php?name='+name+'&info='+info,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';	
}

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

Forum_Widget.prototype.entry_format = function () {
	var topic = this.topic['topic_entry'];
	var table = "<table width='100%' cellpadding='0px' cellspacing='0px' border='0px'><tr><td colspan='2'>"
	
	table += "<div style='width: 200px; text-align: right; padding-top: 4px; padding-right: 6px; float: right'>" + this.scanner() + "</div>";
	
	table += "<div style='border-top: 1px solid #CCC; padding: 4px; font-weight: bold'>" + this.topic['topic_name'] + "</div></td></tr>";

	
	var length = (this.start+this.qty)-1;
	
	for(var i = this.start; i<= length;i++) {
		if(topic[i]) {
			var user_name = topic[i]['user_name'];
			var user = user_name.split('@');
			table += "<tr><td valign='top' align='center' width='20%'><div class='topic_user'>";
			table += "<div class='user_box'>" + user[0] + "<br />";
			table += "<img src='" + topic[i]['img'] + "' width='60px' ";
			table += "/></div><br />";
			table += "" + make_date_time(topic[i]['date']) + "<br />";
			table += "</div></td>";
			table += "<td valign='top' class='topic_entry'><div style='margin-top:10px;'>";
			table += this.make_links(unescape(topic[i]['text']));
			table += "</div></td></tr>";
		}
	}
	table += "</table>";
	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;
}