document.domain = document.domain;

Orbited.settings.port = 8000;
Orbited.settings.hostname = "stream.4sqvision.jazzychad.com";
var TCPSocket = Orbited.TCPSocket;
      
var ts = new TCPSocket();

var checkintime = 0;

var map;
 
function _(id) {
    return document.getElementById(id);
}
function _i(id) {
    return _(id).innerHTML;
}
function __(id, val) {
    _(id).innerHTML = val;
}
function ___(id, val) {
    __(id, _i(id) + val);
}
function log(channel,msg) {
    //___("main", "<br/>" + channel + " : " + msg);
    if (window.console && window.console.log) {
	window.console.log(channel + " : " + msg);
    }
}
function log_plain(msg) {
    //___("main", "<br/><pre>" + msg + "</pre>");
    if (window.console && window.console.log) {
	window.console.log(msg);
    }
    
}
var buffer = '';
function ts_onread(data) {
    aux = data.split("\n");
    //log_plain(aux.length);
    for (var i = 0; i < aux.length-1; i++) {
	line = aux[i];
	if (i == 0) {
	    line = buffer + line;
	}
	var j = line.indexOf('!');
	var channel = line.slice(0,j);
	var msg = line.slice(j+1);
	//log_plain(line);
	//log(channel,msg);
	handler(channel,msg);
    }
    buffer = aux[aux.length-1];
}
function ts_onopen() {
    log_plain("CONNECTED!!!");
}
 
function sendTime() {
    ts.send("time");
}
function sendHello() {
    ts.send("hello");
}
function subscribe(chan) {
    ts.send("subscribe " + chan + "\n");
}
function unsubscribe(chan) {
    ts.send("unsubscribe " + chan + "\n");
}
function closeit() {
    ts.close();
}

/* ----------------------- Plop ---------------------- */
var c = 0;
var tweets = [];
jQuery.extend(jQuery.easing, {
	Plop: function (x, t, b, c, d) {
	    if ((t /= d / 1.5) < 1) return c / 2 * t * t * t * t * t + b;
	    return c / 2 * ((t -= 2) * t * t * t * t + 2) + b;
	}
    });


/* ---------------------- ready ------------------------- */
function rdy() {
    ts.onopen = (function() {ts_onopen(); subscribe("4sq_checkins"); subscribe("4sq_mayors"); subscribe("4sq_badges"); subscribe("4sq_oust"); subscribe("4sq_comment");});
    ts.onread = ts_onread;
    ts.open("stream.4sqvision.jazzychad.com", 8013);
    $(window).unload(function() {
	    //alert('bye');
	    ts.close();
	});
 
    map = new GMap2(document.getElementById("map"));
    map.addControl(new GSmallMapControl());
    map.addControl(new GMapTypeControl());
    map.setCenter(new GLatLng(35.0, 0.0), 13);
    map.setZoom(2);

} /* end rdy */



 
/* -------------------- handler --------------------- */
function handler(channel, msg) {
    var data = eval('(' + msg + ')');
    var divdata;

    if (data.lat != null) {
	log_plain('got coords ' + data.lat + ' ' + data.lng);
	markSite2(data.lat, data.lng);
    }

    if (channel == "4sq_comment") {
	var time = new Date().getTime();
	if (time - checkintime < 1000) {
	    return;
	}
	checkintime = time;
	divdata = render_comment(data);
	displaydata("checkins", divdata);
    } else if (channel == "4sq_checkins") {
	var time = new Date().getTime();
	if (time - checkintime < 1000) {
	    return;
	}
	checkintime = time;
	divdata = render_checkin(data);
	displaydata("checkins", divdata);
    } else if (channel == "4sq_badges") {
	divdata = render_badge(data);
	displaydata("badges", divdata);
    } else if (channel == "4sq_mayors") {
	divdata = render_mayor(data);
	displaydata("mayors", divdata);
    } else if (channel == "4sq_oust") {
	divdata = render_oust(data);
	displaydata("mayors", divdata);
    } else if (channel == "digg_digg") {
	divdata = render_digg(data);
	displaydata("diggs", divdata);
    }


}

function render_digg(obj) {
    var ret;
    ret = '<div class="blah">';
    //ret += '<img src="http://img.tweetimag.es/i/' + obj.user + '_m" /> ';                                                                                                                                                                
    
    ret += obj.item.diggs + ": " + obj.item.title;

    ret += '</div>';
    return ret;

}

function render_checkin(obj) {
    var ret;
    ret = '<div class="blah">';
    //ret += '<img src="http://img.tweetimag.es/i/' + obj.user + '_m" /> ';
    ret += '<img src="http://api.twitter.com/1/users/profile_image/' + obj.user +'?size=mini" height=24 width=24 /> ';
    ret += '<b><a href="http://twitter.com/' + obj.user + '" target="_blank">' + obj.user + '</a></b> checked in at ';
    ret += '<img src="/web/img/loc.png" />' + '&nbsp;<a href="' + obj.sqlink + '" target="_blank">' + obj.location + '</a>';

    ret += '</div>';
    return ret;
}

