$(document).ready(function() {
	$('.stand-container').hide();
	$('.overview_row').on('click', function() {
		$(this).find('.stand-container').toggle();
	});
	$('#wedstrijd_content').on('click','.overview_row',function(){
		showMatchInfo($(this));
	}).on('click','#wedstrijd_info_block_right_locatie_routeLink',function(){
		showRoute($(this));
	}).on('change','#match-form :input',function(){
		$('#match-form').submit();
	});

	$('#trainingsschema_content').on('click','.overview_row',function(){
		showTrainingInfo($(this));
	}).on('change','#Training-form :input', function(){
		$('#Training-form').submit();
	});

	$('#results-container').on('click','.overview_row',function(){
		showPouleStandings($(this));
	});

	$('#results-form').on('change',':input',function(){
		$('#results-form').submit();
	});

	$(function () {
	  $('[data-toggle="popover"]').popover();
	});

	$("#scroller").simplyScroll({
		speed: 3
	});

  $('ul.nav li.dropdown').hover(function() {
      $(this).find('.dropdown-menu').stop(true, true).delay(200).fadeIn(200);
    }, function() {
      $(this).find('.dropdown-menu').stop(true, true).delay(200).fadeOut(200);
  });

    $(window).scroll(function(){
    if ($(this).scrollTop() > 300) {
      $('.scrollToTop').fadeIn();
    } else {
      $('.scrollToTop').fadeOut();
    }
  });
     //Click event to scroll to top
  $('.scrollToTop').click(function(){
    $('html, body').animate({scrollTop : 0},800);
    return false;
  });
});

function showRoute(aLink) {
    if ($.trim(aLink.html()) == "Bekijk route") {
        $("#wedstrijd_locatie_route").show();
        aLink.html("Verberg route");
    }else{
		$("#wedstrijd_locatie_route").hide();
        aLink.html("Bekijk route");
	}
    return false;
}

