// Display login box on click
$(function() {
     $("a#login").toggle(function() {
         $("form#loginbox").slideDown("slow");
         return false;
     }, function() {
         $("form#loginbox").slideUp("slow");
         return false;
     });

     $("form#loginbox").submit(function() {
         if ($("input[name='username']").val() == '') {
             $("input[name='password']").css("border","1px solid green");
             $("span#ajaxmsg").children("h3").text("Please fill in the username field.").end().show();
             $("input[name='username']").css("border","1px solid red");
             return false;
         } else if ($("input[name='password']").val() == '') {
             $("input[name='username']").css("border","1px solid green");
             $("span#ajaxmsg").children("h3").text("Please fill in the password field.").end().show();
             $("input[name='password']").css("border","1px solid red");
             return false;
         } else {
             $("input[name='password']").css("border","1px solid green");
             $("input[name='username']").css("border","1px solid green");
             $("span#ajaxmsg").children("h3").text("Loading...Please wait...").end().show();
             $.ajax({
                 data: { 
                           username: $("input[name='username']").val(),
                           password: $("input[name='password']").val(),
                           callback: 'callback'
                       },
                 dataType: "text",
                 type: "POST",
                 url: "http://modbase.generationstudio.net/ajax/login.php",
                 success: function(callback) {
                         if (callback == "Username") {
                         $("span#ajaxmsg").children("h3").text("Your username is invalid. You must register before logging in!").end().show();
                         $("input[name='username']").css("border","1px solid red");
                         };
                         if (callback == "Password") {
                         $("span#ajaxmsg").children("h3").text("Your password isn't correct. Is caps lock on?").end().show();
                         $("input[name='password']").css("border","1px solid red");
                         };
                         if (callback == "Success") {
                         window.location.reload(true);
                         }
                 }
             }); 
        }; return false;
    });
    $("a[href$='logout.php']").click(function() {
        $("span#ajaxmsg").children("h3").text("Loading...Please wait...").end().show();
        $.ajax({
            url: "http://modbase.generationstudio.net/ajax/logout.php",
            success: function() {
                window.location.reload(true);
            }
        });
        return false;
    });
    $("form#modsearch").submit(function() {
        if ($('input[name="search"]').val() != '') {
             $('div#csb').slideUp("fast");
             $("div#columnsecond").text("Loading...Please wait...");
             $.ajax({
                 data: { 
                           search: $('input[name="search"]').val()
                       },
                 dataType: "html",
                 type: "GET",
                 url: "http://modbase.generationstudio.net/ajax/modsearch.php",
                 success: function(callback) {
                         if (callback == "None") {
                             $('div#columnsecond').text("Sorry, no results found. Make sure you haven't abbreviated words; instead of searching for \"rep\" search for Reputation.");
                         } else {
                             $('div#columnsecond').html(callback);
                         }
                 }
             });
        } else {             $("input[name='search']").css("border","1px solid red"); }; return false;
    });
var clickx = function() {
        var b = $(this);
        var c = $(this).attr('href').split("browse/")[1].split("/")[0];
        $.ajax({
                 beforeSend: function() {
        $('div#csb').slideUp("fast");
        $("div#columnsecond").text("Loading...Please wait...");
  },
                 dataType: "html",
                 type: "GET",
                 url: "http://modbase.generationstudio.net/mods/"+b.attr('href')+"?page=1&cat="+c,
                 success: function(callback) {
                         if (callback == "None") {
                             $('div#columnsecond').text("Sorry, no results for this category yet.");
                         } else {
                             $('div#columnsecond').html(callback);
                         }
                  $("a[href^='browse/']").unbind(); $("a[href^='browse/']").bind("click",clickx); 
                 }
        });
        return false;
};

var focusx = function(event) {
        var x = event.data.dis;
        var c = x.val();
        x.val('');
        x.unbind();
        setTimeout(function() {
            if(x.val() == '') {
                x.val(c);
                x.bind("focus",{dis: x},focusx);
        };
        },"5000");
    };

    $("a[href^='browse/']").bind("click",clickx);
    $("textarea").each(function(i) { var e = []; e[i] = $(this); e[i].bind("focus",{dis: e[i]},focusx)});
});
