/**
 * Albertsons Store Locator
 * Version 1.0.0 - 12/17/2008
 * @author Benjamin Truyman
 **/

var ABS = ABS || {};

// Albertsons Homepage
ABS.Locator = {
	initialize: function () {
		ABS.Locator.StoreLinks.initialize();
		if ($('#find-store').get(0)) { ABS.Locator.FindAStore.initialize(); }
		if ($('#state-selector').get(0)) { ABS.Locator.StoresByState.initialize(); }
		if ($('#map').get(0)) { ABS.Locator.Map.initialize(); }
	}
};

// Find A Store form
ABS.Locator.FindAStore = {
	buttons: {},
	containers: {},
	inputs: {},
	validator: null,
	
	initialize: function () {
		// Define containers
		this.containers.main = $('#find-store').get(0);
		this.containers.form = $('#find-store-form').get(0);
		
		// Define buttons
		this.buttons.submit = $('#find-store-submit').get(0);
		
		// Define inputs
		this.inputs.zip = $('#find-store-zip');
		this.inputs.city = $('#find-store-city');
		this.inputs.state = $('#find-store-state');
		
		// Create shortcut to validator
		this.validator = ABS.Helpers.FormValidator;
		
		// Build search form
		this.buildForm();
		
		// Build form validator
		this.buildValidator();
	},
	
	buildForm: function () {
		$(this.containers.form).submit(ABS.Utils.Delegate.create(this, this.handleSubmit));
		$('state-selector-submit').hide();
	},
	
	buildValidator: function () {
		this.validator = ABS.Helpers.FormValidator({
			form: this.containers.form,
			elements: [
				{
					name: 'zip',
					options: {
						required: true,
						zip: true
					}
				},
				{
					name: 'city',
					options: {
						required: true
					}
				},
				{
					name: 'state',
					options: {
						required: true
					}
				}
			]
		});
	},
	
	handleSubmit: function (e) {
		var that = this;
		var results = this.validator.run();
		if (!results.areValid) {
			if (results.elements.failed.length === 3) {
				alert('To find a store, please enter either your Zip Code or enter both City and State');
				e.preventDefault();
			} else {
				var failed = {
					zip: false,
					city: false,
					state: false
				};
				$(results.elements.failed).each( function () {
					if (this.name === 'zip') failed.zip = true;
					else if (this.name === 'city') failed.city = true;
					else if (this.name === 'state') failed.state = true;
				});
				if (failed.zip && failed.state) {
					alert('Please select the State');
					e.preventDefault();
				} else if (failed.zip && failed.city) {
					alert('Please enter the City');
					e.preventDefault();
				}
			}
		}
	}
};

// Store Links
ABS.Locator.StoreLinks = {
	className: 'store-name',
	links: [],
	
	initialize: function () {
		// Build links
		this.buildLinks();
	},
	
	getLinks: function () {
		return $('a.' + this.className).get();
	},
	
	buildLinks: function () {
		var that = this;
		
		// Clear out links cache
		this.links = [];
		
		$(this.getLinks()).each(function () {
			that.links.push(new ABS.Locator.StoreLinks.Link(this));
		});
	}
};

// Store Links Link Class
ABS.Locator.StoreLinks.Link = function (el) {
	if (arguments.length !== 1) {
		throw new Error('Expected an argument of one but recieved ' + arguments.length);
	}
	
	this.dom = el;
	
	this.overlay = {
		html: '<div class="view-details">View Store Details</div>',
		dom: null
	};
	
	// Set handlers
	$(this.dom).hover(ABS.Utils.Delegate.create(this, this.handleMouseOver), ABS.Utils.Delegate.create(this, this.handleMouseOut));
	
	// Build link
	this.buildLink();
	
	// Build overlay
	this.buildOverlay();
};

ABS.Locator.StoreLinks.Link.prototype = {
	buildLink: function () {
		// Set default styles
		$('> span', this.dom).css('position', 'relative');
	},
	
	buildOverlay: function () {
		// Create overlay
		this.overlay.dom = $(this.overlay.html).appendTo($('> span', this.dom));
	},
	
	handleMouseOver: function (e) {
		$(this.overlay.dom).show();
	},
	
	handleMouseOut: function (e) {
		$(this.overlay.dom).hide();
	}
};

// Stores by State
ABS.Locator.StoresByState = {
	buttons: {},
	containers: {},
	inputs: {},
	
	initialize: function () {
		// Define containers
		this.containers.main = $('#state-selector').get(0);
		this.containers.form = $('#state-selector-form').get(0);
		
		// Define inputs
		this.inputs.states = $('#state-selector-states').get(0);
		
		// Define buttons
		this.buttons.submit = $('#state-selector-submit').get(0);
		
		// Build form
		this.buildForm();
	},
	
	buildForm: function () {
		// Set select list change event
		$(this.inputs.states).change(ABS.Utils.Delegate.create(this, this.handleStateChange));
	},
	
	handleStateChange: function (e) {
		$(this.buttons.submit).click();
	}
};

// Map
ABS.Locator.Map = {
	containers: {},
	initialize: function () {
		// Build analytics
		this.buildAnalytics();
	},
	buildAnalytics: function () {
		$('#map').click(function() {
			ABS.Helpers.Analytics.trackEvent('Store Locator Map', 'Click');
		});
	}
}

$(document).ready(function () {
	ABS.Locator.initialize();
});
