function count(input, inputcount, limit) {
	if (input.value.length > limit) {
		input.value = input.value.substring(0, limit);
	} else {
		inputcount.value = limit - input.value.length;
	}
}

function showUpload() {
	$("#thenoteform").css("display","none");
	$("#upload_send").html("<input type=\"file\" class=\"green_input\" name=\"attach\">");
	$("div:hidden:first").fadeIn("slow");
}

function follow(who, button) {
	layer = "#" + button;
	$(layer).attr("disabled",true);
	$("#loading").html("Working...");
	$('#loading').fadeIn('fast');
	$.post("http://8to6.com/ajax/follow",{who:who},function(data) {
		if (/^ERROR:/.test(data)) {
			$("#loading").html('<img src="http://8to6.com/static/img/smileys/cry.png">');
			data = data.replace('ERROR: ', '');
			alert(data);
			$("#loading").fadeOut("fast");
		} else {
			value = $(layer).attr('value');
			var allFollowers = $("#sfollowers").html();
			if (value == "Follow") {
				$(layer).attr('value',"Unfollow");
				$("#sfollowers").html(++allFollowers);
			} else {
				$(layer).attr('value',"Follow");
				if (allFollowers == 1) { var modify = 0; }
				else { var modify = --allFollowers; }
				$("#sfollowers").html(""+modify+"");	
			}
			$("#loading").fadeOut("fast");
		}
	});
	$(layer).attr("disabled",false);
}

function ignore(who, button) {
	layer = "#" + button;
	$(layer).attr("disabled",true);
	$("#loading").html("Working...");
	$('#loading').fadeIn('fast');
			$.post("http://8to6.com/ajax/ignore",{who:who},function() {
				value = $(layer).attr('value');
					if (value == "Ignore") {
						if(typeof followbtn !== undefined) {
							followlayer = "#" + followbtn;
							$(followlayer).attr('value', "Follow");
							$(followlayer).attr('disabled', true);
						}
						$(layer).attr('value',"Unignore");
					} else {
						if(typeof followbtn !== undefined) {
							followlayer = "#" + followbtn;
							$(followlayer).attr('value', "Follow");
							$(followlayer).attr('disabled', false);
						}
						$(layer).attr('value',"Ignore");
					}
					$("#loading").fadeOut("fast");
				}
			);
	$(layer).attr("disabled",false);
}

function favorite(note) {
	var icons = new Array(2);
	icons[0] = 'http://8to6.com/static/img/icons/fav_add.png';
	icons[1] = 'http://8to6.com/static/img/icons/fav_del.png';
	var layer = "fav"+note;
	var src = document.getElementById(layer).src;
	$("#loading").html("Working...");
	$('#loading').fadeIn('fast');
	if (src == icons[0]) {
		$.get("http://8to6.com/ajax/favorite?rand=" + Math.random(),{id:note},function(data) {
			document.getElementById(layer).src = icons[1];
			var allFavorites = $("#sfavorites").html();
			$("#sfavorites").html(++allFavorites);
			$("#loading").fadeOut("fast");
		});
	}
	else {
		$.get("http://8to6.com/ajax/favorite?rand=" + Math.random(),{id:note},function(data) {
			document.getElementById(layer).src = icons[0];
			var allFavorites = $("#sfavorites").html();
			$("#sfavorites").html(""+--allFavorites+"");
			$("#loading").fadeOut("fast");
		});
	}
}

function reloadNotes() {
		
	var notes = $("#ajax_notes").html();
	var privates = $("#ajax_privates").html();
	
	$.getJSON("http://8to6.com/ajax/mainpage", { notes: notes, privates: privates}, function(json) {
		if (json.notes) { $("#ajax_notes").html(json.notes); }
		if (json.privates) {
			$("#ajax_privates").html(json.privates);
			content = $("#private_tab").attr('class');
			$("#private_tab").attr('class', content+' unread');
		}
	});
}

function doSimpleNoteForm() {
	var note = $("#ttnote").attr("value");
	var layer = $("#thenoteform").html();
	$.post("http://8to6.com/ajax/normalform/",{note:note},function(data) {
    	$("#thenoteform").css("display","none");
        $("#thenoteform").html(data);
	$("#thenoteform").fadeIn("slow");
        $("#upload_send").html("<input type=\"file\" class=\"green_input\" name=\"attach\">");
        $("#simplepost").keypress(function (e) {
		if (e.which == 13) {
			document.getElementById("sendbutton").click();
			return false;
		} /*e.which*/
	} /* function keypress ON*/
); /* simplepost ready */
});
    
}

function showLoading() {
	$("#loading").html("Loading...");
	$('#loading').fadeIn('fast');
}

function startLogin() {
	$("#loading").html("Logging In...");
	$('#loading').fadeIn('fast');
}

$(document).ready(
	function(){
		$("#ttnote").focus();
		$("#ttnote").keypress(function (e) {
			if (e.which == 13) {
				if($(this).attr("value") == '') { return false; }
				$("#ttnote").attr('disabled', 'disabled');
				$("#btsend").attr('disabled', 'disabled');
				$("#loading").html("Publishing...");
				$('#loading').fadeIn('fast');
				var note = $(this).attr("value");
				var auth = $("#hiddenkey", document.body).attr("value");
				clearInterval(timerID);
				$.post("http://8to6.com/ajax/post",{auth:auth,note:note},function(data) {
					if (/^ERROR:/.test(data)) {
						$("#loading").html('<img src="http://8to6.com/static/img/smileys/cry.png">');
						data = data.replace('ERROR: ', '');
						alert(data);
						$("#ttnote").attr("disabled", false);
						$("#btsend").attr("disabled", false);
						$("#loading").fadeOut("fast");
					} else {
						$("#ttnote").attr("value", '');
						$("#ajax_notes").html(($("#ajax_notes").html() / 1)+1);
						reloadNotes();
						$("#loading").fadeOut("fast");
						timerID = setInterval("reloadNotes()",25000);
						$("#ttnote,#btsend").attr("disabled",false);
						$("#latest_note").html(note);
						$(".counter_send .counter").attr('value',140);
					}
				}); /*endpost*/
			} /*e.which*/
	}); 
	
	$("#simplepost").keypress(function (e) {
		if (e.which == 13) {
			document.getElementById("sendbutton").click();
			return false;
		} /*e.which*/
	}); 
	
	/*$("#status_x").mouseover(function(){
		$("#actions_x").fadeIn("fast");
	}).mouseout(function(){
		$("#actions_x").fadeOut("fast");
	});*/
	
	$("a[class^='external']").attr('target','_blank');
 
	$("#btsend").click(function() {
		$("#ttnote,#btsend").attr("disabled",true);
		var note = $("#ttnote").attr("value");
		var auth = $("#hiddenkey", document.body).attr("value");
		$("#loading").html("Publishing...");
		$('#loading').fadeIn('fast');
		clearInterval(timerID);
			$.post("http://8to6.com/ajax/post",{auth:auth,note:note},function(data) {
				if (/^ERROR:/.test(data)) {
					$("#loading").html('<img src="http://8to6.com/static/img/smileys/cry.png">');
					data = data.replace('ERROR: ', ''); /*error*/
					alert(data);
					$("#ttnote,#btsend").attr("disabled",false);
					$("#loading").fadeOut("fast");
				} else {
					$("#ttnote").attr("value", '');
					reloadNotes();
					timerID = setInterval("reloadNotes()",25000);
					$("#ttnote,#btsend").attr("disabled",false);
					$("#latest_note").html(note);
					$(".counter_send .counter").attr('value',140);
				}
			}); /* post */
	}); /* end btsend click */
}); /* document ready*/