window.addEvent('load', function() {
	
	// vars
	
	ie = Browser.Engine.trident;

	citylist=["San Diego", "San Francisco", "San Jose", "California"];latlist=[32.7153292, 37.7749295, 37.3393857];lnglist=[-117.1572551, -122.4194155, -121.8949555];	
	/*
		vars
		----
		t	user latitude
		n	user longitude
		a	area
	*/
	
	setArea = function(a) {
		if (Cookie.read('area')) Cookie.dispose('area');
		Cookie.write('area', (a=='California' ? '' : a), {duration: 90});
		window.location.reload();
	}
	
	getArea = function(t, n) {
		var a = 'California';
		var d = 1000000000;
		latlist.each(function(c, i) {
			var m = Math.acos(Math.sin(c)*Math.sin(t) + Math.cos(c)*Math.cos(t)*Math.cos(n-lnglist[i]))*6371;
			if (d > m) {
				d = m;
				a = citylist[i];
			}
		});
		setArea(a);
	}
	
	setLatLng = function(t, n) {
		if (Cookie.read('lat')) Cookie.dispose('lat');
		if (Cookie.read('lng')) Cookie.dispose('lng');
		if (Cookie.read('lon')) Cookie.dispose('lon');
		Cookie.write('lat', t, {duration: 90});
		Cookie.write('lng', n, {duration: 90});
		getArea(t, n);
	}
	
	function noGeo() {
		$('warning').show();
		window.scroll(0,0);
		$$('#warning a')[0].addEvent('click', function(e) {
			e.stop();
			//$('geoloader').fade(0);
			$('warning').hide();
		});
		$('warning').getElement('input').addEvent('keyup', function() {
			if (this.value.length > 4) {
				$$('#content .loader-000')[0].show();
					if (geocoder = new google.maps.Geocoder())
						geocoder.geocode({
							'address': $$('#warning input')[0].value
						}, function(r, s) {
							if (s == google.maps.GeocoderStatus.OK) {
								il = r[0].geometry.location;
								setLatLng(il.lat(), il.lng());
							}
						});
				$$('#content .loader-000')[0].hide();
				$('warning').hide();
			}
		});
	}
	
	// tiered geolocation
	
	if (!Cookie.read('lat') && !Cookie.read('lng')) {
		if (google.loader.ClientLocation) {
			setLatLng(google.loader.ClientLocation.latitude, google.loader.ClientLocation.longitude);
		}
		if (navigator.geolocation) {
			navigator.geolocation.getCurrentPosition(function(p) {
				setLatLng(p.coords.latitude, p.coords.longitude);
			}, function() {
				noGeo();
			});
		}
		else if (google.gears) {
			var geo = google.gears.factory.create('beta.geolocation');
			geo.getCurrentPosition(function(p) {
				setLatLng(p.latitude, p.longitude);
			}, function() {
				noGeo();
			});
		}
		else {
			noGeo();
		}
	}
	else if (Cookie.read('area') === false && Cookie.read('lat') && Cookie.read('lng')) {
		getArea(Cookie.read('lat'), Cookie.read('lng'));
	}
	
	// location dropdown fx
		
	chk = function (e,o) {
		e.style.display = (o ? 'block' : 'none');
	}
	
	$('loc').addEvents({
		'mouseover': function() {
			chk($$('#top .ri ul')[0], 1);
			$$('#top .ri ul')[0].addEvents({
				'mouseover': function() {
					chk(this, 1);
				},
				'mouseout': function() {
					chk(this, 0);
				}
			});
		},
		'mouseout': function() {
			chk($$('#top .ri ul')[0], 0);
		}
	});
	
	$$('#top .ri a').each(function(i) {
		i.addEvent('click', function(e) {
			e.stop();
			$('loc').addClass('loading');
			setArea(i.get('text'));
		});
	});
});
