﻿function getRandomInt(min, max) {
    return Math.floor(Math.random() * (max - min + 1)) + min;
}

function arrayLength(array) {
    var length = 0;

    for (var object in array) {
        length++;
    }

    return length;
}

function initialize() {
    var map = new GMap2($("#map").get(0));

    var newZealand = new Array();
    newZealand["center"] = new GLatLng(-41.310824, 174.462891); // Center on NZ
    newZealand["Mount Roskill"] = new GLatLng(-36.90460, 174.74170);
    newZealand["Matamata"] = new GLatLng(-37.80957, 175.77325);
    newZealand["Timaru"] = new GLatLng(-44.39680, 171.25010);
    newZealand["Wanaka"] = new GLatLng(-44.69867, 169.13289);
    newZealand["Whitby"] = new GLatLng(-41.11129, 174.88840);
    newZealand["Wellington"] = new GLatLng(-41.28648, 174.77622);
    newZealand["Cambridge"] = new GLatLng(-37.89149, 175.46974);
    newZealand["Tawa"] = new GLatLng(-41.17157, 174.82479);
    newZealand["New Lynn"] = new GLatLng(-36.90979, 174.68339);
    newZealand["Kaiapoi"] = new GLatLng(-43.38328, 172.65364);
    newZealand["Ashburton"] = new GLatLng(-43.90250, 171.74933);
    newZealand["Dunedin"] = new GLatLng(-45.87456, 170.50339);
    newZealand["Cromwell"] = new GLatLng(-45.04294, 169.19669);
    newZealand["Christchurch"] = new GLatLng(-43.53164, 172.63664);
    newZealand["Blenheim"] = new GLatLng(-41.51646, 173.95324);
    newZealand["South Westland"] = new GLatLng(-43.38333, 170.18333);
    newZealand["Queenstown"] = new GLatLng(-45.03110, 168.66273);
    newZealand["Akaroa"] = new GLatLng(-43.80603, 172.96657);
    newZealand["Kaiapoi"] = new GLatLng(-43.38328, 172.65364);
    newZealand["Invercargill"] = new GLatLng(-46.41306, 168.35688);
    newZealand["Nelson"] = new GLatLng(-41.27079, 173.28400);
    newZealand["Manurewa"] = new GLatLng(-37.02127, 174.89590);
    newZealand["Lower Hutt"] = new GLatLng(-41.20843, 174.90453);
    newZealand["Otautau"] = new GLatLng(-46.14267, 167.99696);
    newZealand["Richmond"] = new GLatLng(-43.51541, 172.65168);
    newZealand["Beckenham"] = new GLatLng(-43.56322, 172.64366);
    newZealand["Harewood"] = new GLatLng(-43.47979, 172.56241);
    newZealand["Lake Tekapo"] = new GLatLng(-44.00736, 170.48338);
    newZealand["Timaru"] = new GLatLng(-44.39680, 171.25010);
    newZealand["Howick"] = new GLatLng(-36.89695, 174.92806);
    newZealand["Woodend"] = new GLatLng(-43.32040, 172.66293);
    newZealand["Leeston"] = new GLatLng(-43.76376, 172.29531);
    newZealand["Mangere East"] = new GLatLng(-36.96613, 174.82593);
    newZealand["Oxford"] = new GLatLng(-43.29909, 172.18453);
    newZealand["Wainuiomata"] = new GLatLng(-41.26142, 174.95084);
    var newZealandLength = arrayLength(newZealand) - 1;

    // Center our map
    map.setCenter(newZealand["center"], 6);

    // Show each location only once		
    for (var i in newZealand) {
        nzMarker = new GMarker(newZealand[i]);
        map.addOverlay(nzMarker);
    }

    // Load our testimonials
    var points = [
    // 0
						[newZealand["Mount Roskill"],
						"The Adviser seemed an honest and genuine guy. He was always easy to get in touch with and if I left a message, he always returned my call promptly. Thanks for the great service.",
						"Mrs H"],
						[newZealand["Matamata"],
						"I found the Adviser very thorough in explaining the Health Insurance and in answering my queries",
						"Mrs Mennie"],
						[newZealand["Timaru"],
						"I found Denise very helpful. She was not pushy like other insurance people. A refreshing change - I want to keep her on.",
						"Ms M"],
						[newZealand["Wanaka"],
						"Thank-you for listening to what our needs were, and explaining fully in a way we could understand. Very professional - but you also didn't make us feel like we were just another client.",
						"Mr L &amp; Mrs E"],
						[newZealand["Whitby"],
						"Was very personable and took his time. The man (John) who set up the initial interview was also excellent.",
						"Ms Davis"],
						[newZealand["Wellington"],
						"Helped us make informed decisions. Excellent from the initial phone call to follow-up. Have already given your name to two others.",
						"Mrs V"],
						[newZealand["Cambridge"],
						"First impressions brilliant.",
						"Mr &amp; Mrs S"],
						[newZealand["Tawa"],
						"The Adviser gave me a thorough understanding of what he had for me, making it easy for me to understand what I was getting for my money.",
						"Mr P"],
						[newZealand["New Lynn"],
						"Well presented salesman &ndash; good product knowledge &ndash; very helpful.",
						"Mr &amp; Mrs L"],
						[newZealand["Wellington"],
						"Andrew is a fantastic guy and very approachable.",
						"Mr J"],
    // 10
						[newZealand["Wellington"],
						"Very good Adviser (Chris Boon). Confident. Able to address concerns well. Family manner.",
						"Mr &amp; Mrs S"],
						[newZealand["Kaiapoi"],
						"Andrew Ross was very honest, he was clear and I was impressed with his genuine concern.",
						"Mrs C"],
						[newZealand["Ashburton"],
						"(John) Nice to deal with and on time.",
						"Mr &amp; Mrs W"],
						[newZealand["Tawa"],
						"(The Adviser) was fantastic, bright and bubbly, and more than happy to answer any questions. 10 out of 10.",
						"Mr &amp; Mrs H"],
						[newZealand["Dunedin"],
						"A good policy at a fair price.  The complete procedure has been made very clear by John in an understandable way.",
						"Mr G &amp; Mrs J"],
						[newZealand["Cromwell"],
						"He (Phil Ross) was great to deal with and will recommend to family and friends.",
						"Mr C"],
						[newZealand["Ashburton"],
						"Very pleased with extent of cover. Thanks for pen &ndash; appreciated.",
						"Mr &amp; Mrs M"],
						[newZealand["Christchurch"],
						"It was the elderly man on the telephone that caught my interest and organised the appointment &ndash; thank you for the pen.",
						"Mrs B"],
						[newZealand["Blenheim"],
						"Very Professional (Chris Goddard)",
						"Mr &amp; Mrs B"],
						[newZealand["South Westland"],
						"Already have recommended to neighbours who have taken out a policy.",
						"Mr C & Miss G"],
    // 20
						[newZealand["Timaru"],
						"Phil was very helpful and knowledgeable.",
						"Mr &amp; Mrs S"],
						[newZealand["Christchurch"],
						"Both of us were very happy with the way in which Andrew helped us. He provided us with a very good personalised service.",
						"Mr &amp; Mrs M"],
						[newZealand["Queenstown"],
						"It was ideal for me to have an Adviser come to my house - this is a great part of your service.",
						"Mrs G"],
						[newZealand["Akaroa"],
						"After working 33 years in the Insurance Industry it is rewarding to me to be able to award Absolute Insurance &amp; (John Staite) &lsquo;Top Marks&rsquo;.",
						"Mr &amp; Mrs T"],
						[newZealand["Kaiapoi"],
						"Have already recommended you (Andrew Ross) to friends and family.",
						"Mr &amp; Mrs J"],
						[newZealand["Wellington"],
						"Friendly Adviser (Chris Boon) and office staff. Did what they promised.",
						"Mr J &amp; Mrs L"],
						[newZealand["Christchurch"],
						"Chris provided written documentation that supported the comparisons from our original Health policy to the benefits of our current needs that Absolute Insurance was able to provide. I felt empowered to make a definite decision.",
						"Mr &amp; Mrs T"],
						[newZealand["Invercargill"],
						"Very friendly and helpful service (John Staite).",
						"Mr &amp; Mrs S"],
						[newZealand["Nelson"],
						"The Adviser was very thorough, professional, thoughtful and delightful.",
						"Mr V &amp; Mrs B"],
						[newZealand["Manurewa"],
						"Rockie explained the policy concerned so that we could undertand. We never felt pressured at any time.",
						"Mr &amp; Mrs L"],
    // 30
						[newZealand["Lower Hutt"],
						"The Adviser&rsquo;s manner was excellent, made the meeting pleasureable, gave the feeling of not hard selling. A very pleasant, approachable  manner. Professional Adviser.",
						"Mr &amp; Mrs E"],
						[newZealand["Otautau"],
						"Good follow-up. Friendly on the phone.",
						"Mr &amp; Mrs A"],
						[newZealand["Wellington"],
						"Very good follow up.",
						"Mr C"],
						[newZealand["Christchurch"],
						"Chris Boon was pleasant and efficient and replied to any queries quickly.",
						"Mr &amp; Mrs W"],
						[newZealand["Richmond"],
						"Lovely friendly lady. Listened to our needs but wasn&rsquo;t pushy.",
						"Mr &amp; Mrs Day"],
						[newZealand["Beckenham"],
						"Chris explained things very clearly to us and was most informative &ndash; would be a strong recommendation to any future client.",
						"Mr &amp; Mrs H"],
						[newZealand["Christchurch"],
						"This is our first policy and we are very happy with it, and how easy it was to set up.",
						"Mr &amp; Mrs N"],
						[newZealand["Harewood"],
						"Very friendly and professional (Chris Goddard)",
						"Mr B"],
						[newZealand["Lake Tekapo"],
						"The Adviser was very easy to deal with and punctual to meetings.",
						"Mr M"],
						[newZealand["Timaru"],
						"I understood more from Phil Ross than any other insurance person.",
						"Mrs A"],
    // 40
						[newZealand["Christchurch"],
						"Chris Goddard was very professional and all things happened as he said they would. Very efficient.",
						"Ms H"],
						[newZealand["Wellington"],
						"Excellent experiece.",
						"Mr &amp; Mrs W"],
						[newZealand["Akaroa"],
						"John Staite is a very nice man and did all in his power to help me understand what I am insured for. Thank you.",
						"Mrs D"],
						[newZealand["Howick"],
						"Rockie was a pleasure to deal with.",
						"Mr &amp; Mrs R"],
						[newZealand["Woodend"],
						"Very impressed with Andrew's approach.",
						"Mrs J"],
						[newZealand["Leeston"],
						"Having been cautious of medical insurance agents before, I found Phil totally honest and no pressure to take out a policy, just the facts and helpful advice.",
						"Mr &amp; Mrs M"],
						[newZealand["Mangere East"],
						"Rockie turned up on time and ensured we understood the policy fully. Very pleased with his professional conduct.",
						"Mr &amp; Mrs I"],
						[newZealand["Christchurch"],
						"We appreciate the personal contact with Chris.",
						"Mr &amp; Mrs C"],
						[newZealand["Oxford"],
						"Our broker was very professional and caring. Thank you Chris.",
						"Mr &amp; Mrs O"],
						[newZealand["Christchurch"],
						"The consideration and care by Chris G has been very impressive &ndash; unlike any previous dealings I have had with insurance companies &ndash; great!",
						"Mr C"],
    // 50
						[newZealand["Christchurch"],
						"Very professional &ndash; always returns phone calls promptly.",
						"Mr C"],
						[newZealand["Christchurch"],
						"Andrew was professional yet friendly and helpful.",
						"Mrs L"],
						[newZealand["Ashburton"],
						"Mr Staite&rsquo;s approach and manner is to be admired.",
						"Mrs C"],
						[newZealand["Nelson"],
						"The Adviser was well dressed and an excellent adviser. There was no pressure and advice I found valuable. The nice thing about it no rushing anything through and he explained everything in simple plain language.",
						"Mrs Q"],
						[newZealand["Wainuiomata"],
						"The Adviser was very friendly, listened to us and professional. Compared to someone else we had in a while ago, he was fantastic. Highly recommend him and thank heaps for your help.",
						"Mr &amp; Mrs J"]
					];

    /*
    var bounds = map.getBounds();
    var southWest = bounds.getSouthWest();
    var northEast = bounds.getNorthEast();
    var lngSpan = northEast.lng() - southWest.lng();
    var latSpan = northEast.lat() - southWest.lat();
    */
    var emptyIcon = new GIcon();
    emptyIcon.image = "/Custom/AbsoluteInsurance/Images/spacer.gif";
    emptyIcon.shadow = "/Custom/AbsoluteInsurance/Images/spacer.gif";
    emptyIcon.iconSize = new GSize(1, 1);
    emptyIcon.shadowSize = new GSize(1, 1);
    emptyIcon.iconAnchor = new GPoint(1, 1);
    emptyIcon.infoWindowAnchor = new GPoint(1, 1);
    // Set up our GMarkerOptions object literal
    markerOptions = { icon: emptyIcon };


    var markers = [];
    for (var i = 0; i < points.length; i++) {
        // var point = new GLatLng(southWest.lat() + latSpan * Math.random(), southWest.lng() + lngSpan * Math.random());
        //var point = new GLatLng(points[i]);
        marker = new GMarker(points[i][0], markerOptions);
        map.addOverlay(marker);

        // Disable most map interaction
        map.disableDragging();
        map.disableDoubleClickZoom();
        map.disableContinuousZoom();
        map.disableScrollWheelZoom();
        
        markers[i] = marker;
        // $("#map_points").html($("#map_points").html() + points[i][0] + " : " + i + "<br />");
    }

    $(markers).each(function(i, marker) {
        /*
        $("<li />")
        .html("Point "+i)
        .click(function(){
        displayPoint(marker, i);
        })
        .appendTo("#list");
        */
        /*
        GEvent.addListener(marker, "click", function() {
            displayPoint(marker, i);
        });
        */
    });

    $(".speech-mark-pointer, #message").appendTo(map.getPane(G_MAP_FLOAT_SHADOW_PANE));

    var ii = 0;

    function displayPoint(marker, index) {
        $(".speech-mark-pointer, #message").hide();

        var moveEnd = GEvent.addListener(map, "moveend", function() {
            var markerOffset = map.fromLatLngToDivPixel(marker.getLatLng());
            $(".speech-mark-pointer")
					.fadeIn()
					.css({ top: markerOffset.y, left: markerOffset.x });
            $("#message")
					.fadeIn()
					.css({ top: (markerOffset.y + 11), left: markerOffset.x })
					.html("<p>&ldquo;" + points[index][1] + "&rdquo;</p>" + "<cite>&mdash; " + points[index][2] + "</cite>");

            GEvent.removeListener(moveEnd);
        });
        map.panTo(marker.getLatLng());

        ii = getRandomInt(0, (points.length - 1));
        /*
        if (ii == (points.length - 1)) {
        ii = 0;
        } else {
        ii++;
        }
        */

        setTimeout(function() { displayPoint(markers[ii], ii); }, 5000);
    }

    setTimeout(function() { displayPoint(markers[ii], ii); }, 3500);
}