XPRIZE.tracker = function() {
    // private
    var teams = [];
    var cookie_expires = new Date();
    // set the cookie expiration date to 6 months in the future
    cookie_expires.setDate(cookie_expires.getDate() + 180);

    function populate() {
		teams = validate(teams);
        var html = [];
        for (var i=0; i<teams.length; i++) {
            if (html.length > 0) html.push(", ");
            html.push("<a class='no_underline_link' id='" + teams[i] + "' href='#'>");
            html.push(XPRIZE.teamlist[teams[i]].teamName);
            html.push("</a>");
        }
        $("#tracking_content").html(html.join(''));
        if (teams.length == 0) return;
        $("#tracking_content a").click(function(e) {
            XPRIZE.main.trackedTeamSelected(this.id);
//            return false;
        });
    }
    function updateCookie() {
        var opts = {
            path: '',
            expiresAt: cookie_expires
        };
        $.cookies.set("xprize_teams", teams.join(","), opts);
    }
	function validate(teams_array) {
		valid_teams = [];
		for (var i=0; i< teams_array.length; i++) {
			if (XPRIZE.teamlist.hasOwnProperty(teams_array[i]))
				valid_teams.push(teams_array[i]);
		}
		return valid_teams;
	}
    return {
        init: function() {
            // get the teams from cookie
            var cookie = $.cookies.get("xprize_teams");
            if (cookie)
                teams = cookie.split(",");
            // check for the teamlist file and load it if not
            if (!XPRIZE.teamlist) {
                // request data
                $.getJSON("json/teamlist.json", function(data) {
					if (data && typeof data == "object") {
						XPRIZE.teamlist = data;
						 populate();
					}
					else alert ("Sorry, team data file seems to be empty. Please alert the webmaster.");
                });
            }
            else 
                // populate the tracking section
                populate();
        },
        trackTeam: function(teamKey) {
            if (teams.length >= 5) {
                alert("Sorry, You can only track 5 teams at a time. If you want to track this team you must first remove one from your list by selecting \"Stop Tracking Team\" on the team's info-window.");
                return false;
            }
            teams.push(teamKey);
            populate();
            updateCookie();
            return true;
        },
        stopTracking: function(teamKey) {
            var id = teams.indexOf(teamKey);
            teams.splice(id, 1);
            populate();
            updateCookie();
        },
        isTracking: function(teamKey) {
            for (var i=0, len=teams.length; i<len; i++) {
                if (teamKey == teams[i])
                    return true;
            }
            return false;
        },
		getTrackedTeams: function() {
			return teams;
		},
        addTrackingButton: function(button_id, key) {
            var button = $("#" + button_id);
            if (XPRIZE.tracker.isTracking(key))
                button.addClass("stop_track");
            // click function
            button.click(function(e) {
                var btn = $(this);
                // start or stop the tracking and swap the button content 
                if (btn.hasClass("stop_track")) {
                    XPRIZE.tracker.stopTracking(key);
                    btn.removeClass("stop_track");
                }
                else {
                    if( XPRIZE.tracker.trackTeam(key))
                        btn.addClass("stop_track");
                }
                return false;
            });
        }
    }
}();

