function switchMap(el,map,mapType) {
	map.setMapType(mapType);
	$('#map-type-controls .control').removeClass('active');
	el.addClass('active');
}
	
function initMap() {
	if (GBrowserIsCompatible()) {
		var gmap = $('#gmap');
		var map = new GMap2(gmap[0]);
		var centerLatLong = new GLatLng(30.2701870864447784, -97.74587631225586);
		var markerLatLong = new GLatLng(30.2665589, -97.7374028);
		map.setCenter(centerLatLong, 14);
		map.addOverlay(new GMarker(markerLatLong));
		map.setMapType(G_NORMAL_MAP);
		
		// ADD CUSTOM CONTROLS VIA JQUERY
		
			// ZOOM
				
				// zoom in
				$("<div id='map-zoom-in' class='imgswap'>Zoom In</div>")
					.appendTo(gmap)
					.click(function(){
						map.zoomIn();
					});
				
				// zoom out
				$("<div id='map-zoom-out' class='imgswap'>Zoom Out</div>")
					.appendTo(gmap)
					.click(function(){
						map.zoomOut();
					});
				
				// rollovers
				$('#gmap .imgswap')
					.hover(
						function(){ $(this).addClass('hover') },
						function(){ $(this).removeClass('hover') }
					);
			
			// MAP TYPE
				
				// container
				var controls = $("<div id='map-type-controls'></div>").appendTo(gmap);
				
				// hybrid button
				$("<div id='map-hybrid' class='control'>Hybrid</div>")
					.appendTo(controls)
					.click(function(){
						switchMap($(this),map,G_HYBRID_MAP);
					});
					
				// satellite button
				$("<div id='map-satellite' class='control'>Satellite</div>")
					.appendTo(controls)
					.click(function(){
						switchMap($(this),map,G_SATELLITE_MAP);
					});
					
				// normal button
				$("<div id='map-normal' class='control active'>Map</div>")
					.appendTo(controls)
					.click(function(){
						switchMap($(this),map,G_NORMAL_MAP);
					});
				
				// rollovers
				$('#map-type-controls .control')
					.hover(
						function(){ $(this).addClass('hover') },
						function(){ $(this).removeClass('hover') }
					);
		
	}
}

function formMessage(msg) {
	var box = $('#feedback');
	if (msg === undefined) {
		box.html('').stop().removeAttr('style').removeClass('active');
	} else {
		box.html(msg).stop().removeAttr('style').addClass('active').animate({ backgroundColor: '#FAECA6' }, 1500, 'easeOutQuart');
	}
}

$(document).ready(function() {
	
	// initialize values
	
	var showMapSpeed = 1000;
	var buttonSwitchSpeed = 300;
	var hideMapSpeed = showMapSpeed / 2;
	var showMap = $('#show-map');
	var showMapMsg = showMap.html();
	var hideMapMsg = 'Hide Map';
	
	// Call Google Map
	
	initMap();
	
	// SEND button rollover
	
	$('#send-button').hover(
		function() {
			$(this).attr('src','/images/send-button-over.png');
		},
		function() {
			$(this).attr('src','/images/send-button-static.png');
		}
	);
	
	// SHOW/HIDE MAP behaviors
	
	$('#show-map').toggle(
		function() {
			$('#gmap').stop(true).animate({ top: 0 }, showMapSpeed, 'easeOutCubic');
			$(this).stop(true).animate({opacity: 0}, buttonSwitchSpeed, 'linear', function() {
				$(this)
					.html(hideMapMsg)
					.addClass('hide')
					.animate({opacity: 1.00}, buttonSwitchSpeed, 'linear');
			});
			return false;
		},
		function() {
			$('#gmap').stop(true).animate({ top: -300 }, hideMapSpeed, 'easeOutCubic');
			$(this).stop(true).animate({opacity: 0}, buttonSwitchSpeed, 'linear', function() {
				$(this)
					.html(showMapMsg)
					.removeClass('hide')
					.animate({opacity: 1.00}, buttonSwitchSpeed, 'linear');
			});
			return false;
		}
	);
	
	// Response Method Phone Requirement Toggle
	
	$('#response-method').change(function(){
		if ($('#response-method option:selected').val() == 2) {
			$('#phone').prev('label').addClass('required');
		} else {
			$('#phone').prev('label').removeClass('required');
		}
	});
	
	// Form validation default messages
	$.extend($.validator.messages, {
		required: "Required",
		email: "Please use a valid email address format"
	});
	
	// Form validation
	$('#contact-form').validate({
		rules: {
			'first-name' : 'required',
			'last-name' : 'required',
			'address' : {
				required : true,
				email : true
			},
			'phone' : {
				required : function(el) {
					return $('#response-method').val() == 2
				}
			},
			'reason' : 'required',
			'message' : 'required'
		},
		messages : {
			phone : 'Required, as a reply by phone was requested'
		},
		invalidHandler: function(form, validator) {
			formMessage('Please correct the indicated fields below.');
		},
		submitHandler: function(form, validator) {
			formMessage();
			form.submit();
		},
		onkeyup: false
	});
	
	// Animate form message if it already exists
	
	if ($('#feedback').hasClass('active')) $('#feedback').animate({ backgroundColor: '#FAECA6' }, 1500, 'easeOutQuart');
	
	
}); // END document.ready

$(window).unload(function () {
	GUnload();
});