$(document).ready(function(){
	
	//INFO JUGADOR
	$("span.jugador").hover(function() {
    	$(this).append('<div class="burbuja"><div class="flecha"></div><span></span></div>');			
		datosJugador($(this).attr("carnet"), $(this).attr("cto"));
      $('span.jugador span')
      .css({
      'background-image' : 'url(images/loading_stat.gif)',
      'backgroundRepeat': 'no-repeat',
      'backgroundPosition': 'center',
      'height' : '20px'
      });
	}, function() {
		$("span.jugador div.burbuja").remove();
	});
	
	function datosJugador(carnet, cto) { 
	    var dir = 'nro='+carnet+'&cto='+cto; 
	    $.ajax({ 
	        url: "jugador.php",   
	        type: "GET",         
	        data: dir,      
	        cache: false, 
	        success: function (html) {       
	            $('span.jugador span')
	            .css({
              'background' : 'none',
              'height' : '100%'
              });
	            $('span.jugador span').html(html);      
	        }        
	    }); 
	}
	
	//TWITTER
	$(".twitter").tweet({
        username: "LPVFinfo",
        join_text: "auto",
        avatar_size: 0,
        count: 10,
        loading_text: "Cargando tweets..."
    });
    
    
    //ORDENAR TABLAS
	$(".posiciones").tablesorter();
	$(".goleadores").tablesorter();
	$(".tarjetas").tablesorter();
	$(".equipos").tablesorter();
	$(".inhabilitados").tablesorter();
	$(".tarjetas-equipos").tablesorter();
	$(".correccion").tablesorter();

	//MENU NOMBRE EQUIPO
	$("ul.equipos li").hover(function() {
		$(this).find(".burbuja").stop()
		.css("display","block")

	}, function() {
		$(this).find(".burbuja").stop()
		.css("display","none")
	});


	//FECHA FIXTURE
	function cargarFecha(cto, cat, div, fecha)
	{
		var height = 401;
		if (cat == 45)
		  height = 348;
		
		$("div.fecha.cat"+cat+".div"+div+".cto"+cto)
		.css({
		  'background-image' : 'url(images/loading_fixture.gif)',
		  'backgroundRepeat': 'no-repeat',
		  'backgroundPosition': 'center',
		  'opacity' : '0.4',
		  'height' : height + 'px'
		});
    
		$.ajax
		({
			type: "POST",
			url: "fecha.php",
			data: "cto="+cto+"&nro="+fecha,
			success: function(msg)
			{
				$("div.fecha.cat" + cat + ".div" + div + ".cto" + cto).ajaxComplete(function(event, request, settings)
				{
          $(this).css({
            'background-image' : 'none',
            'opacity' : '1',
            'height' : '100%'
          });
					$(".fecha.cat" + cat + ".div" + div + ".cto" + cto).html(msg);
				});
			}
		});
	}
	
	jQuery.each(ctos, function() {
		var cto = this[0];
		var cat = this[1];		
		var div = this[2];
		var fecha = this[3];
		
		fecha++;
		
		cargarFecha(cto, cat, div, fecha);

		$("ul.fixture.cat"+cat+".div"+div+".cto"+cto+" li").live('click',function(){
			$("ul.fixture.cat"+cat+".div"+div+".cto"+cto+" li.selected").removeClass("selected");
			$(this).addClass("selected");
			var fechaLista = $(this).attr("fecha");
			cargarFecha(cto, cat, div, fechaLista);
		});
   });


  
  	//CARGAR CANCHAS
    var latlng = new google.maps.LatLng(-34.773204,-56.160049);
    var myOptions = {
      zoom: 11,
      center: latlng,
      mapTypeId: google.maps.MapTypeId.HYBRID,
      scrollwheel: false,
      disableDefaultUI: false,
	  streetViewControl: false,
	  navigationControl: true,
	  mapTypeControl: false,
      navigationControlOptions: { style: google.maps.NavigationControlStyle.ANDROID, position: google.maps.ControlPosition.TOP_CENTER }
	};
	var map = new google.maps.Map(document.getElementById("map-canvas"), myOptions);
	var bounds = new google.maps.LatLngBounds();

	var ibOptions = {
		disableAutoPan: false
		,maxWidth: 0
		,pixelOffset: new google.maps.Size(-203, 0)
		,zIndex: null
		,closeBoxMargin: "2px 2px 2px 2px"
		,closeBoxURL: "images/close.png"
		,infoBoxClearance: new google.maps.Size(1, 1)
		,isHidden: false
		,pane: "floatPane"
		,enableEventPropagation: false
	};

	var ib = new InfoBox(ibOptions);

	$.getJSON("cargar_canchas.php", function(json) {
		if (json.Canchas.length > 0) {
			for (i=0; i<json.Canchas.length; i++) {
				var cancha = json.Canchas[i];
				addLocation(cancha);
			}
			map.fitBounds(bounds);
		}
	});
	
	function partidos(location){
		var html = '<div class="flecha"></div><h1>' + location.nombre + '</h1><h2>' + location.direccion + '</h2>';
		if (location.partidos.length > 0) {
			html += '<h3>Partidos por jugarse:</h3>';
			for (i=0; i<location.partidos.length; i++) {
				html += "<p>" + location.partidos[i].fecha + " " + location.partidos[i].hora + " - <a href='equipo.php?nro=" + location.partidos[i].nro_equipo_1 + "&cto=" + location.partidos[i].campeonato + "'>" + location.partidos[i].equipo_1 + "</a> - <a href='equipo.php?nro=" + location.partidos[i].nro_equipo_2 + "&cto=" + location.partidos[i].campeonato + "'>" +  location.partidos[i].equipo_2 + "</a></p>";
			}
		}
		return html;	
	}
	
	
	function addLocation(location) {
		
		var point = new google.maps.LatLng(location.latitud, location.longitud);		
		var marker = new google.maps.Marker({position: point, map: map, icon: 'images/football.png'});
		marker.setMap(map);
		bounds.extend(marker.getPosition());

		$("<li />")
			.html(location.nombre)
			.click(function(){
				map.setZoom(20);
							
				ib.setContent(partidos(location));			
				ib.open(map, marker);
				
				$(".active").removeClass("active");
				$(this).addClass("active");
			})
			.appendTo("#map-list");
		
		
		google.maps.event.addListener(marker, "click", function(){	
			ib.setContent(partidos(location));
			ib.open(map, this);
		});
	}

});

/*
var numShown = 4; // Initial rows shown & index
var numMore = 5; // Increment
var numRows = $('table').find('tr').length; // Total # rows

//alert(numRows);

$(document).ready(function(){
 // Hide rows and add clickable div
 $('table')
  .find('tr:gt(' + (numShown - 1) + ')').hide().end()
  .after('<div id="more">Show <span>' + numMore + '</span> More</div>');

 $('#more').click(function(){
  numShown = numShown + numMore;
  // no more show more if done
  if ( numShown >= numRows ) $('#more').remove();
  // change rows remaining if less than increment
  if ( numRows - numShown < numMore ) $('#more span').html(numRows - numShown);
  $('table').find('tr:lt('+numShown+')').show();
 })

})
*/
