/**
* World Cup Roster Mapping Code
* Written by Oscar Merida <oscar@soccreblogs.net>, May 20, 2006
* You are free to use this code under the terms of the LGPL License.
* http://www.opensource.org/licenses/lgpl-license.php
*
* Original works at details http://www.soccerblogs.net/world_cup_map/
*/
/*
Data Manipulating and Map Plotting functions
*/
var Map_Markers = new Array();

function get_club_latlong(team)
{
    if (!Club_Coords[team])
    {
        alert("Club " + team + "not found");
        return false;
    }
    else
    {
        return Club_Coords[team];
    }
}

function plot_player(player, team)
{


    // Map_Markers is a global array holding all the enabled markers

    // figure out which club lat long to use, this is for venues
    // that may be used by the same clubs ala LA Galaxy and Chivas
    if ( player.loc )
    {
        coord_key = player.loc
    }
    else
    {
        coord_key = player.club
    }
    // get the lat long for the players marker
    ll_coords = get_club_latlong(coord_key);

    // coords
    if(Map_Markers[coord_key])
    {
        // we need to add the players to this marker
        ul_list = document.getElementById(Map_Markers[coord_key].dom_id);
    }
    else
    {
        /* WE NEEd to create this marker */
    var point = new GLatLng(ll_coords.lat, ll_coords.lng);

        var icon = new GIcon();

        icon.image = "/world_cup_map/pushpins/webhues/" + team.icon_color + ".png";
        icon.shadow = "/world_cup_map/pushpins/templates/shadow50.png";
        icon.iconSize = new GSize(20,34);
        icon.shadowSize = new GSize(37,34);
        icon.iconAnchor = new GPoint(10, 34);
        icon.infoWindowAnchor = new GPoint(10, 1);

        // Create our "tiny" marker icon
        Map_Markers[coord_key] = new PdMarker(point, icon);

        // create an ul to display in the the GMarker
        ul_list = document.createElement('ul');
        ul_list.id = 'info_' + player.club;
        ul_list.club = coord_key;

        info_elt = document.getElementById('info');
        info_elt.appendChild(ul_list);
        Map_Markers[coord_key].dom_id = ul_list.id;
    }

    list_item = document.createElement('li');
    list_item.innerHTML = '<span class="fake_link">' + player.name + '('+  player.position + ')</span>'
                            + '<br /><em>' + player.club + '</em>';
    ul_list.appendChild(list_item);

    ul_list.onclick = zoom_to_club;
}

function zoom_to_club()
{
    // this is the ul list in the info pane
    coord_key = this.club;

    var marker = map.getFirstMarker();
    while (marker != null)
    {
        marker.restoreImage();
        marker = map.getNextMarker();
    }

    // bring marker to front, turn white
    Map_Markers[coord_key].setImage("/world_cup_map/pushpins/webhues/216.png");
    Map_Markers[coord_key].topMarkerZIndex();

    ll_coords = get_club_latlong(this.club);
    pt = new GLatLng(ll_coords.lat, ll_coords.lng);
    map.panTo(pt);

    /*
    if ( 6 != map.getZoom() )
    {
        window.setTimeout( function() { map.setCenter(p, 6);}, 2000);
    }
    */
}

function click_Map_Marker()
{
    // this is the Map_Marker;
    list = document.getElementById(this.dom_id);
    list.style.color = '#f00';
}

function showInfoWindow()
{
    alert(this);
    this.openInfoWindow(this.dom_id);
}
/**
Displays an entire teams roster
* @param team object (see players.js)
*/
function show_team(a_team)
{
    var el = document.getElementById('info');
    el.innerHTML = "";
    for ( i=0; i < a_team.roster.length; i++)
    {
        plot_player(a_team.roster[i], a_team);
    }

    // display all Map_Markers
    for ( var Club in Map_Markers)
    {
        info_el = document.getElementById(Map_Markers[Club].dom_id);
        Map_Markers[Club].setDetailWinHTML(info_el.innerHTML);
        map.addOverlay(Map_Markers[Club]);
    }
}

function clear_all_markers()
{
    // display all Map_Markers
    map.clearOverlays();

    // return Map_Markers to empty
    for ( var Club in Map_Markers)
    {
        delete Map_Markers[Club];
    }

}

function change_team_displayed(an_event)
{
    select_el = document.getElementById('country_pick');

    // first we need to clear the map
    clear_all_markers();

    map.setCenter(new GLatLng(0,0), 2);

    // now display the selected team
    show_team(Countries[select_el.value].data);
}

function show_teams_menu()
{
    select_el = document.getElementById('country_pick');
    // this is also where we bind the onChange function
    select_el.onchange = change_team_displayed;

    for ( a_team in Countries )
    {
        opt = document.createElement('option');
        opt.value = a_team;
        opt.innerHTML = a_team;
        select_el.appendChild(opt);
    }
}

var map;

function Main()
{
    if (GBrowserIsCompatible())
    {
        el = document.getElementById("map");

        map = new GMap2(el);

        map.setCenter(new GLatLng(0,0), 2);
        map.addControl(new GScaleControl);
        map.addControl(new GMapTypeControl);
        map.addControl(new GLargeMapControl);

        show_teams_menu();
    }
}
