google.load("maps", "2");
function initialize() {
    var strInputCode = document.getElementById("address_for_map").innerHTML;
 	strInputCode = strInputCode.replace(/&(lt|gt);/g, function (strMatch, p1){
	 	return (p1 == "lt")? "<" : ">";
	});
	var strTagStrippedText = strInputCode.replace(/<\/?[^>]+(>|$)/g, "");
    if (strTagStrippedText) {
        var address=strTagStrippedText;
    } else {
        var address="Liverpool";
    }
    var map = new GMap2(document.getElementById("googleMap"));
	var allMarkersArray = new Array();
    map.enableScrollWheelZoom();
    map.addControl(new GMapTypeControl());
    map.setUI(map.getDefaultUI());
    map.removeMapType(G_PHYSICAL_MAP);
    map.removeMapType(G_SATELLITE_MAP);
	G_NORMAL_MAP.getMinimumResolution = function() { return 8 };
    var mnpaIcon = new GIcon();
    mnpaIcon.image = 'http://www.mynetworkingpa.com/res/builtin/map/image.png';
    mnpaIcon.printImage = 'http://www.mynetworkingpa.com/res/builtin/map/printImage.gif';
    mnpaIcon.mozPrintImage = 'http://www.mynetworkingpa.com/res/builtin/map/mozPrintImage.gif';
    mnpaIcon.iconSize = new GSize(34,34);
    mnpaIcon.transparent = 'http://www.mynetworkingpa.com/res/builtin/map/transparent.png';
    mnpaIcon.iconAnchor = new GPoint(0,34);
    mnpaIcon.infoWindowAnchor = new GPoint(17,0);
    mnpaIcon.imageMap = [22,0,24,1,26,2,27,3,28,4,29,5,30,6,31,7,31,8,32,9,32,10,33,11,33,12,33,13,33,14,33,15,33,16,33,17,33,18,33,19,33,20,33,21,33,22,32,23,32,24,31,25,31,26,30,27,29,28,28,29,27,30,26,31,24,32,22,33,11,33,9,32,7,31,6,30,5,29,4,28,3,27,2,26,2,25,1,24,1,23,0,22,0,21,0,20,0,19,0,18,0,17,0,16,0,15,0,14,0,13,0,12,0,11,1,10,1,9,2,8,2,7,3,6,4,5,5,4,6,3,7,2,9,1,11,0];
    markerOptions = { icon:mnpaIcon };
    geocoder = new GClientGeocoder();
    geocoder.getLatLng(address,
        function(point) {
			map.setCenter(point,14);
			var bounds = map.getBounds();
			var bLat = bounds.getSouthWest().lat();
			var bLng = bounds.getSouthWest().lng();
			getMarkers(point.lat(),point.lng(),bLat,bLng);
        }
    );
	function listenerAction() {
		var bounds = map.getBounds();
		var bLat = bounds.getSouthWest().lat();
		var bLng = bounds.getSouthWest().lng();
		var newPoint = map.getCenter();
		getMarkers(newPoint.lat(),newPoint.lng(),bLat,bLng);
		$(function(){ $("#loadMap").fadeIn(); });	
	}
	GEvent.addListener(map, "dragend", function() { listenerAction(); });
	GEvent.addListener(map, "zoomend", function() { listenerAction(); });
	function getMarkers(lat,lng,bLat,bLng){
		var request = GXmlHttp.create();
		request.open("GET", "http://www.mynetworkingpa.com/markers.xml", true);
		request.onreadystatechange = function() {
			if (request.readyState == 4) {
				var xmlDoc = GXml.parse(request.responseText);
				var markers = xmlDoc.documentElement.getElementsByTagName("marker");
				var countMarkers = markers.length;
				map.clearOverlays();
				for (i=0;i<countMarkers;i++) {
                    var title = markers[i].getAttribute("title");
                    var mLink = markers[i].getAttribute("link");
					var point =  new GLatLng(parseFloat(markers[i].getAttribute("lat")),parseFloat(markers[i].getAttribute("lng")));                
					allMarkersArray[i] = createMarker(point,title,mLink);
				}
				$(function(){ $("#loadMap").fadeOut(); });
			}
		}
		request.send(null);	
	}
    function createMarker(point,title,mLink) {
        var marker = new GMarker(point,markerOptions);
        map.addOverlay(marker);
        GEvent.addListener(marker, "click", function() {
            marker.openCustomInfoWindow("<p class=\"map_title\">"+title+"</p><p class=\"map_description\">"+title+"</p><a href=\"http://www.mynetworkingpa.com/events/event-detail.phuse?id="+mLink+"&eventid="+mLink+"\" class=\"map_link\">Find out more</a>");
            map.panTo(point);
        });
        return marker;
    }
    $(function() {
		function load(){
			$("#loadMap").fadeIn("slow");
			var address=$("#search_input").val()+", UK";
			searchLocation(address);
			return false;
		}
        $("#search_function").click(function(){load();});
		$("#search_input").focus(function(){
			$(document).keypress(function(event){
				if (event.keyCode==13){ load();return false; }
			});
		});
        $("#search_input").focus(function(){ $(this).attr("value",""); });
        $("#search_input").blur(function(){ if ($(this).val()=="") {  $(this).attr("value","Search map by town / city or postcode"); } });
    });
    
    function searchLocation(address) {
        geocoder.getLatLng(address,
            function(point) {
                if (!point) {
                    alert(address+" not found.");
                } else {
                    map.panTo(point,14);
					listenerAction();
                }
            }
        );
    }
	
    function CustomInfoWindow(marker,html,width) {
        this.html_ = html;
        this.width_ = ( width ? width + 'px' : 'auto');
        this.marker_ = marker;
    }

    CustomInfoWindow.prototype = new GOverlay();
    CustomInfoWindow.prototype.initialize = function(map) {
        this.map_ = map;
        var container = document.createElement("div");
        container.style.display='none';
        map.getPane(G_MAP_FLOAT_PANE).appendChild(container);
        this.container_ = container;
    }

    CustomInfoWindow.prototype.remove = function() { this.container_.parentNode.removeChild(this.container_); }

    CustomInfoWindow.prototype.copy = function() { return new CustomInfoWindow(this.marker_,this.html_,this.width_); }

    CustomInfoWindow.prototype.redraw = function(force) {
        if (!force) return;
        var content = document.createElement("span");
        content.innerHTML = this.html_;
        content.style.margin='0';
        content.style.padding='0';
        content.style.border='0';
        content.style.display='inline';
        content.style.color='white';
        content.style.fontSize='11px';

        if(!this.width_ || this.width_=='auto' || this.width_ <= 0) {
            content.style.minWidth = '240px';
            content.style.maxWidth = '240px';
            content.style.width = '240px';
        } else {
            content.style.width= '240px';
        }
        content.style.visibility='hidden';
        this.map_.getContainer().appendChild(content);
        var contentWidth = content.offsetWidth;
        var contentHeight = content.offsetHeight;
        content.parentNode.removeChild(content);
        content.style.visibility='visible';
		if ((BrowserDetect.browser=="Mozilla")|(BrowserDetect.browser=="Safari")) {
			content.style.width='240px';
			content.style.height='125px';
			content.style.position='absolute';
			content.style.left='55px';
			content.style.top='-15px';
			content.style.background='#333';
			var wrapper = document.createElement("div");
			wrapper.appendChild(content);
	
			var wrapperParts = {
				arrow:{l:37, t:35, w:18, h:16},
				close:{l:275, t:-3, w:8, h:8}
			}
		} else {
			content.style.width='240px';
			content.style.height='125px';
			content.style.position='absolute';
			content.style.left='55px';
			content.style.top='105px';
			content.style.background='#333';
			var wrapper = document.createElement("div");
			wrapper.appendChild(content);
	
			var wrapperParts = {
				arrow:{l:37, t:155, w:18, h:16},
				close:{l:275, t:117, w:8, h:8}
			}
		}

        for (i in wrapperParts) {
            var img = document.createElement('img');
            img.src = 'http://www.mynetworkingpa.com/res/builtin/map/' + i + '.png';
            img.style.position='absolute';
            img.style.cursor='pointer';
            img.style.top=wrapperParts[i].t+'px';
            img.style.left=wrapperParts[i].l+'px';
            img.style.width=wrapperParts[i].w+'px';
            img.style.height=wrapperParts[i].h+'px';
            wrapper.appendChild(img);
            wrapperParts[i].img = img;
        }
        var marker = this.marker_;
	    GEvent.addDomListener(wrapperParts.close.img, "click", function() {
		    marker.closeCustomInfoWindow();
	    });

        var pixelLocation = this.map_.fromLatLngToDivPixel(
        this.marker_.getPoint()
        );

        this.container_.style.position='absolute';
        this.container_.style.left = (pixelLocation.x-3) + "px";
        this.container_.style.top = (pixelLocation.y
            - contentHeight
            - 25
            - this.marker_.getIcon().iconSize.height
        ) + "px";
        this.container_.style.border = '0';
        this.container_.style.margin = '0';
        this.container_.style.padding = '0';
        this.container_.style.display = 'block';
        this.container_.appendChild(wrapper);
        var mapNE = this.map_.fromLatLngToDivPixel( this.map_.getBounds().getNorthEast() );
        var panX=0;
        var panY=0;
        if(this.container_.offsetTop < mapNE.y) { panY = mapNE.y - this.container_.offsetTop; }
        if(this.container_.offsetLeft+contentWidth+10 > mapNE.x) { panX = (this.container_.offsetLeft+contentWidth+10) - mapNE.x; }
        if(panX!=0 || panY!=0) { this.map_.panBy(new GSize(-panX-10,panY+30));  }
    }

    GMarker.prototype.CustomInfoWindowInstance = null;
    GMarker.prototype.openCustomInfoWindow = function(content,width) {
        if(this.CustomInfoWindowInstance == null) {
            this.CustomInfoWindowInstance = new CustomInfoWindow(
                this,
                content,
                width
            );
            map.addOverlay(this.CustomInfoWindowInstance);
        }
    }
    GMarker.prototype.closeCustomInfoWindow = function() {
        if(this.CustomInfoWindowInstance != null) {
            map.removeOverlay(this.CustomInfoWindowInstance);
            this.CustomInfoWindowInstance = null;
        }
    }
    $(function(){
        var positionX = $("#googleMap").offset().left-10;
        var positionY = $("#googleMap").offset().top;
        $("#full_screen").toggle(function(){
            $("#userLoggedOut,.typeface-js,#frmSS15").hide();
            $("#googleMap").css({"position":"fixed","z-index":"100"}).animate({top:"0px",left:"-10px",width:$(document).width(),height:$(document).height()},"slow",function(){
                map.checkResize();
                if ((BrowserDetect.browser=="Explorer")&&(BrowserDetect.version<="7")) {
                    $("#map_search").hide().css({"position":"fixed","z-index":"101","top":"10px","left":"100px"}).fadeIn("slow");
                } else {
                    $("#map_search").hide().css({"position":"fixed","z-index":"101","top":"-50px","left":"100px"}).fadeIn("slow");
                }
                $("#full_screen").removeClass("maximise").addClass("minimise");
            });
        },function(){
            $("#full_screen").hide();
            $("#userLoggedOut,.typeface-js,#frmSS15").show();
            $("#map_search").hide().css({"position":"relative","z-index":"0","top":"0px","left":"0px"}).show();
            $("#googleMap").animate({left:positionX,top:positionY,width:"610px",height:"465px"},"slow",function(){
                $(this).css({"position":"relative","z-index":"0","top":"0px","left":"0px"});
                $("#full_screen").removeClass("minimise").addClass("maximise").fadeIn("slow");
                $("#map_container").css("top","20px");
                map.checkResize();
            });
        });
    });
}
google.setOnLoadCallback(initialize);
