function chat_modify(id) {
	jQuery(id +' .right').show('slow');
	jQuery(id +' .text').hide('slow');
	jQuery(id +' .textarea').show('slow');
}

function generateChatUrl() {
	windowOpener(700, 500, "chat", "/wp-content/plugins/chat/chat_live.php");
}

function windowOpener(windowWidth, windowHeight, windowName, windowUri) {
    var centerWidth = (window.screen.width - windowWidth) / 2;
    var centerHeight = (window.screen.height - windowHeight) / 2;

    newWindow = window.open(windowUri, windowName, 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=0,,width=' + windowWidth + 
        ',height=' + windowHeight + 
        ',left=' + centerWidth + 
        ',top=' + centerHeight);

    newWindow.focus();
    return newWindow.name;
}

function chat_post_question() {
	var chatterer = $('#chatterer').attr('value');
	var question = $('#question').attr('value');
	
	if(question.length == 0) {
		$('#question').animate( { backgroundColor: '#ffd2d2' }, 75).animate( { backgroundColor: '#ffffff' }, 75);
		return;
	}
	
	question = nl2br(question, true);

	$.post("/wp-content/plugins/chat/ajax_post.php", {chatterer: chatterer, question: question}, function(data) {
		$('#post-message').show(500);
		$('#question').attr('value', '');
		$.timer(5000, function (timer) {
			$('#post-message').hide(500);
			timer.stop();
		});
	});
}

function chat(holder, refresh, id) {
	chat_fetch(holder, id);
	
	if(refresh == true) {
		$.timer(10000, function (timer) {
			chat_fetch(holder, id);
		});
	}
}

function chat_fetch(holder, id) {
	var url = "/wp-content/plugins/chat/ajax.php?rnd="+ Math.random()*3465432;
	
	// get open chat or archived one?
	if(id > 0)
		url += "&id="+ id;

	$(holder).load(url);
}

function nl2br (str, is_xhtml) { 
    breakTag = '<br />';
    if (typeof is_xhtml != 'undefined' && !is_xhtml) {
        breakTag = '<br>';
    }
 
    return (str + '').replace(/([^>]?)\n/g, '$1'+ breakTag +'\n');
}