/*
-----------------------------------------------
SepticPreserveration.com
Stylesheet: SPSUtil.js
Author:   Ben Glassman
Created: 28 Sep 2010
----------------------------------------------- */

var sps = {};
sps.util = {
	init : function() {
		$('#dealer_zip').magicLabel();
		$('#find-a-dealer-form').bind('submit', function(e) {
			e.preventDefault();
			
			// Validate zip
			zip = $('#dealer_zip').val().replace(/[^\d]/, '');
			if (isNaN(zip)) {
				alert('Invalid zip code');
				return false;
			}
			
			// Retrieve latitude and longitude via google maps
			$.get('/assets/snippets/dealer_locator/ajax.google.geocode.api.php', 'address='+zip, function(data) {
				if (data.status == 'OK') {
					lat = data.results[0]['geometry']['location']['lat'];
					lon = data.results[0]['geometry']['location']['lng'];
					state = data.results[0]['address_components'][3]['short_name'];
					$('#dealer_lat').val(lat);
					$('#dealer_lon').val(lon);
					$('#state').val(state);
					$('#find-a-dealer-form')[0].submit();
				} else {
					alert('There was an error retrieving location data for the zip code you entered');
				}
			}, 'json');
		});
	}
};

(function($) {
	$.fn.parentButton = function() {
		return this.each(function() {
			$this = $(this);
			$this.hover(function() { $(this).toggleClass('hover'); });
			$this.bind('click', function() { window.location.href = $(this).find('a').eq(0).attr('href'); });
		});
	};
	$.fn.magicLabel = function() {
		return this.each(function() {
			$this = $(this);
			$this.data('label', $('label[for="' + this.id + '"]').text())
				.bind('focus', function() {
					if ($(this).val() == $(this).data('label')) {
						$(this).val('');
					}
				})
				.bind('blur', function() {
					if ($(this).val() == '') {
						$(this).val($(this).data('label'));
					}
				})
				.val($this.data('label'));
		});
	};
})(jQuery);

jQuery.fn.equalizeCols = function(){
  var height = 0;
  return this.css("height","auto").each(function(){
  	$this = jQuery(this);
    height = Math.max( height, ($this.outerHeight() - parseInt($this.css('padding-top')) - parseInt($this.css('padding-bottom'))));
  }).css("height", height);

};

jQuery.validator.setDefaults({
	onfocusout : false,
	onkeyup : false,
	onclick : false,
	showErrors: function(errorMap, errorList) {
		if (jQuery(this.currentForm).find('.errors').length == 0) {
			jQuery('<div id="error-container" class="errors"><h2>The following errors occured</h2><ul></ul></div>').prependTo(jQuery(this.currentForm)).hide();
		}
		var errors = this.numberOfInvalids();
		if (errors) {
			var error_list_html = '';
			for (var i in errorList) {
				this.settings.highlight.call(this, errorList[i].element, this.settings.errorClass, this.settings.validClass);
				error_list_html += $.format('<li>{0}</li>', errorList[i].message);
			}
			$('.errors ul').html(error_list_html).parent().slideDown('fast');
			window.location.hash = '#error-container';
			if (this.settings.unhighlight) {
				for ( var i = 0, elements = this.validElements(); elements[i]; i++ ) {
					this.settings.unhighlight.call(this, elements[i], this.settings.errorClass, this.settings.validClass);
				}
			}
		}
	}
});

$(sps.util.init);
