var newshouturl = "";
var sid = "0";



function resize(add)
{
	if(!usingIframe) {
		return;
	}
	
	iframe = my_getbyid('ibspeakframe');
	height = parseInt(iframe.style.height) ? parseInt(iframe.style.height ) : 100;
	
	
	if(add == "1") {
		// Increase
		nheight = height + 20;
	} else {
		// Decrease
		if(height != "20") {
			nheight = height - 20;
		}
	}
	
	if(nheight < 0) {
		nheight = 0;
	}
	
	iframe.style.height = nheight + "px";
	
	my_setcookie('ibspeak_size', nheight, 1);
}

function fastShout() {
	posttext = my_getbyid('shouttxt');
	
	var ourmin = posttext.value.length;
	var ourmax = posttext.value.length;
	
	if(ourmin < minlength) {
		alert(error_msgtooshort);
		return false;
	}
	
	if(ourmax > maxlength) {
		alert(error_msgtoolong);
		return false;
	}
	
	
	var fields = Array();
	fields['Post'] = posttext.value;
	
	req = function()
	{
		if (xmlobj.readystate_ready_and_ok())
		{
			posttext.value = "";
			var html = xmlobj.xmlhandler.responseText;
			eval(html);
		}

	}

	xmlobj = new ajax_request();
	xmlobj.onreadystatechange(req);
	
	var u = ipb_var_base_url+'autocom=ibspeak&do=ajax&func=newshout';

	xmlobj.process(u, 'POST', xmlobj.format_for_post(fields));
	
	
	return false;
}

function showOptions(obj) {
	object = my_getbyid('ibspeakoptionsmenu');

	toggleview('ibspeakoptionsmenu');
	
	if(object.style.display != "none"){
		width = parseInt(object.style.width) ? parseInt(object.style.width) : object.offsetWidth;
		width2 = parseInt(obj.style.width) ? parseInt(obj.style.width) : obj.offsetWidth;
		height = parseInt(obj.style.height) ? parseInt(obj.style.height) : obj.offsetHeight;
		
		left = parseInt(_get_obj_leftpos(obj) - width + width2);
		
		object.style.left = left +"px";
		object.style.top = parseInt(_get_obj_toppos(obj) + height) +"px";
		object.style.zIndex = 100; 
		
		object.onclick = function() {
			closeOptions();
		}
		
	}
}

function closeOptions() {
	object = my_getbyid('ibspeakoptionsmenu');
	object.style.display = "none";
}

function ibSpeak_speak() {

	showIt();
	var form = my_getbyid('shoutingform');
	
	form.onsubmit = addNewShout;
	
}


function ibspeak_delete(id) {
	if(confirm(AreYouSure)) {
	
		req = function()
		{
			if (xmlobj.readystate_ready_and_ok())
			{
				doUpdateShouts();
			}
	
		}	
		
		xmlobj = new ajax_request();
		
		xmlobj.onreadystatechange(req);
		var u = ipb_var_base_url+'autocom=ibspeak&do=ajax&func=delshout&id='+parseInt(id);
		
		xmlobj.process(u, 'GET', null);
		
	}
}

function showIt() {

	if(!my_getbyid("shoutcontent")) {
		parent.showIt();
		return;
	}
	

	var textArea = my_getbyid("shoutcontent");
	textArea.value = "";
	

	
	var ibSpeak = my_getbyid("ipbspeaknewshout");
	if(ibSpeak.style.display == "none") {
		toggleview("ipbspeaknewshout");
	}
	
	if ( typeof( window.innerWidth ) == 'number' ) {
		my_width  = window.innerWidth;
		my_height = window.innerHeight;
	} else if ( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		my_width  = document.documentElement.clientWidth;
		my_height = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		my_width  = document.body.clientWidth;
		my_height = document.body.clientHeight;
	}	
	
	var divheight = parseInt( ibSpeak.style.Height );
	var divwidth  = parseInt( ibSpeak.style.Width );	

	divheight = divheight ? divheight : 400;
	divwidth  = divwidth  ? divwidth  : 400;	
	
	
	var top = (my_height / 2) - (divheight / 2);
	var left = (my_width / 2) - (divwidth / 2);
	
	
	ibSpeak.style.left = left +"px";
	ibSpeak.style.top = top +"px";	

}

function ibspeak_edit(id) {
	sid = id;
	
	var form = parent.my_getbyid('shoutingform');
	
	form.onsubmit = doEditShout;
	
	showIt();
	
	if(my_getbyid("ibspeak-msg-"+id)) {
		var ourMsg = my_getbyid("ibspeak-msg-"+id);
	} else {
		var ourMsg = my_getbyid("ibspeak-dis-"+id);
	}

	
	if(!my_getbyid("shoutcontent")) {
		var textArea = parent.my_getbyid("shoutcontent");
	} else {
		var textArea = my_getbyid("shoutcontent");
	}
	
	
	textArea.value = ourMsg.innerHTML;
}

