// JavaScript Document
function show_new_topic_form(elementId) {
	var html = "<div><input type='hidden' name='topic' id='topic' value='$topic_id'/>";

	html += "<table><tr><td valign='top'>";
	html += "<span class='submitTitle'>Post Title: </span></td><td><input style='width: 170px;' name='new_topic_name' id='new_topic_name' class='basicInput'/></div>";
	html += "</td><td rowspan=2 valign=top>";
	html += "<span class='submitTitle'>Image: </span>";
	html += "</td><td rowspan=2 valign=top>";
	html += "<input id='new_topic_image' class='basicInput'/> insert file location <b>OR</b> <a>Upload</a>";
	html += "</td></tr><tr><td valign='top'>";
	html += "<div><span class='submitTitle'>Summary: </span></td><td><textarea id='new_topic_summary' name='new_topic_summary'></textarea></div>";
	html += "</td></tr></table>";

	html += "<div><span class='submitTitle'>Content:</span> <textarea id='new_topic_info' name='new_topic_info' class='submitContent'></textarea></div>";
	html += "<input type='submit' class='button' style='width: 74px;' onClick='new_topic()' value='Create'/>";

	document.getElementById(elementId).innerHTML = html;
}

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/topics.php?topic=' + topic_serial, true);
	XObj.send(null);
}

function topic_bigups(id, bigups) {
	var a = auth;
	var o = owner;
	var bigups = (bigups*1) + 1;

	if(!auth) {
		alert('You must login to give BigUps and post forum comments.');
		return false;
	}
	
	if (owner == serial) {
		alert("BigUps! You can only give BigUps on other websites.");
		return false;
	}	

	var callback = function () {
      		var html = "<img style='vertical-align: middle; cursor: pointer; margin-right: 10px' height='24px' title='Click to give your BigUps' " + "onClick=\"topic_bigups('" + id + "', '" + bigups + "');\" " + "src='http://bigupsforum.com/go/images/bigups_logo.png'/>" + bigups;
		document.getElementById('bigupsdiv:'+id).innerHTML = html;
	}
	
	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/bigups.php?topic=true&user=' + serial + '&topic=' + id + '&count=' + bigups, true);
	XObj.send(null);
}


function new_post() {
	var post = escape(document.getElementById('post').value);
	var topic = escape(document.getElementById('topic').value); 

	if(!topic) {
		alert('You have not given your post a title.');
		return false;
	}
	
	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() {

	if(!document.getElementById('new_topic_name').value || document.getElementById('new_topic_name').value == 'title...') {
		alert('You can not make a blank post.');
		return false;
	}

	if(!document.getElementById('new_topic_info').value || document.getElementById('new_topic_info').value == 'message...') {
		alert('You can not make a blank post.');
		return false;
	}

	var name = document.getElementById('new_topic_name').value;
	var summary = ""; //document.getElementById('new_topic_summary').value;
	var image = ""; //document.getElementById('new_topic_image').value;
	var info = document.getElementById('new_topic_info').value;
	
	var callback = function () {
		document.getElementById('new_topic_name').value = "";
		document.getElementById('new_topic_info').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?name='+name+'&summ='+summary+'&image='+image+'&info='+escape(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;
}
