$(document).ready(function(){
    // Link teaser boxes
    $('#teaser > div').bind('click', function(){
        window.location.href = $(this).find('a.more').attr('href');
    });
    
    // Adding column support for browsers which do not support this natively.
    // This is primarly intended for IE9, as IE10 will support this property.
    // Only works for lists.
    if (
        !('MozColumnCount' in document.body.style)
        && !('WebkitColumnCount' in document.body.style)
        && !('OColumnCount' in document.body.style)
        && !('MsColumnCount' in document.body.style)
        && !('columnCount' in document.body.style)
    ) {
        $('ul.two-column-list').each(function(){
            var currentColumn = $(this);
            var container     = $('<div/>');
            var secondColumn  = $('<ul/>');
            var totalHeight   = 0;
            var currentHeight = 0;
            var lis           = currentColumn.find('li');
            
            lis.each(function(){
                totalHeight += $(this).height();
            });
            
            var halfHeight = totalHeight / 2;
            
            lis.each(function(){
                var li = $(this);
                
                if (currentHeight >= halfHeight) {
                    li.detach();
                    li.appendTo(secondColumn);
                }
                
                currentHeight += li.height();
            });
            
            currentColumn.before(container);
            currentColumn.detach();
            
            container.append(currentColumn)
                     .append(secondColumn);
            
            currentColumn.css('float', 'left')
                         .css('width', '457px');
            secondColumn.css('float', 'left')
                        .css('width', '457px');
                        
            container.height(Math.max(currentColumn.height()));
        });
    }
    
    // Forms
    $('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-submit').bind('mouseenter', function(event){
        $(this).attr('src', $(this).attr('src').match(/^(.*)\-(default|hover)/)[1] + '-hover.png');
    }).bind('mouseleave', function(event){
        $(this).attr('src', $(this).attr('src').match(/^(.*)\-(default|hover)/)[1] + '-default.png');
    });
    
    $('#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);
        });
    });
    
    $('#submit').bind('click', function(event){       
        $('form').submit();
    });
});

