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("Trabajando...");
	$('#loading').fadeIn('fast');
	$.post("http://miopinion.com/ajax/follow",{who:who},function(data) {
		if (/^ERROR:/.test(data)) {
			$("#loading").html('<img src="http://miopinion.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 == "Seguir") {
				$(layer).attr('value',"No seguir");
				$("#sfollowers").html(++allFollowers);
			} else {
				$(layer).attr('value',"Seguir");
				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("Trabajando...");
	$('#loading').fadeIn('fast');
			$.post("http://miopinion.com/ajax/ignore",{who:who},function() {
				value = $(layer).attr('value');
					if (value == "Ignorar") {
						if(typeof followbtn !== undefined) {
							followlayer = "#" + followbtn;
							$(followlayer).attr('value', "Seguir");
							$(followlayer).attr('disabled', true);
						}
						$(layer).attr('value',"No ignorar");
					} else {
						if(typeof followbtn !== undefined) {
							followlayer = "#" + followbtn;
							$(followlayer).attr('value', "Seguir");
							$(followlayer).attr('disabled', false);
						}
						$(layer).attr('value',"Ignorar");
					}
					$("#loading").fadeOut("fast");
				}
			);
	$(layer).attr("disabled",false);
}

function favorite(note) {
	var icons = new Array(2);
	icons[0] = 'http://miopinion.com/static/img/icons/fav_add.png';
	icons[1] = 'http://miopinion.com/static/img/icons/fav_del.png';
	var layer = "fav"+note;
	var src = document.getElementById(layer).src;
	$("#loading").html("Trabajando...");
	$('#loading').fadeIn('fast');
	if (src == icons[0]) {
		$.get("http://miopinion.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://miopinion.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() {
		$("#loading").html("Actualizando notas...");
	$('#loading').fadeIn('fast');
	var islogged = false;
	$.getJSON('http://miopinion.com/ajax/public',{page:"1", rand: Math.random()},function(json) {
		$("#note_list").css("display","none");
		$("#note_list").html('');
		
		for(i=0; i < json.length; i++) {
			var divNote = document.createElement('div');
			if (json[i].replying === true) { divNote.className = 'note unread'; }
			else if (json[i].type == 'twitter') { divNote.className = 'note_t'; }
			else divNote.className = 'note';
			divNote.id = 'status_' + json[i].id;
			divAvatarNote = document.createElement('div');
			divAvatarNote.className = 'avatar_note';
			var inner = '<img src="' + json[i].avatar + '" alt="avatar"';
			if (json[i].type != 'twitter' && (json[i].type != 'twitter_reply')) { inner += ' onmouseover="tooltip.ajax_delayed(event, \'profile\', \'' + json[i].user_id + '\');" onmouseout="tooltip.hide(event);"'; }
			divAvatarNote.innerHTML = inner + ' width="48" height="48"/>';
			divNote.appendChild(divAvatarNote);
			divInfoNote = document.createElement('div');
			divInfoNote.className = 'info_note';
			divUserNote = document.createElement('div');
			divUserNote.className = 'user_note';
			var inner = '<a href="http://miopinion.com/' + json[i].username + '"';
			if (json[i].type != 'twitter'  && (json[i].type != 'twitter_reply')) { inner += ' onmouseover="return tooltip.ajax_delayed(event, \'profile\', \'' + json[i].user_id + '\');" onmouseout="tooltip.hide(event);"'; }
			divUserNote.innerHTML = inner + '>' + json[i].username + '</a>';
			divInfoNote.appendChild(divUserNote);
			divActionsNote = document.createElement('div');
			divActionsNote.className = 'actions_note';
			divActionsNote.id = 'actions_' + json[i].id;
			var actions = '';
						if (json[i].attached_file) {
				actions += '&nbsp;<a href="http://miopinion.com/download/' + json[i].id + '/' + json[i].attached_file + '" rel="nofollow">';
				actions += '<img src="http://miopinion.com/static/img/icons/download.png" alt="Descargar adjunto" title="Descargar adjunto" /></a>';
			}
			if (islogged === true && (json[i].type == 'public' && (json[i].type != 'twitter' && (json[i].type != 'twitter_reply')))) {
				if (json[i].favorite) { actions += '&nbsp;<img src="http://miopinion.com/static/img/icons/fav_del.png" id="fav' + json[i].id + '" alt="Quitar favorito" title="Quitar favorito" onclick="favorite(' + json[i].id + ');" style="cursor: pointer;" />'; }
				else { actions += '&nbsp;<img src="http://miopinion.com/static/img/icons/fav_add.png" id="fav' + json[i].id + '" alt="Marcar favorito" title="Marcar favorito" onclick="favorite(' + json[i].id + ');" style="cursor: pointer;" />'; }
			}
			if (json[i].type != 'private') {
				if (json[i].type != 'twitter' && (json[i].type != 'twitter_reply')) { actions += '&nbsp;<a href="http://miopinion.com/' + json[i].username + '/' + json[i].id + '" title="Permalink"><img src="http://miopinion.com/static/img/icons/plink.png" alt="Permalink" /></a>'; }
				else { actions += '&nbsp;<a href="http://twitter.com/' + json[i].username + '/statuses/' + json[i].id + '" title="Permalink"><img src="http://miopinion.com/static/img/icons/plink.png" alt="Permalink" /></a>'; }
			}
			if (islogged === true && ('' != json[i].username)) {
				actions += '&nbsp;<a onclick="document.note_form.note.value';
				if (json[i].type == 'twitter') { var network = '%'; }
				else if (json[i].type == 'private') { var network = '!'; }
				else { var network = '@'; }
				actions += ' += \'' + network + json[i].username + ' \';';
				actions += 'document.note_form.note.focus(); return false;" href="" title="Reply"><img src="http://miopinion.com/static/img/icons/reply.png" alt="Responder" /></a>';
			}
			divActionsNote.innerHTML = actions;
			divInfoNote.appendChild(divActionsNote);
			divDateNote = document.createElement('div');
			divDateNote.className = 'date_note';
			datenote = json[i].time_ago + ' (<span class="from">';
			if (json[i].from == 'mobile') { datenote = datenote + '<img src="http://miopinion.com/static/img/icons/phone.png" title="móvil">)</span>'; }
			else if (json[i].type == 'twitter') { datenote = datenote + 'twitter)</span>'; }
			else { datenote = datenote + json[i].from + ')</span>'; }
			divDateNote.innerHTML = datenote;
			divInfoNote.appendChild(divDateNote);
			divNote.appendChild(divInfoNote);
			divTextNote = document.createElement('div');
			divTextNote.className = 'text_note';
			divTextNote2 = document.createElement('div');
			divTextNote2.id = 'text_note_' + json[i].id;
			divTextNote2.innerHTML = json[i].text;
			divTextNote.appendChild(divTextNote2);
			divNote.appendChild(divTextNote);
			divSeparator = document.createElement('div');
			divSeparator.className = 'separator';
			divNote.appendChild(divSeparator);
			document.getElementById('note_list').appendChild(divNote);
		}
		$("#loading").fadeOut("fast");
		$("#note_list").fadeIn("slow");
		$("#ttnote,#btsend").attr("disabled",false);
	});
		
	var notes = $("#ajax_notes").html();
	var privates = $("#ajax_privates").html();
	
	$.getJSON("http://miopinion.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://miopinion.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("Cargando...");
	$('#loading').fadeIn('fast');
}

function startLogin() {
	$("#loading").html("Iniciando sesi&oacute;n...");
	$('#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("Publicando...");
				$('#loading').fadeIn('fast');
				var note = $(this).attr("value");
				var auth = $("#hiddenkey", document.body).attr("value");
				clearInterval(timerID);
				$.post("http://miopinion.com/ajax/post",{auth:auth,note:note},function(data) {
					if (/^ERROR:/.test(data)) {
						$("#loading").html('<img src="http://miopinion.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("Publicando...");
		$('#loading').fadeIn('fast');
		clearInterval(timerID);
			$.post("http://miopinion.com/ajax/post",{auth:auth,note:note},function(data) {
				if (/^ERROR:/.test(data)) {
					$("#loading").html('<img src="http://miopinion.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*/