$(function() {

    $('#newsletter_form').submit(function(e) {
        e.preventDefault();
        $('#newsletter_submit').attr('disabled', 'disabled');
        $('#loader').show();

        email = $('#newsletter_field', this).val();

        var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
        if(reg.test(email) == false) {
            $('#newsletter').append('<div class="error"><span>Felaktig</span></div>');
            $('#loader').hide();
            $('#newsletter_submit').removeAttr('disabled');
            return false;
        }

        $.post('/', { "subscriber_email": email, "js": "true" },
             function(data){
               if (data.message=='OK') {
                    $('#loader').hide();
                    $('#newsletter_message').fadeIn().html('<h3>Tack för ditt intresse!</h3>').delay(3000).fadeOut();
               }
             }, "json");
        
         $('input[type=text]', this).val('');
            
    return false;
    });

    $('#newsletter_field').focus(function() {
        $('#newsletter .error').fadeOut('slow', function() {
            $(this).remove();
          });
        $('#newsletter_submit').removeAttr('disabled');
    });

    $('#searchform').submit(function(e) {
        e.preventDefault();
        word = $('#searchfield', this).val().replace(/\//g,'')
        var search = '/search/' + encodeURIComponent(word) + '/';
        location.href = search;
        return false;
    });

    $('.story.wide').each(function(index) {
        var h = $(this).height();        
        if (h > 170) {
            $(this).find('.post_image').css('height',h);
        }
    });
    
    var m = 0;
    var c = 0;
    $('#left .story.medium').each(function () {
        if (c == 2){
            m = 0;
        }
        if ($(this).height() > m ){
            m = $(this).height();
        }
        
        if ($(this).height() < m ){
            $(this).css('height',m);
        }
        c++;
    });

    jQuery.fn.reverse = [].reverse;
    var l = 0;
    $('#left .block.latest').reverse().each(function () {
        if ($(this).height() > l ){
            l = $(this).height();
        }
        $(this).css('height',l);
    });

});
