    var mapMarkers = [];
    var mapMarkers2 = [];
    var map;



    // Initialize map - called when page is loaded
    function initMap(){
    
      if (GBrowserIsCompatible()) {
        map = new GMap2(document.getElementById("googleMapContainer"));
        map.setCenter(new GLatLng(53.72,-7.50), 8);
        map.enableScrollWheelZoom();
        map.addControl(new GLargeMapControl());
        map.addControl(new GMapTypeControl());
       // map.setUIToDefault();       
              
      }  
      //showEpisode();
    }
    
    // Clear map points
    function clearMap(){
      mapPoints = []; 
       mapMarkers = [];
          mapMarkers2 = [];
       map.clearOverlays();
    }
   
 
    function createEpisodelMarker(point,markerImage) {

      var marker = new GMarker(point,markerImage);
      GEvent.addListener(marker, "click", function() {
        var opts = {pixelOffset:new GSize(32,5), maxWidth:280}; 
        marker.openInfoWindowHtml(point.name, opts);
      });
      return marker;
    }
    
    
  

    function loadEpisodes() {

      
      GDownloadUrl("/"+countryCode+"/webisode/jsp/marker/?id="+id, function(data, responseCode) {	
        var xml = GXml.parse(data);
        var markers;
        var extmarkers;
        var info;
        var watch;
        var places;        
        try{      
        markers = xml.documentElement.getElementsByTagName("episode");       
        extmarkers = xml.documentElement.getElementsByTagName("extepisode-unused");        
        info = xml.documentElement.getElementsByTagName("info")[0].firstChild.nodeValue;
        watch = xml.documentElement.getElementsByTagName("watch")[0].firstChild.nodeValue;
        places = xml.documentElement.getElementsByTagName("places")[0].firstChild.nodeValue;
	
	}catch(error){}
     function createIcon (color,position){
    var colorIcon = new GIcon(G_DEFAULT_ICON);
    colorIcon.image = "/"+countryCode+"/webisode/markers/"+color+"/marker_"+color+"_"+position+".png";
    colorIcon.iconSize = new GSize(50, 40);
    return colorIcon;
    }
               
 
        
        if (markers.length == 0){
	          alert("Your query returned no results. Please broaden your search criteria and try again.");
	        } else  {
	          
	          for (var i = 0; i < markers.length; i++) {          
	          var j = i+1;
	 
	 
	            var point = new GLatLng(parseFloat(markers[i].getElementsByTagName("latitude")[0].firstChild.nodeValue),
	              parseFloat(markers[i].getElementsByTagName("longitude")[0].firstChild.nodeValue));
	        
	            var jid = markers[i].getElementsByTagName("jid")[0].firstChild.nodeValue;
	            var id = markers[i].getElementsByTagName("id")[0].firstChild.nodeValue;
	            var title = markers[i].getElementsByTagName("title")[0].firstChild.nodeValue;
	            var img = "";
	            var extIcon;
	            var ext;
	            try{
	            img = markers[i].getElementsByTagName("image")[0].firstChild.nodeValue;
	            }catch(error){}
	            var address  = markers[i].getElementsByTagName("address")[0].firstChild.nodeValue;
	            var firstName = markers[i].getElementsByTagName("person")[0].firstChild.nodeValue;
	            var ImageText = "";
	            try{
	            ImageText = markers[i].getElementsByTagName("imageText")[0].firstChild.nodeValue;
	            }catch(error){}
	            var position = markers[i].getElementsByTagName("position")[0].firstChild.nodeValue;
	            try{
	            	extIcon = markers[i].getElementsByTagName("internal"); // named to ext-unused - TI request for external episodes to be taken off
	             	
	             }catch(error){}
	             if (extIcon.length !== 0){markerOptions = { icon:createIcon('purple',position)};}
	          
	          else if(i == 0 || i == markers.length - 1){ 
	          markerOptions = {icon:createIcon('black',position)};
	          
	          }
	          
	       
	          
	          else {markerOptions = { icon:createIcon('green',position)};
	         
	          }        
	                        
	          point.name = "<div id = 'info-div' class='info-window'><h4 style='color:black'>" + title + "</h4><table style='width:280px;height:90px;'><tr><td><a style='color:black;background:transparent url(/shared/webisodes/assets/img/links/link_arrow_green.png) no-repeat scroll left center;padding:0 5px 0 12px;' id='" + id + "' href='/" + countryCode + "/webisode/jsp/video/?id=" + jid + "' onclick='ajaxDynamicContent(\"/" + countryCode + "/webisode/jsp/video/?id=" + jid + "&eid=" + id + "&official=true\"); return false;'>"+watch+"</a><br/><a style='color:black;background:transparent url(/shared/webisodes/assets/img/links/link_arrow_green.png) no-repeat scroll left center;padding:0 5px 0 12px;' id = '" + title + "' href='/" + countryCode + "/webisode/jsp/episode/?id=" + jid + "&official=true&fullPage=yes' onclick='ajaxDynamicContent(\"/" + countryCode + "/webisode/jsp/episode/?id=" + jid + "&eid=" + id + "&official=true\"); return false;' >"+info+"</a></td><td><img src='"+ img + "' height='61' width='90'></td></tr>"; 
	        
	            
	                    
	          try{
	          ext = markers[i].getElementsByTagName("internal");//renamed to remove it
	          }catch(error){}
	          if(ext.length == 0){
	          point.name = point.name + "</table></div>";
	          
	          }
	          if(ext.length !== 0){
	          var mainPoint;
	          mainPoint = point.name + "<tr><td colspan='2'><h4 style='color:black'>"+places+"</h4><ul class='suggestedEpisodes'>"
	          
	          for(var k = 0 ; k < ext.length ; k++){
	          
	                  var point = new GLatLng(parseFloat(ext[k].getElementsByTagName("latitude")[0].firstChild.nodeValue),
			parseFloat(ext[k].getElementsByTagName("longitude")[0].firstChild.nodeValue));
	
	      		var jid = markers[i].getElementsByTagName("jid")[0].firstChild.nodeValue;
	      		var id = ext[k].getElementsByTagName("id")[0].firstChild.nodeValue;
	      		var title = ext[k].getElementsByTagName("title")[0].firstChild.nodeValue;
	      		var img = "";
	      		try{
	      		img = ext[k].getElementsByTagName("image")[0].firstChild.nodeValue;
	      		}catch(error){}
	      		var address  = ext[k].getElementsByTagName("address")[0].firstChild.nodeValue;
	      		var firstName = ext[k].getElementsByTagName("person")[0].firstChild.nodeValue;
	            var ImageText = "";
	            try{
	            ImageText = ext[k].getElementsByTagName("imageText")[0].firstChild.nodeValue; 
	            }catch(error){}
	             var position = ext[k].getElementsByTagName("position")[0].firstChild.nodeValue;
	            
	            
	            point.name  = mainPoint + "<li><a style='color:black' href='/"+countryCode+"/webisode/jsp/episode/?id="+jid+"&fullPage=yes&eid="+id+"&official=true' onclick='ajaxDynamicContent(\"/" + countryCode + "/webisode/jsp/episode/?id=" + jid + "&eid=" + id + "&official=true\"); return false;'><span style='color:#ACD267;float:left;width:2em;'>" + position+ ".</span>"+ title + "</a></li>";
	            mainPoint = point.name;       
	          
	          
	          }	           
	          point.name  = point.name  + "</ul></td></tr></table></div>";
	       
	          }
	          
	          
	           mapMarkers.push(createEpisodelMarker(point,markerOptions));
	                      
	          }          
	          
	        for (var j = 0; j < extmarkers.length; j++) {
	      
			
	              var point = new GLatLng(parseFloat(extmarkers[j].getElementsByTagName("latitude")[0].firstChild.nodeValue),
	                parseFloat(extmarkers[j].getElementsByTagName("longitude")[0].firstChild.nodeValue));
	          
	              var jid = extmarkers[j].getElementsByTagName("jid")[0].firstChild.nodeValue;
	              var id = extmarkers[j].getElementsByTagName("id")[0].firstChild.nodeValue;
	              var title = extmarkers[j].getElementsByTagName("title")[0].firstChild.nodeValue;
	              var img = "";
	              try{
	              img = extmarkers[j].getElementsByTagName("image")[0].firstChild.nodeValue;
	              }catch(error){}
	              var address  = extmarkers[j].getElementsByTagName("address")[0].firstChild.nodeValue;
	              var firstName = extmarkers[j].getElementsByTagName("person")[0].firstChild.nodeValue;
	              var ImageText = "";
	              try{
	              ImageText = extmarkers[j].getElementsByTagName("imageText")[0].firstChild.nodeValue;
	              }catch(error){}
	               var position = extmarkers[j].getElementsByTagName("position")[0].firstChild.nodeValue;
	               
	              
	             markerOptions = {icon:createIcon('white',position)};
	                          
	                  point.name  = "<div class='info-window'><h4 style='color:black'>" + title + "</h4><table style='width:280px;height:90px;'><tr><td><a style='color:black;background:transparent url(/shared/webisodes/assets/img/links/link_arrow_green.png) no-repeat scroll left center;padding:0 5px 0 12px;' id = '"+title+"' href='/"+countryCode+"/webisode/jsp/episode/?id="+jid+"&official=false&eid="+id+"&fullPage=yes' onclick='ajaxDynamicContent(\"/" + countryCode + "/webisode/jsp/episode/?id=" + jid + "&eid=" + id + "&official=false\"); return false;'>"+info+"</a></p></td><td><img src='"+ 
	                  img + "' height='61' width='90'></td></tr></table></div>";
	          
	            
	             mapMarkers2.push(createEpisodelMarker(point,markerOptions));
	                        
	          }
	          
	         showMap();
	        }
	      });           
	    }
	
	     
	   
	    // Show Google map with episode data
	    function showMap(){    
	      // Find boundary points of episode location
	      
	             var bounding = map.getBounds();
	      var sw = bounding.getSouthWest();
	      var ne = bounding.getNorthEast();
	      var centre = new GLatLng(sw,ne)
	      var bounds = new GLatLngBounds();
	      var bounds2 = new GLatLngBounds(sw,ne);
	
	
	      for (var i=0;i < mapMarkers.length;i++)  {
	           
	          map.addOverlay(mapMarkers[i]);
	          bounds.extend(mapMarkers[i].getPoint());
	       }
	       
	     for (var i=0;i < mapMarkers2.length;i++)  {
	           
	          map.addOverlay(mapMarkers2[i]);
	          bounds.extend(mapMarkers2[i].getPoint());
	       }
	       
	      // Reset center and zoom level based on queried episode locations 
	      map.setCenter(bounds.getCenter()); 
	      map.setZoom(map.getBoundsZoomLevel(bounds));
	            
	    }
	
	    function showEpisodes(){
	      clearMap();
	      loadEpisodes();
	    }
