google.load("feeds", "1");
google.setOnLoadCallback(function () {

    $("#filter").click(function () { return false; });

    var entries = [];

    var version = 2;

    function robocallSlice(from, to) {
        var url = "http://www.editgrid.com/user/alangutierrez/Geocode.type.json.atom?_from=" +from + "&_to=" + to + "&_v=" + version;
        var calls = new google.feeds.Feed(url);
        calls.setNumEntries(999);
        return calls;
    }

    var baseIcon = new GIcon();
    baseIcon.image = "http://maps.google.com/mapfiles/ms/micons/phone.png";
    baseIcon.shadow = "http://maps.google.com/mapfiles/ms/micons/phone.shadow.png";
    baseIcon.iconSize = new GSize(32, 32);
    baseIcon.shadowSize = new GSize(59, 32);
    baseIcon.iconAnchor = new GPoint(9, 34);
    baseIcon.infoWindowAnchor = new GPoint(9, 2);

    function createMarker(ll, call, baseIcon) {
        var marker = new GMarker(ll, { icon: baseIcon });
        var content = '<div style="width: 220px" class="balloon">';
        
        content += '<div>';
        if (call.city.length != 0)
            content += call.city + ', ' + call.state;
        else
            content += call.state;
        content += '</div>';
        
        content += '<div>';
        if (call.user.indexOf('@') == -1)
            content += '@<a href="http://twitter.com/' + call.user +
'">' + call.user + '</a>';
        else
            content += '<a href="mailto:' + call.user + '">' + call.user
+ '</a>';
        content += '</div>';
            
        content += '<p class="keywords">' + call.keywords + '</p>';

        if (call.audio != '')
            content += '<object type="application/x-shockwave-flash" data="dewplayer-mini.swf?mp3=' +
                call.audio + '" width="200" height="20"></object>';
        content += '</div>';
        GEvent.addListener(marker, "click", function() {
            marker.openInfoWindowHtml(content);
        });
        return marker;
    }

    var start = 0, step = 50, stop = 150, offset = 0;
    var bounds = new GLatLngBounds();

            var map = new GMap2(document.getElementById("map"));
            map.setCenter(new GLatLng(37.4419, -122.1419), 4);
            
            map.addControl(new GSmallMapControl());
            map.addControl(new GMapTypeControl());

    robocallSlice(offset, offset + step).load(function (result) {
        if (!result.error) {
            var call;
            
            for (var i = 0; i < result.feed.entries.length; i++) {
                var entry = result.feed.entries[i];
                eval('call = ' + entry.content + ';');
                entry.call = call;
                var ll = new GLatLng(call.latitude, call.longitude);
                bounds.extend(ll);
                entry.marker = createMarker(ll, call, baseIcon);
                map.addOverlay(entry.marker);
                entries.push(entry);
            }
        
            offset+= step;
            if (offset < stop) robocallSlice(offset, offset + step).load(arguments.callee)
            else {
                bind();
                map.setCenter(bounds.getCenter(), 3);
                fetchTweets();
            }
        }
    });

    function bind() {
        $("#filter").click(function () {
            if ($(this).html() == 'Only Show Calls with Audio') {
                for (var i = 0; i < entries.length; i++)
                    if (entries[i].call.audio == '')
                        entries[i].marker.hide();
                $(this).html('Show All Calls');
            }
            else {
                for (var i = 0; i < entries.length; i++)
                    entries[i].marker.show();
                $(this).html('Only Show Calls with Audio');
            }
            return false;
        });
    }

    function fetchTweets() {
                var feed = new google.feeds.Feed("http://robocall.appjet.net/search?rpp=30&q=robocall%2BOR%2Brobocalls%2BOR%2B%22robo%2Bcall%22%2BOR%2B%22robo%2Bcalls%22%2B+OR+roboscalls+-from%3Aendtherobocalls&v=" + version);
                feed.setNumEntries(999);
                feed.load(function(result) {
                    var sponsors = [
                        '<div class="sponsor"><img src="http://s3.amazonaws.com/twitter_production/profile_images/61146305/2907817944_88caebd98f_d_normal.jpg"><div class="text">The code is a mashup of Web 2.0 technologies by @<a href="http://twitter.com/bigeasy">bigeasy</a>. User interface design is by @<a href="http://twitter.com/tunie">tunie</a>.</div></div>',
                        '<div class="sponsor"><img src="http://s3.amazonaws.com/twitter_production/profile_images/61698238/NPDNC_logo_final_normal.jpg"><div class="text">Outreach is the work of @<a href="http://twitter.com/endtherobocalls">endtherobocalls</a>, who would like to invite you to register with the <a href="http://www.stoppoliticalcalls.org/ht/d/Join/pid/196">National Political Do Not Call Registry</a>.</div></div>',
                        '<div class="stop"><a href="http://www.stoppoliticalcalls.org"><img src="NPDNC_logo_100.gif"/></a></div>'
                    ];
                    if (!result.error) {
                        var entries = result.feed.entries;
                        var deck = [];
                        for (var i = 0; i < entries.length; i++) {
                            deck.push('<div class="tweet">' + entries[i].content + '</div>');
                            deck.push(sponsors[i % 3]);
                        }
                        $("#tweets").html(deck.join(''));
                        setTimeout(function () {
                            $('#tweets .tweet').find('img').addClass('avatar');
                            $('#tweets .tweet').find('div:eq(1)').addClass('user');
                            $('#tweets .tweet').find('div:eq(2)').addClass('when');
                            $('#tweets .tweet').find('div:eq(3)').addClass('text');
                            $('#tweets').cycle({ fx: 'scrollUp' });
                        }, 1000);
                    }
                });
            }
});
