/*   _    _
 *  | |__| |___  __ _ __ _ _  _
 *  | '_ \ / _ \/ _` / _` | || |
 *  |_.__/_\___/\__, \__, |\_, |
 *              |___/|___/ |__/
 *
 *
 *              waaaaaazap?
 *
 */
function showflickr(flickrid, divid, imgid) {
  $.getJSON("http://api.flickr.com/services/feeds/photos_public.gne?id=" + flickrid + "&lang=en-us&format=json&jsoncallback=?", function(data){
    $.each(data.items, function(i,item){
        if(item.media.m.indexOf(imgid) > 0)
            $("<img/>").attr("src", item.media.m.replace(/_m/,'_s')).appendTo("#flickr" + divid).wrap("<a href='" + item.link + "'></a>");
    });
  });
}
function removefeed(id, group) {

    $('#feededit').html(loading);

    $.ajax({
        url: "/removefeed.php",
        type: "POST",
        data: {feedid: id, group: group},
        success: function(r){
                $("#feededit").load("/feedlist.php");
        }
    });

/*    $.get("/removefeed.php", {feedid: id, group: group}, function() { 
        $('#feededit').html(loading);
        $('#feededit').load("/feedlist.php", { group: group} );
    }); */
} 
function forgotpasswd() {
    $('#forgotpasswd_text').slideDown();
}
function gtranslate() {

    $("#results .info" ).each(
        function( intIndex ){
            $(this).translate("en")
        }   
    );

    $("#results .msg" ).each(
        function( intIndex ){
            $(this).translate("en")
        }   
    );
}
function hiderss() {

  createCookie("hiderss", 1);
  reloadposts();

}

function createCookie(name,value,days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        var expires = "; expires="+date.toGMTString();
    }
    else var expires = "";
    document.cookie = name+"="+value+expires+"; path=/; domain=bloggy.se";
}

function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}

