$(document).ready(function(){
	if( $('#application').length > 0 ){
		application_validation();
	}
	
	$('#tab_menu a').click(function(){
		var slider = $(this).attr('rel');
		var target = $('#'+slider);
		
		if (target.css('display') == 'block') {
			target.slideUp();
		}
		
		else {
			$('.slider').slideUp('fast');
			target.slideDown();
		}
		
	});
	
	if ($('#google_maps').size() > 0) {
		load_google_maps();
		
		$('#page .locations').hover(function(){
			$(this).animate({backgroundColor:'#eee'}).find('.addresses').slideDown();	
		},function(){
			$(this).animate({backgroundColor:'transparent'}).find('.addresses').slideUp();
		});
		
		$('a.address').click(function(){
			var title = $(this).attr('rel');
			showAddress($(this).text(), title, 'yes');
		});
			
	}
	
});

function request_info_submit() {
	
	var tab = $('#request_slide .container');
	var form = $('#request_info_form').serialize();
	$.post(base_url+'contact/request_info_submit', {data:form}, function(data){
		tab.fadeOut('slow', function(){
			if (data == 'success') {
				tab.html('<h3>Thanks, your request has been submitted!</h3>');
				tab.fadeIn();
			}
		});
	});
	
}

function application_validation() {
	$('#application').validate({
		meta: "validate",
		messages: {
			'last': "Please specify your Last Name",
			'first': "Please specify your First Name",
			'Address': "Please specify your Address",
			'City': "Please specify your City",
			'State': "Please specify your State",
			'Zip': "Please specify your Zip",
			'Telephone': "Please specify your Telephone Number",
			'email': {
				required: "We need your email address to contact you",
				email: "Your email address must be in the format of name@domain.com"
			}
			
		},
		errorContainer: "#application .form_errors, #application .error_mesg",
		errorLabelContainer: "#application .form_errors ul",
		wrapper: "li",
		errorClass: "invalid",
		submitHandler: function(form) {
			var tab = $('#application_slide .container');
			var form = $('#application').serialize();
			$.post(base_url+'contact/app_submit', {data:form}, function(data){
				tab.fadeOut('slow', function(){
					if (data == 'success') {
						tab.html('<h3>Thanks, your application has been submitted!</h3>');
						tab.fadeIn();
					}else{
						tab.fadeIn();
					}
				});
			});
		}
		
	});
	
}

var map = null;
var geocoder = null;

function load_google_maps() {

        map = new GMap2(document.getElementById("google_maps"));
        
        geocoder = new GClientGeocoder();
        geocoder.getLatLng(
          'Des Plaines, Illinois', function(point) {map.setCenter(point, 9);}
        );
        populate_map();

}
	
function populate_map(){
		$('div.addresses a.address').each(function(){
			address = $(this).text();
			title = $(this).attr('rel');
			showAddress(address, title, 'no');
		});
}
    
function showAddress(address, title, clicked) {
      var lt = address.split(' ').join('+');	
      var origin = '';
      var address_link = '<br/><strong><a target="_blank" href="http://local.google.com/maps?f=d&source=s_d&saddr='+origin+'&daddr='+lt+'&hl=en&geocode=&mra=ls&sll=41.901226,-87.686398&sspn=0.008481,0.019205&ie=UTF8&z=13">Get Directions</a></strong>';
      if (geocoder) {
        geocoder.getLatLng(
          address,
          function(point) {
            if (!point) {
              //alert(address + " not found");
            } else {
              
              if (clicked=='yes') {
              	map.setCenter(point, 15);
              }
              var marker = new GMarker(point);
              
              map.addOverlay(marker);
              
              if (clicked=='yes') {
              	marker.openInfoWindowHtml('<h3>'+title+'</h3>'+address+address_link);
              } else {
              	marker.bindInfoWindowHtml('<h3>'+title+'</h3>'+address+address_link);
              }
            }
          }
        );
       }
}