function render_comment(obj) {
    var ret;
    ret = '<div class="blah">';
    //ret += '<img src="http://img.tweetimag.es/i/' + obj.user + '_m" /> ';
    ret += '<img src="http://api.twitter.com/1/users/profile_image/' + obj.user +'?size=mini" height=24 width=24 /> ';
    ret += '<b><a href="http://twitter.com/' + obj.user + '" target="_blank">' + obj.user + '</a></b>: ' + obj.comment;
    ret += '<br/><img src="/web/img/loc.png" />' + '&nbsp;<a href="' + obj.sqlink + '" target="_blank">' + obj.location + '</a>';
    ret += '</div>';
    return ret;
}

function render_badge(obj) {
    var ret;
    var badge_img = "http://foursquare.com/img/badge/";
    var img_part = obj.badge.toLowerCase().replace(/ /gi, "").replace("'", "").replace("!", "").replace("%20", "") + '.png';
    /* img exceptions */
    if (img_part == "imonaboat.png") {
	img_part = "boat.png";
    } else if (img_part == "jobs.png") {
	img_part = "apple.png";
    } else if (img_part == "ziggyswagon.png") {
	img_part = "foodtruck.png";
    } else if (img_part == "dontstopbelievin.png") {
	img_part = "karaoke.png";
    } else if (img_part == "dontstopbelievin'.png") {
	img_part = "karaoke.png";
    } else if (img_part == "lookinforlove.png") {
	img_part = "bravo_millionaire_matchmaker.png";
    } else if (img_part == "fashionista.png") {
	img_part = "bravo_fashionista.png";
    } else if (img_part == "bravonewbie.png") {
	img_part = "bravo_newbie.png";
    } else if (img_part == "16candles.png") {
	img_part = "birthday.png";
    } else if (img_part == "babysitter.png") {
	img_part = "playground.png";
    } else if (img_part == "zoetrope.png") {
	img_part = "movie.png";
    }
    badge_img += img_part;


    ret = '<div class="blah">';
    ret += '<a href="' + obj.sqlink + '" target="_blank"><img src="' + badge_img + '" height=48 /></a>';
    //ret += ' <img src="http://img.tweetimag.es/i/' + obj.user + '_m" /> ';
    ret += ' <img src="http://api.twitter.com/1/users/profile_image/' + obj.user +'?size=mini" height=24 width=24 /> ';
    ret += ' ' + '<b><a href="http://twitter.com/' + obj.user + '" target="_blank">' + obj.user + '</a></b> earned the <b>' + obj.badge + '</b> badge!';
    ret += '</div>';
    return ret;

}

function render_mayor(obj) {
    var ret;
    ret = '<div class="blah">';
    ret += '<img src="http://foursquare.com/img/badge/supermayor.png" height=24 /> ';
    //ret += '<img src="http://img.tweetimag.es/i/' + obj.user + '_m" /> ';
    ret += '<img src="http://api.twitter.com/1/users/profile_image/' + obj.user +'?size=mini" height=24 width=24 /> ';
    ret += '<b><a href="http://twitter.com/' + obj.user + '" target="_blank">' + obj.user + '</a></b> is the new mayor of ';
    ret += '<img src="/web/img/loc.png" />' + '&nbsp;<a href="' + obj.sqlink + '" target="_blank">' + obj.location + '</a>';

    ret += '</div>';
    return ret;

}

function render_oust(obj) {
    var ret;
    ret = '<div class="blah">';
    ret += '<img src="http://foursquare.com/img/badge/supermayor.png" height=24 /> ';
    //ret += '<img src="http://img.tweetimag.es/i/' + obj.user + '_m" /> ';
    ret += '<img src="http://api.twitter.com/1/users/profile_image/' + obj.user +'?size=mini" height=24 width=24 /> ';
    ret += '<b><a href="http://twitter.com/' + obj.user + '" target="_blank">' + obj.user + '</a></b> just ousted ';
    //ret += '<img src="http://img.tweetimag.es/i/' + obj.ousted + '_m" /> ';
    ret += '<img src="http://api.twitter.com/1/users/profile_image/' + obj.ousted +'?size=mini" height=24 width=24 /> ';
    ret += '<b><a href="http://twitter.com/' + obj.ousted + '" target="_blank">' + obj.ousted + '</a></b> as the mayor of ';
    ret += '<img src="/web/img/loc.png" />' + '&nbsp;<a href="' + obj.sqlink + '" target="_blank">' + obj.location + '</a>';

    ret += '</div>';
    return ret;

}


/* ---------- display -------- */

function displaydata(div, data) {
    $('#' + div).prepend(data).css({
	    top: 0 - 78
		}).animate({
			top: 0
			    }, 500, "Plop", function () {
			if ($('#' + div + ' .blah').size() > 50) {
			    $('#' + div + ' .blah:last').remove();
			}
		    });
}



//---------------------------------------------- markSite2 -----------------------//
 
function markSite2(lat, lng) {
   
 
    var point = new GLatLng(parseFloat(lat), parseFloat(lng));
    var newIcon = new GIcon(G_DEFAULT_ICON);
 
    newIcon.image = "http://labs.google.com/ridefinder/images/mm_20_blue.png";//"/web/img/loc.png";
    newIcon.iconSize = new GSize(12, 20);
    newIcon.shadow = "";
    
    
 
 
    var newMarker = new GMarker(point, newIcon);
    map.addOverlay(newMarker);
} // end markSite;
