// JavaScript Document

function getStores() {
	if ($.cookie('my_zip')) {
		writeStores(parseInt($.cookie('my_zip')));
	} else {
		$.getJSON('http://api.ipinfodb.com/v2/ip_query.php?callback=?', {
		    key: 'b221017c9fbedc8a2e5c039ee1b3f00df74474045bce23c3c5b6f95da968a29e',
		    output: 'json',
		    timezone: true
		  }, function(data) {
		  writeStores(parseInt(data.ZipPostalCode));
		});
	}
	
	function writeStores(zip) {
		$.cookie('my_zip', zip);
		
		if (isNaN(zip)) {
			return false;
		}
		
		var stores_in_zip = _.select(storeDirectory.stores, function(store) {
			return _.include(store.zipCodes, zip);
		});
		
		_.each(stores_in_zip, function(store) {
			$('#store1').append("<ul class=\"locationList\"> <li class=\"address2\">" + store.name + "<br/>" + store.streetAddress + "<br/>"+ store.city + ", " + store.state + " " + store.zip + "<br/> <a class=\"button\" href=\"Locations/Default.aspx\">Search Map</a></li> <li class=\"address2\">ph. " + store.phone + "<br/>fax " + store.fax + "<br/> <a href=\"http://" + store.website + "\">view website</a><br/> <a class=\"button\" href=\"Request-a-Demo.aspx\">Request a Demo</a></li> </ul><div class=\"clear\">&nbsp;</div>"); // writes your location
			$('#defaultStore').hide(); // Hides the default address
		});
	}
}
