var navVisible = false;
var navOver = false;

$(document).ready(function() {
    // Why is this not working?
    $('.autoclear').focus(function() {
        if(!$(this).is('.changed')) {
            if($(this).is('input')) {
                if($(this).is('.autoclear-password')) {
                    //$(this).attr('type', 'text');
                    $(this)[0].setAttribute('type', "text");
                }
                $(this).attr('oldValue', $(this).val());
                $(this).val('');
            } else if ($(this).is('textarea')) {
                $(this).attr('oldValue', $(this).html());
                $(this).html('');
            }
        }
    });
    $('.autoclear').keypress(function() {
        if(!$(this).is('.changed')) {
            if($(this).is('.autoclear-password')) {
                //$(this).attr('type', 'password');
                $(this)[0].setAttribute('type', "password");
            }
            $(this).addClass('changed');
        }
    });
    $('.autoclear').blur(function() {
        if($(this).is('input')) {
            if(!$(this).is('.changed') || $(this).val() == '') {
                if($(this).is('.autoclear-password')) {
                    $(this)[0].setAttribute('type', "text");
                }
                $(this).removeClass('changed');
                $(this).val($(this).attr('oldValue'));
            }
        } else if ($(this).is('textarea')) {
            if(!$(this).is('.changed') || ($(this).html() == '' && $(this).val() == '')) {
                $(this).removeClass('changed');
                $(this).html($(this).attr('oldValue'));
            }
        }
    });

    $('#holding-register-form').submit(function() {
        if($('#register-email').val() == $('#register-email-2').val()) {
            $.post('/register.php',
                {email: $('#register-email').val()},
                function() {
                    $('#holding-register-form').fadeOut();
                    $('#holding-register-thanks').fadeIn();
                }
            );
        } else {
            alert("The email addresses you have entered do not match.");
        }
        return false;
     });

    $('.nav-box a.primary').click(function() {
        var parentObj = $(this).parent();
        parentObj.toggleClass('expanded-nav-box');
        $('.expanded-nav-box:not(#'+parentObj.attr('id')+')').removeClass('expanded-nav-box');

        if($('.expanded-nav-box').length) navVisible = true;
        else navVisible = false;
    });

    $('.nav-box').livequery('mouseover', function() { navOver = true; });
    $('.nav-box').livequery('mouseout', function() { navOver = false; });

    $('body').click(function() {
        if(navVisible && !navOver) {
            $('.expanded-nav-box').removeClass('expanded-nav-box');
            navVisible = false;
        }
    });

    $('#show-register-link').click(function() {
            $('#login-div').fadeOut();
            $('#register-div').fadeIn();
        });

    $('#show-login-link').click(function() {
            $('#login-div').fadeIn();
            $('#register-div').fadeOut();
        });
    
    $('#commentform').submit(function() {
            var errors = new Array();
            if($('#commentform input[@name=author]').length) {
                if(!$('#commentform input[@name=author]').val() || 
                    $('#commentform input[@name=author]').val() == "Name (required)") {
                    errors.push('You need to enter your name');
                }
                if(!$('#commentform input[@name=email]').val()) {
                    errors.push('You need to enter your email address');
                } else if(!checkEmail($('#commentform input[@name=email]').val())) {
                    errors.push('You need to enter a valid email address');
                }
            }
            if(!$('#commentform textarea').val() || 
                $('#commentform textarea').val() == "Your comment") {
                errors.push('You need to enter a comment');
            }
            if(errors.length) {
                var errorString = "";
                for(var i=0; i<errors.length; i++) {
                    errorString += errors[i] + "\n";
                }
                alert(errorString);
                return false;
            } else {
                if($('#commentform input[@name=url]').val() == "Website") {
                    $('#commentform input[@name=url]').val('');
                }
            }
            return true;
        });
});

function checkEmail(email) {
    var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
    if (!filter.test(email)) {
        return false;
    }
    return true;
}
