$(document).ready(function() {
    $('li.listTab').addClass('active');
    $('#posts').show();
    
    $('li.mapTab, li.compareTab').addClass('inactive');
    $('#mapContent, #compareContent').hide();

    $('ul.resultsTabs li a').click(function(){
        $(this).parent().addClass('active');
        $(this).parent().siblings().removeClass('active').addClass('inactive');
        if($(this).parent().hasClass('active')){
            if($(this).parent().hasClass('listTab')){
                $('#mapContent, #compareContent').hide();
                $('#posts').show();
            }
            
            if($(this).parent().hasClass('mapTab')){
                $('#posts, #compareContent').hide();
                $('#mapContent').show();
                initializeMap();
            }
            
            if($(this).parent().hasClass('compareTab')){
                $('#mapContent, #posts').hide();
                $('#compareContent').show();
            }
            
        }else{
            return false;
        }
        return false;
    });
});
    
     var bounds = new GLatLngBounds();
    
     function initializeMap() {
			
            if (GBrowserIsCompatible()) { 
              // Display the map, with some controls and set the initial location 
              var map = new GMap2(document.getElementById("map_canvas")); 
              map.addControl(new GLargeMapControl()); 
              map.addControl(new GMapTypeControl()); 
              
              map.setCenter(new GLatLng(0,0),0);
              
              // Set up markers with info windows from the database  
              
             mappoints(map);
             
             map.setZoom(map.getBoundsZoomLevel(bounds));
             map.setCenter(bounds.getCenter());
             
            }
            // display a warning if the browser was not compatible
            else {
              alert("Sorry, the Google Maps API is not compatible with this browser");
            }
          }
          
          
              
            
          function mappoints(gmap)
          {
             // A function to create the marker and set up the event window 
             function createMarker(point,html) { 
				
                var marker = new GMarker(point,markerOptions); 
                GEvent.addListener(marker, "click", function() { 
                marker.openInfoWindowHtml(html); 
                }); 
                return marker; 
              } 
              
              $('.hotelHolderMap').each(function(){
				 var baseIcon = new GIcon(G_DEFAULT_ICON);
                baseIcon.iconSize = new GSize(20, 20);
                baseIcon.iconAnchor = new GPoint(9, 20);
                baseIconinfoWindowAnchor = new GPoint(20,0);
				var plazaIcon = new GIcon(baseIcon);
				var expressIcon = new GIcon(baseIcon);
				var holidayIcon = new GIcon(baseIcon);
				plazaIcon.image = "../images/plaza.png";
				expressIcon.image = "../images/express.png";
				holidayIcon.image = "../images/holiday.png";

				var brand = $(".brand", $(this)).html();
				markerOptions = { icon:holidayIcon };
				if(brand == "6c19277f-fc72-4297-b913-1dde63018a2a"){
					markerOptions = { icon:plazaIcon };
					$(this).find('.HotelName a').addClass("crownPlaza");
				}
				if(brand == "644a7f05-0e3e-4db7-8e8b-311d96cd4ca3"){
					$(this).find('.HotelName a').addClass("express");
					markerOptions = { icon:expressIcon };
				}
				if(brand == "a5859d7e-f286-4873-a584-4a1a50674148"){
					markerOptions = { icon:holidayIcon };
				}
				//alert(markerOptions);
				var lat = $(".latitude", $(this)).html();
				var lon = $(".longitude", $(this)).html();
                lat = lat.replace(/,/gi, ".");
                lon = lon.replace(/,/gi, ".");
                var name = $(".popUpDetail ", $(this)).html();
                var point = new GLatLng(lat, lon);
                //alert(point);
                marker = createMarker(point, name);
                gmap.addOverlay(marker);
                
                bounds.extend(marker.getPoint());
            });  
              
            
          }