function eraseCookie(name) {
    createCookie(name,"",-1);
}
function reloadlikepage() {

        $('#thecontent').html(loading);
        $.ajax({
          url: "/gillar",
          type: "GET",
          success: function(r) {
            $("#thecontent").html(r);
          }
        });
}
function likes(crc32,id,page) {

    if(!readCookie('heart'+crc32) && !page) {
        createCookie('heart'+crc32, 1);
        $("#heart"+crc32).css("background-image", "url(/images/heart.png)");
        $.ajax({
            url: "/editlikes.php",
            type: "GET",
            data: {likes: id}
      });
    } else {
        eraseCookie('heart'+crc32);
        $("#heart"+crc32).css("background-image", "url(/images/silver-heart.png)");
        $.ajax({
            url: "/editlikes.php",
            type: "GET",
            data: {notlikes: id}
        });
        if(page) {
            reloadlikepage();
            reloadhearts();
        }
    }
}
function update_progress() {
    $.ajax({ 
        url: "/progress.php",
        type: "GET",
        success: function(r){ 
                $("#postall").html(r);
        }
    });
}
function noajax() {
    notajax = 1;
}
function colorpicker(pickerdiv, inputdiv) {
    $(pickerdiv).farbtastic(inputdiv);
}
function reloadposts() {
    $.ajax({ 
        url: "/getposts.php",
        type: "GET",
        data: {time: myTimestamp},
        success: function(r){ 
                $("#thecontent").html(r);
                $('a[rel*=facebox]').facebox();
        }
    });
}
function settings(p) {
            document.location.href = "/settings?page=" + p;
}
function friendfollow(userid, username, id) {
    $.ajax({ 
        url: "/addfriend.php",
        type: "GET",
        timeout: 20000,
        data: {time: myTimestamp, pqu: userid},
        success: function(r){ 
                $("#f"+id).html('<div class="smallok">You are now following @'+username+'</div>');
        }
    });
}
function follow(userid, username, id) {
    $.ajax({ 
        url: "/addfriend.php",
        type: "GET",
        timeout: 20000,
        data: {time: myTimestamp, pqu: userid},
        success: function(r){ 
                $("#"+id).append('<div class="smallok">You are now following '+username+'</div>')
                $(".smallok").animate({opacity: 1.0}, 3000).fadeOut('slow', function() {
                                        $(".smallok").remove();
                });
        }
    });
}
function friendnofollow(f,id) {
    $.ajax({ 
        url: "/nofollow.php",
        timeout: 30000,
        type: "POST",
        data: {time: myTimestamp, nofollow: f},
        success: function(r){ 
                $("#f"+id).html('<div class="smallok">You are now following @'+f+' any longer</div>');
        }
    });
}
function nofollow(id, f) {
    $("#f"+id).html(loading);
    $.ajax({ 
        url: "/nofollow.php",
        timeout: 30000,
        type: "POST",
        data: {time: myTimestamp, nofollow: f},
        success: function(r){ 
                $("#f"+id).html('<div class="smallok">You are now following @' + f + ' any longer</div>');
        }
    });
}
function deletepost(f,num, where) {
    if(!confirm("Erase? Are you sure?")) {
        return;
    }
    var old = $("#del"+num).html();
    $('#del'+num).html(loading);
    $.ajax({
        url: "/deletepost.php",
        timeout: 20000,
        data: {f: f},
        success: function(r){
            //document.location.href = "/";
            //$('#results').html(r);
            if(where == 'posts')
              reloadposts();
            else {
             if(num == 0)
                 document.location.href = "/";
             else
                reloadcomments();
           }
        } 
    });

}
function erasegrouppic(group) {
    var old = $("#erasepic").html();
    $("#erasepic").html(loading);
    $.ajax({ 
        url: "/groups",
        type: "GET",
        data: {page: "avatar", erasepic: "1", group: group},
        success: function(r){ 
                $("#profilepic").fadeOut('slow');
                $('#erasepic').html(old);
        }
    });
}
function erasepic() {
    var old = $("#erasepic").html();
    $("#erasepic").html(loading);
    $.ajax({ 
        url: "/settings",
        type: "GET",
        data: {page: "avatar", erasepic: "1"},
        success: function(r){ 
                $("#profilepic").fadeOut('slow');
                $('#erasepic').html(old);
        }
    });
}
function nofriend(f, id) {
    $.ajax({ 
        timeout: 20000,
        url: "/acceptfriend.php",
        type: "GET",
        data: {time: myTimestamp, nofriend: f},
        success: function(r){ 
            $("#"+id).html('<div class="smallok">@' +  f + ' is following you</div>');
        }
    });
}

function acceptfriend(f, id) {
    $.ajax({ 
        timeout: 20000,
        url: "/acceptfriend.php",
        type: "GET",
        data: {time: myTimestamp, friend: f},
        success: function(r){ 
            $("#"+id).html('<div class="smallok">You are now following @' + f +'</div>');
        }
    });
}
function checkmsg() {
    $.ajax({ 
        url: "/getmessage.php",
        type: "GET",
        data: {time: myTimestamp},
        success: function(r){ 
            if(r != 'false') {
              if($("#newfriendmsg").is(":hidden")) {
                $("#newfriendmsg").html(r)
                .slideDown('slow');
              } else {
                $("#newfriendmsg").html(r);
              }
            }
        }
    });
}
function canceladdfriend() {
        $("#addfriendknapp").show();
        $("#addfriendmsg").slideUp('fast');

}
function addfriend(f) {

    if($("#addfriendmsg").is(":hidden")) {
        $("#addfriendknapp").slideUp('fast');
        $("#addfriendmsg").slideDown('slow');
    }
    
}

function myTimestamp() {
  var d = new Date();
  // formated as yyyy-mm-dd HH:MM:ss
  return d.getFullYear() + '-' +
    pad(d.getMonth()+1) + '-' + // because month starts at zero
    pad(d.getDate()) + ' ' +
    pad(d.getHours()) + ':' +
    pad(d.getMinutes()) + ':' +
    pad(d.getSeconds());
}

function pad(n) {
  n = n.toString();
  return n.length == 1 ? '0' + n : n;
}
function showcommentform(id) {
    if($("#commentform"+id).is(":hidden")) {
        $("#commentform"+id).slideDown('fast');
        clearInterval(rp);
    } else {
        $("#commentform"+id).slideUp('fast');
    }
}


