(function($){
    function rotateList(elem){
        var current = elem.find('li.activeSlide');

        if(!current || !current.length)
            current = elem.find('li:first-child');

        var direction = elem.data('direction');
        var next = current[direction ? 'prev' : 'next']();

        if(!next || !next.length){
            elem.data('direction', !direction);
            return rotateList(elem);
        }

        current.removeClass('activeSlide');
        next.addClass('activeSlide');

        elem.animate({
            'margin-left': -next.position().left
        }, 1000);
    }
    
    $.fn.rotate = function(){
        this.each(function(){
            var self = $(this);
            self.data('rotator', setInterval(function(){
                rotateList(self);
            }, 3000));
        });

        return this;
    };
})(jQuery);

function cpEvents() {
}

cpEvents.events = null;
cpEvents.pointer = 0;
cpEvents.step = 1;
cpEvents.lastEventId = null;
cpEvents.intervalRotate = null;
cpEvents.intervalFindNew = null;

cpEvents.addEvent = function(c) {
    
    var newel = $('<li style="display:none;" userid="' + c.id + '"><div><strong>' + c.name + '</strong> ' + c.message + '</div></li>');
    $('#EventList').prepend(newel);
    
    newel.show('drop', {direction: "up"}, 1000);
    
}

cpEvents.replaceEvents = function(data) {
    
    $('#EventList li:gt(' + (6-data.length) + ')').each(function() {
        $(this).hide("drop", {direction: "down"}, 1000, 
            function() {

                var c = data.pop();
                cpEvents.addEvent(c);
                $(this).remove();
                
            }
        );
    });    
}

cpEvents.init = function() {
    
    for (var i = 0; i < 7; i++)
    {
        if (cpEvents.events[i]) {
            var newel = $('<li userid="' + cpEvents.events[i].id + '"><div><strong>' + cpEvents.events[i].name + '</strong> ' + cpEvents.events[i].message + '</div></li>');
            $('#EventList').append(newel);
        }
        
    }
    cpEvents.pointer = 7;
    cpEvents.step = 1;
    
}

cpEvents.findNew = function() {
    $.ajax({
        url: globalparams.home_url + '/home/lastEvent',
        dataType: 'json',
        data: {'last': cpEvents.lastEventId},
        success: function(data) {

            if (data.length > 0) {
                
                cpEvents.lastEventId = data[0].id;
                $.each(data, function(key, value) {
                    cpEvents.events.splice(cpEvents.pointer, 0, value);
                });
                
                cpEvents.step = cpEvents.step + data.length - 1;
            }
            
        }
    });
};




cpEvents.rotate = function() {

    if (cpEvents.events[cpEvents.pointer]) {
        
        cpEvents.replaceEvents(cpEvents.events.slice(cpEvents.pointer, cpEvents.pointer + cpEvents.step))
        cpEvents.pointer = cpEvents.pointer + cpEvents.step;
        cpEvents.step = 1;
    } else {
        
        cpEvents.replaceEvents(cpEvents.events.slice(0, cpEvents.step))
        cpEvents.pointer = cpEvents.step;
        cpEvents.step = 1;
    }
    
}


$(function() {
    
    var intro = $('#cpIntro');
    
    //Events -----------
    if (globalparams.events.length > 0) {
        cpEvents.lastEventId = globalparams.events[0].id;
    }
    
    cpEvents.events = globalparams.events;

    cpEvents.init();
    cpEvents.intervalRotate = setInterval('cpEvents.rotate()', 3000);
    cpEvents.intervalFindNew = setInterval('cpEvents.findNew()', 10000);
    // ------------------
    

    if(!intro || !intro.length)
        return;

    var toSiteButton = intro.find('.cpCloseBtn a');

    var closePopup = function(){
        intro.fadeOut(400, function(){
            $(document.documentElement).css('overflow', 'auto');
            intro.remove();
        });
        return false;
    };
    
    
    intro.add(toSiteButton).click(closePopup);
    $('#cpContent').click(function(evt){
        evt.stopPropagation()
        })

    var currentScreen = intro.find('#cpContent div[id ^= "intro"]:first-child');

    intro.find('*[data-screen]').click(function(){
        var scr = $($(this).attr('data-screen'));

        if(scr && scr.length){
            currentScreen.fadeOut(400, function(){
                scr.fadeIn(400, function(){
                    var t = scr.find('.mySlider ul');

                    if(t && t.length && !t.data('slider-started')){
                        t.rotate();
                        t.data('slider-started', true);
                    }
                });
            });

            currentScreen = scr;
        }

        return false;
    });
    var slider = $('.mySlider ul', currentScreen);
    if(slider.size()){
        slider.rotate();
        slider.data('slider-started', true);
    }

    $('#showScreen').change(function(){
        $.get(globalparams.home_url + '/home/introScreen?show=' + ($(this).attr('checked') ? 0 : 1));
    });

    $(document.documentElement).css('overflow', 'hidden');
    intro.fadeIn(1200);
});

