/**
 * @author Pau
 */

var refrescarMarkers = true;
var map;
var panelMapa;

function mostrarCompeticionesSinContinente(transport){
	var competiciones = transport.responseText.evalJSON();
	var opciones = '';
	
	opciones += '<ul id="mapaCuadro" style="z-index: 0;">';
	
	
	  
//  <li><img src="/img/cham.gif" alt="Champions League" width="20" height="19" class="imgTxtCentro"/> Champions League</li>
//  <li><img src="/img/caf.gif" alt="Champions League" width="20" height="19"class="imgTxtCentro" /> CAF Champions League</li>
//  <li><img src="/img/copamerica.gif" alt="Champions League" width="20" height="19" class="imgTxtCentro" /> Copa Am�rica</li>
//  <li><img src="/img/afc.gif" alt="AFC" width="20" height="19" class="imgTxtCentro"/> AFC Champions League</li>
//  <li><img src="/img/uae.gif" alt="AFC" width="20" height="19" class="imgTxtCentro"/>Emiratos �rabes Unidos</li>
//  <li><img src="/img/interTour.gif" alt="AFC" width="20" height="19" class="imgTxtCentro"/> International Tournaments</li>
//  <li><img src="/img/libertadores.gif" alt="AFC" width="20" height="19"class="imgTxtCentro" /> Copa Libertadores</li>

	for(var i = 0; i < competiciones.length && i < 7 ; i++){ // 8 como mucho para que no sobresalga del cuadro
		var comp = competiciones[i];
		opciones += 
			'<li class="mapaCuadro">' +
				((comp.logotipo != null)?
					'<img src="'+comp.logotipo+'" alt="'+ comp.logotipo + '" width="20" height="19" class="imgTxtCentro" /> ':
					'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'
				) +
				'<a href="javascript:void(0);" onclick="javascript:cg.seleccionarContinenteyPaisPorSigla(\'\',id);cg.cargarTorneosCompeticion('+comp.id+');">'+
				cortaCadena(comp.nombre,19) +
			 	'</a>' +
			 '</li>';

		

	}
	opciones += '</ul>';

	panelMapa.panel.update(opciones);
}

/** Nuevo Panel Flotante */
function MyPane() {}
	
	MyPane.prototype = new GControl();
	MyPane.prototype.initialize = function(map) {
	var me = this;
	me.panel = new Element("div", {'id':'panelMapaIndex'} );
	map.getContainer().appendChild(me.panel);

	return me.panel;
};

MyPane.prototype.getDefaultPosition = function() {
	return new GControlPosition( G_ANCHOR_TOP_LEFT, new GSize(15, 10) );
};

MyPane.prototype.getPanel = function() {
	return this.panel;
}


/** FIN Nuevo Panel Flotante */

function getIcon(images){
    var icon = null;
    if (images) {
        icon = new GIcon();
        icon.image = images;
        icon.iconSize = new GSize(34, 24);
        icon.iconAnchor = new GPoint(24 >> 1, 34 >> 1);
        icon.infoWindowAnchor = new GPoint(24 >> 1, 34 >> 1);
    }
    return icon;
}


function initialize(){

	map = new GMap2($('mapaLocalizador'));
    map.setCenter(new GLatLng(40.24599150419903, -3.51562500000000), 2);
    map.setMapType(G_NORMAL_MAP);
    //map.addControl(new GSmallZoomControl());
    //map.enableScrollWheelZoom();
    //map.enableContinuousZoom();
    //map.addControl(new GMenuMapTypeControl());
    map.addMapType(G_PHYSICAL_MAP);

    panelMapa = new MyPane();
    map.addControl(panelMapa);
        
	GEvent.addListener(map, "moveend", function(){ //conforme movemos el mapa se obtiene los markers
		if(refrescarMarkers) {
			getMarkers();
		}
    });

	GEvent.addListener(map, "dragend", function(){
		refrescarMarkers = true;
    });

	GEvent.addListener(map, "zoomend", function(){
		refrescarMarkers = true;
    });

}

function getMarkers(){
	if(cg.deporteSeleccionado == null){
		return;
	}
	/*if(map == null){
		window.setTimeout("getMarkers();",1000); // a esperar
		return;
	} */

	//detectamos el  zoom y pintamos todos los markers segun el zoom
	var zoom = map.getZoom();
	var swlat = map.getBounds().getSouthWest().lat();
	var swlng = map.getBounds().getSouthWest().lng()
	var nelat = map.getBounds().getNorthEast().lat();
	var nelng = map.getBounds().getNorthEast().lng();
	
	var url = '/clasificacion/includes/obtenerMarkers.php';
	var pars = {
		'zoom' : zoom,
		'deporte': cg.deporteSeleccionado,
		'swlat': swlat,
		'swlng': swlng,
		'nelat': nelat,
		'nelng': nelng
	};
		
	var myAjaxLanzador = new Ajax.Request(url, {
		method: 'get',
		parameters: pars,
		onComplete: function(transport){
			mostrarMarkers(transport);
		}
	});

}

function createMarkerCompeticion(point, id, nom, continente){
    var label;
    var marker = new GMarker(point, {
        "icon": getIcon("/img/iconodestinociudad.png"),
        "clickable": true,
        "title": nom,
        "labelOffset": new GSize(-6, -10)
    });
    GEvent.addListener(marker, "click", function(){
		//ComboDestinos.cargarCombo(cont, pais, '', id);
		cg.seleccionarContinenteyPaisPorSigla(continente,id);
		cg.cargarTorneosCompeticion(id);
	});
    //GEvent.addListener(marker, "mouseover", function(){ComboDestinos.miga(cont, pais, id, "miga");});
    return marker;
}

function mostrarMarkers(transport){
	
	var aMD = transport.responseText.evalJSON();
	map.clearOverlays();

	for(var i = 0; i < aMD.length; i++){
		var marker = aMD[i];
		var point = new GLatLng(marker.latitud_pais, marker.longitud_pais);

		var mark = null;
		mark = createMarkerCompeticion(point, marker.id, marker.nombre_pais, marker.continente);

		if(mark != null){
			map.addOverlay(mark);
		}
	}
}
