var interavl_ID = null;

var numTopicOfDay = null;

$(document).ready(function() {

    numTopicOfDay = $('#navigationSubjectTitle li').length + 1;

    function rotateTopicOfDay() {
        var current = parseInt($('#navigationSubjectTitle a.active').text());
        var next = (current + 1) % numTopicOfDay;
        if (0 == next) {
            next += 1;
        }

        $('#navigationSubjectTitle a').removeClass('active');
        $('.topicOfDayNav' + next + ' a').addClass('active');
        $('.topicOfDay' + next).show();
        $('.topicOfDay' + current).hide();
    }

    interavl_ID = window.setInterval(rotateTopicOfDay, 20000);

    $('#navigationSubjectTitle a').click(function () {
        window.clearInterval(interavl_ID);
        interavl_ID = window.setInterval(rotateTopicOfDay, 20000);
        var current = parseInt($(this).text());
        $('#navigationSubjectTitle a').removeClass('active');
        $(this).addClass('active');
        $('#daySubject ul.clean li').hide();
        $('.topicOfDay' + current).show();
        return false;
    });

    $('#smallNavigationBar a').click(function () {
        if ('block' == $('#lastWeek').css('display')) {
            $('#lastWeek').hide();
            $('#lastMonth').show();
            $('#lastWeekNav').removeClass('active');
            $('#lastMonthNav').addClass('active');
        } else {
            $('#lastWeek').show();
            $('#lastMonth').hide();
            $('#lastWeekNav').addClass('active');
            $('#lastMonthNav').removeClass('active');
        }
        return false;
    });

    //Przesuwanie boxów
    $('body').click(function (e) {
        $('div.showItem', 'div.categoryBox').hide();
    });

    $('a.move', 'div.categoryBox').live('click', function (e) {
        $('div.showItem', 'div.categoryBox').hide();
        $(e.target).next().children('.showItem').show();
        return false;
    });

    $('div.showItem a.up').live('click', function (e) {
        var id = $(e.target).closest('div.newsBox').attr('id');
        var boxs = $('div.newsBox');

        var list = new Array();

        for (var i = 0;i < boxs.length;i += 1) {
            list.push($(boxs[i]).attr('id'));
        }

        var max = list.length;
        var index = list.indexOf(id);

        if (0 == index || -1 == index) {
            $(e.target).parent().hide();
            return false;
        }

        var prev = $('#' + list[index - 1]);
        var curr = $('#' + id);
        var tmp = prev.html();
        prev.html(curr.html());
        curr.html(tmp);

        tmp = list[index - 1];
        list[index - 1] = list[index];
        list[index] = tmp;

        var date = new Date();
        var days = 366;
        date.setTime(date.getTime()+(days*24*60*60*1000));

        var section = $('a.move').attr('rel');

        if(section == "business") {
            document.cookie = 'boxBusiness=' + list.join(',') + '; expires=' + date.toGMTString() + '; path=/';
        } else {
            document.cookie = 'box=' + list.join(',') + '; expires=' + date.toGMTString() + '; path=/';
        }

        $(e.target).parent().hide();
        return false;
    });

    $('div.showItem a.down').live('click', function (e) {
        var id = $(e.target).closest('div.newsBox').attr('id');
        var boxs = $('div.newsBox');

        var list = new Array();

        for (var i = 0;i < boxs.length;i += 1) {
            list.push($(boxs[i]).attr('id'));
        }

        var max = list.length;
        var index = list.indexOf(id);

        if ((max - 1) == index || -1 == index) {
            $(e.target).parent().hide();
            return false;
        }

        var next = $('#' + list[index + 1]);
        var curr = $('#' + id);
        var tmp = next.html();
        next.html(curr.html());
        curr.html(tmp);

        tmp = list[index + 1];
        list[index + 1] = list[index];
        list[index] = tmp;

        var date = new Date();
        var days = 366;
        date.setTime(date.getTime()+(days*24*60*60*1000));

        var section = $('a.move').attr('rel');

        if(section == "business") {
            document.cookie = 'boxBusiness=' + list.join(',') + '; expires=' + date.toGMTString() + '; path=/';
        } else {
            document.cookie = 'box=' + list.join(',') + '; expires=' + date.toGMTString() + '; path=/';
        }

        $(e.target).parent().hide();
        return false;
    });
});

