var sBaseUrl = 'http://chicagogolfreport.com';
//var sBaseUrl = 'http://wally';

jQuery(document).ready(function(){
	if (jQuery("#map").get(0)){
		if(GBrowserIsCompatible()){
			jQuery("#map").html('Loading...');
	        inMap = new inMaps(jQuery("#map"));
	        setTimeout(function(){inMap.init();}, 2000);
	        
		}
		else{
		    alert("Sorry, the maps are not compatible with your browser.");
		}
		jQuery(window).unload( function () { GUnload(); } );
	}
	else{
		if (jQuery("#singlemap").get(0)){
			setTimeout(function(){
				var address = jQuery("#singlemap").html();
				var myMap = new GMap2(jQuery("#singlemap").get(0));
				myMap.addControl(new GLargeMapControl());
				myMap.addControl(new GScaleControl());
				var geocoder = new GClientGeocoder();
				var icon = new GIcon(G_DEFAULT_ICON);
			    icon.image = sBaseUrl + "/wp-content/plugins/golfmap/images/golf.png";
			    icon.shadow = null;
			    icon.shadowSize = null;
			    icon.iconSize = new GSize(16, 28);
			    icon.iconAnchor = new GPoint(1, 28);
			    icon.infoWindowAnchor = new GPoint(1, 28);
			    geocoder.getLatLng(
				    address,
				    function(point) {
				      if (!point) {
				        //alert(address + " not found");
				      } else {
				    	myMap.setCenter(point, 13);
				        var marker = new GMarker(point, icon);
				        myMap.addOverlay(marker);
				      }
				    }
				  );
			}, 2000);
		}
	}
	
	
});

function inMaps(mapElement){
    this.GMap = null;
    this.inMapElement = mapElement;
    this.inEvents = [];
    this.markerBatch = [];
    this.markerManager = null;
}

inMaps.prototype.init = function(SCROLL){

    if (this.inMapElement == null) {
        alert('The Map could not be initialized. Map element is not specified!');
        return;
    }
    
    this.GMap = new GMap2(this.inMapElement.get(0));

    this.GMap.addControl(new GLargeMapControl());
    this.GMap.addControl(new GScaleControl());
    this.GMap.setCenter(new GLatLng(37.702366072696655, -88.06640625), 4);
    this.markerBatch = [];
    
    this.setBehaviour();
    this.getJsonInEvents();
}

inMaps.prototype.setBehaviour = function(){
    
}

inMaps.prototype.getJsonInEvents = function(){
	var oInMaps = this;
	$.getJSON(sBaseUrl + "/wp-content/plugins/golfmap/data.json", function(json){
		oInMaps.parseJsonInEvents(json);  
	});
}

inMaps.prototype.parseJsonInEvents = function(json){
	var fLength = json.length;
	var oBounds = new GLatLngBounds();
    if (fLength > 0) {
    
        var i = fLength - 1;
        while (i >= 0) {
            var oEvent = new inEvent();
            if (json[i].lng && json[i].lat){
	            oEvent.additionalinfo 	= json[i].additionalinfo;
	            oEvent.longitude 		= json[i].lng;
	            oEvent.latitude 		= json[i].lat;
	            oEvent.address 			= json[i].address;
	            oEvent.city 			= json[i].city;
	            oEvent.phone 			= json[i].phone;
	            oEvent.coursename 		= json[i].coursename;
	            
	            oEvent.init();
	            
	            //oEvent.infoTabs = InfoWindowTabs(oEvent);
	            oEvent.inMap = this;
	            this.markerBatch.push(oEvent.marker);
	            this.addInEvent(oEvent);
	            oBounds.extend(new GLatLng(oEvent.latitude, oEvent.longitude));
            }
            i--;
        }
        
        this.markerManager = new MarkerClusterer(this.GMap, this.markerBatch, {maxZoom: 10, styles: [{
        	'url': sBaseUrl + "/wp-content/plugins/golfmap/images/golfclub_m.png",
            'height': 32,
            'width': 32,
            'opt_anchor': [16, 10]
          }] 
        }); 
        this.GMap.setZoom(this.GMap.getBoundsZoomLevel(oBounds));
	    this.GMap.setCenter(oBounds.getCenter());
    }
}

inMaps.prototype.addInEvent = function(oEvent){
    this.inEvents.push(oEvent);
}

function inEvent(){
    this.id;
    this.point;
    this.icon;
    this.iconType;
    this.marker;
    this.inMap;
    this.longitude = '';
    this.latitude = '';
    this.additionalinfo ='';
    this.address ='';
    this.city ='';
    this.coursename ='';
    this.phone ='';
}

inEvent.prototype.init = function(){
    this.point = new GLatLng(parseFloat(this.latitude), parseFloat(this.longitude));
    this.icon = this.getIcon();
    this.marker = new GMarker(this.point, this.icon);
    this.setBehaviours();
}

inEvent.prototype.setBehaviours = function(){
    this.marker.parent = this;
    
    GEvent.addListener(this.marker, "click", function(){
        this.parent.showInfoWindow();
    });
}

inEvent.prototype.getIcon = function(type){

    var icon = new GIcon(G_DEFAULT_ICON);
    icon.image = sBaseUrl + "/wp-content/plugins/golfmap/images/golf.png";
    icon.shadow = null;
    icon.shadowSize = null;
    icon.iconSize = new GSize(16, 28);
    icon.iconAnchor = new GPoint(1, 28);
    icon.infoWindowAnchor = new GPoint(1, 28);
   
    return icon;
}

inEvent.prototype.showInfoWindow = function(){
	var str = (this.additionalinfo) ? '<p><a href="'+ this.additionalinfo +'">More info</a></p>' : '';
    this.marker.openInfoWindowHtml('<div class="mappopup"><h4>' + this.coursename + '</h4><p><strong>Address:</strong> '+ this.address +', ' + this.city + '</p><p><strong>Phone:</strong> '+ this.phone +'</p>'+ str + '</div>');
}

