/*
	moopop: unobtrusive javascript popups via late binding using mootools 1.2
	copyright (c) 2007-2008 by gonchuki - http://blog.gonchuki.com
	version:	1.1
	released: June 23, 2008
	This work is licensed under a Creative Commons Attribution-Share Alike 3.0 License.
	http://creativecommons.org/licenses/by-sa/3.0/
*/

var moopop={width:0,height:0,captureByRel:function(attrVal,parent){this.capture((parent||document).getElements('a[rel*='+(attrVal||'popup')+']'));},capture:function(el,width,height){if($defined(width)&&$defined(height)){this.width=width;this.height=height;}
switch($type(el)){case'string':el=$$(el);case'element':case'array':$splat(el).each(this.add_pop_to,this);}
this.width=null;this.height=null;},add_pop_to:function(el){el.addEvent('click',function(e){e.stop();this.popup(el);}.bind(this));var size=el.get('rel').match(/\[(\d+),\s*(\d+)/)||['',this.width,this.height];var resizable=el.get('rel').match(/,(r)/)||[];if(size[1])el.store('popupprops','top=0, left=0, scrollbars=yes, width='+size[1]+', height='+size[2]+(resizable[1]?', scrollbars=yes, resizable=yes':''));},popup:function(el){window.open(el.get('href'),el.get('name')||'',el.retrieve('popupprops')||'');}};window.addEvent('domready',function(){moopop.captureByRel('popup');});

/*

	@Class:	InputClearAndReplace
	@Author:	Brandon Gray for O3 World 2008
	@Brief:	Loops through all inputs/textareas with a class name of 'clear_replace' and clears the default text when focused
			when focus is lost it checks to see if the field is blank, if value is blank the default text is placed back in
			
*/

var InputClearAndReplace = new Class ({
	
	options: {
		
		InputElement: '.clear_replace'
		
	},
	
	initialize: function()  {
		
		var inputElementList = $$( this.options.InputElement );
		var originalValue = new Array();
		
		inputElementList.each( function( element, i ) {
			
			originalValue.push( inputElementList[ i ].title );
			
			element.onfocus = function() {
				if( this.value == originalValue[ i ] ) this.value = '';
				
			},
			
			element.onblur = function() {
				if( this.value == '' ) this.value = originalValue[ i ];
				
			}
			
		});
		
	}

});

// Google Maps functionality
function displayMap() {
	
	if ( GBrowserIsCompatible() ) {

		// create custom icon
		var icon = new GIcon(G_DEFAULT_ICON);
		icon.image = "images/icon_google_map.png";
		icon.iconSize = new GSize(39, 31);
		icon.shadow = "images/icon_google_map_shadow.png";
		icon.shadowSize = new GSize(39, 31);
		icon.infoWindowAnchor = new GPoint(0, 0);
		icon.infoShadowAnchor = new GPoint(0, 0);
		
		// create the map
		var map = new GMap2( document.getElementById( 'evt_map' ) );
		map.addControl(new GLargeMapControl());
		map.addControl(new GMapTypeControl());
		// center will be determined by bounds but must set a default here
		map.setCenter( new GLatLng( 0, 0 ), 0 );
		
		var bounds = new GLatLngBounds();
	
		var request = GXmlHttp.create();
		request.open( 'GET', 'data/map_data.cfm', true );
		request.onreadystatechange = function() {
		
			if ( request.readyState == 4 ) {
				
				var xmlDoc = GXml.parse( request.responseText );
				// obtain the array of markers and loop through it
				var markers = xmlDoc.documentElement.getElementsByTagName( 'marker' );
				
				for (var i = 0; i < markers.length; i++) {
		
					// obtain the attribues of each marker
					var lat = parseFloat( markers[ i ].getAttribute( 'lat' ) );
					var lng = parseFloat( markers[ i ].getAttribute( 'lng' ) );
					var point = new GLatLng( lat, lng );
					var html = markers[ i ].getAttribute( 'html' );
					var label = markers[ i ].getAttribute( 'label' );
					// create the marker
					var marker = createMarker( point, label, html );
					map.addOverlay( marker );
					
					// each time a point is created, extend the bounds ato include it
					bounds.extend( point );
					
				}
				
				// determine the zoom level from the bounds
				map.setZoom( map.getBoundsZoomLevel( bounds ) );
				// determine the center from the bounds
				map.setCenter( bounds.getCenter() );
				
			}
			
		}
		
		request.send( null );
		
		// create the marker and event listener to open info window
		function createMarker( point, name, html ) {
		
			var marker = new GMarker( point, icon );
			
			GEvent.addListener( marker, 'click', function() {
				
				marker.openInfoWindowHtml( html );
	
			});
			
			return marker;
	
		}
	
	} else {
		
		alert("Sorry, the Google Maps API is not compatible with this browser");
	
	}
	
}

window.addEvent( 'load', function() {
							  
	var InputList = new InputClearAndReplace();
	
	if ( $( 'evt_map' ) ) { displayMap(); }
	
});