﻿var markers = new Array();
var map;
var markersArray = new Array();
var infoWindowArray = new Array();
var map;
var bounds = new google.maps.LatLngBounds();
var directionDisplay;
var directionsService = new google.maps.DirectionsService();
var haight = new google.maps.LatLng(37.7699298, -122.4469157);

jQuery(document).ready(function () {
  $("a[rel^='prettyPhoto']").prettyPhoto( {social_tools:"", deeplinking:false});

  /* Count number of items */
  jQuery("a[href=#nyheder]").append( " (" + jQuery("div#nyheder tr").length + ")" );  
  jQuery("a[href=#aktiviteter]").append( " (" + jQuery("div#aktiviteter tr").length + ")" );
  jQuery("a[href=#tourneringer]").append( " (" + jQuery("div#tourneringer tr").length + ")" );
  
    if ($("#frontpageTabs").length > 0) {
        $("#frontpageTabs").idTabs(function (id) {
            link = "/Herning-volleyball-klub/Nyheder.aspx";
            text = "FLERE NYHEDER";
            switch (id) {
                case "#aktiviteter":
                    link = "/Herning-volleyball-klub/Aktiviteter.aspx";
                    text = "FLERE AKTIVITETER";
                    break;
                case "#tourneringer":
                    link = "/Herning-volleyball-klub/Turnering.aspx";
                    text = "TOURNERINGSPLAN";
                    break;
            }
            $("#tabsMoreLink").attr("href", link).text(text);


            return true;
        });
    }
    if (jQuery("#theMap").length > 0) {
        initGoogleMapLinks();
    }

    if (jQuery("#googleMap").length > 0) {
        initGoogleTournamentMap();
    }

    jQuery(".chkall").click(function () {
        var chkCount = jQuery(".chk").length;
        if (jQuery(".chk:checked").length != chkCount) {
            jQuery(".chk").attr("checked", "checked")
        } else {
            jQuery(".chk").removeAttr("checked")
        }

        
    });

});

function clearMarkers() {
    for (var i = 0; i < markers.length; i++) {
        markers[i].setMap(null);
        markers[i] = null;
    }
    markers.length = 0;
}

function clearWindows() {
    for (var i = 0; i < infoWindowArray.length; i++) {
        infoWindowArray[i].close();
        infoWindowArray[i] = null;
    }
    infoWindowArray.length = 0;
}

function initGoogleMapLinks() {
    var myOptions = {
        zoom: 15,
        mapTypeId: google.maps.MapTypeId.HYBRID,
        navigationControl: false,
        mapTypeControl: true,
        scaleControl: false,
        disableDefaultUI: false
    };
    map = new google.maps.Map(jQuery("#theMap").get(0), myOptions);

    if ($("a.mapLinks[adresse!='']").length > 0) {

        $("a.mapLinks[adresse!='']").fancybox({
            'hideOnContentClick': false,
            'onStart': function (obj) {
                directionsDisplay = new google.maps.DirectionsRenderer();
                var myOptions = {
                    zoom: 14,
                    mapTypeId: google.maps.MapTypeId.ROADMAP
                }
                directionsDisplay.setMap(map);

                //geocoder = new google.maps.Geocoder();
                var address = jQuery(obj).attr("adresse");
                calcRoute("Valdemarsvej 339, 7400 Herning", address);
                /*geocoder.geocode({ 'address': address }, function (results, status) {
                if (status == google.maps.GeocoderStatus.OK) {
                map.setCenter(results[0].geometry.location);
                var marker = new google.maps.Marker({
                map: map,
                title: address,
                position: results[0].geometry.location,
                icon: 'http://google-maps-icons.googlecode.com/files/tennis.png'
                });
                markers[markers.length] = marker;

                google.maps.event.addListener(marker, 'click', function () {
                map.setZoom(17);
                });
                } else {
                //alert("Geocode was not successful for the following reason: " + status);
                }
                });
                */
                //var map = null;
            },
            'onClosed': function () {
                directionsDisplay.setMap(null)
                clearMarkers();
                //marker.setMarker(null);
            }
        });
    }
}

function calcRoute(adr1, adr2) {
    var request = {
        origin: adr1,
        destination: adr2,
        // Note that Javascript allows us to access the constant
        // using square brackets and a string value as its
        // "property."
        travelMode: google.maps.DirectionsTravelMode["DRIVING"]
    };
    directionsService.route(request, function (response, status) {
        if (status == google.maps.DirectionsStatus.OK) {
            directionsDisplay.setDirections(response);
        }
    });
}

function initGoogleTournamentMap() {
    var myOptions = {
        zoom: 15,
        mapTypeId: google.maps.MapTypeId.ROADMAP,
        navigationControl: false,
        mapTypeControl: false,
        scaleControl: false,
        disableDefaultUI: true
    };
    map = new google.maps.Map(jQuery("#googleMap").get(0), myOptions);


    jQuery("a.mapLink[adresse!='']").each(function () {
        geocoder = new google.maps.Geocoder();
        var address = jQuery(this).attr("adresse");
        geocoder.geocode({ 'address': address }, function (results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
                map.setCenter(results[0].geometry.location);
                var marker = new google.maps.Marker({
                    map: map,
                    title: address,
                    position: results[0].geometry.location,
                    icon: 'http://google-maps-icons.googlecode.com/files/tennis.png'
                });

                var infowindow = new google.maps.InfoWindow({
                    content: '<div class="gTooltip">' + address + '</div>'
                });
                infoWindowArray.push(infowindow);

                bounds.extend(results[0].geometry.location);
                map.fitBounds(bounds);

                markersArray.push(marker);

                google.maps.event.addListener(marker, 'click', function () {
                    clearWindows();
                    //infowindow.open(map, marker);
                });
            } else {
               // alert("Geocode was not successful for the following reason: " + status + " | " + address);
            }
        });
    });
}
