﻿var markersArray = [];
var mrks = new Array();


$(document).ready(function () {
    $(".directions").hide();
    $(".address").hide();
    $(".btnPrint").hide();
    //$(".map").css("width", "500px");

    $(".btnDirections").each(function () {
        $(this).click(function () {
            var regIndex = /\d+$/;

            var divAddress = $(this).parent().find(".address").get(0);
            var directions = $(this).parent().find(".directions").get(0);
            var btPrint = $(this).parent().find(".btnPrint").get(0);
            var btnDirections = $(this).parent().find(".btnDirections").get(0);
           
            $(this).parent().find(".map").hide();
            
           

            $(divAddress).show();
            $(btPrint).hide();
            $(directions).hide();
            $(btnDirections).hide();

        });
    });


    $(".mapit").each(function () {
        $(this).click(function () {
            var regIndex = /\d+$/;

            var divAddress = $(this).parent().parent().find(".address").get(0);
            var directions = $(this).parent().parent().find(".directions").get(0);
            var btPrint = $(this).parent().parent().find(".btnPrint").get(0);
            $(this).parent().parent().find(".map").show();
            $(divAddress).show();
            $(btPrint).show();


            $(divAddress).hide();
            $(directions).show();



            var divAddress = $(this).parent().parent().find(".map").get(0);
            var index = $(divAddress).attr("id").match(regIndex);
            var address = $(divAddress).attr("address");


            var origAddr = $(this).parent().parent().find(".Address1").get(0).value;
            origAddr = origAddr + ' ' + $(this).parent().parent().find(".Address2").get(0).value;
            origAddr = origAddr + ', ' + $(this).parent().parent().find(".City").get(0).value;
            origAddr = origAddr + ', ' + $(this).parent().parent().find(".State").get(0).value;
            origAddr = origAddr + ' ' + $(this).parent().parent().find(".Zip").get(0).value;


            var map;
            map = mrks[index][0].map;
            //alert(mrks[index][0].map)
            mrks[index][0].setMap(null);


            //******************************************************************/
            var directionsRenderer = new google.maps.DirectionsRenderer();


            directionsRenderer.setMap(map);
            directionsRenderer.setPanel($(this).parent().parent().find(".directions").get(0));


            var directionsService = new google.maps.DirectionsService();
            var request = {
                origin: origAddr,
                destination: address,
                travelMode: google.maps.DirectionsTravelMode.DRIVING,
                unitSystem: google.maps.DirectionsUnitSystem.ENGLISH //,

            };
            directionsService.route(request, function (response, status) {
                if (status == google.maps.DirectionsStatus.OK) {
                    directionsRenderer.setDirections(response);
                } else {
                    alert('Error: ' + status);
                }
            });





            //*******************************************************


        });

    });


    $(".btnPrint").each(function () {
        $(this).click(function () {
            //http://www.huntingground.freeserve.co.uk/main/mainfram.htm?../scripts/printing.htm


            var directions = $(this).parent().parent().find(".directions").get(0).innerHTML;
            //alert(directions);
            var str = directions;

            //function printContent(id) {
            //str = document.getElementById(id).innerHTML
            newwin = window.open('', 'printwin', 'left=100,top=100,width=400,height=400')
            newwin.document.write('<HTML>\n<HEAD>\n')
            newwin.document.write('<TITLE>Print Page</TITLE>\n')
            newwin.document.write('<script>\n')
            newwin.document.write('function chkstate(){\n')
            newwin.document.write('if(document.readyState=="complete"){\n')
            newwin.document.write('window.close()\n')
            newwin.document.write('}\n')
            newwin.document.write('else{\n')
            newwin.document.write('setTimeout("chkstate()",2000)\n')
            newwin.document.write('}\n')
            newwin.document.write('}\n')
            newwin.document.write('function print_win(){\n')
            newwin.document.write('window.print();\n')
            newwin.document.write('chkstate();\n')
            newwin.document.write('}\n')
            newwin.document.write('<\/script>\n')
            newwin.document.write('</HEAD>\n')
            newwin.document.write('<BODY onload="print_win()">\n')
            newwin.document.write(str)
            newwin.document.write('</BODY>\n')
            newwin.document.write('</HTML>\n')
            newwin.document.close()
            //}












        });



    });








    $(".map").each(function () {
        var regIndex = /\d+$/;


        var divAddress = $(this).get(0);
        var index = $(divAddress).attr("id").match(regIndex);
        var address = $(divAddress).attr("address");


        var latlng = new google.maps.LatLng(0, 0);
        var myOptions = {
            zoom: parseInt($(this).attr("zoom")),
            mapTypeId: google.maps.MapTypeId.ROADMAP
        }


        var map;
        map = new google.maps.Map($(this).get(0), myOptions);

        var directionDisplay = new google.maps.DirectionsRenderer();
        var geocoder = new google.maps.Geocoder();

        directionDisplay.setMap(map);


        if (geocoder) {
            geocoder.geocode({ 'address': address }, function (results, status) {
                if (status == google.maps.GeocoderStatus.OK) {

                    map.setCenter(results[0].geometry.location);

                    addMarker(results[0].geometry.location, map, index);
                } else {
                    alert("Geocode was not successful for the following reason: " + status);
                }
            });


        }



    });






});




function addMarker(location, map,index) {
    marker = new google.maps.Marker({
        position: location,
        map: map
    });
    //markersArray.push(marker);
    if (mrks[index] == null)
        mrks[index] = new Array();
    mrks[index].push(marker);
}



function ReAddWayPoints() {
    if (markersArray) {
        for (i in markersArray) {
            markersArray[i].setMap(map);
        }
    }

}


//http://net.tutsplus.com/tutorials/javascript-ajax/turn-by-turn-directions-with-the-google-maps-api/

//Really nice article on directions, may also provide other articles.
//http://googlegeodevelopers.blogspot.com/2009/10/directions-in-maps-api-v3-where-will.html  
function overlayDirections() {

    clearOverlays();

    var directionsRenderer = new google.maps.DirectionsRenderer();
    directionsRenderer.setMap(map);
    directionsRenderer.setPanel(document.getElementById('diretions1'));

    var directionsService = new google.maps.DirectionsService();
    var request = {
        origin: siteLocation,
        destination: siteDestination,
        travelMode: google.maps.DirectionsTravelMode.DRIVING,
        unitSystem: google.maps.DirectionsUnitSystem.ENGLISH //,
        //provideTripAlternatives: true
    };
    directionsService.route(request, function (response, status) {
        if (status == google.maps.DirectionsStatus.OK) {
            directionsRenderer.setDirections(response);
        } else {
            alert('Error: ' + status);
        }
    });

}










