var gmaps = {

	map : false,
	geocoder : false,
	marker : false,

	// Google Maps integration initialization.
	load : function(address,overlayHTML,myMarker) {
		if (GBrowserIsCompatible()) {
	
			gmaps.map = new GMap2(document.getElementById("google-map"));
			gmaps.geocoder = new GClientGeocoder();

			// Create custom icon for the marker.
			var markerIcon = new GIcon();
  		
			markerIcon.image  =  myMarker.image; 
			//markerIcon.shadow =  myMarker.shadow;
			markerIcon.iconSize = new GSize(27,34);
			//markerIcon.shadowSize = new GSize(27,34);
			markerIcon.iconAnchor = new GPoint(12,30);
			markerIcon.infoWindowAnchor = new GPoint(12, 8);

			markerOptions = { icon:markerIcon };

			gmaps.showAddress(address, markerOptions, overlayHTML);
			gmaps.map.setUIToDefault();
		}
	}, // load

	// showAddress function centers the map on the given address
	// and attaches a marker and overlay HTML to that point.
	showAddress : function(address, marketOptions, overlayHTML) {
		gmaps.geocoder.getLatLng(
			address,
			function(point) {
				if (point) {
					gmaps.map.setCenter(point, 13);
					gmaps.marker = new GMarker(point, marketOptions);
					gmaps.map.addOverlay(gmaps.marker);

					GEvent.addListener(gmaps.marker, "click", function() {
						gmaps.marker.openInfoWindowHtml('<div id="maps-overlay-html">'+overlayHTML+'</div>');
					});

					gmaps.map.setZoom(7);
				}
			}
		);
	} // showAddress
} // gmaps





$(document).ready(function(){
	$("ul#sub-nav").tabs("div.panes > div");
	
	
	// google maps functionality
	if ($("#google-map").length){
		var address = "5537 Twin Knolls Road, Columbia, Maryland 21045"
	  	var overlay_html = ""; 
		var marker={
			image:"/static/img/background/gmap-marker.png",
			shadow:"/static/img/background/gmap-marker.png"
		}
	
		gmaps.load(address, overlay_html, marker);
		$("body").bind('unload',GUnload); 
	}
	
	// contact us form functionality
	$('ul.sub-nav a').click(function(e) {
		e.preventDefault();
		$('p.sub-nav a').removeClass('selected-item');
		$(this).addClass('selected-item');
	});

	var form = $("#theform");

   form.validate({
		rules: {
		     name: "required",
		     email: {
		       required: true,
		       email: true
		     },
			referral:"required",
			summary:"required",
			phone:{
				required:true,
				digits:true
			},
			imgtext:"required"
		 },
	   errorClass: "error",
	   errorLabelContainer: "#messageBox",
	   wrapper: "li",
	   showErrors: function(errorMap, errorList) {
				$("div.error-summary span").html("Your form contains <span class='number'>"
		                                   + this.numberOfInvalids() 
		                                   + "</span> errors, see details below.");
				this.defaultShowErrors();
				if (this.numberOfInvalids() == 0){
					$("div.error-summary span").hide()
				}
		},
		highlight: function(element, errorClass) {
		     $(element).parent().addClass(errorClass);
		},
		unhighlight: function(element, errorClass) {
		     $(element).parent().removeClass(errorClass);
		}	
	});
	
	

	

	$("input.submit", form).click(function() {
		
	  	if (!form.valid()) return;
	
		$('#imgtext').val( ($('#imgtext').val()).toUpperCase() );
		$('#subject').val( "AriumAE Form Contact :: "  + $('form#theform ol li.select select option:selected').val() );
		
		
		var form_data = form.serialize();
		
		$.ajax({
				type:"post",
				url:".",
				data:form_data,
				success:function(data){
					if(data.indexOf("captcha-fail") >= 0 ){
						$("li.submit div.captcha").addClass("error");
						
					} else if (data.indexOf("submit-success") >= 0){
						$("div#contact-us").after(data).hide();

						$("div.submit-success a.send-another").click(function(){
							$("div.submit-success").remove();
							$("div#contact-us").show();
						});
							
					}
				}
		})
		
		
	  return false
	 
	});

});
