﻿
$(document).ready(function() {
     newsAccordion3();
});

/*function loadNews() {
    $('.rightcolumn_index').load("/Press/pressAndNews.aspx .news0:lt(5)",newsAccordion3);    
}*/

function newsAccordion3() {

    $('.content_box0 .news0').attr({ status: "closed" });
    $('.content_box0 .news').attr({ status: "opened" });

    $('.news0 .show').click(test);
    setFirstElementHight();
}


function setFirstElementHight() {

    $('.news0:first').css({'borderTop':'0'});
    var headerHeight = $('.news0:first').find('h1').outerHeight(true);
    var textHeight = 0;
    var totalHeight = 0;
    var imgHeight = 0;

    $('.news0:first').find('p').each(function() {
        textHeight += $(this).outerHeight(true);
    });
    
    textHeight = textHeight + 50;
    totalHeight = headerHeight + textHeight;
    if (totalHeight > imgHeight) {
        $('.news0:first').animate({ height: totalHeight + 'px' }, 200);
    }
    else {
        $('.news0:first').animate({ height: imgHeight + 'px' }, 200);
    }
    $('.head').find('.arrow a').addClass('on');
}

function johaniscracy() {

    $('.news0').animate({ height: '36px' }, 300);
    $('.news0').attr({ status: "closed" });
}

function test() {
    //om title är closed kör öppna-funktionen
    
    if ($(this).parents('.news0').attr("status") == "closed") {
        johaniscracy();
        var headerHeight = $(this).parents('.news0').find('h1').outerHeight(true);
        var textHeight = 0;
        var totalHeight = 0;
        var imgHeight = 0;

        $(this).parents('.news0').find('p').each(function() {
            textHeight += $(this).outerHeight(true);
        });

        textHeight = textHeight + 50;
        totalHeight = headerHeight + textHeight;
        $('.arrow a').removeClass();
        $('.arrow a').addClass('off');

        if (totalHeight > imgHeight) {
            $(this).parents('.news0').animate({ height: totalHeight + 'px' }, 200);
        }
        else {
            $(this).parents('.news0').animate({ height: imgHeight + 'px' }, 200);
        }
        $(this).parents('.head').find('.arrow a').addClass('on');
        $(this).parents('.news0').attr({ status: "opened" });
    }
    //om inte kör stäng funktion
    else {
        $(this).parents('.news0').animate({ height: '36px' }, 300);
        $(this).parents('.news0').attr({ status: "closed" })
        return false;
    }
}

function ajaxCall(url) {
    $.post(url+"?showHead=false", {},callback);
}

function callback(data) {
    $(".content").replace(data);
}