var itb_prev = null;

function createCookie(name,value,days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        var expires = "; expires="+date.toGMTString();
    }
    else var expires = "";
    document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}

function eraseCookie(name) {
    createCookie(name,"",-1);
}

function itb_set(name, value) {
    document.getElementById(name)
        .style.display = value ? 'block' : 'none';
    document.getElementById(name + '_tab')
        .className = value ? 'active' : 'inactive';
}

function itb_switch(name) {
    if (itb_prev) {
        itb_set(itb_prev, false);
        itb_set(name, true);
        itb_prev = name;
        createCookie('itb', name, 365);
    }
}

function news_in(comtxt, compic) {
    $('#'+comtxt).css('color', '#f00');
    $('#'+compic).attr('src', '/images/com2.gif');
}

function news_out(comtxt, compic) {
    $('#'+comtxt).css('color', '#cdcdcd');
    $('#'+compic).attr('src', '/images/com.gif');
}

function show_comment(cid, value) {
    var scom = document.getElementById('scom_' + cid);
    var hcom = document.getElementById('hcom_' + cid);
var scom_was_visible = scom.style.display != 'none';
try {
    scom.style.display = value ? 'table' : 'none';
} catch(e) {
    scom.style.display = value ? 'block' : 'none';
}
    hcom.style.display = value ? 'none' : 'block';

    var scom_hide = document.getElementById('scom_hide_' + cid);
if (scom_hide && !scom_was_visible) {
    scom_hide.style.display = 'inline';
}
}

function rate_comment(cid, add_rating) {
    // Sende Anfrage zum Server...
    $.ajax({url: 'news/rate_comment.php?cid=' + cid + '&rate=' + add_rating});

    // Rating-Buttons ausgrauen...
    var voteup      = document.getElementById('vote_up_' + cid);
    var votedown    = document.getElementById('vote_down_' + cid);
    var votedown100 = document.getElementById('vote_down100_' + cid);

    if (voteup) {
        voteup.src = "/images/vote_up_gray.gif";
        voteup.onclick = null;
        voteup.style.cursor = "none";
    }
    if (votedown) {
        votedown.src = "/images/vote_down_gray.gif";
        votedown.onclick = null;
        votedown.style.cursor = "none";
    }
    if (votedown100) {
        votedown100.src = "/images/vote_down_gray.gif";
        votedown100.onclick = null;
        votedown100.style.cursor = "none";
    }

    // Beitrag ausblenden wenn negativ bewertet...
    show_comment(cid, add_rating > 0);
}

function updateCountdown() {
    var div = document.getElementById('index_countdown');
    if (div) {
        var now = new Date();
        var rel = new Date(2010, 01, 25, 24, 0);
        var mil = rel.getTime() - now.getTime();

        if (mil > 0) {
            var secs = Math.round(mil / 1000);

            var days = (secs - secs % (60 * 60 * 24)) / (60 * 60 * 24);
            secs = secs % (60 * 60 * 24);

            var hrs = (secs - secs % (60 * 60)) / (60 * 60);
            secs = secs % (60 * 60);

            var mnts = (secs - secs % 60) / 60;
            secs = secs % 60;

            var pad = function(num) { 
                return num < 10 ? ("0" + num) : num;
            }

            div.innerHTML = "" + pad(days) + " D : " + pad(hrs) + " H : " + pad(mnts) + " M";

            window.setTimeout('updateCountdown()', 60000);
        }
    }
}

if ($) {
    $(function() {
        updateCountdown();

        if ($('#itb_active').length)
            itb_prev = $('#itb_active').html();

        // prefetch images
        var com = new Image(18, 11);
        com.src = "/images/com.gif";
        var com2 = new Image(18, 11);
        com2.src = "/images/com2.gif";
    }); 

    $(window).load(resizeImages);
}
