jQuery.noConflict();
jQuery(document).ready(function() {
    jQuery('.status_alert').hide();
    jQuery('.mystatus_loading').hide();

    //if submit button is clicked
    jQuery('#submit').click(function() {

        //Get the data from all the fields
        var status = jQuery('input[name=status]');
        var uid = jQuery('input[name=uid]');

        //Simple validation to make sure user entered something
        //If error found, add hightlight class to the text field
        if (status.val() === '') {
            status.addClass('invalid_field');
            return false;
        } else {
            status.removeClass('invalid_field');
        }


        //organize the data properly
        var data = 'uid=' + uid.val() + '&status=' + status.val();

        //disabled all the text fields
        //jQuery('.text').attr('disabled','true');
        //show the loading sign
        jQuery('.validation_loading').show();

        //start the ajax
        jQuery.ajax({
            //this is the php file that processes the data and send mail
            url: "process-mystatus.php",

            //GET method is used
            type: "GET",

            //pass the data            
            data: data,

            //Do not cache the page
            cache: false,

            //success
            success: function(html) {
                //if process.php returned 1/true (send mail success)
                if (html == 1) {

                    //Fanciness - update the current status on change without pulling it from the Database... yes, tricksy hobitses
                    jQuery('.recentstatuses').prepend('<tr><td class="trow2" colspan="2"><div class="showstatus" id="currentstatus">' + status.val().substring(0,140) + '</div><span class="smalltext right">Just a second ago...</td></tr>');

                    //empty the text box
                    jQuery('.text').attr('value', '');

                    //re-hide the loading symbol
                    jQuery('.mystatus_loading').hide();


                    //if process.php returned 0/false (db update failure)
                } else {
                    alert('Sorry, unexpected error. Please try again later.');
                }

            }
        });

        //cancel the submit button default behaviours
        return false;
    });
});
