// Development site  -  http://kbdev.staplegun.us
//var apikey = 'ABQIAAAAlUR188h4rgkW3-IO0kz5iBTDpNgOnfJIc0vIxPqnNcAj3sGsFRTw0s96S6XHrQ3NllovG1kZUDpUbA';

// Live Site  -  http://www.kirkpatrickbank.com
var apikey = 'ABQIAAAAlUR188h4rgkW3-IO0kz5iBRZ00Zvk-MawYfyJBdKjemaqWAhvhTo_BMEYi4RNS_oZO1PSsWEu_4WLA';

// Include Google Maps API
document.writeln('<sc' + 'ript ' + 'src="http://maps.google.com/maps?file=api&amp;v=2&amp;key=' + apikey + '" type="text/javascript"></sc' + 'ript>');

var locSliders = new Array();
var locMarkers = new Array();
var map = Object;

window.addEvent('domready', function(){
	// Initialize the Gmap
	initMap();

	var i = 0;
	// Hide the .moreinfo divs
	$$('.location').each(function(o){
		o.setProperty('title', 'Click to toggle more info');
		o.setProperty('bubbleText', o.get('html'));
		locSliders[i] = new Fx.Slide(o.getElement('.moreinfo')).hide();

		// Save the index of the Fx.Slide
		o.setProperty('fxId', i);

		// Make the mouse pointer be the hand
		o.setStyle('cursor', 'pointer');
		
		// Set the default state for the slider to closed
		o.setProperty('slState', 0);

		// Make it toggle when we click the div
		o.addEvent('click', function(){
			//map.panTo(new GLatLng(this.getProperty('lat'), this.getProperty('lng')));
			var id = this.getProperty('fxId');
			locSliders[id].toggle();
			
			if (this.getProperty('slState') == 0){
				locMarkers[id].openInfoWindowHtml('<div class="bubbleText">' + this.getProperty('bubbleText') + '</div>');
				map.setZoom(16);
				this.setProperty('slState', 1);
			} else {
				this.setProperty('slState', 0);
			}
		});

		// Draw the icon on the map
		var lat = o.getElement('.latitude').get('text');
		o.setProperty('lat', lat);
		var lng = o.getElement('.longitude').get('text');
		o.setProperty('lng', lng);
		var ico = new GIcon(G_DEFAULT_ICON);
		ico.image = "/img/kbLogo24px.png";
		ico.iconSize = new GSize(24,23);
		ico.shadow = "/img/kbLogo24px_shadow.png";
		ico.shadowSize = new GSize(37,23);

		locMarkers[i] = new GMarker(new GLatLng(lat,lng),{
			icon: ico
		});
		map.addOverlay(locMarkers[i]);

		i++;
	});
	
	
	// When the user mouse's over the submit btn, load the action for the form
	// an effort to fight spam
	//$('submitBtn').addEvent('focus', function(){
		//document.forms.locationForm.action = window.location;
	//});
	
	//$('submitBtn').addEvent('click', function(){
		//this.fireEvent('focus');
	//});
	
	//document.forms.locationForm.onSubmit = function(){
		//if ($('submitBtn')) $('submitBtn').fireEvent('focus');
	//};
});


function initMap(){
	if (GBrowserIsCompatible()){
		map = new GMap2(document.getElementById('themap'), {
			'size': new GSize(500,400)
		});
		
		// Center somewhere in Western Oklahoma
		map.setCenter(new GLatLng(37.0201, -100.8545), 6);
		
		// Center in Edmond
		//map.setCenter(new GLatLng(35.6098, -97.4943), 6);
		map.addControl(new GSmallMapControl());
	}
}
