﻿function buzzLoad() {
    // do basic validation of the configurations
    if ($('#buzz').length === 0) return;

    var conf = $('#buzz').attr('buzz-config');

    if (typeof conf !== 'string') return;

    try{
        conf = conf.replace(/'/g, '"');
        conf = $.parseJSON(conf);

        if (conf === null) return;

        if(!$.isArray(conf) || conf.length === 0) return;
    }
    catch (e) {
        $('#buzz').html('Error in the News & Buzz configuration. <div style="display:none">' + e.toString() + '</div>');
        return;
    }

    // declaration of aux functions

    var timer = null;

    function update() {

        if (timer !== null)
            window.clearTimeout(timer);

        // options
        var timeout = 5000;
        var speed = 400;
        var i = 0;

        function showNext() {
            // vars
            var c = $('#buzz li').length;
            // debug
            $('#actual').html(String(i));
            $('#count').html(String(c));

            if (timer === null) {

                $('#buzz li').eq(i).fadeIn(speed, function () {

                    timer = window.setTimeout(showNext, timeout);

                });

            } else {

                window.clearTimeout(timer);

                $('#buzz li').eq(i).fadeOut(speed, function () {

                    i = i + 1;

                    i = i % c;

                    $('#buzz li').eq(i).fadeIn(speed, function () {

                        timer = window.setTimeout(showNext, timeout);

                    });

                });

            }
        }
        $('#buzz li').hide();
        showNext();
    }

    function getFacebook(c) {
        // Facebook: limit = number of messages
        var fb_url = document.location.protocol + '//graph.facebook.com/' + encodeURIComponent(c.search) + '/posts?limit=5&callback=?';
        $.getJSON(fb_url, function (json) {
            var html = "";
            var link = "";
            $.each(json.data, function (i, fb) {
                link = typeof fb.link !== 'undefined' ? fb.link : c.url;
                html += '<li style="display:none"><a href="' + link + '" target="_blank">' + fb.message + '</a></li>';
            });

            $('#buzz li.cs-remove-item').remove();
            $('#buzz ul').append(html);

            // animation
            update();
        });
    }

    function getTwitter(c) {
        // Twitter: rpp = results per page (number of messages), page = page number starting in 1, q = query
        var tw_url = document.location.protocol + '//search.twitter.com/search.json?q=' + encodeURIComponent(c.search) + '&rpp=5&page=1&callback=?';
        $.getJSON(tw_url, function (json) {
            var html = "";
            var link = "";
            $.each(json.results, function (i, tw) {
                link = c.url;
                html += '<li style="display:none"><a href="' + link + '" target="_blank">' + tw.text + '</a></li>';
            });

            $('#buzz li.cs-remove-item').remove();
            $('#buzz ul').append(html);

            // animation
            update();
        });
    }

    // iterate over all sources
    for (var i = 0; i < conf.length; i++) {
        switch (conf[i].source) {
            case 'facebook':
                getFacebook(conf[i]);
                break;
            case 'twitter':
                getTwitter(conf[i]);
                break;
            default:
                break;
        }
    }
}
