$(document).ready(function(){
    // Align main with contact if available
    mainAlign = $('#main');
    
    if (mainAlign.length > 0) {
        var contactContainer = $('#wrap > section > aside > .contact');

        if (mainAlign.height() > contactContainer.height()) {
            contactContainer.height(mainAlign.height());
        } else {
            mainAlign.height(contactContainer.height() + 20);
        }
    }
    
    // Add hover/active state to image buttons
    $('input[type="image"]').bind('mouseenter', function(event){
        $(this).attr('src', $(this).attr('src').match(/^(.*)-(default|hover|active)/)[1] + '-hover.png');
    }).bind('mouseleave', function(event){
        $(this).attr('src', $(this).attr('src').match(/^(.*)-(default|hover|active)/)[1] + '-default.png');
    }).bind('mousedown', function(event){
        $(this).attr('src', $(this).attr('src').match(/^(.*)-(default|hover|active)/)[1] + '-active.png');
    }).bind('mouseup', function(event){
        $(this).attr('src', $(this).attr('src').match(/^(.*)-(default|hover|active)/)[1] + '-hover.png');
    });
    
    // Contact form
    $('input[data-placeholder], textarea[data-placeholder]').bind('focus', function(event){
        if ($(this).val() == $(this).attr('data-placeholder')) {
            $(this).val('');
        }
    }).bind('blur', function(event){
        if ($(this).val() == '') {
            $(this).val($(this).attr('data-placeholder'));
        }        
    });
    
    $('#contact-form').bind('submit', function(event){
        $(this).find('input[data-placeholder], textarea[data-placeholder]').each(function(){
            if ($(this).val() == $(this).attr('data-placeholder')) {
                $(this).val('');
            }
        });
    });
    
    $('#form-captcha-row > img').bind('click', function(event){       
        var uri = window.location.href;
        var img = $(this);
        
        $.post(uri, {newCaptcha: true}, function(data){
            img.attr('src', data.captchaUri);
            img.next('input[type="hidden"]').val(data.captchaId);
        });
    });
});