function doEditShout() {
	posttext = parent.my_getbyid('shoutcontent');
	
	var ourmin = posttext.value.length;
	var ourmax = posttext.value.length;
	
	if(ourmin < minlength) {
		alert(error_msgtooshort);
		return false;
	}
	
	if(ourmax > maxlength) {
		alert(error_msgtoolong);
		return false;
	}
	
	
	var fields = Array();
	fields['Post'] = posttext.value;
	
	req = function()
	{
		if (xmlobj.readystate_ready_and_ok())
		{
			if(my_getbyid("ipbspeaknewshout")) {
				toggleview("ipbspeaknewshout");
			} else {
				parent.toggleview("ipbspeaknewshout");
			}
			var html = xmlobj.xmlhandler.responseText;
			eval(html);
		}

	}

	xmlobj = new ajax_request();
	xmlobj.onreadystatechange(req);
	
	var u = ipb_var_base_url+'autocom=ibspeak&do=ajax&func=editshout&id='+sid;
	
	xmlobj.process(u, 'POST', xmlobj.format_for_post(fields));
	
	
	return false;
}


function wait(seconds) {
	var time = parseInt(seconds * 1000);
	for(i=0; i<time; i++) {
		setTimeout('return true;', time);
	}
}



function loadingLayer() {
	toggleview("ibspeakLoadingLayer");
	
	obj = my_getbyid("ibspeakLoadingLayer");
	
	if ( typeof( window.innerWidth ) == 'number' ) {
		my_width  = window.innerWidth;
		my_height = window.innerHeight;
	} else if ( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		my_width  = document.documentElement.clientWidth;
		my_height = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		my_width  = document.body.clientWidth;
		my_height = document.body.clientHeight;
	}	
	
	var divheight = parseInt( obj.style.Height );
	var divwidth  = parseInt( obj.style.Width );	

	divheight = divheight ? divheight : 100;
	divwidth  = divwidth  ? divwidth  : 100;	
	
	
	var top = (my_height / 2) - (divheight / 2);
	var left = (my_width / 2) - (divwidth / 2);
	
	
	obj.style.left = left +"px";
	obj.style.top = top +"px";	
}


function doUpdateShouts() {

	if(usingIframe) {
		parent.ibspeakframe.location.href = ipb_var_base_url+'autocom=ibspeak&do=iframe';
		if(my_getbyid("messagetable2")) {
			updateMainShouts();
		}	
	} else {
	
		loadingLayer();
	
		req = function()
		{
			if (xmlobj.readystate_ready_and_ok())
			{
				var html = xmlobj.xmlhandler.responseText;
				table = my_getbyid('messagetable');
				table.innerHTML = html;
				loadingLayer();
				if(my_getbyid("messagetable2")) {
					updateMainShouts();
				}			
			}
	
		}
	
		xmlobj = new ajax_request();
		xmlobj.onreadystatechange(req);
		var u = ipb_var_base_url+'autocom=ibspeak&do=ajax&func=showshout';
		xmlobj.process(u, 'GET', null);
	}
}

function updateMainShouts() {
	
	loadingLayer();

	req = function()
	{
		if (xmlobj.readystate_ready_and_ok())
		{
			var html = xmlobj.xmlhandler.responseText;
			table = my_getbyid('messagetable2');
			table.innerHTML = html;
			loadingLayer();
		}

	}

	xmlobj = new ajax_request();
	xmlobj.onreadystatechange(req);
	var u = ipb_var_base_url+'autocom=ibspeak&do=ajax&func=showshoutlong&p='+IBSCURURL;
	xmlobj.process(u, 'GET', null);
}



function updateShouts() {
	doUpdateShouts();
	setTimeout("updateShouts()", timeToUpdate);
}



function addNewShout() {
	
	posttext = my_getbyid('shoutcontent');
	
	var ourmin = posttext.value.length;
	var ourmax = posttext.value.length;
	
	if(ourmin < minlength) {
		alert(error_msgtooshort);
		return false;
	}
	
	if(ourmax > maxlength) {
		alert(error_msgtoolong);
		return false;
	}
	
	
	var fields = Array();
	fields['Post'] = posttext.value;
	
	req = function()
	{
		if (xmlobj.readystate_ready_and_ok())
		{
			toggleview("ipbspeaknewshout");
			var html = xmlobj.xmlhandler.responseText;
			eval(html);
		}

	}

	xmlobj = new ajax_request();
	xmlobj.onreadystatechange(req);
	
	var u = ipb_var_base_url+'autocom=ibspeak&do=ajax&func=newshout';

	xmlobj.process(u, 'POST', xmlobj.format_for_post(fields));
	
	
	return false;
}


function showhide(obj, id) {
	object = my_getbyid(id);
	
	if(object.style.display == "block"){
		toggleview(id);
	} else {
		toggleview(parent.ibSpeakClass.lastid);
		parent.ibSpeakClass.lastid = id;
		
		object.style.width = "150px"; 
		height = obj.offsetHeight;
		width = obj.offsetWidth;
		
		object.style.position = "absolute";
		left = parseInt(_get_obj_leftpos(obj) - parseInt(object.style.width) + width);
		
		object.style.left = left +"px";
		
		object.style.top = _get_obj_toppos(obj) + height +"px";
		
		object.style.zIndex = 100; 
	}
	toggleview(id);
}

