﻿function makeMarker(info) {
    if (info) {
        var thepoint;
        if ((info.lat != null) && (info.lng != null)) {
            thepoint = new GLatLng(info.lat, info.lng);
        }
        var runMark = function() {
            if (thepoint) {
                if (lowLat == '') {
                    lowLat = thepoint.lat();
                    hiLat = thepoint.lat();
                    lowLng = thepoint.lng();
                    hiLng = thepoint.lng();
                }
                else {
                    if (thepoint.lat() < lowLat) {
                        lowLat = thepoint.lat().toFloat();
                    }
                    else if (thepoint.lat() > hiLat) {
                        hiLat = thepoint.lat().toFloat();
                    }
                    if (thepoint.lng() < lowLng) {
                        lowLng = thepoint.lng().toFloat();
                    }
                    else if (thepoint.lng() > hiLng) {
                        hiLng = thepoint.lng().toFloat();
                    }
                }
                var thisIcon = new GIcon(G_DEFAULT_ICON);
                var thisAddress = info.address + " ";
                if (info.city != null) {
                    thisAddress = thisAddress + info.city + ", ";
                }
                if (info.state != null) {
                    thisAddress = thisAddress + info.state + " ";
                }
                if (info.zip != null) {
                    thisAddress = thisAddress + info.zip;
                }

                var iconLocation;
                var myClass;

                var writeLeft = true;
                switch (info.type) {
                    case 'express':
                        info.title = 'Express Care';
                        myClass = 'express';
                        iconLocation = "./images/express.png";
                        thisIcon.shadow = "./images/express_shadow.png";
                        break;

                    case 'vioc':
                        info.title = 'Valvoline Instant Oil Change';
                        myClass = 'vioc';
                        iconLocation = "./images/vioc.png";
                        thisIcon.shadow = "./images/vioc_shadow.png";
                        break;

                    case 'home':
                        writeLeft = false;
                        iconLocation = "./images/home.png";
                        break;

                    case 'diy':
                        var letter = String.fromCharCode("A".charCodeAt(0) + letterNum);
                        iconLocation = "http://www.google.com/mapfiles/marker_orange" + letter + ".png";
                        letterNum++;
                        break;

                    default:
                        var letter = String.fromCharCode("A".charCodeAt(0) + letterNum);
                        iconLocation = "http://www.google.com/mapfiles/marker" + letter + ".png";
                        letterNum++;
                }

                thisIcon.image = iconLocation;
                info.image = iconLocation;
                var markerOptions = { icon: thisIcon, zIndexFunction: myIconZ };
                var marker = new GMarker(thepoint, markerOptions);

                if (writeLeft && $('locations')) {

                    var thisDiv = buildDiv(info);
                    var thisInfo = buildInfo(info);
                    if (myClass) {
                        thisDiv.addClass(myClass);
                    }

                    var gaTitle = 'pageTracker._trackPageview(\'/locationSearch/' + encodeURI(homeAddress) + '/' + getFilter() + '/info/title/' + info.id + '\')';
                    var gaIcon = 'pageTracker._trackPageview(\'/locationSearch/' + encodeURI(homeAddress) + '/' + getFilter() + '/info/icon/' + info.id + '\')';

                    GEvent.addDomListener(thisDiv.getElement('.title'), "click", function() { map.openInfoWindowHtml(thepoint, thisInfo.get('html')); eval(gaTitle); });
                    GEvent.addDomListener(thisDiv.getElement('.icon'), "click", function() { map.openInfoWindowHtml(thepoint, thisInfo.get('html')); eval(gaIcon) });

                    thisDiv.inject($('locations').getElement('div'));
                    GEvent.addListener(marker, "click", function() { map.openInfoWindowHtml(thepoint, thisInfo.get('html')); });
                }
                else {
                    var gaMark = 'pageTracker._trackPageview(\'/locationSearch/' + encodeURI(homeAddress) + '/' + getFilter() + '/marker/' + info.id + '\')';
                    GEvent.addListener(marker, "click", function() { map.openInfoWindowHtml(thepoint, info.address); eval(gaMark); });
                }
                map.addOverlay(marker);
            }
        }
        runMark.run();
    }
}