function showMatchInfo(obj) {

    $("#overview_info").hide();

    if ($(obj).attr("class") == "overview_row overview_row_visited") {
        $(obj).removeClass("overview_row_visited");
    } else {

        $("#wedstrijd_info_block_right_locatie_routeLink").show();
		$("#wedstrijd_locatie_map").hide();
		$(".overview_row").each(function () {
            $(this).removeClass("overview_row_visited");
        });

        obj.addClass("overview_row_visited");
        var iMatchId = obj.children(":first").val();

        $(".awayrow").hide();
        $("#wedstrijd_locatie_route").hide();

		var jsonObj = {};
		jsonObj.iMatchId = iMatchId;

          $.ajax({
            type: 'POST',
            url: 'overzichten/ajax/getMatchInfoByMatchId.asp',
            data: jsonObj,
            dataType: "json",
            success: function (json) {
				// MatchDetails
				$("#wedstrijd_info_block_right_matchdetails_referees").html("");
				$('.wedstrijd_overview_row_referees').hide();
				$("#wedstrijd_info_block_right_matchdetails_matchnumber").html(json[0].Number);
				$("#wedstrijd_info_block_right_matchdetails_tegenstander").html(json[0].Teams);
				$("#wedstrijd_info_block_right_matchdetails_verzamelen").html(json[0].ArriveTime);
				$("#wedstrijd_info_block_right_matchdetails_time").html(json[0].MatchTime);
				$("#wedstrijd_info_block_right_matchdetails_remark").html(json[0].Remark);
				$("#wedstrijd_info_block_right_matchdetails_federationremark").html(json[0].FederationRemark);
				$("#wedstrijd_info_block_right_matchdetails_digital").html(json[0].IsDigital);
				$("#wedstrijd_info_block_right_matchdetails_traveltime").html(json[0].TravelTime);
				$("#wedstrijd_info_block_right_matchdetails_distance").html(json[0].Distance);
				$("#wedstrijd_info_block_right_matchdetails_field").html(json[0].FieldName);
				$("#wedstrijd_info_block_right_matchdetails_fieldtype").html(json[0].FieldType);
				$("#wedstrijd_info_block_right_matchdetails_fieldpart").html(json[0].FieldPart);
				if (json[0].Drivers != null){
					arrDrivers = json[0].Drivers.split(',');
					strDriversHTML = ""
					for (i=0 ; i < arrDrivers.length ; i++){
						strDriversHTML += "<br />" + arrDrivers[i];
					}
					strDriversHTML = strDriversHTML.substring(6,strDriversHTML.length-1);
					$("#wedstrijd_info_block_right_matchdetails_drivers").html(strDriversHTML);
				}
				// Uitwedstrijd of zaalwedstrijd locatie
				if (json[0].IsHome == "Uit" || json[0].IsIndoor == "True") {
					$(".awayrow").show();
					$('.wedstrijd_overview_row_referees').hide();
					$("#wedstrijd_info_block_right_locatie_clubname").html(json[0].Address.LocationName);
					$("#wedstrijd_info_block_right_locatie_phone").html(json[0].Address.PhoneNumber);

					var strAddress1 = "";

					if (json[0].Address.Street != null) {
						strAddress1 += json[0].Address.Street;
					}

					if (json[0].Address.HouseNumber != null) {
						strAddress1 += " " + json[0].Address.HouseNumber;
					}
					if (json[0].Address.HouseNumberAddition != null) {
						strAddress1 += " " + json[0].Address.HouseNumberAddition;
					}

					$("#wedstrijd_info_block_right_locatie_address").html(strAddress1);

					var strAddress2 = "";
					if (json[0].Address.PostalCode != null) {
						strAddress2 += json[0].Address.PostalCode;
					}

					if (json[0].Address.City != null) {
						strAddress2 += " " + json[0].Address.City;
					}
					$("#wedstrijd_info_block_right_locatie_address2").html(strAddress2);
					$("#wedstrijd_info_block_right_locatie_website a").html(json[0].Website).attr('href','http://'+json[0].Website);

					if (json[0].Address.Route != null) {
						$("#wedstrijd_info_block_right_locatie_routeLink").show();
						$("#wedstrijd_info_block_right_locatie_route").html(json[0].Address.Route);
					}

					if (json[0].Coordinates.Latitude != "" && json[0].Coordinates.Longitude != "" && json[0].Coordinates.Latitude != 0 && json[0].Coordinates.Longitude != 0) {
						$("#wedstrijd_locatie_map_img").attr("src", "https://maps.googleapis.com/maps/api/staticmap?center=" + json[0].Coordinates.Latitude + "," + json[0].Coordinates.Longitude + "&markers=icon:http://kampongnieuw.lisa-is.nl/Styles/Images/map_pin.png|" + json[0].Coordinates.Latitude + "," + json[0].Coordinates.Longitude + "&zoom=14&size=420x200&sensor=false&visual_refresh=true");
						$("#wedstrijd_locatie_map").show();
					}
					else {
						$("#wedstrijd_locatie_map_img").attr("src", "");
					}

				}else{
					if (json[0].Referees != null && json[0].Referees != "")
					{
						$('.wedstrijd_overview_row_referees').show();
						$("#wedstrijd_info_block_right_matchdetails_referees").html(json[0].Referees);
					}
				}

				// Zaal dus scheidsrechters tonen
				if( json[0].IsIndoor == "True" || json[0].IsHome == "Thuis")
				{
					if (json[0].Referees != null && json[0].Referees != "")
					{
						$('.wedstrijd_overview_row_referees').show();
						$("#wedstrijd_info_block_right_matchdetails_referees").html(json[0].Referees);
					}
				}

				var iStand = json[0].Standings.length;

				$("tr.wedstrijdpouleStandRow:gt(0)").remove();

				var trClone = $("tr.wedstrijdpouleStandRow:last").clone();

				if (iStand > 0) {
					for (var i = 0; i < json[0].Standings.length; i++) {

						e = json[0].Standings[i];

						$("tr.wedstrijdpouleStandRow:last").after("<tr class='wedstrijdpouleStandRow'>" + trClone.html() + "</tr>");

						$("tr.wedstrijdpouleStandRow:last td .wedstrijdpouleStandCounter").html(i + 1);
						$("tr.wedstrijdpouleStandRow:last td .wedstrijdpouleStandTeam").html(e.TeamName);
						$("tr.wedstrijdpouleStandRow:last td .wedstrijdpouleStandMatches").html(e.TotalMatches);
						$("tr.wedstrijdpouleStandRow:last td .wedstrijdpouleStandPoints").html(e.TotalPoints);
					}

					$("tr.wedstrijdpouleStandRow:first").hide();
					$("#wedstrijdpouleStand").show();
				}
				else {
					$("#wedstrijdpouleStand").hide();
				}
				$("#overview_info").insertAfter($(obj));
				$("#overview_info").show();

            }
        });
    }
}

