jQuery.fn.fadeToggle = function(speed, easing, callback) {
    return this.animate({opacity: 'toggle'}, speed, easing, callback);
};

jQuery.fn.inputLabel = function() {
    var label = this.val();
    $(this).click(function(){
        if( $(this).val() == $.trim(label)) {
            $(this).val('');
        }
        $(this).removeClass('error').parent('div.form-item').addClass('form-item-focus');
    });

    $(this).blur(function(){
        if( $.trim($(this).val()).length == 0) {
            $(this).val(label);
        }
        $(this).parent('div.form-item').removeClass('form-item-focus');
    });
}

$(document).ready(function(){

    // Fontes cufon
    Cufon.replace('h1')('h2')('#rsmtext')('#rsmla')('#rsmny')('#email')
 ('#email a', { hover: true });

    $('a[href!=#]').click( function() {
        window.open(this.href);
        return false;
    });

    // Fade in/out no menu
    $('#email a')
        .mouseover(function(){
            $(this).stop().animate(
                {opacity: 0.5}, 
                {duration: 300})
            })
        .mouseout(function(){
            $(this).stop().animate(
                {opacity: 1}, 
                {duration: 300})
            });

    // Caixa de contacto
    $('#footer li.contact a, #contact #close').click(function(){
        $('#footer li.contact').toggleClass('contact-active');
        $('#contact').fadeToggle(250);
        if($('#message:visible').length > 0) {
            $('#message').css({ display: 'none' });
            $('form').show();
            $('input.form-text, textarea').val('').blur();
        }
        return false;
    });

    // Labels no formulario
    $('form div.form-item').each(function(){
        $('input, textarea', this).val( $('label', this).text() ).inputLabel();
    });

    // Validacao e envio do formulario
    $('#contact form').validate({
        rules: {
            name:    { required: true, minlength: 3 },
            email:   { required: true, email: true },
            message: { required: true, minlength: 10 }
        },
        messages: {
            name: '',
            email: '',
            message: ''
        },
        submitHandler: function () {
            $('form').hide();
            $('#loading').show();
            $.post('inc/mailme.php', {
                subject: 'Sent from site',
                name: $('input[name=name]').val(),
                email: $('input[name=email]').val(),
                message: $('textarea[name=message]').val(),
                key1: $('input[name=key1]').val(),
                key2: $('input[name=key2]').val()
            },
    
            function (a) {
                $('#loading').css({ display: 'none' });
                if (a == 'success') {
                    $('#message').removeClass().addClass('success').fadeIn();
                } else {
                    $('#message').removeClass().addClass('error').fadeIn();
                }
            })
        }
    });

    // Define dimensoes da caixa de contacto no IE6
    if ($.browser.msie && $.browser.version < 7) {
        $('#contact')
            .width( $('#content').width() - 54 )
            .height( $('#content').height() - 62 );
    }

    // Anti spam
    var spamkey = Math.random() * 999 / 0.13;
    $('input[name=key1]').val(spamkey);
    $('input[name=key2]').val(spamkey.toString().length);
});