function showPopUp(ind) {
	ind.fadeIn(300, function() { $(this).removeClass('hidden'); });
	$('i.close', ind).click( hidePopUp);
}
function hidePopUp() {
	$(this).parents('.popUp:first')
		.fadeOut(300,
			function() {
				$(this).addClass('hidden').removeAttr('style');
			}
		);
}

function bindTips() {
	if (auth) {
		$('#btn_ask').click( function(){
			$("#afterReg .b-wrpr").addClass('hidden');
			$("#cnt_ask").removeClass('hidden');
			$("#afterReg").removeClass('unsuc');
			$("#afterReg").removeClass('success');
			showPopUp($('#afterReg'));
			$('#status_text').text('@'+nickname+' ');
			$('#status_text').get(0).focus();
			$('#form_notice [name=reply]').val('');
			return !1;
		});
		$('#btn_tour').click( function(){
			$("#afterReg .b-wrpr").addClass('hidden');
			$("#cnt_tour").removeClass('hidden');
			$("#afterReg").removeClass('unsuc');
			$("#afterReg").removeClass('success');
			showPopUp($('#afterReg'));
			$('#status_text').text('@'+nickname+' ');
			$('#status_text').get(0).focus();
			$('#form_notice [name=reply]').val('');
			return !1;
		});
		$('#btn_contact').click( function(){
			$("#afterReg .b-wrpr").addClass('hidden');
			$("#cnt_contact").removeClass('hidden');
			$("#afterReg").removeClass('unsuc');
			$("#afterReg").removeClass('success');
			showPopUp($('#afterReg'));
			$('#status_text').text('@'+nickname+' ');
			$('#status_text').get(0).focus();
			$('#form_notice [name=reply]').val('');
			return !1;
		});
		$('#content ul.notes a.anwr').click(
			function() {
				replyId = $(this).attr('id').replace('reply', '')
				$('#status_text').text('@'+nickname);
				$('#form_notice [name=reply]').val(replyId);
				$('#status_text').text('@'+nickname+' ');
				$('#status_text').get(0).focus();
				return false;
			}
		);
	} else {

		$('#btn_ask').click( function(){
			showPopUp($('#regNotice'));
			return !1;
		});

		$('#btn_tour').click( function(){
			showPopUp($('#regNotice'));
			return !1;
		});

		$('#btn_contact').click( function(){
			showPopUp($('#regNotice'));
			return !1;
		});

		$('#content ul.notes a.anwr').click(
			function() {
				showPopUp($('#regNotice'));
				return !1;
			}
		);
		
	}
}
function unbindTips() {
	$('#btn_ask').unbind('click');
	$('#btn_tour').unbind('click');
	$('#btn_contact').unbind('click');
	$('#content ul.notes a.anwr').unbind('click');
}
function remakeBlocks(_auth, _nickname) {
	auth = _auth;
	nickname = _nickname;
	
	if (auth) {
		$('#loginLink').hide();
		$('#logoutLink').show();
		$('#topIntro').hide();
		$('#makeNotice').show();
	} else {
		$('#logoutLink').hide();
		$('#loginLink').show();
		$('#makeNotice').hide();
		$('#topIntro').show();
	}
	unbindTips();
	bindTips();
}

$(function() {
	$('#topIntro, #content, #rCol').prepend('<i class="blc t"><i class="c l" /><i class="c r" /></i>').append('<i class="blc b"><i class="c l" /><i class="c r" /></i>');
	$('#makeNotice').append('<i class="c lt" /><i class="c lb" />');
	$('#rCol a.bbox').prepend('<i class="t"><i class="c l" /><i class="c r" /></i>').append('<i class="b"><i class="c l" /><i class="c r" /></i><i class="sh" />');
	$('div.popUp').prepend('<i class="blc t"><i class="c l" /><i class="c r" /></i>').append('<i class="blc b"><i class="c l" /><i class="c r" /></i><i class="bsh"><i class="c l" /><i class="c r" /></i><i class="rsh"><i class="c t" /></i><i class="tail" />').children('.b-wrpr').append('<i class="icon close" />');
});

$(document).ready(function(){
	
	function counter(event){
		var maxLength = 140;
		var currentLength = $("#status_text").val().length;
		var remaining = maxLength - currentLength;
		var counter = $("#status_text_count");
		counter.text(remaining);

		if (remaining <= 0) {
			$("#form_notice").addClass("warning");
		} else {
			$("#form_notice").removeClass("warning");
		}
	}
	
	function submitonreturn(event) {
		if (event.keyCode == 13) {
			$("#form_notice").submit();
			return false;
		}
		return true;
	}
	
	$("#form_notice").submit(function(){
			$.post('/send/', {
				nickname : $('#form_notice [name=nickname]').val(),
				message : $('#status_text').val(),
				reply : $('#form_notice [name=reply]').val()
			}, function(responseText) {
				if (parseInt(responseText) == 1) {
					$("#afterReg").removeClass('unsuc');
					$("#afterReg").addClass('success');
					$("#afterReg .b-wrpr").addClass('hidden');
					$("#cnt_success").removeClass('hidden');
					showPopUp($('#afterReg'));
					$("#status_text").val('');
				} else {
					$("#afterReg").removeClass('success');
					$("#afterReg").addClass('unsuc');
					$("#afterReg .b-wrpr").addClass('hidden');
					$("#cnt_error").removeClass('hidden');
					showPopUp($('#afterReg'));
					$("#status_text").val('');
				}
			}, 'text');
			return false;
	});
	
	if ($("#status_text").length) {
		$("#status_text").bind("keyup", counter);
		$("#status_text").bind("keydown", submitonreturn);

		// run once in case there's something in there
		counter();

		// set the focus
		$("#status_text").focus();
	}
	
	
});