function startReload() {
  if(rpreload == 1) {
     rp = setInterval(reloadposts,30000);
  }
  if(rcreload == 1) {
     rc = setInterval(reloadcomments,30000);
  }

}
function stopReload() {
    try {
        clearTimeout(rp);
        rpreload = 1;
    } catch(e) { }
    try {
        clearTimeout(rc);
        rcreload = 1;
    } catch(e) {  }
}

function reloadhearts(bgset) {
        $('.heart').each(function() {
            id = this.id;
            if(readCookie(id) == 1 || bgset == 1) {
                $("#"+id).css("background-image", "url(/images/heart.png)");
            }
        });
}
$(document).ready(function(){

        var posttext = "Write your post..";
        var fileuphtml  = '<input id="fup" onClick="noajax();" name="fil" type="file" class="settingsinput" size="25"/><div class="filedesc">';
        var linkhtml  = '<input name="link" type="text" id="linkpostinput" value="http://"/>';
        var close = 0;
        var hideprofiles = 0;

        reloadhearts();

        $('#moreinfo').click(function() {
            $('#theinfo').slideDown('slow');
            $('#moreinfo').hide();
        });

        $('#newgroup').click(function() {
            $('#newgroup').hide();
            $('#newgrphidden').slideDown('slow');
        });

        $('#showtypes').click(function() {
            $('#showtypes').hide();
            $('#hidetypes').fadeIn('slow');
        });

        $('.nofollowhover').hover(function() {
            $('.followh').remove();
            $('.nofollowh').remove();
            $(this).append('<div class="nofollowh">You are following ' + this.title + '</div>');
        }, function() {
             $('.nofollowh').animate({opacity: 1.0}, 1000).fadeOut('slow', function() { 
                 $('.nofollowh').remove(); 
             });
        });

        $('.followhover').hover(function() {
            $('.followh').remove();
            $('.nofollowh').remove();
            $(this).append('<div class="followh"><a href="javascript:follow(' + this.name + ',\''+this.title+'\',\''+this.id+'\')">F&ouml;lj ' + this.title+ '</a></div>');

            $('.followh').hover(function() {  
                close = 1; 
            }, function() {
                if(close != 0) {
                     $('.followh').animate({opacity: 1.0}, 1000).fadeOut('slow', function() { $('.followh').remove(); });
                     close = 0;
                }
            });

        }, function() {
           if(close != 1) {
             $('.followh').animate({opacity: 1.0}, 1000).fadeOut('slow', function() { 
                    $('.followh').remove(); 
             }); 
           }
        });

        $("#showmore").click(function() {
            $("#showmore").remove();
            $("#showmorerss").slideDown();
        });
        
        $("#textpost").click(function() {
            notajax = 0;
            $("#postinfo").html("");
            /*$('#id_note_body').html(posttext); */
        });

        $('.videoplayer').bind('mouseenter mouseleave',function(){ 
            stopReload();
        });

        $('.mp3player').bind('mouseenter mouseleave',function(){ 
            stopReload();
        });

        $('.youtube').bind('mouseenter mouseleave',function(){ 
            stopReload();
        });

        $("#soundpost").click(function() {
            $("#postinfo").html(fileuphtml + 'Max '+maxup+' and MP3<\/div>').slideDown('fast');
            $('#id_note_body').html("Write music description here..");
        });
        $("#filepost").click(function() {
            $("#postinfo").html(fileuphtml + 'Max '+maxup+'<\/div>').slideDown('fast');
            $('#id_note_body').html("Write filedescription here..");
        });
        $("#imgpost").click(function() {
            $("#postinfo").html(fileuphtml + 'Max '+maxup+' and only JPG, PNG or GIF<\/div>').slideDown('fast');
            $('#id_note_body').html("Write image description here..");
        });
        $("#moviepost").click(function() {
            $("#postinfo").html('<div class="filedesc">YouTube-url:</div>' + linkhtml + '<div class="filedesc">Or upload a movie:</div>'+ fileuphtml + 'Max '+maxup+' and filetype MPG or 3GP<\/div>').slideDown('fast');
            $('#id_note_body').html("Write movie description here..");
        });
        $("#linkpost").click(function() {
            notajax = 0;
            $("#postinfo").html(linkhtml).slideDown('fast');
            $('#id_note_body').html("Write link description here..");
        });

       // setMaxLength();

/*        $('#id_note_body').html(posttext);  */
/*        $("#id_note_body").click( function() { 
                $('#id_note_body').html(""); 
        });  */

//        $('#minknapp').attr("disabled", true); 

        $(".emailinviteclick").click(function() {
            if ($("#emailinvite").is(":hidden")) 
                $("#emailinvite").slideDown("slow");
            else
                $("#emailinvite").slideUp("slow");
        });
        $(".smsinviteclick").click(function() {
            if ($("#smsinvite").is(":hidden")) 
                $("#smsinvite").slideDown("slow");
            else
                $("#smsinvite").slideUp("slow");
        });

        $("#showmore").click(function() {
                $("#showmore").hide();
                $("#more").slideDown("slow");
        }); 

       /* $().ajaxStop(function(r,s){
            $("#contentLoading").fadeOut("fast");
        }); */

        // *****************************************************
        //
        // When logging in from first page
        //
        // *****************************************************
        $('#loginform').submit(function() {
          $("#loginstatus").html(loading);
          var inputs = [];
          $(':input', this).each(function() {
            //inputs.push(this.name + '=' + escape(this.value));
            if (this.name != "") {  // Ignore inputs without a name
                    if ((this.type == "radio" || this.type == "checkbox") && !this.checked) {
                        // ignore this item
                    } else {
                        //inputs[this.name] = this.value;
                        inputs.push(this.name + '=' + escape(this.value));
                    }
            }

          });

          // now if I join our inputs using '&' we'll have a query string
          jQuery.ajax({
            data: inputs.join('&'),
            url: this.action,
            type: "POST",
            timeout: 15000,
            error: function() {
                  $('#loginstatus').html("<div class='myerror'>timeout..</div>");
            },
            success: function(r) {
                if(r == 'false') 
                  $('#loginstatus').html("<div class='myerror'>Wrong E-mail or Password!</div>")
                  .fadeIn('slow')
                  .animate({opacity: 1.0}, 3000)
                  .fadeOut('slow', function() {
                    $(this).html("");
                  });
                else {
                  //$("#loginform")[0].submit();
                  // Login function returns url
                  document.location.href = "http://" + r;
               }
            }
          });
          return false;
        });

        // *****************************************************
        // 
        // Adding feeds
        //
        // *****************************************************
        $('#rssform').submit(function() {
          $('#rssknapp').attr("disabled", true);
          $('#rss_status').fadeIn('slow');
          $('#rss_status').html(loading);
          var inputs = [];
          $(':input', this).each(function() {
            //inputs.push(this.name + '=' + escape(this.value));
            if (this.name != "") {  // Ignore inputs without a name
                    if ((this.type == "radio" || this.type == "checkbox") && !this.checked) {
                        // ignore this item
                    } else {
                        //inputs[this.name] = this.value;
                        inputs.push(this.name + '=' + escape(this.value));
                    }
            }

          });

          // now if I join our inputs using '&' we'll have a query string
          jQuery.ajax({
            data: inputs.join('&'),
            url: this.action,
            timeout: 15000,
            error: function() {
              $('#rssknapp').attr("disabled", false);
              $("#rss_status").text("");
              $('#rss_status').html("<div class='bigerror'>Felaktig URL eller s&aring; kan ej RSS-feed hittas</div>")
              .fadeIn('slow')
              .animate({opacity: 1.0}, 6000)
              .fadeOut('slow', function() {
                  $("#rss_status").text("");
              });
            },
            success: function(r) {
                if(r != "false") {
                  $('#rssknapp').attr("disabled", false);
                  $('input[@name=url]').val(''); 
                  $('#rss_status').html("<div class='ok'>" + r + "</div>")
                  .fadeIn('slow')
                  .animate({opacity: 1.0}, 4000)
                  .fadeOut('slow', function() {
                    $("#rss_status").text("");
                  }); 
                  $('#feededit').html(loading);
                  $('#feededit').load("/feedlist.php");

                } else {
                  $('#rssknapp').attr("disabled", false);
                  $('#rss_status').html("<div class='bigerror'>Felaktig URL eller s&aring; kan ej RSS-feed hittas</div>")
                    .fadeIn('slow')
                    .animate({opacity: 1.0}, 4000)
                    .fadeOut('slow', function() {
                        $('#rss_status').text("");
                  });
                    
                }
            }
          });

          return false;
        });

        // *****************************************************
        //
        // New user signup form
        //
        // *****************************************************
        $('#signupform').submit(function() {
          $('#status').html("");
          $("#contentLoading").show();
          var inputs = [];
          $(':input', this).each(function() {
            //inputs.push(this.name + '=' + escape(this.value));
            if (this.name != "") {  // Ignore inputs without a name
                    if ((this.type == "radio" || this.type == "checkbox") && !this.checked) {
                        // ignore this item
                    } else {
                        //inputs[this.name] = this.value;
                        inputs.push(this.name + '=' + escape(this.value));
                    }
            }

          });

          // now if I join our inputs using '&' we'll have a query string
          jQuery.ajax({
            data: inputs.join('&'),
            url: this.action,
            type: "POST",
            timeout: 20000,
            error: function() {
               $('#status').hide();
               $('#status').css({border: "1px solid #c00"});
               $('#status').css({background: "#ffebe8"});
               $('#status').css({padding: "10px"});
               $('#status').html("Fel: Beg&auml;ran gjorde timeout!").slideDown('slow')
               .animate({opacity: 1.0}, 4000)
               .fadeOut('slow', function() {
                   $(this).hide();
               });
            },
            success: function(r) {
                if(r == "true") {
                    $('#helasidan').slideUp("fast");
                    if ($("#alltok").is(":hidden")) {
                        $('#alltok').show();
                    }
                } else {
                    $('#status').hide();
                    $('#status').html(r).slideDown('slow')
                    .animate({opacity: 1.0}, 4000)
                    .fadeOut('slow', function() {
                        $(this).hide();
                    });
                }
            }
          });

          return false;
        });


        // *****************************************************
        //
        // Facebox popups
        //
        // *****************************************************
        $('a[rel*=facebox]').facebox();

        // *****************************************************
        //
        // New user signup validator
        //
        // *****************************************************

        var validator = $("#signupform").validate({
        rules: {
            url: {
                required: true,
                minlength: 2,
                remote: "urlverify.php"
            },
            epost: {
                required: true,
                email: true,
                remote: "emailverify.php"
            },
            password1: {
                required: true,
                rangeLength:[5,30]
            },
            password2: {
                required: true,
                equalTo: "#password1"
            },
            terms: "required"
        },
        // the errorPlacement has to take the table layout into account
        errorPlacement: function(error, element) {
            if ( element.is(":radio") )
                error.appendTo( element.parent().next().next() );
            else if ( element.is(":checkbox") )
                error.appendTo ( element.next() );
            else
                error.appendTo( element.parent().next() );
        },
        // set this class to error-labels to indicate valid fields
        success: function(label) {
            // set &nbsp; as text for IE
            label.html("&nbsp;").addClass("checked");
        }
    });
    $('.btn').each(function(){
        var b = $(this);
        var tt = b.text() || b.val();
        if ($(':submit,:button',this)) {
            b = $('<a>').insertAfter(this). addClass(this.className).attr('id',this.id);
            $(this).remove();
        }
        b.text('').css({cursor:'pointer'}). prepend('<i></i>').append($('<span>').
        text(tt).append('<i></i><span></span>'));
    });


        // Update passsword
        $('#passwordform').submit(function() {

          var old = $("#settingsknapp").html();
          $("#settingsknapp").html(loading);

          if(notajax == 1)
              return true;

          var inputs = [];

          $(':input', this).each(function() {
            if (this.name != "") {
                    if ((this.type == "radio" || this.type == "checkbox") && !this.checked) {
                        inputs.push(this.name + '=' + "off");
                    } else {
                        inputs.push(this.name + '=' + escape(this.value));
                    }
            }

          })

          // now if I join our inputs using '&' we'll have a query string
          jQuery.ajax({
            data: inputs.join('&'),
            url: this.action,
            timeout: 10000,
            type: "POST",
            error: function() {
               $("#settingsknapp").html(old);
               $('#settingsstatus').css({border: "1px solid #c00"});
               $('#settingsstatus').css({background: "#ffebe8"});
               $('#settingsstatus').css({padding: "10px"});
                 $('#settingsstatus').html("Fel: Beg&auml;ran gjorde timeout!").slideDown('slow')
                .fadeIn('slow')
               .animate({opacity: 1.0}, 4000)
               .fadeOut('slow', function() {
                   $(this).hide();
               });

            },
            success: function(r) {

               /*if($('#settingsstatus').is(":hidden")) {
                    $('#settingsstatus').show();
               } */

               $('#settingsstatus').slideUp('fast');
               $("#settingsknapp").html(old);
               $('#settingsstatus').html(r).slideUp('slow')
               .fadeIn('slow')
               .animate({opacity: 1.0}, 4000)
               .fadeOut('slow', function() {
                   $(this).hide();
                }); 
            }
          })

          return false;
        });

        //
        // Save settings
        //
        $('#settingsform').submit(function() {
          var old = $("#settingsknapp").html();
          $("#settingsknapp").html(loading);
          var inputs = [];

          $(':input', this).each(function() {
            //inputs.push(this.name + '=' + escape(this.value));
            if (this.name != "") {  // Ignore inputs without a name
                    if ((this.type == "radio" || this.type == "checkbox") && !this.checked) {
                        // ignore this item
                    } else {
                        //inputs[this.name] = this.value;
                        inputs.push(this.name + '=' + escape(this.value));
                    }
            }

          })

          // now if I join our inputs using '&' we'll have a query string
          jQuery.ajax({
            data: inputs.join('&'),
            url: this.action,
            timeout: 10000,
            error: function() {
               $("#settingsknapp").html(old);
               $('#settingsstatus').css({border: "1px solid #c00"});
               $('#settingsstatus').css({background: "#ffebe8"});
               $('#settingsstatus').css({padding: "10px"});
                 $('#settingsstatus').html("Fel: Beg&auml;ran gjorde timeout!").slideDown('slow')
                .fadeIn('slow')
               .animate({opacity: 1.0}, 4000)
               .fadeOut('slow', function() {
                   $(this).hide();
               });

            },
            success: function(r) {
               $('#settingsstatus').slideUp('fast');
               $("#settingsknapp").html(old);
               $('#settingsstatus').html(r).slideUp('slow')
               .fadeIn('slow')
               .animate({opacity: 1.0}, 4000)
               .fadeOut('slow', function() {
                   $(this).hide();
                }); 
            }
          })

          return false;
        });

        //
        // Send comment
        //
        $('#commentf').submit(function() {
          var old = $("#commentknapp").html();
          $("#commentknapp").html(loading);
          var inputs = [];
          $('#textareac').attr("disabled", true);

          $(':input', this).each(function() {
            //inputs.push(this.name + '=' + escape(this.value));
            if (this.name != "") {  // Ignore inputs without a name
                    if ((this.type == "radio" || this.type == "checkbox") && !this.checked) {
                        // ignore this item
                    } else {
                        //inputs[this.name] = this.value;
                        inputs.push(this.name + '=' + encodeURIComponent(this.value));
                    }
            }

          })

          // now if I join our inputs using '&' we'll have a query string
          jQuery.ajax({
            type: "POST",// Needed for longer comments
            data: inputs.join('&'),
            url: this.action,
            timeout: 20000,
            error: function(r) {
                $('#textareac').attr("disabled", false);
                document.forms['commentf'].reset();
                $('#commentknapp').html(old);
                reloadcomments();
            },
            success: function(r) {
                $('#textareac').attr("disabled", false);
                document.forms['commentf'].reset();
                $('#commentknapp').html(old);
                reloadcomments();
            }
          })

          return false;
        });

        //
        // Friend add/request
        //
        $('#addfriendform').submit(function() {
          var old = $("#addfriendsub").html();
          $("#addfriendsub").html(loading);
          var inputs = [];

          $(':input', this).each(function() {
            if (this.name != "") {  // Ignore inputs without a name
                    if ((this.type == "radio" || this.type == "checkbox") && !this.checked) {
                        // ignore this item
                    } else {
                        inputs.push(this.name + '=' + escape(this.value));
                    }
            }

          })

          // now if I join our inputs using '&' we'll have a query string
          jQuery.ajax({
            data: inputs.join('&'),
            url: this.action,
            timeout: 20000,
            error: function() {
               $("#addfriendsub").html(old);
               $('#addfriendstatus').css({border: "1px solid #c00"});
               $('#addfriendstatus').css({background: "#ffebe8"});
               $('#addfriendstatus').css({display: "block"});
               $('#addfriendstatus').css({padding: "10px"});
                 $('#addfriendstatus').html("Fel: Beg&auml;ran gjorde timeout!").slideDown('slow')
                .fadeIn('slow')
               .animate({opacity: 1.0}, 4000)
               .fadeOut('slow', function() {
                   $(this).hide();
               });

            },
            success: function(r) {
               $('#addfriendmsg').slideUp('fast');
               $('#addfriendstatus').html(r).slideUp('slow')
               .fadeIn('slow')
               .animate({opacity: 1.0}, 4000)
               .fadeOut('slow', function() {
                   $(this).hide();
                }); 
            }
          })

          return false;
        });

        //
        // New post handeling
        //
        $('#postform').submit(function() {
          var inputs = [];
          var up;

          $("#status").html("");

          if($("#loadingdiv").is(":hidden")) 
              $("#loadingdiv").slideDown('fast');

          $('#minknapp').attr("disabled", true);

          if(notajax == 1) { // File upload
              setInterval("update_progress()", 2000);  // Update progressbar every 2 sec
              return true;
          }
          $('#id_note_body').attr("disabled", true);

          $(':input', this).each(function() {
            if (this.name != "") {  // Ignore inputs without a name
                    if ((this.type == "radio" || this.type == "checkbox") && !this.checked) {
                        // ignore this item
                    } else {
                        inputs.push(this.name + '=' + encodeURIComponent(this.value));
                        //inputs.push(this.name + '=' + escape(this.value));
                    }
            }

          })

          jQuery.ajax({
            data: inputs.join('&'),
            type: "POST",// Needed for longer posts?! Added 2009-01-26
            url: this.action,
            timeout: 40000,
            error: function() {
               $("#loadingdiv").slideUp("fast");
               $('#status').html('<div class="bigerror">Fel: Beg&auml;ran gjorde timeout<\/div>').slideDown('slow')
               .fadeIn('slow')
               .animate({opacity: 1.0}, 3000)
               .fadeOut('slow', function() {
                   $(this).hide();
               });
               $('#minknapp').attr("disabled", false);
               $('#id_note_body').attr("disabled", false);
               window.setTimeout(function() {
                    $('#status').slideUp('slow');
                }, 4000);
            },
            success: function(r) {
               reloadposts();
               $('#id_note_body').attr("disabled", false);
               if(r != 'true') {
                 $("#loadingdiv").slideUp("fast");
                 $('#status').html('<div class="bigerror">' + r + '<\/div>').slideDown('slow')
                 .fadeIn('slow')
                 .animate({opacity: 1.0}, 3000)
                 .fadeOut('slow', function() {
                   $(this).hide();
                 });
                 $('#minknapp').attr("disabled", false);
               } else { // OK allt gick bra!
                 $("#loadingdiv").hide("fast");
                 $('#status').hide();
                 start = 0;
                 $('#status').html('<div class="ok">Inl&auml;gg skapat!<\/div>').slideDown('slow');
                 document.forms['postform'].reset(); // reset postform
                 $('#id_note_body').html("");
                 $('#minknapp').attr("disabled", true);
                 window.setTimeout(function() {
                    $('#status').slideUp('slow');
                 }, 3000);
                 //document.getElementsByTagName('textarea')[0].onkeyup();
               }
            }
          })

          return false;
        })

});
