
window.onload = init;

function init(){
	$("#ttnote").focus();
}

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://jisko.net/ajax/follow",{who:who},function(data) {
		if (/^ERROR:/.test(data)) {
			$("#loading").html('<img src="http://jisko.net/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://jisko.net/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('http://jisko.net/static/img/icons/fav_add.png','http://jisko.net/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://jisko.net/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://jisko.net/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');
    $.get("http://jisko.net/ajax/public?rand=" + Math.random(),{page:"1" },function(data) {
        $("#note_list").html(data);
   		$("#note_list").css("display","none");
		$("div:hidden:first").fadeIn("slow");
		$("#ttnote,#btsend").attr("disabled",false);
		$("#loading").fadeOut("fast");
    });
}

function doSimpleNoteForm() {
	var note = $("#ttnote").attr("value");
	var layer = $("#thenoteform").html();
    $.post("http://jisko.net/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").keypress(function (e) {
			if (e.which == 13) {
				if($(this).attr("value") == '') return false;
				$("#ttnote,#btsend").attr("disabled", true);
				$("#loading").html("Publicando...");
				$('#loading').fadeIn('fast');
				var note = $(this).attr("value");
				var auth = $("#hiddenkey", document.body).attr("value");
				clearInterval(timerID);
					$.post("http://jisko.net/ajax/post",{auth:auth,note:note},function(data) {
						if (/^ERROR:/.test(data)) {
							$("#loading").html('<img src="http://jisko.net/static/img/smileys/cry.png">');
							data = data.replace('ERROR: ', '');
							alert(data);
							$("#ttnote,#btsend").attr("disabled", false);
							$("#loading").fadeOut("fast");
						} else {
						$("#ttnote").attr("value", '');
						reloadNotes();
						timerID = setInterval("reloadNotes()",30000);
						$("#ttnote,#btsend").attr("disabled",false);
						var allNotes = $("#snotes").html()
						$("#snotes").html(++allNotes);
						$("#latest_note").html(note);
						$(".counter_send .counter").attr('value',140);
					}
						}
				   ); /*endpost*/
			  } /*e.which*/
	} /* function keypress ON*/
	); 
	
		$("#simplepost").keypress(function (e) {
			if (e.which == 13) {
				document.getElementById("sendbutton").click();
				return false;
			  } /*e.which*/
		} /* function keypress ON*/
	); 
	
	/*$("#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://jisko.net/ajax/post",{auth:auth,note:note},function(data) {
			if (/^ERROR:/.test(data)) {
				$("#loading").html('<img src="http://jisko.net/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()",30000);
			$("#ttnote,#btsend").attr("disabled",false);
			var allNotes = $("#snotes").html()
			$("#snotes").html(++allNotes);
			$("#latest_note").html(note);
			$(".counter_send .counter").attr('value',140);
		}
			} /* function data */
			); /* post */
	} /* end funct */
	); /* end btsend click */
} /* function() after doc.ready */
); /* document ready*/