function showTrainingInfo(obj) {
    $("#overview_info").hide();
    $("#training_info_block_right_tainingdetails_trainers").html("");
    $("#training_info_block_right_tainingdetails_memo").html("");
    $("#training_info_block_right_tainingdetails_locationpart").html("");

    if ($(obj).attr("class") == "overview_row overview_row_visited") {
        $(obj).removeClass("overview_row_visited");
    } else {
        $(".overview_row").each(function () {
            $(this).removeClass("overview_row_visited");
        });
        obj.addClass("overview_row_visited");
        var iTrainingId = obj.children(":first").val();
		var iIndoor = obj.children(':first').next().val();

		var jsonObj = {};
		jsonObj.iTrainingID = iTrainingId;
		jsonObj.iIndoor = iIndoor;

        $.ajax({
            type: 'POST',
            url: 'overzichten/ajax/getTrainingInfoById.asp',
            data: jsonObj,
            dataType: "json",
			error: function(jar){

			},
            success: function (json) {

				var strLocation = "";
				$("#training_info_block_right_trainingdetails_trainers").html(json[0].Trainers);
				$("#training_info_block_right_trainingdetails_memo").html(json[0].Memo);

				if (json[0].iIndoor == "0")
				{
					if (json[0].LocationPart != "") {
						if (json[0].LocationPart == "1") {
							strLocation = "Fiets";
						}
						else {
							strLocation = "Spoor";
						}
					}
					$('.training_info_zaaltraininginfo').hide();
					$("#training_info_block_right_trainingdetails_locationpart").html(strLocation);
					$(".training_info_veldtraininginfo").show();
				}else{
					$('#training_info_block_right_trainingdetails_location').html(json[0].Location);
					$('#training_info_block_right_trainingdetails_locationaddress').html(json[0].LocationAddress);
					$('#training_info_block_right_trainingdetails_locationcity').html(json[0].LocationCity);
					$('.training_info_veldtraininginfo').hide();
					$('.training_info_zaaltraininginfo').show();
				}
				$("#overview_info").insertAfter($(obj));
				$("#overview_info").show();
            }
        });
    }
}

function showPouleStandings(obj) {
	$("#overview_info").hide();

    if ($(obj).attr("class") == "overview_row overview_row_visited") {
        $(obj).removeClass("overview_row_visited");
    } else {
		$(".overview_row").each(function () {
            $(this).removeClass("overview_row_visited");
        });
		$(obj).addClass("overview_row_visited");
		var iPouleId = obj.find("input[name='iPouleId']").val();
		var iTeamCode = obj.find("input[name='iTeamCode']").val();
		var jsonObj = {};
		jsonObj.iPouleId = iPouleId;
		jsonObj.iTeamCode = iTeamCode;
		$.ajax({
			type: 'POST',
			url: 'overzichten/ajax/getPouleByPouleId.asp',
			data: jsonObj,
			dataType: "json",
			success: function (json) {
				var iStand = json[0].Standings.length;

				$("tr.wedstrijdpouleStandRow:gt(0)").remove();

				var trClone = $("tr.wedstrijdpouleStandRow:last").clone();

				if (iStand > 0) {
					for (var i = 0; i < json[0].Standings.length; i++) {
						e = json[0].Standings[i];
						$("tr.wedstrijdpouleStandRow:last").after("<tr class='wedstrijdpouleStandRow'>" + trClone.html() + "</tr>");
						$("tr.wedstrijdpouleStandRow:last td .wedstrijdpouleStandCounter").html(i + 1);
						$("tr.wedstrijdpouleStandRow:last td .wedstrijdpouleStandTeam").html(e.TeamName);
						$("tr.wedstrijdpouleStandRow:last td .wedstrijdpouleStandMatches").html(e.TotalMatches);
						$("tr.wedstrijdpouleStandRow:last td .wedstrijdpouleStandPoints").html(e.TotalPoints);
						if (e.bOwnTeam == 'True'){
							$("tr.wedstrijdpouleStandRow:last td .wedstrijdpouleStandTeam").css('font-weight','bold');
						}
					}
					$("tr.wedstrijdpouleStandRow:first").hide();
					$("#wedstrijdpouleStand").show();
				}
				else {
					$("#wedstrijdpouleStand").hide();
				}
				$("#overview_info").insertAfter($(obj));
				$("#overview_info").show();
			}
		});
	}
}
