jQuery.noConflict();
(function ($) {
    $j(function () {
        initMap();
    });
    var initMap = function () {
        var mapContainer = $j("locationsMap");
        if (mapContainer != null) {
            var jsonRequest = $.getJSON("/index.php?c=venues&a=get-locations", addMarkers);
            $j(document.body).unload(GUnload());
        }
    };
    var venues = new Array();
    var map;
    var venuePoint;
    var windowOptions;

    function createMarker(point, venue) {
        //var infoHtml = "";
        var customIcon = new GIcon();
        customIcon.image = "http://www.etcvenues.co.uk/images/pink-map-locator.png";
        customIcon.iconSize = new GSize(24, 24);
        customIcon.iconAnchor = new GPoint(12, 12);
        customIcon.infoWindowAnchor = new GPoint(12, 1);
        markerOptions = {
            icon: customIcon
        };
        var marker = new GMarker(point, markerOptions);
    
        var infoTabs = [
		new GInfoWindowTab("Info", '<div class="gMapBubble"><p><img src="/images/get/' + venue.mapImageId + '" width="265" height="75" alt="' + venue.mapImageCaption + '" title="' + venue.mapImageCaption + '" /></p><p><strong>' + venue.name + '</strong><br />' + venue.address1 + ', ' + venue.town + ', ' + venue.postcode + '</p><p><strong>Sales: </strong>' + venue.telephone_sales + '<br /><strong>Switchboard: </strong>' + venue.telephone_switchboard + '<br /><a href="/venues/' + venue.urlTitle + '" title="View more information about this venue" target="_parent"><img src="/images/gmap-more-button.jpg" title="More Details" alt="More Details" width="42" height="14" align="right" style="margin-right: 10px; border:0;" /></a><strong>Email: </strong><a href="mailto:' + venue.email + '" title="Email ' + venue.name + '">' + venue.email + '</a></p></div>'),
                new GInfoWindowTab("Directions", '<div class="gMapBubble">' + venue.directions + '</div>'),
                new GInfoWindowTab("Plan Route", '<div class="gMapBubble"><h4>walking directions...</h4><p>Enter your starting point (e.g. postcode or tube station) to get walking directions to ' + venue.name + '.</p><form action="http://www.walkit.com/walkit_pipe.php" target="_blank" method="post" id="walkit_form"><input type="hidden" name="walkit_to_id" value="' + venue.walkitId + '" id="walkit_to_id" /><input type="hidden" name="walkit_to" value="' + venue.latitude + ',' + venue.longitude + '" id="walkit_to" /><input type="hidden" name="walkit_city" value="' + venue.walkitCity + '" id="walkit_city" /><input type="hidden" name="referid" value="6geo07o1as08kwg48sg00wgc0" id="referid" /><input type="hidden" name="refproprid" value="0" id="refproprid" /><input name="walkit_from" type="text" class="inputField" id="walkit_from" /><input name="walkit_go" value="Go" type="submit" id="Button1" /></form><h4>driving directions...</h4><p>Get driving directions to the ' + venue.name + ' simply by entering your postcode below</p><form action="/" id="directionsForm" name=directionsForm" method="post" target="_blank"><input class="inputField" type="text" id="postcode"><input type="hidden" id="destination" name="destinaton" value="' + venue.postcode + '" /><input type="button" onclick="getDirections();" value="Go"></form>' + '</div>')
	];
        
        GEvent.addListener(marker, "click", function () {
            marker.openInfoWindowTabsHtml(infoTabs);
        });
        return marker;
    }
    var mapLoad = function () {
        if (GBrowserIsCompatible()) {
            map = new GMap2(document.getElementById("locationsMap"));
            map.setCenter(new GLatLng(51.507574, -0.108747), 13);
            map.addControl(new GLargeMapControl3D());
            map.addControl(new GOverviewMapControl());
            map.addControl(new GMapTypeControl());
        }
    };

    var addMarkers = function (locations) {
        mapLoad();
        $.each(locations.London, function (i, item) {
            var tempMark = createMarker(new GLatLng(item.latitude, item.longitude), item);
            map.addOverlay(tempMark);
        });
    };
})(jQuery);